terminal.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. open_mapping = [[<c-t>]],
  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 = vim.o.shell, -- 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. -- Add executables on the config.lua
  38. -- { exec, keymap, name}
  39. -- lvim.builtin.terminal.execs = {{}} to overwrite
  40. -- lvim.builtin.terminal.execs[#lvim.builtin.terminal.execs+1] = {"gdb", "tg", "GNU Debugger"}
  41. execs = {
  42. -- TODO: this should probably be removed since it's hard to hit <leader>gg within the timeoutlen
  43. { "lazygit", "<leader>gg", "LazyGit", "float" },
  44. { "lazygit", "<c-\\>", "LazyGit", "float" },
  45. },
  46. }
  47. end
  48. M.setup = function()
  49. local terminal = require "toggleterm"
  50. terminal.setup(lvim.builtin.terminal)
  51. -- setup the default terminal so it's always reachable
  52. local default_term_opts = {
  53. cmd = lvim.builtin.terminal.shell,
  54. keymap = lvim.builtin.terminal.open_mapping,
  55. label = "Toggle terminal",
  56. count = 1,
  57. direction = lvim.builtin.terminal.direction,
  58. size = lvim.builtin.terminal.size,
  59. }
  60. M.add_exec(default_term_opts)
  61. for i, exec in pairs(lvim.builtin.terminal.execs) do
  62. local opts = {
  63. cmd = exec[1],
  64. keymap = exec[2],
  65. label = exec[3],
  66. count = i + 1,
  67. direction = exec[4] or lvim.builtin.terminal.direction,
  68. size = lvim.builtin.terminal.size,
  69. }
  70. M.add_exec(opts)
  71. end
  72. if lvim.builtin.terminal.on_config_done then
  73. lvim.builtin.terminal.on_config_done(terminal)
  74. end
  75. end
  76. M.add_exec = function(opts)
  77. local binary = opts.cmd:match "(%S+)"
  78. if vim.fn.executable(binary) ~= 1 then
  79. Log:debug("Skipping configuring executable " .. binary .. ". Please make sure it is installed properly.")
  80. return
  81. end
  82. local exec_func = string.format(
  83. "<cmd>lua require('lvim.core.terminal')._exec_toggle({ cmd = '%s', count = %d, direction = '%s'})<CR>",
  84. opts.cmd,
  85. opts.count,
  86. opts.direction
  87. )
  88. require("lvim.keymappings").load {
  89. normal_mode = { [opts.keymap] = exec_func },
  90. term_mode = { [opts.keymap] = exec_func },
  91. }
  92. local wk_status_ok, wk = pcall(require, "whichkey")
  93. if not wk_status_ok then
  94. return
  95. end
  96. wk.register({ [opts.keymap] = { opts.label } }, { mode = "n" })
  97. wk.register({ [opts.keymap] = { opts.label } }, { mode = "t" })
  98. end
  99. M._exec_toggle = function(opts)
  100. local Terminal = require("toggleterm.terminal").Terminal
  101. local term = Terminal:new { cmd = opts.cmd, count = opts.count, direction = opts.direction }
  102. term:toggle(lvim.builtin.terminal.size, opts.direction)
  103. end
  104. ---Toggles a log viewer according to log.viewer.layout_config
  105. ---@param logfile string the fullpath to the logfile
  106. M.toggle_log_view = function(logfile)
  107. local log_viewer = lvim.log.viewer.cmd
  108. if vim.fn.executable(log_viewer) ~= 1 then
  109. log_viewer = "less +F"
  110. end
  111. log_viewer = log_viewer .. " " .. logfile
  112. local term_opts = vim.tbl_deep_extend("force", lvim.builtin.terminal, {
  113. cmd = log_viewer,
  114. open_mapping = lvim.log.viewer.layout_config.open_mapping,
  115. direction = lvim.log.viewer.layout_config.direction,
  116. -- TODO: this might not be working as expected
  117. size = lvim.log.viewer.layout_config.size,
  118. float_opts = lvim.log.viewer.layout_config.float_opts,
  119. })
  120. local Terminal = require("toggleterm.terminal").Terminal
  121. local log_view = Terminal:new(term_opts)
  122. log_view:toggle()
  123. end
  124. return M