terminal.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. local M = {}
  2. local Log = require "lvim.core.log"
  3. M.config = function()
  4. lvim.builtin["terminal"] = {
  5. active = true,
  6. on_config_done = nil,
  7. -- size can be a number or function which is passed the current terminal
  8. size = 20,
  9. open_mapping = [[<c-\>]],
  10. hide_numbers = true, -- hide the number column in toggleterm buffers
  11. shade_filetypes = {},
  12. shade_terminals = true,
  13. shading_factor = 2, -- the degree by which to darken to terminal colour, default: 1 for dark backgrounds, 3 for light
  14. start_in_insert = true,
  15. insert_mappings = true, -- whether or not the open mapping applies in insert mode
  16. persist_size = false,
  17. -- direction = 'vertical' | 'horizontal' | 'window' | 'float',
  18. direction = "float",
  19. close_on_exit = true, -- close the terminal window when the process exits
  20. auto_scroll = true, -- automatically scroll to the bottom on terminal output
  21. shell = nil, -- change the default shell
  22. -- This field is only relevant if direction is set to 'float'
  23. float_opts = {
  24. -- The border key is *almost* the same as 'nvim_win_open'
  25. -- see :h nvim_win_open for details on borders however
  26. -- the 'curved' border is a custom border type
  27. -- not natively supported but implemented in this plugin.
  28. -- border = 'single' | 'double' | 'shadow' | 'curved' | ... other options supported by win open
  29. border = "curved",
  30. -- width = <value>,
  31. -- height = <value>,
  32. winblend = 0,
  33. highlights = {
  34. border = "Normal",
  35. background = "Normal",
  36. },
  37. },
  38. winbar = {
  39. enabled = false,
  40. },
  41. -- Add executables on the config.lua
  42. -- { cmd, keymap, description, direction, size }
  43. -- lvim.builtin.terminal.execs = {...} to overwrite
  44. -- lvim.builtin.terminal.execs[#lvim.builtin.terminal.execs+1] = {"gdb", "tg", "GNU Debugger"}
  45. -- TODO: pls add mappings in which key and refactor this
  46. execs = {
  47. { nil, "<M-1>", "Horizontal Terminal", "horizontal", 0.3 },
  48. { nil, "<M-2>", "Vertical Terminal", "vertical", 0.4 },
  49. { nil, "<M-3>", "Float Terminal", "float", nil },
  50. },
  51. }
  52. end
  53. --- Get current buffer size
  54. ---@return {width: number, height: number}
  55. local function get_buf_size()
  56. local cbuf = vim.api.nvim_get_current_buf()
  57. local bufinfo = vim.tbl_filter(function(buf)
  58. return buf.bufnr == cbuf
  59. end, vim.fn.getwininfo(vim.api.nvim_get_current_win()))[1]
  60. if bufinfo == nil then
  61. return { width = -1, height = -1 }
  62. end
  63. return { width = bufinfo.width, height = bufinfo.height }
  64. end
  65. --- Get the dynamic terminal size in cells
  66. ---@param direction number
  67. ---@param size number
  68. ---@return integer
  69. local function get_dynamic_terminal_size(direction, size)
  70. size = size or lvim.builtin.terminal.size
  71. if direction ~= "float" and tostring(size):find(".", 1, true) then
  72. size = math.min(size, 1.0)
  73. local buf_sizes = get_buf_size()
  74. local buf_size = direction == "horizontal" and buf_sizes.height or buf_sizes.width
  75. return buf_size * size
  76. else
  77. return size
  78. end
  79. end
  80. M.init = function()
  81. for i, exec in pairs(lvim.builtin.terminal.execs) do
  82. local direction = exec[4] or lvim.builtin.terminal.direction
  83. local opts = {
  84. cmd = exec[1] or lvim.builtin.terminal.shell or vim.o.shell,
  85. keymap = exec[2],
  86. label = exec[3],
  87. -- NOTE: unable to consistently bind id/count <= 9, see #2146
  88. count = i + 100,
  89. direction = direction,
  90. size = function()
  91. return get_dynamic_terminal_size(direction, exec[5])
  92. end,
  93. }
  94. M.add_exec(opts)
  95. end
  96. end
  97. M.setup = function()
  98. local terminal = require "toggleterm"
  99. terminal.setup(lvim.builtin.terminal)
  100. if lvim.builtin.terminal.on_config_done then
  101. lvim.builtin.terminal.on_config_done(terminal)
  102. end
  103. end
  104. M.add_exec = function(opts)
  105. local binary = opts.cmd:match "(%S+)"
  106. if vim.fn.executable(binary) ~= 1 then
  107. Log:debug("Skipping configuring executable " .. binary .. ". Please make sure it is installed properly.")
  108. return
  109. end
  110. vim.keymap.set({ "n", "t" }, opts.keymap, function()
  111. M._exec_toggle { cmd = opts.cmd, count = opts.count, direction = opts.direction, size = opts.size() }
  112. end, { desc = opts.label, noremap = true, silent = true })
  113. end
  114. M._exec_toggle = function(opts)
  115. local Terminal = require("toggleterm.terminal").Terminal
  116. local term = Terminal:new { cmd = opts.cmd, count = opts.count, direction = opts.direction }
  117. term:toggle(opts.size, opts.direction)
  118. end
  119. ---Toggles a log viewer according to log.viewer.layout_config
  120. ---@param logfile string the fullpath to the logfile
  121. M.toggle_log_view = function(logfile)
  122. local log_viewer = lvim.log.viewer.cmd
  123. if vim.fn.executable(log_viewer) ~= 1 then
  124. log_viewer = "less +F"
  125. end
  126. Log:debug("attempting to open: " .. logfile)
  127. log_viewer = log_viewer .. " " .. logfile
  128. local term_opts = vim.tbl_deep_extend("force", lvim.builtin.terminal, {
  129. cmd = log_viewer,
  130. open_mapping = lvim.log.viewer.layout_config.open_mapping,
  131. direction = lvim.log.viewer.layout_config.direction,
  132. -- TODO: this might not be working as expected
  133. size = lvim.log.viewer.layout_config.size,
  134. float_opts = lvim.log.viewer.layout_config.float_opts,
  135. })
  136. local Terminal = require("toggleterm.terminal").Terminal
  137. local log_view = Terminal:new(term_opts)
  138. log_view:toggle()
  139. end
  140. M.lazygit_toggle = function()
  141. local Terminal = require("toggleterm.terminal").Terminal
  142. local lazygit = Terminal:new {
  143. cmd = "lazygit",
  144. hidden = true,
  145. direction = "float",
  146. float_opts = {
  147. border = "none",
  148. width = 100000,
  149. height = 100000,
  150. zindex = 200,
  151. },
  152. on_open = function(_)
  153. vim.cmd "startinsert!"
  154. end,
  155. on_close = function(_) end,
  156. count = 99,
  157. }
  158. lazygit:toggle()
  159. end
  160. return M