Skip to content

Commit

Permalink
feat(statusline): prefer 'mini.icons' as icon provider
Browse files Browse the repository at this point in the history
  • Loading branch information
echasnovski committed Jul 3, 2024
1 parent c0ce60c commit b10ffff
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 48 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- It is default in Neovim>=0.10.
- Tree-sitter parser is built-in in Neovim 0.9.x, needs manual enabling via `vim.treesitter.start()`.
- Has visual regressions on Neovim 0.8.0 and 0.8.1 without enabled tree-sitter (code blocks are highlighted as normal text). Use 0.8.2 or newer.
- Universally prefer 'mini.icons' module over 'nvim-tree/nvim-web-devicons'.

## mini.doc

Expand All @@ -29,6 +30,7 @@
## mini.statusline

- BREAKING FEATURE: update `section_fileinfo()` to show non-empty filetype even in not normal buffers (like plugin's scratch buffers, help, quickfix, etc.). Previously it showed nothing, which was a mistake as filetype can be a valuable information.
- FEATURE: prefer using 'mini.icons' as icon provider for `section_fileinfo()`.


# Version 0.13.0
Expand Down
13 changes: 8 additions & 5 deletions doc/mini-statusline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ Features:

# Dependencies ~

Suggested dependencies (provide extra functionality, statusline will work
without them):
Suggested dependencies (provide extra functionality, will work without them):

- Nerd font (to support extra icons).

- Enabled |MiniIcons| module for |MiniStatusline.section_fileinfo()|.
Falls back to using 'nvim-tree/nvim-web-devicons' plugin or shows nothing.

- Enabled |MiniGit| module for |MiniStatusline.section_git()|.
Falls back to using 'lewis6991/gitsigns.nvim' plugin or shows nothing.

- Enabled |MiniDiff| module for |MiniStatusline.section_diff()|.
Falls back to using 'lewis6991/gitsigns.nvim' plugin or shows nothing.

- Plugin 'nvim-tree/nvim-web-devicons' for filetype icons
in |MiniStatusline.section_fileinfo()|. If missing, no icons will be shown.

# Setup ~

This module needs a setup with `require('mini.statusline').setup({})`
Expand Down Expand Up @@ -317,6 +317,9 @@ width is lower than `args.trunc_width` or buffer is not normal.

Nothing is shown if there is no 'filetype' set (treated as temporary buffer).

If `config.use_icons` is true and icon provider is present (see
"Dependencies" section in |mini.statusline|), shows icon near the filetype.

Parameters ~
{args} `(table)` Section arguments.

Expand Down
8 changes: 3 additions & 5 deletions doc/mini.txt
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,9 @@ Table of contents:

- |MiniStatusline| - minimal and fast statusline. Has ability to use custom
content supplied with concise function (using module's provided section
functions) along with builtin default. For full experience needs [Nerd
font](https://www.nerdfonts.com/),
[gitsigns.nvim](https://github.com/lewis6991/gitsigns.nvim) plugin, and
[nvim-web-devicons](https://github.com/nvim-tree/nvim-web-devicons)
plugin (but works without any them).
functions) along with builtin default. For full experience needs
enabled |MiniDiff|, |MiniGit|, and |MiniIcons| modules (but works without
any of them).

- |MiniSurround| - fast and feature-rich surround plugin. Add, delete,
replace, find, highlight surrounding (like pair of parenthesis, quotes,
Expand Down
8 changes: 3 additions & 5 deletions lua/mini/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,9 @@
---
--- - |MiniStatusline| - minimal and fast statusline. Has ability to use custom
--- content supplied with concise function (using module's provided section
--- functions) along with builtin default. For full experience needs [Nerd
--- font](https://www.nerdfonts.com/),
--- [gitsigns.nvim](https://github.com/lewis6991/gitsigns.nvim) plugin, and
--- [nvim-web-devicons](https://github.com/nvim-tree/nvim-web-devicons)
--- plugin (but works without any them).
--- functions) along with builtin default. For full experience needs
--- enabled |MiniDiff|, |MiniGit|, and |MiniIcons| modules (but works without
--- any of them).
---
--- - |MiniSurround| - fast and feature-rich surround plugin. Add, delete,
--- replace, find, highlight surrounding (like pair of parenthesis, quotes,
Expand Down
35 changes: 22 additions & 13 deletions lua/mini/statusline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
---
--- # Dependencies ~
---
--- Suggested dependencies (provide extra functionality, statusline will work
--- without them):
--- Suggested dependencies (provide extra functionality, will work without them):
---
--- - Nerd font (to support extra icons).
---
--- - Enabled |MiniIcons| module for |MiniStatusline.section_fileinfo()|.
--- Falls back to using 'nvim-tree/nvim-web-devicons' plugin or shows nothing.
---
--- - Enabled |MiniGit| module for |MiniStatusline.section_git()|.
--- Falls back to using 'lewis6991/gitsigns.nvim' plugin or shows nothing.
---
--- - Enabled |MiniDiff| module for |MiniStatusline.section_diff()|.
--- Falls back to using 'lewis6991/gitsigns.nvim' plugin or shows nothing.
---
--- - Plugin 'nvim-tree/nvim-web-devicons' for filetype icons
--- in |MiniStatusline.section_fileinfo()|. If missing, no icons will be shown.
---
--- # Setup ~
---
--- This module needs a setup with `require('mini.statusline').setup({})`
Expand Down Expand Up @@ -393,6 +393,9 @@ end
---
--- Nothing is shown if there is no 'filetype' set (treated as temporary buffer).
---
--- If `config.use_icons` is true and icon provider is present (see
--- "Dependencies" section in |mini.statusline|), shows icon near the filetype.
---
---@param args __statusline_args
---
---@return __statusline_section
Expand All @@ -404,7 +407,7 @@ MiniStatusline.section_fileinfo = function(args)

-- Add filetype icon
H.ensure_get_icon()
if H.get_icon ~= nil then filetype = H.get_icon() .. ' ' .. filetype end
if H.get_icon ~= nil then filetype = H.get_icon(filetype) .. ' ' .. filetype end

-- Construct output string if truncated or buffer is not normal
if MiniStatusline.is_truncated(args.trunc_width) or vim.bo.buftype ~= '' then return filetype end
Expand Down Expand Up @@ -672,14 +675,20 @@ H.get_filesize = function()
end

H.ensure_get_icon = function()
local use_icons = H.use_icons or H.get_config().use_icons
if not use_icons then H.get_icon = nil end
if use_icons and H.get_icon == nil then
-- Have this `require()` here to not depend on plugin initialization order
if not (H.use_icons or H.get_config().use_icons) then
-- Show no icon
H.get_icon = nil
elseif H.get_icon ~= nil then
-- Cache only once
return
elseif _G.MiniIcons ~= nil then
-- Prefer 'mini.icons'
H.get_icon = function(filetype) return (_G.MiniIcons.get('filetype', filetype)) end
else
-- Try falling back to 'nvim-web-devicons'
local has_devicons, devicons = pcall(require, 'nvim-web-devicons')
if has_devicons then
H.get_icon = function() return devicons.get_icon(vim.fn.expand('%:t'), nil, { default = true }) end
end
if not has_devicons then return end
H.get_icon = function() return (devicons.get_icon(vim.fn.expand('%:t'), nil, { default = true })) end
end
end

Expand Down
13 changes: 8 additions & 5 deletions readmes/mini-statusline.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@

### Minimal and fast statusline module with opinionated default look

For full experience needs (still works without any of suggestions):

- [Nerd font](https://www.nerdfonts.com/) and [nvim-tree/nvim-web-devicons](https://github.com/nvim-tree/nvim-web-devicons) plugin to show icons.
- Enabled ['mini.git'](https://github.com/echasnovski/mini.nvim/blob/main/readmes/mini-git.md) and ['mini.diff'](https://github.com/echasnovski/mini.nvim/blob/main/readmes/mini-diff.md) modules to show Git and diff related information. Can fall back to using [lewis6991/gitsigns.nvim](https://github.com/lewis6991/gitsigns.nvim) plugin.

See more details in [Features](#features) and [help file](../doc/mini-statusline.txt).

---
Expand All @@ -37,6 +32,14 @@ https://user-images.githubusercontent.com/24854248/173045208-42463c8f-a2ac-488d-
- Built-in active mode indicator with colors.
- Sections can hide information when window is too narrow (specific window width is configurable per section).

## Dependencies

For full experience needs (still works without any of suggestions):

- [Nerd font](https://www.nerdfonts.com/) and enabled ['mini.icons'](https://github.com/echasnovski/mini.nvim/blob/main/readmes/mini-icons.md) module to show filetype icons. Can fall back to using [nvim-tree/nvim-web-devicons](https://github.com/nvim-tree/nvim-web-devicons) plugin.

- Enabled ['mini.git'](https://github.com/echasnovski/mini.nvim/blob/main/readmes/mini-git.md) and ['mini.diff'](https://github.com/echasnovski/mini.nvim/blob/main/readmes/mini-diff.md) modules to show Git and diff related information. Can fall back to using [lewis6991/gitsigns.nvim](https://github.com/lewis6991/gitsigns.nvim) plugin.

## Installation

This plugin can be installed as part of 'mini.nvim' library (**recommended**) or as a standalone Git repository.
Expand Down
2 changes: 0 additions & 2 deletions tests/dir-statusline/lua/nvim-web-devicons.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
return {
get_icon = function(filename, extension, options)
_G.devicons_args = { filename = filename, extension = extension, options = options }

if filename == 'LICENSE' then return '', 'DevIconLicense' end
if vim.endswith(filename, 'lua') then return '', 'DevIconLua' end
if vim.endswith(filename, 'txt') then return '', 'DevIconTxt' end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
1|aaaaaaaaa │
2|~ │~
3|~ │~
4| Normal  main|bisect (MM)  #4 +3 ~2 -1  E4 W3 I2 H1 󰰎 + <tatusline/mocked.lua lua utf-8[unix] 10B 2/9 1|1│ 2|9 [No Name]
4| Normal  main|bisect (MM)  #4 +3 ~2 -1  E4 W3 I2 H1 󰰎 + <tatusline/mocked.lua 󰢱 lua utf-8[unix] 10B 2/9 1|1│ 2|9 [No Name]
5|/a [2/9]

-|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
1|aaaaaaaaa │
2|~ │~
3|~ │~
4| N <statusline/mocked.lua lua 1│ 2 [No Name]
4| N <statusline/mocked.lua 󰢱 lua 1│ 2 [No Name]
5|/a [2/9]

-|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
1|aaaaaaaaa │
2|~ │~
3|~ │~
4| N  main|bisect (MM) <ua lua 1│ 2 [No Name]
4| N  main|bisect (MM) <ua 󰢱 lua 1│ 2 [No Name]
5|/a [2/9]

-|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|
Expand Down
24 changes: 14 additions & 10 deletions tests/test_statusline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ local get_two_windows = function()
end

-- Mocks
local mock_devicons = function() child.cmd('set rtp+=tests/dir-statusline') end
local mock_miniicons = function() child.lua('require("mini.icons").setup()') end

local mock_gitsigns = function()
child.b.gitsigns_head, child.b.gitsigns_status = 'main', '+1 ~2 -3'
Expand Down Expand Up @@ -420,7 +420,7 @@ T['section_lsp()']['respects `config.use_icons`'] = function()
eq(child.lua_get([[MiniStatusline.section_lsp({})]]), '󰰎 +')
end

T['section_fileinfo()'] = new_set({ hooks = { pre_case = mock_devicons, post_case = unmock_file } })
T['section_fileinfo()'] = new_set({ hooks = { pre_case = mock_miniicons, post_case = unmock_file } })

local validate_fileinfo = function(args, pattern)
local command = ('MiniStatusline.section_fileinfo({ %s })'):format(args)
Expand All @@ -432,7 +432,7 @@ T['section_fileinfo()']['works'] = function()
child.cmd('edit ' .. mocked_filepath)
local encoding = child.bo.fileencoding or child.bo.encoding
local format = child.bo.fileformat
local pattern = '^ lua ' .. vim.pesc(encoding) .. '%[' .. vim.pesc(format) .. '%] 10B$'
local pattern = '^󰢱 lua ' .. vim.pesc(encoding) .. '%[' .. vim.pesc(format) .. '%] 10B$'
validate_fileinfo('', pattern)
end

Expand All @@ -441,9 +441,9 @@ T['section_fileinfo()']['respects `args.trunc_width`'] = function()
child.cmd('edit ' .. mocked_filepath)

set_width(100)
validate_fileinfo('trunc_width = 100', '^ lua...')
validate_fileinfo('trunc_width = 100', '^󰢱 lua...')
set_width(99)
validate_fileinfo('trunc_width = 100', '^ lua$')
validate_fileinfo('trunc_width = 100', '^󰢱 lua$')
end

T['section_fileinfo()']['respects `config.use_icons`'] = function()
Expand All @@ -455,12 +455,16 @@ T['section_fileinfo()']['respects `config.use_icons`'] = function()

-- Should also use buffer local config
child.b.ministatusline_config = { use_icons = true }
validate_fileinfo('', ' lua...')
validate_fileinfo('', '󰢱 lua...')
end

T['section_fileinfo()']["correctly asks 'nvim-web-devicons' for icon"] = function()
T['section_fileinfo()']["can fall back to 'nvim-web-devicons'"] = function()
child.lua('_G.MiniIcons = nil')
-- Mock 'nvim-web-devicons'
child.cmd('set rtp+=tests/dir-statusline')

child.cmd('e tmp.txt')
eq(child.lua_get('_G.devicons_args'), { filename = 'tmp.txt', options = { default = true } })
validate_fileinfo('', ' text...')
end

T['section_fileinfo()']['uses correct filetype'] = function()
Expand All @@ -486,7 +490,7 @@ T['section_fileinfo()']['is shown only in buffers with filetypes'] = function()

-- Should still show even if buffer is not normal
child.cmd('help')
validate_fileinfo('', '^ help$')
validate_fileinfo('', '^󰋖 help$')
end

T['section_filename()'] = new_set()
Expand Down Expand Up @@ -751,7 +755,7 @@ T['Default content']['active'] = new_set({
pre_case = function()
child.set_size(5, 160)

mock_devicons()
child.lua('require("mini.icons").setup()')
mock_file(10)

-- Mock filename section to use relative path for consistent screenshots
Expand Down

0 comments on commit b10ffff

Please sign in to comment.