From 20590f151204e9e50334682b752c0dba2a3d77ac Mon Sep 17 00:00:00 2001 From: Pranav Rao <56097527+pranavrao145@users.noreply.github.com> Date: Fri, 22 Oct 2021 09:07:44 -0400 Subject: [PATCH] feat(tmux): added extra term cmd manipulation functions to tmux module In the future, these should probably be refactored out into the utils file beacuse they are the exact same functions as the one in the term module. --- lua/harpoon/tmux.lua | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/lua/harpoon/tmux.lua b/lua/harpoon/tmux.lua index 661e6d5..fecfe29 100755 --- a/lua/harpoon/tmux.lua +++ b/lua/harpoon/tmux.lua @@ -85,6 +85,16 @@ local function find_terminal(args) return window_handle end +local function get_first_empty_slot() + log.trace("_get_first_empty_slot()") + for idx, cmd in pairs(harpoon.get_term_config().cmds) do + if cmd == "" then + return idx + end + end + return M.get_length() + 1 +end + M.gotoTerminal = function(idx) log.trace("tmux: gotoTerminal(): Window:", idx) local window_handle = find_terminal(idx) @@ -139,4 +149,51 @@ M.clear_all = function() tmux_windows = {} end +M.get_length = function() + log.trace("_get_length()") + return table.maxn(harpoon.get_term_config().cmds) +end + +M.valid_index = function(idx) + if idx == nil or idx > M.get_length() or idx <= 0 then + return false + end + return true +end + +M.emit_changed = function() + log.trace("_emit_changed()") + if harpoon.get_global_settings().save_on_change then + harpoon.save() + end +end + +M.add_cmd = function(cmd) + log.trace("add_cmd()") + local found_idx = get_first_empty_slot() + harpoon.get_term_config().cmds[found_idx] = cmd + M.emit_changed() +end + +M.rm_cmd = function(idx) + log.trace("rm_cmd()") + if not M.valid_index(idx) then + log.debug("rm_cmd(): no cmd exists for index", idx) + return + end + table.remove(harpoon.get_term_config().cmds, idx) + M.emit_changed() +end + +M.set_cmd_list = function(new_list) + log.trace("set_cmd_list(): New list:", new_list) + for k in pairs(harpoon.get_term_config().cmds) do + harpoon.get_term_config().cmds[k] = nil + end + for k, v in pairs(new_list) do + harpoon.get_term_config().cmds[k] = v + end + M.emit_changed() +end + return M