terminal.lua 4.0 KB

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