Skip to content

Commit 1823e0d

Browse files
committed
optimisation
~ updated setup() to use a loop instead of manually assigning all values + added execute_tmux_cmd() helper function to minimise tmux related code duplication - removed if-else from command dispatch and implemented a table of functions for mapping options to their corresponding functions ~ moved error handling for missing commands into execute_tmux_cmd() ~ simplified and optimised the logic for checking tmux window existence and project directory matching in new_window() ~ improved readability by reducing code redundancy and structuring functions more clearly ~ fixed default save condition on readme. its false, i.e. save is off by default
1 parent 2068d8a commit 1823e0d

File tree

2 files changed

+46
-66
lines changed

2 files changed

+46
-66
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ And setup it with:
2222
```lua
2323
require('tmux-compile').setup({
2424
-- Overriding default configurations. [OPTIONAL]
25-
save_session = true, -- Save file before action
25+
save_session = false, -- Save file before action
2626
overlay_sleep = 1, -- Pause before overlay autoclose; seconds
2727
overlay_width_percent = 80, -- Overlay width percentage
2828
overlay_height_percent = 80, -- Overlay height percentage

lua/tmux-compile/init.lua

Lines changed: 45 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ M.config = {
1414
}
1515

1616
function M.setup(config)
17-
M.config.save_session = config.save_session or M.config.save_session
18-
M.config.overlay_sleep = config.overlay_sleep or M.config.overlay_sleep
19-
M.config.overlay_width_percent = config.overlay_width_percent or M.config.overlay_width_percent
20-
M.config.overlay_height_percent = config.overlay_height_percent or M.config.overlay_height_percent
21-
M.config.build_run_window_title = config.build_run_window_title or M.config.build_run_window_title
22-
M.config.build_run_config = config.build_run_config or M.config.build_run_config
17+
for key, value in pairs(config) do
18+
M.config[key] = value or M.config[key]
19+
end
2320
end
2421

2522

@@ -46,16 +43,12 @@ end
4643

4744
-- get build, run & debug commands based on file extension
4845
local function get_commands_for_extension(extension)
49-
if extension then
50-
for _, cfg in ipairs(M.config.build_run_config) do
51-
if vim.tbl_contains(cfg.extension, extension) then
52-
return cfg.build, cfg.run, cfg.debug
53-
end
46+
for _, cfg in ipairs(M.config.build_run_config) do
47+
if vim.tbl_contains(cfg.extension, extension) then
48+
return cfg.build, cfg.run, cfg.debug
5449
end
55-
print("Error: No build and run commands found for this extension")
56-
else
57-
print("Error: No file extension found")
5850
end
51+
print("Error: No build and run commands found for this extension")
5952
return nil, nil, nil
6053
end
6154

@@ -65,19 +58,23 @@ local function tmux_window_exists(window_name)
6558
return result ~= ""
6659
end
6760

61+
-- common function to prepare and execute tmux commands
62+
local function execute_tmux_cmd(cmd, error_name, tmux_cmd_head)
63+
if not cmd then
64+
local extension = get_file_extension()
65+
print("Error: " .. error_name .. " command not found for " .. extension)
66+
return 1
67+
end
68+
vim.cmd(tmux_cmd_head .. " '" .. cmd .. "; zsh'")
69+
end
70+
6871

6972
--# CALL FUNCTIONS #------------------------------------------------------------
7073

7174
-- run command in a new or existing tmux window
7275
local function new_window(cmd, error_name)
7376
local window_name = M.config.build_run_window_title
7477

75-
if not cmd then
76-
local extension = get_file_extension()
77-
print("Error: " .. error_name .. " command not found for " .. extension)
78-
return 1
79-
end
80-
8178
if tmux_window_exists(window_name) then
8279
local proj_dir = vim.fn.trim(vim.fn.system("pwd"))
8380
local win_dir = vim.fn.trim(vim.fn.system("tmux display -p -t " .. window_name .. " '#{pane_current_path}'"))
@@ -86,19 +83,17 @@ local function new_window(cmd, error_name)
8683
cmd = "cd " .. proj_dir .. "; " .. cmd
8784
end
8885

89-
local cmd_head = "silent !tmux select-window -t " .. window_name
90-
vim.cmd(cmd_head .. " \\; send-keys '" .. cmd .. "' C-m")
86+
execute_tmux_cmd(cmd, error_name, "silent !tmux select-window -t " .. window_name .. " \\; send-keys ")
9187
else
92-
local cmd_head = "silent !tmux new-window -n " .. window_name
93-
vim.cmd(cmd_head .. " '" .. cmd .. "; zsh'")
88+
execute_tmux_cmd(cmd, error_name, "silent !tmux new-window -n " .. window_name)
9489
end
9590
end
9691

9792
-- run command in an overlay pane
9893
local function overlay(cmd, sleep_duration, error_name)
9994
if not cmd then
10095
local extension = get_file_extension()
101-
print("Error: " .. error_name .. " command not found for " .. extension)
96+
print("Error: " .. error_name .. " command not found for " .. extension)
10297
return 1
10398
end
10499

@@ -114,14 +109,7 @@ end
114109

115110
-- run command in same window on a new pane
116111
local function split_window(cmd, side, error_name)
117-
if not cmd then
118-
local extension = get_file_extension()
119-
print("Error: " .. error_name .. " command not found for " .. extension)
120-
return 1
121-
end
122-
123-
local cmd_head = "silent !tmux split-window " .. side
124-
vim.cmd(cmd_head .. " '" .. cmd .. "; exec zsh'")
112+
execute_tmux_cmd(cmd, error_name, "silent !tmux split-window " .. side)
125113
end
126114

127115
-- run lazygit in an overlay pane
@@ -133,7 +121,6 @@ local function lazygit()
133121
end
134122
end
135123

136-
137124
--# NVIM DISPATCH #-------------------------------------------------------------
138125

139126
-- call the appropriate function based on the option
@@ -155,32 +142,24 @@ function M.dispatch(option)
155142
local extension = get_file_extension()
156143
local make, run, debug = get_commands_for_extension(extension)
157144

158-
if option == "lazygit" then
159-
lazygit()
160-
elseif option == "Run" then
161-
overlay(run, M.config.overlay_sleep, "Run")
162-
elseif option == "RunV" then
163-
split_window(run, "-v", "Run")
164-
elseif option == "RunH" then
165-
split_window(run, "-h", "Run")
166-
elseif option == "RunBG" then
167-
new_window(run, "Run")
168-
elseif option == "Make" then
169-
overlay(make, M.config.overlay_sleep, "Make")
170-
elseif option == "MakeV" then
171-
split_window(make, "-v", "Make")
172-
elseif option == "MakeH" then
173-
split_window(make, "-h", "Make")
174-
elseif option == "MakeBG" then
175-
new_window(make, "Make")
176-
elseif option == "Debug" then
177-
overlay(debug, M.config.overlay_sleep, "Debug")
178-
elseif option == "DebugV" then
179-
split_window(debug, "-v", "Debug")
180-
elseif option == "DebugH" then
181-
split_window(debug, "-h", "Debug")
182-
elseif option == "DebugBG" then
183-
new_window(debug, "Debug")
145+
local commands = {
146+
lazygit = lazygit,
147+
Run = function() overlay(run, M.config.overlay_sleep, "Run") end,
148+
RunV = function() split_window(run, "-v", "Run") end,
149+
RunH = function() split_window(run, "-h", "Run") end,
150+
RunBG = function() new_window(run, "Run") end,
151+
Make = function() overlay(make, M.config.overlay_sleep, "Make") end,
152+
MakeV = function() split_window(make, "-v", "Make") end,
153+
MakeH = function() split_window(make, "-h", "Make") end,
154+
MakeBG = function() new_window(make, "Make") end,
155+
Debug = function() overlay(debug, M.config.overlay_sleep, "Debug") end,
156+
DebugV = function() split_window(debug, "-v", "Debug") end,
157+
DebugH = function() split_window(debug, "-h", "Debug") end,
158+
DebugBG = function() new_window(debug, "Debug") end
159+
}
160+
161+
if commands[option] then
162+
commands[option]()
184163
else
185164
print("Error: Invalid option.")
186165
end
@@ -192,11 +171,12 @@ vim.api.nvim_create_user_command('TMUXcompile', function(args)
192171
end, {
193172
nargs = 1,
194173
complete = function()
195-
return { "lazygit",
196-
"Run", "RunV", "RunH", "RunBG",
197-
"Make", "MakeV", "MakeH", "MakeBG",
198-
"Debug", "DebugV", "DebugH", "DebugBG"
199-
}
174+
return {
175+
"lazygit",
176+
"Run", "RunV", "RunH", "RunBG",
177+
"Make", "MakeV", "MakeH", "MakeBG",
178+
"Debug", "DebugV", "DebugH", "DebugBG"
179+
}
200180
end,
201181
})
202182

0 commit comments

Comments
 (0)