Skip to content

Commit

Permalink
feat(nvim): toggle log buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
kkharji committed May 26, 2022
1 parent 85ebe86 commit 8059b24
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ local defaults = {
"iPad (9th generation)",
},
},
--- Mappings
mappings = {
--- Whether xbase mapping should be disabled.
enable = true,
Expand All @@ -106,7 +105,11 @@ local defaults = {
watch_picker = "<leader>s", --- set to 0 to disable
--- A list of all the previous pickers
all_picker = "<leader>ef", --- set to 0 to disable
},
--- horizontal toggle log buffer
toggle_split_log_buffer = "<leader>ls",
--- vertical toggle log buffer
toggle_vsplit_log_buffer = "<leader>lv",
}
}
```

Expand Down
4 changes: 4 additions & 0 deletions lua/xbase/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ local defaults = {
watch_picker = "<leader>s", --- set to 0 to disable
--- A list of all the previous pickers
all_picker = "<leader>ef", --- set to 0 to disable
--- horizontal toggle log buffer
toggle_split_log_buffer = "<leader>ls",
--- vertical toggle log buffer
toggle_vsplit_log_buffer = "<leader>lv",
},
}

Expand Down
51 changes: 40 additions & 11 deletions lua/xbase/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local M = {}
local pid = vim.fn.getpid()

vim.g.xbase = {
---@type Project[]
Expand Down Expand Up @@ -51,22 +52,50 @@ M.run = function(opts)
require("libxbase").run(opts)
end

M.watch = function(opts)
require("libxbase").watch_target(opts)
local function try_map(key, fun)
if type(key) == "string" then
vim.keymap.set("n", key, fun, { buffer = true })
end
end

local function bind(config)
local function try_map(key, fun)
if type(key) == "string" then
vim.keymap.set("n", key, require("xbase.pickers")[fun], { buffer = true })
end
M.toggle_log_buffer = function(vsplit)
local bufnr = vim.g.xbase.clients[string.format("%s", pid)].log_bufnr
local win = vim.fn.win_findbuf(bufnr)[1]
if win then
vim.api.nvim_win_close(win, false)
end
print "Opening log buffer"
local cmd = vsplit and "vert sbuffer" or "sbuffer"
local open = string.format("%s %s", cmd, bufnr)
vim.cmd(open)

local mappings = require("xbase.config").values.mappings

try_map(mappings.toggle_vsplit_log_buffer, function()
vim.cmd "close"
end)

try_map(mappings.toggle_split_log_buffer, function()
vim.cmd "close"
end)

vim.keymap.set("n", "q", "close", { buffer = true })
end

local function bind(config)
local pickers = require "xbase.pickers"

if config.mappings.enable then
try_map(config.mappings.build_picker, "build")
try_map(config.mappings.run_picker, "run")
try_map(config.mappings.watch_picker, "watch")
try_map(config.mappings.all_picker, "actions")
try_map(config.mappings.build_picker, pickers.build)
try_map(config.mappings.run_picker, pickers.run)
try_map(config.mappings.watch_picker, pickers.watch)
try_map(config.mappings.all_picker, pickers.actions)
try_map(config.mappings.toggle_split_log_buffer, function()
M.toggle_log_buffer(false)
end)
try_map(config.mappings.toggle_vsplit_log_buffer, function()
M.toggle_log_buffer(true)
end)
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/daemon/requests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Watchable for BuildRequest {
let ref mut logger = nvim.logger();

logger.set_title(format!("Rebuild:{}", config.target));
let success = build_with_logger(logger, root, &args, true, false).await?;
let success = build_with_logger(logger, root, &args, true, self.ops.is_once()).await?;
if !success {
let ref msg = format!("Failed: {} ", config.to_string());
nvim.echo_err(msg).await?;
Expand Down

0 comments on commit 8059b24

Please sign in to comment.