diff options
| author | kylo252 <[email protected]> | 2021-08-09 19:02:37 +0200 | 
|---|---|---|
| committer | GitHub <[email protected]> | 2021-08-09 19:02:37 +0200 | 
| commit | 405423108fc31981c40116a827e845a1179c9053 (patch) | |
| tree | 41a7cb23536c982ccdc3402ab9d4602f2538eb40 /lua/core/terminal.lua | |
| parent | 625df947dcacf3804f4ec7335478535ecd8219af (diff) | |
feat: Add an async logger using plenary (#1207)
Co-authored-by: rebuilt <[email protected]>
Diffstat (limited to 'lua/core/terminal.lua')
| -rw-r--r-- | lua/core/terminal.lua | 48 | 
1 files changed, 45 insertions, 3 deletions
| diff --git a/lua/core/terminal.lua b/lua/core/terminal.lua index bd7815aa..818038fd 100644 --- a/lua/core/terminal.lua +++ b/lua/core/terminal.lua @@ -1,8 +1,11 @@  local M = {} +local Log = require "core.log" +local utils = require "utils" +  M.config = function()    lvim.builtin["terminal"] = {      -- size can be a number or function which is passed the current terminal -    size = 5, +    size = 20,      -- open_mapping = [[<c-\>]],      open_mapping = [[<c-t>]],      hide_numbers = true, -- hide the number column in toggleterm buffers @@ -11,7 +14,7 @@ M.config = function()      shading_factor = 2, -- the degree by which to darken to terminal colour, default: 1 for dark backgrounds, 3 for light      start_in_insert = true,      insert_mappings = true, -- whether or not the open mapping applies in insert mode -    persist_size = true, +    persist_size = false,      -- direction = 'vertical' | 'horizontal' | 'window' | 'float',      direction = "float",      close_on_exit = true, -- close the terminal window when the process exits @@ -36,13 +39,16 @@ M.config = function()      -- { exec, keymap, name}      -- lvim.builtin.terminal.execs = {{}} to overwrite      -- lvim.builtin.terminal.execs[#lvim.builtin.terminal.execs+1] = {"gdb", "tg", "GNU Debugger"} -    execs = { { "lazygit", "gg", "LazyGit" } }, +    execs = { +      { "lazygit", "gg", "LazyGit" }, +    },    }  end  M.setup = function()    local status_ok, terminal = pcall(require, "toggleterm")    if not status_ok then +    Log:get_default().error "Failed to load toggleterm"      print(terminal)      return    end @@ -88,4 +94,40 @@ M._exec_toggle = function(exec)    exec_term:toggle()  end +local function get_log_path(name) +  --handle custom paths not managed by Plenary.log +  local logger = require "core.log" +  local file +  if name == "nvim" then +    file = CACHE_PATH .. "/log" +  else +    file = logger:new({ plugin = name }):get_path() +  end +  if utils.is_file(file) then +    return file +  end +end + +---Toggles a log viewer according to log.viewer.layout_config +---@param name can be the name of any of the managed logs, e,g. "lunarvim" or the default ones {"nvim", "lsp", "packer.nvim"} +M.toggle_log_view = function(name) +  local logfile = get_log_path(name) +  if not logfile then +    return +  end +  local term_opts = vim.tbl_deep_extend("force", lvim.builtin.terminal, { +    cmd = lvim.log.viewer.cmd .. " " .. logfile, +    open_mapping = lvim.log.viewer.layout_config.open_mapping, +    direction = lvim.log.viewer.layout_config.direction, +    -- TODO: this might not be working as expected +    size = lvim.log.viewer.layout_config.size, +    float_opts = lvim.log.viewer.layout_config.float_opts, +  }) + +  local Terminal = require("toggleterm.terminal").Terminal +  local log_view = Terminal:new(term_opts) +  -- require("core.log"):get_default().debug("term", vim.inspect(term_opts)) +  log_view:toggle() +end +  return M | 
