8000 feat: show "fallback" in prompt title if, er, falling back · wincent/command-t@c16b272 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit c16b272

Browse files
committed
feat: show "fallback" in prompt title if, er, falling back
As discussed here: - #393 (comment) That comment suggested being a bit more verbose: CommandT [ find (fall-backed from git) ] but here I'm going with something a little less descriptive: CommandT [fallback] seeing as the extra detail in the title doesn't tell you _what_ went wrong; really, all we can do here is show a hint that _something_ went wrong, so we might as well be brief about it. Two things to note about implementation: - One is that I am playing around here trying to figure out how much metatable magic I want in here without making it all too "clever". So here I have a `__newindex` implementation that allows us to write `prompt.name = 'fallback'` instead of `prompt:set_name('fallback')`. The former is visually cleaner, but I don't necessarily like that it hides the fact that an imperative side-effect is going to take place. I will probably "dumb this down" as opposed to trying to use the pattern in other places in a uniform way. - The other is that my earlier claims about top-down React-style dataflow are pretty much not true any more. It _was_ true at the start, when each component rendered using data passed into it; now we have muddied the waters considerably with imperative methods. I will almost certainly want to revisit this topic in the future.
1 parent a699dc9 commit c16b272

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

lua/wincent/commandt/private/finders/fallback.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
return function(finder, directory, options)
77
return function()
88
finder.fallback = require('wincent.commandt.private.finders.file')(directory ~= '' and directory or '.', options)
9-
return finder.fallback
9+
return finder.fallback, 'fallback'
1010
end
1111
end

lua/wincent/commandt/private/prompt.lua

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ local Prompt = {}
1010

1111
local mt = {
1212
__index = Prompt,
13+
__newindex = function(t, k, v)
14+
if k == 'name' then
15+
Prompt.set_name(t, v)
16+
else
17+
rawset(t, k, v)
18+
end
19+
end,
1320
}
1421

1522
function Prompt.new(options)
@@ -47,6 +54,17 @@ function Prompt:close()
4754
end
4855
end
4956

57+
function Prompt:set_name(name)
58+
self._name = name
59+
if self._window then
60+
self._window:set_title(self:title())
61+
end
62+
end
63+
64+
function Prompt:title()
65+
return self._name and ('CommandT [' .. self._name .. ']') or 'CommandT'
66+
end
67+
5068
function Prompt:show()
5169
local bottom = nil
5270
local top = nil
@@ -64,7 +82,6 @@ function Prompt:show()
6482
end
6583

6684
if self._window == nil then
67-
local title = self._name and ('CommandT [' .. self._name .. ']') or 'CommandT'
6885
self._window = Window.new({
6986
bottom = bottom,
7087
buftype = 'prompt',
@@ -79,7 +96,7 @@ function Prompt:show()
7996
self._window = nil
8097
end,
8198
on_leave = self._on_leave,
82-
title = title,
99+
title = self:title(),
83100
top = top,
84101
})
85102
end

lua/wincent/commandt/private/ui.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ ui.show = function(finder, options)
9999
-- Once we've proved a finder works, we don't ever want to use fallback.
100100
current_finder.fallback = nil
101101
elseif current_finder.fallback then
102-
current_finder = current_finder.fallback()
102+
current_finder, name = current_finder.fallback()
103+
prompt.name = name or 'fallback'
103104
results = current_finder.run(query)
104105
end
105106
if #results == 0 then

lua/wincent/commandt/private/window.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,19 @@ function Window:replace_lines(lines, options)
198198
end
199199
end
200200

201+
function Window:set_title(title)
202+
self._title = title
203+
self._padded_title = title ~= '' and (' ' .. title .. ' ') or ''
204+
self:_reposition()
205+
vim.api.nvim_buf_set_lines(
206+
self._title_buffer,
207+
0, -- start
208+
-1, -- end
209+
false, -- strict indexing
210+
{ self._padded_title }
211+
)
212+
end
213+
201214
function Window:show()
202215
if self._main_buffer == nil then
203216
self._main_buffer = vim.api.nvim_create_buf(
@@ -407,7 +420,7 @@ function Window:_reposition()
407420
col = position.col + #self._prompt,
408421
height = 1,
409422
row = math.max(0, position.row),
410-
width = #(' ' .. self._title .. ' '),
423+
width = #self._padded_title,
411424
})
412425
)
413426
end

0 commit comments

Comments
 (0)
0