terminal.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. local M = {}
  2. local utils = require "utils"
  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. { "lazygit", "gg", "LazyGit" },
  43. },
  44. }
  45. end
  46. M.setup = function()
  47. local terminal = require "toggleterm"
  48. for _, exec in pairs(lvim.builtin.terminal.execs) do
  49. require("core.terminal").add_exec(exec[1], exec[2], exec[3])
  50. end
  51. terminal.setup(lvim.builtin.terminal)
  52. if lvim.builtin.terminal.on_config_done then
  53. lvim.builtin.terminal.on_config_done(terminal)
  54. end
  55. end
  56. M.add_exec = function(exec, keymap, name)
  57. vim.api.nvim_set_keymap(
  58. "n",
  59. "<leader>" .. keymap,
  60. "<cmd>lua require('core.terminal')._exec_toggle('" .. exec .. "')<CR>",
  61. { noremap = true, silent = true }
  62. )
  63. lvim.builtin.which_key.mappings[keymap] = name
  64. end
  65. M._split = function(inputstr, sep)
  66. if sep == nil then
  67. sep = "%s"
  68. end
  69. local t = {}
  70. for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
  71. table.insert(t, str)
  72. end
  73. return t
  74. end
  75. M._exec_toggle = function(exec)
  76. local binary = M._split(exec)[1]
  77. if vim.fn.executable(binary) ~= 1 then
  78. local Log = require "core.log"
  79. Log:error("Unable to run executable " .. binary .. ". Please make sure it is installed properly.")
  80. return
  81. end
  82. local Terminal = require("toggleterm.terminal").Terminal
  83. local exec_term = Terminal:new { cmd = exec, hidden = true }
  84. exec_term:toggle()
  85. end
  86. local function get_log_path(name)
  87. --handle custom paths not managed by Plenary.log
  88. local logger = require "core.log"
  89. local file
  90. if name == "nvim" then
  91. file = CACHE_PATH .. "/log"
  92. else
  93. file = logger:new({ plugin = name }):get_path()
  94. end
  95. if utils.is_file(file) then
  96. return file
  97. end
  98. end
  99. ---Toggles a log viewer according to log.viewer.layout_config
  100. ---@param name can be the name of any of the managed logs, e,g. "lunarvim" or the default ones {"nvim", "lsp", "packer.nvim"}
  101. M.toggle_log_view = function(name)
  102. local logfile = get_log_path(name)
  103. if not logfile then
  104. return
  105. end
  106. local term_opts = vim.tbl_deep_extend("force", lvim.builtin.terminal, {
  107. cmd = lvim.log.viewer.cmd .. " " .. logfile,
  108. open_mapping = lvim.log.viewer.layout_config.open_mapping,
  109. direction = lvim.log.viewer.layout_config.direction,
  110. -- TODO: this might not be working as expected
  111. size = lvim.log.viewer.layout_config.size,
  112. float_opts = lvim.log.viewer.layout_config.float_opts,
  113. })
  114. local Terminal = require("toggleterm.terminal").Terminal
  115. local log_view = Terminal:new(term_opts)
  116. -- require("core.log"):debug("term", vim.inspect(term_opts))
  117. log_view:toggle()
  118. end
  119. return M