Skip to content

Commit

Permalink
refactor: local functions, better names, comments
Browse files Browse the repository at this point in the history
  • Loading branch information
opalmay committed Feb 5, 2023
1 parent 4916d6a commit 4e6f588
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions lua/lvim/core/terminal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,43 @@ local function get_dynamic_terminal_size(direction, size)
end
end

local function exec_toggle(exec)
local Terminal = require("toggleterm.terminal").Terminal
local term = Terminal:new(exec)
term:toggle(exec.size, exec.direction)
end

local function add_exec_keymap(exec)
local binary = exec.cmd:match "(%S+)"
if vim.fn.executable(binary) ~= 1 then
Log:debug("Skipping configuring executable " .. binary .. ". Please make sure it is installed properly.")
return
end

vim.keymap.set({ "n", "t" }, exec.keymap, function()
exec_toggle(exec)
end, { desc = exec.desc, noremap = true, silent = true })
end

--- Setup the terminal execs
M.init = function()
for i, exec in ipairs(lvim.builtin.terminal.execs) do
-- size == 1 is a special case for full screen
if exec.size == 1 then
exec.direction = "float"
exec.float_opts = {
border = "none",
width = 100000,
height = 100000,
}
end

exec.direction = exec.direction or lvim.builtin.terminal.execs.defaults.direction
exec.size = exec.size or lvim.builtin.terminal.execs.defaults[exec.direction .. "_size"]
-- size is calculated dynamically as a percentage of the current buffer
exec.size = get_dynamic_terminal_size(exec.direction, exec.size)
exec.cmd = exec.cmd or lvim.builtin.terminal.shell
-- desc is used for the keymap description
exec.desc = exec.desc
if exec.desc == nil then
if exec.cmd == nil then
Expand All @@ -98,7 +126,8 @@ M.init = function()

exec.count = i + 100

M.add_exec(exec)
-- the table is passed to toggleterm:new directly
add_exec_keymap(exec)
end
end

Expand All @@ -110,29 +139,6 @@ M.setup = function()
end
end

M.add_exec = function(exec)
local binary = exec.cmd:match "(%S+)"
if vim.fn.executable(binary) ~= 1 then
Log:debug("Skipping configuring executable " .. binary .. ". Please make sure it is installed properly.")
return
end

vim.keymap.set({ "n", "t" }, exec.keymap, function()
M._exec_toggle(exec)
end, { desc = exec.desc, noremap = true, silent = true })
end

M._exec_toggle = function(exec)
local Terminal = require("toggleterm.terminal").Terminal
exec.float_opts = {
border = "none" and exec.size == 1,
width = exec.size == 1 and 100000,
height = exec.size == 1 and 100000,
}
local term = Terminal:new(exec)
term:toggle(exec.size, exec.direction)
end

---Toggles a log viewer according to log.viewer.layout_config
---@param logfile string the fullpath to the logfile
M.toggle_log_view = function(logfile)
Expand Down

0 comments on commit 4e6f588

Please sign in to comment.