terminal.lua 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. local M = {}
  2. M.config = function()
  3. lvim.builtin["terminal"] = {
  4. -- size can be a number or function which is passed the current terminal
  5. size = 5,
  6. -- open_mapping = [[<c-\>]],
  7. open_mapping = [[<c-t>]],
  8. hide_numbers = true, -- hide the number column in toggleterm buffers
  9. shade_filetypes = {},
  10. shade_terminals = true,
  11. shading_factor = 2, -- the degree by which to darken to terminal colour, default: 1 for dark backgrounds, 3 for light
  12. start_in_insert = true,
  13. insert_mappings = true, -- whether or not the open mapping applies in insert mode
  14. persist_size = true,
  15. -- direction = 'vertical' | 'horizontal' | 'window' | 'float',
  16. direction = "float",
  17. close_on_exit = true, -- close the terminal window when the process exits
  18. shell = vim.o.shell, -- change the default shell
  19. -- This field is only relevant if direction is set to 'float'
  20. float_opts = {
  21. -- The border key is *almost* the same as 'nvim_win_open'
  22. -- see :h nvim_win_open for details on borders however
  23. -- the 'curved' border is a custom border type
  24. -- not natively supported but implemented in this plugin.
  25. -- border = 'single' | 'double' | 'shadow' | 'curved' | ... other options supported by win open
  26. border = "curved",
  27. -- width = <value>,
  28. -- height = <value>,
  29. winblend = 0,
  30. highlights = {
  31. border = "Normal",
  32. background = "Normal",
  33. },
  34. },
  35. -- Add executables on the lv-config file
  36. -- { exec, keymap, name}
  37. -- lvim.builtin.terminal.execs = {{}} to overwrite
  38. -- lvim.builtin.terminal.execs[#lvim.builtin.terminal.execs+1] = {"gdb", "tg", "GNU Debugger"}
  39. execs = { { "lazygit", "gg", "LazyGit" } },
  40. }
  41. end
  42. M.setup = function()
  43. local status_ok, terminal = pcall(require, "toggleterm")
  44. if not status_ok then
  45. print(terminal)
  46. return
  47. end
  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. end
  53. local function is_installed(exe)
  54. return vim.fn.executable(exe) == 1
  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 is_installed(binary) ~= true then
  78. print("Please install executable " .. binary .. ". Check documentation for more information")
  79. return
  80. end
  81. local Terminal = require("toggleterm.terminal").Terminal
  82. local exec_term = Terminal:new { cmd = exec, hidden = true }
  83. exec_term:toggle()
  84. end
  85. return M