terminal.lua 4.2 KB

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