terminal.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. shell = nil, -- change the default shell
  21. -- This field is only relevant if direction is set to 'float'
  22. float_opts = {
  23. -- The border key is *almost* the same as 'nvim_win_open'
  24. -- see :h nvim_win_open for details on borders however
  25. -- the 'curved' border is a custom border type
  26. -- not natively supported but implemented in this plugin.
  27. -- border = 'single' | 'double' | 'shadow' | 'curved' | ... other options supported by win open
  28. border = "curved",
  29. -- width = <value>,
  30. -- height = <value>,
  31. winblend = 0,
  32. highlights = {
  33. border = "Normal",
  34. background = "Normal",
  35. },
  36. },
  37. terminals_defaults = {
  38. direction = "horizontal",
  39. horizontal_size = 0.3,
  40. vertical_size = 0.4,
  41. },
  42. terminals = {
  43. { keymap = "<M-1>", desc = "Horizontal Terminal", direction = "horizontal" },
  44. { keymap = "<M-2>", desc = "Vertical Terminal", direction = "vertical" },
  45. { keymap = "<M-3>", desc = "Float Terminal", direction = "float" },
  46. { keymap = "<leader>gg", desc = "LazyGit", cmd = "lazygit", size = 1 },
  47. },
  48. }
  49. end
  50. --- Get current buffer size
  51. ---@return {width: number, height: number}
  52. local function get_buf_size()
  53. local cbuf = vim.api.nvim_get_current_buf()
  54. local bufinfo = vim.tbl_filter(function(buf)
  55. return buf.bufnr == cbuf
  56. end, vim.fn.getwininfo(vim.api.nvim_get_current_win()))[1]
  57. if bufinfo == nil then
  58. return { width = -1, height = -1 }
  59. end
  60. return { width = bufinfo.width, height = bufinfo.height }
  61. end
  62. --- Get the dynamic terminal size in cells
  63. ---@param direction number
  64. ---@param size number
  65. ---@return integer
  66. local function get_dynamic_terminal_size(direction, size)
  67. size = size or lvim.builtin.terminal.size
  68. if direction ~= "float" and tostring(size):find(".", 1, true) then
  69. size = math.min(size, 1.0)
  70. local buf_sizes = get_buf_size()
  71. local buf_size = direction == "horizontal" and buf_sizes.height or buf_sizes.width
  72. return buf_size * size
  73. else
  74. return size
  75. end
  76. end
  77. local function term_toggle(term_opts)
  78. local Terminal = require("toggleterm.terminal").Terminal
  79. local term = Terminal:new(term_opts)
  80. term:toggle(term_opts.size, term_opts.direction)
  81. end
  82. local function add_term_keymap(term_opts)
  83. local binary = term_opts.cmd:match "(%S+)"
  84. if vim.fn.executable(binary) ~= 1 then
  85. Log:debug("Skipping configuring executable " .. binary .. ". Please make sure it is installed properly.")
  86. return
  87. end
  88. local modes = { "n" }
  89. if not term_opts.keymap:find "<leader>" and not term_opts.keymap:find "<space>" then
  90. table.insert(modes, "t")
  91. end
  92. vim.keymap.set(modes, term_opts.keymap, function()
  93. term_toggle(term_opts)
  94. end, { desc = term_opts.desc, silent = true })
  95. end
  96. --- Setup the terminal cmds
  97. M.init = function()
  98. for i, term_opts in ipairs(lvim.builtin.terminal.terminals) do
  99. -- size == 1 is a special case for full screen
  100. if term_opts.size == 1 then
  101. term_opts.direction = "float"
  102. term_opts.float_opts = term_opts.float_opts
  103. or {
  104. border = "none",
  105. width = 100000,
  106. height = 100000,
  107. }
  108. end
  109. term_opts.direction = term_opts.direction or lvim.builtin.terminal.terminals_defaults.direction
  110. term_opts.size = term_opts.size or lvim.builtin.terminal.terminals_defaults[term_opts.direction .. "_size"]
  111. -- size is calculated dynamically as a percentage of the current buffer
  112. term_opts.size = get_dynamic_terminal_size(term_opts.direction, term_opts.size)
  113. term_opts.cmd = term_opts.cmd or lvim.builtin.terminal.shell or vim.o.shell
  114. -- desc is used for the keymap description
  115. term_opts.desc = term_opts.desc or term_opts.cmd
  116. term_opts.count = i + 100
  117. -- the table is passed to toggleterm:new directly
  118. add_term_keymap(term_opts)
  119. end
  120. end
  121. M.setup = function()
  122. local terminal = require "toggleterm"
  123. terminal.setup(lvim.builtin.terminal)
  124. if lvim.builtin.terminal.on_config_done then
  125. lvim.builtin.terminal.on_config_done(terminal)
  126. end
  127. end
  128. ---Toggles a log viewer according to log.viewer.layout_config
  129. ---@param logfile string the fullpath to the logfile
  130. M.toggle_log_view = function(logfile)
  131. local log_viewer = lvim.log.viewer.cmd
  132. if vim.fn.executable(log_viewer) ~= 1 then
  133. log_viewer = "less +F"
  134. end
  135. Log:debug("attempting to open: " .. logfile)
  136. log_viewer = log_viewer .. " " .. logfile
  137. local term_opts = vim.tbl_deep_extend("force", lvim.builtin.terminal, {
  138. cmd = log_viewer,
  139. open_mapping = lvim.log.viewer.layout_config.open_mapping,
  140. direction = lvim.log.viewer.layout_config.direction,
  141. -- TODO: this might not be working as expected
  142. size = lvim.log.viewer.layout_config.size,
  143. float_opts = lvim.log.viewer.layout_config.float_opts,
  144. })
  145. local Terminal = require("toggleterm.terminal").Terminal
  146. local log_view = Terminal:new(term_opts)
  147. log_view:toggle()
  148. end
  149. return M