Skip to content

Commit

Permalink
Merge pull request #229 from zhengpd/feat/configurable-terminal-displ…
Browse files Browse the repository at this point in the history
…ay-options

feat: more flexible terminal display options
  • Loading branch information
michaelb authored May 8, 2023
2 parents ba15b95 + 1fda456 commit 08dcac3
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
3 changes: 3 additions & 0 deletions doc/sniprun.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ require'sniprun'.setup({
live_display = { "VirtualTextOk" }, --# display mode used in live_mode

display_options = {
terminal_scrollback = vim.o.scrollback, -- change terminal display scrollback lines
terminal_line_number = false, -- whether show line number in terminal window
terminal_signcolumn = false, -- whether show signcolumn in terminal window
terminal_width = 45, --# change the terminal display option width
notification_timeout = 5 --# timeout for nvim_notify output
},
Expand Down
3 changes: 3 additions & 0 deletions doc/sources/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ require'sniprun'.setup({
live_display = { "VirtualTextOk" }, --# display mode used in live_mode
display_options = {
terminal_scrollback = vim.o.scrollback, -- change terminal display scrollback lines
terminal_line_number = false, -- whether show line number in terminal window
terminal_signcolumn = false, -- whether show signcolumn in terminal window
terminal_width = 45, --# change the terminal display option width
notification_timeout = 5 --# timeout for nvim_notify output
},
Expand Down
3 changes: 3 additions & 0 deletions doc/sources/display_modes.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ lua << EOF
require'sniprun'.setup({
display = { "Terminal" },
display_options = {
terminal_scrollback = vim.o.scrollback, -- change terminal display scrollback lines
terminal_line_number = false, -- whether show line number in terminal window
terminal_signcolumn = false, -- whether show signcolumn in terminal window
terminal_width = 45,
},
})
Expand Down
10 changes: 9 additions & 1 deletion lua/sniprun.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ M.config_values = {
live_display = { "VirtualTextOk" }, -- displayed only for live mode

display_options = {
terminal_scrollback = vim.o.scrollback, -- change terminal display scrollback lines
terminal_line_number = false, -- whether show line number in terminal window
terminal_signcolumn = false, -- whether show signcolumn in terminal window
terminal_width = 45, -- change the terminal display option width
notification_timeout = 5 -- timeout for nvim_notify output
},
Expand Down Expand Up @@ -80,6 +83,8 @@ end

function M.setup(opts)
opts = opts or {}

-- pre-process config keys
for key,value in pairs(opts) do
if M.config_values[key] == nil then
error(string.format('[Sniprun] Key %s does not exist in config values',key))
Expand All @@ -91,8 +96,11 @@ function M.setup(opts)
if key == 'live_mode_toggle' and opts[key] == 'enable' then
require('sniprun.live_mode')
end
M.config_values[key] = value
end

-- merge user config into default config values
M.config_values = vim.tbl_deep_extend("force", M.config_values, opts)

M.configure_keymaps()
M.setup_highlights()
M.setup_autocommands()
Expand Down
25 changes: 18 additions & 7 deletions lua/sniprun/display.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,22 @@ function M.term_set_window_handle()
end

function M.term_set_buffer_chan(winid)
if M.term.buffer ~= -1 then return end
if M.term.buffer ~= -1 then
vim.api.nvim_win_set_buf(winid, M.term.buffer)
return
end

local buf = vim.api.nvim_create_buf(false, true)
vim.fn.win_execute(winid, "set scrollback=1")
vim.fn.win_execute(winid, "setlocal nonu")
vim.fn.win_execute(winid, "setlocal signcolumn=no")

vim.api.nvim_win_set_buf(winid, buf)
local display_options = require("sniprun").config_values.display_options
vim.fn.win_execute(winid, "setlocal scrollback=" .. display_options.terminal_scrollback)

local lnumber = display_options.terminal_line_number and "number" or "nonumber"
vim.fn.win_execute(winid, "setlocal " .. lnumber)

local scl = display_options.terminal_signcolumn and vim.o.signcolumn or "no"
vim.fn.win_execute(winid, "setlocal signcolumn=" .. scl)

M.term.buffer = buf
M.term.chan = vim.api.nvim_open_term(buf, {})
Expand All @@ -59,8 +69,6 @@ end
function M.term_open()
M.term_set_window_handle()
M.term_set_buffer_chan(M.term.window_handle)

vim.api.nvim_win_set_buf(M.term.window_handle, M.term.buffer)
end

function M.write_to_term(message, ok)
Expand All @@ -84,9 +92,12 @@ function M.write_to_term(message, ok)
vim.api.nvim_chan_send(M.term.chan, line)
vim.api.nvim_chan_send(M.term.chan, "\n\r");
end
vim.api.nvim_chan_send(M.term.chan, "\n\r");

M.term.current_line = h

if M.term.current_line > vim.fn.line("w$") then
vim.fn.win_execute(M.term.window_handle, "normal " .. M.term.current_line .. "gg")
end
end


Expand Down
3 changes: 3 additions & 0 deletions ressources/display_terminal.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ EOF
You can change the width of the terminal by using the display option in the configuration:
```
display_options = {
terminal_scrollback = vim.o.scrollback, -- change terminal display scrollback lines
terminal_line_number = false, -- whether show line number in terminal window
terminal_signcolumn = false, -- whether show signcolumn in terminal window
terminal_width = 45, -- change the terminal display option width
},
```
Expand Down

0 comments on commit 08dcac3

Please sign in to comment.