terminal.lua 4.5 KB

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