feat: add scrolling in preview window (#47)
* feat: add scrolling in preview window refactor into two layers * remove redundant code * add error message for scroll_fn and send_input * consider count for preview scrolling
This commit is contained in:
@@ -142,6 +142,9 @@ j k next | previous (in normal mode)
|
|||||||
<C-v> go to file selection as a vertical split
|
<C-v> go to file selection as a vertical split
|
||||||
<C-t> go to a file in a new tab
|
<C-t> go to a file in a new tab
|
||||||
|
|
||||||
|
<C-u> scroll up in preview window
|
||||||
|
<C-d> scroll down in preview window
|
||||||
|
|
||||||
<C-c> close telescope
|
<C-c> close telescope
|
||||||
<Esc> close telescope (in normal mode)
|
<Esc> close telescope (in normal mode)
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -34,6 +34,14 @@ function actions.get_selected_entry(prompt_bufnr)
|
|||||||
return actions.get_current_picker(prompt_bufnr):get_selection()
|
return actions.get_current_picker(prompt_bufnr):get_selection()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function actions.preview_scrolling_up(prompt_bufnr)
|
||||||
|
actions.get_current_picker(prompt_bufnr).previewer:scroll_fn(-30)
|
||||||
|
end
|
||||||
|
|
||||||
|
function actions.preview_scrolling_down(prompt_bufnr)
|
||||||
|
actions.get_current_picker(prompt_bufnr).previewer:scroll_fn(30)
|
||||||
|
end
|
||||||
|
|
||||||
-- TODO: It seems sometimes we get bad styling.
|
-- TODO: It seems sometimes we get bad styling.
|
||||||
local function goto_file_selection(prompt_bufnr, command)
|
local function goto_file_selection(prompt_bufnr, command)
|
||||||
local picker = actions.get_current_picker(prompt_bufnr)
|
local picker = actions.get_current_picker(prompt_bufnr)
|
||||||
|
|||||||
@@ -42,6 +42,9 @@ local default_mappings = {
|
|||||||
["<C-x>"] = actions.goto_file_selection_split,
|
["<C-x>"] = actions.goto_file_selection_split,
|
||||||
["<C-v>"] = actions.goto_file_selection_vsplit,
|
["<C-v>"] = actions.goto_file_selection_vsplit,
|
||||||
["<C-t>"] = actions.goto_file_selection_tabedit,
|
["<C-t>"] = actions.goto_file_selection_tabedit,
|
||||||
|
|
||||||
|
["<C-u>"] = actions.preview_scrolling_up,
|
||||||
|
["<C-d>"] = actions.preview_scrolling_down,
|
||||||
},
|
},
|
||||||
|
|
||||||
n = {
|
n = {
|
||||||
@@ -57,6 +60,9 @@ local default_mappings = {
|
|||||||
|
|
||||||
["<Down>"] = actions.move_selection_next,
|
["<Down>"] = actions.move_selection_next,
|
||||||
["<Up>"] = actions.move_selection_previous,
|
["<Up>"] = actions.move_selection_previous,
|
||||||
|
|
||||||
|
["<C-u>"] = actions.preview_scrolling_up,
|
||||||
|
["<C-d>"] = actions.preview_scrolling_down,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ local Previewer = {}
|
|||||||
Previewer.__index = Previewer
|
Previewer.__index = Previewer
|
||||||
|
|
||||||
-- TODO: Should play with these some more, ty @clason
|
-- TODO: Should play with these some more, ty @clason
|
||||||
local bat_options = " --style=plain --paging=never --color=always "
|
local bat_options = " --style=plain --color=always "
|
||||||
|
|
||||||
local previewer_ns = vim.api.nvim_create_namespace('telescope.previewers')
|
local previewer_ns = vim.api.nvim_create_namespace('telescope.previewers')
|
||||||
|
|
||||||
@@ -28,6 +28,8 @@ function Previewer:new(opts)
|
|||||||
state = nil,
|
state = nil,
|
||||||
_setup_func = opts.setup,
|
_setup_func = opts.setup,
|
||||||
_teardown_func = opts.teardown,
|
_teardown_func = opts.teardown,
|
||||||
|
_send_input = opts.send_input,
|
||||||
|
_scroll_fn = opts.scroll_fn,
|
||||||
preview_fn = opts.preview_fn,
|
preview_fn = opts.preview_fn,
|
||||||
}, Previewer)
|
}, Previewer)
|
||||||
end
|
end
|
||||||
@@ -55,6 +57,22 @@ function Previewer:teardown()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Previewer:send_input(input)
|
||||||
|
if self._send_input then
|
||||||
|
self:_send_input(input)
|
||||||
|
else
|
||||||
|
vim.api.nvim_err_writeln("send_input is not defined for this previewer")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Previewer:scroll_fn(direction)
|
||||||
|
if self._scroll_fn then
|
||||||
|
self:_scroll_fn(direction)
|
||||||
|
else
|
||||||
|
vim.api.nvim_err_writeln("scroll_fn is not defined for this previewer")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
previewers.new = function(...)
|
previewers.new = function(...)
|
||||||
return Previewer:new(...)
|
return Previewer:new(...)
|
||||||
end
|
end
|
||||||
@@ -186,6 +204,22 @@ previewers.cat = defaulter(function(opts)
|
|||||||
}
|
}
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
send_input = function(self, input)
|
||||||
|
termcode = vim.api.nvim_replace_termcodes(input, true, false, true)
|
||||||
|
if self.state.termopen_id then
|
||||||
|
pcall(vim.fn.chansend, self.state.termopen_id, termcode)
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
|
||||||
|
scroll_fn = function(self, direction)
|
||||||
|
if not self.state then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if direction > 0 then input = "d" else input = "u" end
|
||||||
|
local count = math.abs(direction)
|
||||||
|
self:send_input(count..input)
|
||||||
|
end,
|
||||||
|
|
||||||
teardown = function(self)
|
teardown = function(self)
|
||||||
if not self.state then
|
if not self.state then
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user