Skip to content

Commit

Permalink
feat: show "fallback" in prompt title if, er, falling back
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wincent committed Sep 5, 2022
1 parent a699dc9 commit c16b272
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lua/wincent/commandt/private/finders/fallback.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
return function(finder, directory, options)
return function()
finder.fallback = require('wincent.commandt.private.finders.file')(directory ~= '' and directory or '.', options)
return finder.fallback
return finder.fallback, 'fallback'
end
end
21 changes: 19 additions & 2 deletions lua/wincent/commandt/private/prompt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ local Prompt = {}

local mt = {
__index = Prompt,
__newindex = function(t, k, v)
if k == 'name' then
Prompt.set_name(t, v)
else
rawset(t, k, v)
end
end,
}

function Prompt.new(options)
Expand Down Expand Up @@ -47,6 +54,17 @@ function Prompt:close()
end
end

function Prompt:set_name(name)
self._name = name
if self._window then
self._window:set_title(self:title())
end
end

function Prompt:title()
return self._name and ('CommandT [' .. self._name .. ']') or 'CommandT'
end

function Prompt:show()
local bottom = nil
local top = nil
Expand All @@ -64,7 +82,6 @@ function Prompt:show()
end

if self._window == nil then
local title = self._name and ('CommandT [' .. self._name .. ']') or 'CommandT'
self._window = Window.new({
bottom = bottom,
buftype = 'prompt',
Expand All @@ -79,7 +96,7 @@ function Prompt:show()
self._window = nil
end,
on_leave = self._on_leave,
title = title,
title = self:title(),
top = top,
})
end
Expand Down
3 changes: 2 additions & 1 deletion lua/wincent/commandt/private/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ ui.show = function(finder, options)
-- Once we've proved a finder works, we don't ever want to use fallback.
current_finder.fallback = nil
elseif current_finder.fallback then
current_finder = current_finder.fallback()
current_finder, name = current_finder.fallback()
prompt.name = name or 'fallback'
results = current_finder.run(query)
end
if #results == 0 then
Expand Down
15 changes: 14 additions & 1 deletion lua/wincent/commandt/private/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@ function Window:replace_lines(lines, options)
end
end

function Window:set_title(title)
self._title = title
self._padded_title = title ~= '' and (' ' .. title .. ' ') or ''
self:_reposition()
vim.api.nvim_buf_set_lines(
self._title_buffer,
0, -- start
-1, -- end
false, -- strict indexing
{ self._padded_title }
)
end

function Window:show()
if self._main_buffer == nil then
self._main_buffer = vim.api.nvim_create_buf(
Expand Down Expand Up @@ -407,7 +420,7 @@ function Window:_reposition()
col = position.col + #self._prompt,
height = 1,
row = math.max(0, position.row),
width = #(' ' .. self._title .. ' '),
width = #self._padded_title,
})
)
end
Expand Down

0 comments on commit c16b272

Please sign in to comment.