terminal.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. local M = {}
  2. local utils = require "utils"
  3. local Log = require "core.log"
  4. M.config = function()
  5. lvim.builtin["terminal"] = {
  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. open_mapping = [[<c-t>]],
  11. hide_numbers = true, -- hide the number column in toggleterm buffers
  12. shade_filetypes = {},
  13. shade_terminals = true,
  14. shading_factor = 2, -- the degree by which to darken to terminal colour, default: 1 for dark backgrounds, 3 for light
  15. start_in_insert = true,
  16. insert_mappings = true, -- whether or not the open mapping applies in insert mode
  17. persist_size = false,
  18. -- direction = 'vertical' | 'horizontal' | 'window' | 'float',
  19. direction = "float",
  20. close_on_exit = true, -- close the terminal window when the process exits
  21. shell = vim.o.shell, -- change the default shell
  22. -- This field is only relevant if direction is set to 'float'
  23. float_opts = {
  24. -- The border key is *almost* the same as 'nvim_win_open'
  25. -- see :h nvim_win_open for details on borders however
  26. -- the 'curved' border is a custom border type
  27. -- not natively supported but implemented in this plugin.
  28. -- border = 'single' | 'double' | 'shadow' | 'curved' | ... other options supported by win open
  29. border = "curved",
  30. -- width = <value>,
  31. -- height = <value>,
  32. winblend = 0,
  33. highlights = {
  34. border = "Normal",
  35. background = "Normal",
  36. },
  37. },
  38. -- Add executables on the config.lua
  39. -- { exec, keymap, name}
  40. -- lvim.builtin.terminal.execs = {{}} to overwrite
  41. -- lvim.builtin.terminal.execs[#lvim.builtin.terminal.execs+1] = {"gdb", "tg", "GNU Debugger"}
  42. execs = {
  43. { "lazygit", "gg", "LazyGit" },
  44. },
  45. }
  46. end
  47. M.setup = function()
  48. local terminal = require "toggleterm"
  49. for _, exec in pairs(lvim.builtin.terminal.execs) do
  50. require("core.terminal").add_exec(exec[1], exec[2], exec[3])
  51. end
  52. terminal.setup(lvim.builtin.terminal)
  53. if lvim.builtin.terminal.on_config_done then
  54. lvim.builtin.terminal.on_config_done(terminal)
  55. end
  56. end
  57. M.add_exec = function(exec, keymap, name)
  58. vim.api.nvim_set_keymap(
  59. "n",
  60. "<leader>" .. keymap,
  61. "<cmd>lua require('core.terminal')._exec_toggle('" .. exec .. "')<CR>",
  62. { noremap = true, silent = true }
  63. )
  64. lvim.builtin.which_key.mappings[keymap] = name
  65. end
  66. M._split = function(inputstr, sep)
  67. if sep == nil then
  68. sep = "%s"
  69. end
  70. local t = {}
  71. for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
  72. table.insert(t, str)
  73. end
  74. return t
  75. end
  76. M._exec_toggle = function(exec)
  77. local binary = M._split(exec)[1]
  78. if vim.fn.executable(binary) ~= 1 then
  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 file
  89. if name == "nvim" then
  90. file = utils.join_paths(vim.fn.stdpath "cache", "log")
  91. elseif name == "packer.nvim" then
  92. file = utils.join_paths(vim.fn.stdpath "cache", "packer.nvim.log")
  93. else
  94. file = Log:get_path()
  95. end
  96. if utils.is_file(file) then
  97. return file
  98. end
  99. end
  100. ---Toggles a log viewer according to log.viewer.layout_config
  101. ---@param name can be the name of any of the managed logs, e,g. "lunarvim" or the default ones {"nvim", "lsp", "packer.nvim"}
  102. M.toggle_log_view = function(name)
  103. local logfile = get_log_path(name)
  104. if not logfile then
  105. return
  106. end
  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. -- require("core.log"):debug("term", vim.inspect(term_opts))
  123. log_view:toggle()
  124. end
  125. return M