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:
haorenW1025
2020-09-17 00:20:45 +08:00
committed by GitHub
parent 9bdf5b3d4a
commit 7d1a8292b6
4 changed files with 52 additions and 1 deletions

View File

@@ -142,6 +142,9 @@ j k next | previous (in normal mode)
<C-v> go to file selection as a vertical split
<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
<Esc> close telescope (in normal mode)
```

View File

@@ -34,6 +34,14 @@ function actions.get_selected_entry(prompt_bufnr)
return actions.get_current_picker(prompt_bufnr):get_selection()
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.
local function goto_file_selection(prompt_bufnr, command)
local picker = actions.get_current_picker(prompt_bufnr)

View File

@@ -42,6 +42,9 @@ local default_mappings = {
["<C-x>"] = actions.goto_file_selection_split,
["<C-v>"] = actions.goto_file_selection_vsplit,
["<C-t>"] = actions.goto_file_selection_tabedit,
["<C-u>"] = actions.preview_scrolling_up,
["<C-d>"] = actions.preview_scrolling_down,
},
n = {
@@ -57,6 +60,9 @@ local default_mappings = {
["<Down>"] = actions.move_selection_next,
["<Up>"] = actions.move_selection_previous,
["<C-u>"] = actions.preview_scrolling_up,
["<C-d>"] = actions.preview_scrolling_down,
},
}

View File

@@ -11,7 +11,7 @@ local Previewer = {}
Previewer.__index = Previewer
-- 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')
@@ -28,6 +28,8 @@ function Previewer:new(opts)
state = nil,
_setup_func = opts.setup,
_teardown_func = opts.teardown,
_send_input = opts.send_input,
_scroll_fn = opts.scroll_fn,
preview_fn = opts.preview_fn,
}, Previewer)
end
@@ -55,6 +57,22 @@ function Previewer:teardown()
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(...)
return Previewer:new(...)
end
@@ -186,6 +204,22 @@ previewers.cat = defaulter(function(opts)
}
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)
if not self.state then
return