Skip to content

Commit 6a0518f

Browse files
committed
feat: teach match_listing.icons to accept a function
For future-proofing and flexibility.
1 parent 717a466 commit 6a0518f

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

doc/command-t.txt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -631,16 +631,27 @@ Possible vaules are:
631631
require('wincent.commandt').setup({ traverse = 'file' })
632632

633633
*commandt.setup.match_listing.icons*
634-
boolean (default: true)
634+
boolean or function (default: true)
635635

636-
Controls whether to show icons in the match listing window (only for file-like
637-
results, not for things like |:CommandTHelp| results and other non-file
638-
entities).
636+
Controls whether (and how) to show icons in the match listing window (only
637+
for file-based results, not for things like |:CommandTHelp| results and other
638+
non-file entities).
639639

640-
Requires the mini.icons plug-in.
640+
Using the default implementation (`true`) requires the mini.icons plug-in:
641641

642642
- https://github.com/echasnovski/mini.icons
643643

644+
Alternatively you can pass a function that takes a file path and returns an
645+
icon glyph. For example, here is a function that uses |MiniIcons.get()|:
646+
647+
require('wincent.commandt').setup({
648+
match_listing = {
649+
icons = function (name)
650+
return _G.MiniIcons.get('file', name)
651+
end,
652+
},
653+
})
654+
644655
*commandt.setup.match_listing.truncate*
645656
string (default: 'middle')
646657

lua/wincent/commandt/init.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,12 @@ local options_spec = {
143143
},
144144
},
145145
icons = {
146-
kind = 'boolean',
146+
kind = {
147+
one_of = {
148+
{ kind = 'boolean' },
149+
{ kind = 'function' },
150+
},
151+
},
147152
},
148153
truncate = {
149154
kind = {

lua/wincent/commandt/private/match_listing.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,22 @@ function MatchListing:close()
5050
end
5151

5252
function MatchListing:icon_getter()
53-
if self._icons and _G.MiniIcons then
53+
if self._icons == true and _G.MiniIcons then
5454
return function(name)
5555
return _G.MiniIcons.get('file', name)
5656
end
57+
elseif type(self._icons) == 'function' then
58+
return self._icons
5759
end
5860
end
5961

6062
local format_line = function(line, width, selected, truncate, get_icon)
6163
local prefix = selected and '> ' or ' '
6264

6365
local icon = get_icon and get_icon(line)
66+
local icon_length = 0
6467
if icon then
68+
icon_length = #(icon .. ' ')
6569
prefix = prefix .. icon .. ' '
6670
end
6771

@@ -77,7 +81,7 @@ local format_line = function(line, width, selected, truncate, get_icon)
7781

7882
if #line + #prefix < width then
7983
-- Line fits without trimming.
80-
elseif #line < (get_icon and 8 or 5) then
84+
elseif #line < (5 + icon_length) then
8185
-- Line is so short that adding an ellipsis is not practical.
8286
elseif truncate == true or truncate == 'true' or truncate == 'middle' then
8387
local half = math.floor((width - 2) / 2)

0 commit comments

Comments
 (0)