Skip to content

Commit 7e47f37

Browse files
committed
refactor: commands
reduce code repeat
1 parent 4a4bb5b commit 7e47f37

File tree

1 file changed

+64
-27
lines changed

1 file changed

+64
-27
lines changed

lua/tmux-compile/commands.lua

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
local Actions = require( "tmux-compile.actions" )
55
local Helpers = require( "tmux-compile.helpers" )
6-
local Env = require( "tmux-compile.env" )
6+
local Env = require( "tmux-compile.env" )
77

88
local Commands = {}
99

@@ -26,37 +26,74 @@ function Commands.dispatch( aOption, aConfig )
2626

2727
local lExtension = Helpers.get_file_extension()
2828
local lMake, lRun, lDebug = Helpers.get_commands_for_extension( lExtension, aConfig )
29-
local lEveryTime = aConfig.new_pane_everytime
30-
local lBHeight = aConfig.bottom_height_percent
31-
local lSWidth = aConfig.side_width_percent
32-
local lOHeight = aConfig.overlay_height_percent
33-
local lOWidth = aConfig.overlay_width_percent
34-
35-
local lCommands = {
36-
Run = function() Actions.overlay( lRun, aConfig.overlay_sleep, lOWidth, lOHeight, "Run" ) end,
37-
RunV = function() Actions.split_window( lRun, "v", lSWidth, lBHeight, lEveryTime, "Run" ) end,
38-
RunH = function() Actions.split_window( lRun, "h", lSWidth, lBHeight, lEveryTime, "Run" ) end,
39-
RunBG = function() Actions.new_window( lRun, aConfig.build_run_window_title, "Run" ) end,
40-
41-
Make = function() Actions.overlay( lMake, aConfig.overlay_sleep, lOWidth, lOHeight, "Make" ) end,
42-
MakeV = function() Actions.split_window( lMake, "v", lSWidth, lBHeight, lEveryTime, "Make" ) end,
43-
MakeH = function() Actions.split_window( lMake, "h", lSWidth, lBHeight, lEveryTime, "Make" ) end,
44-
MakeBG = function() Actions.new_window( lMake, aConfig.build_run_window_title, "Make" ) end,
45-
46-
Debug = function() Actions.overlay( lDebug, aConfig.overlay_sleep, lOWidth, lOHeight, "Debug" ) end,
47-
DebugV = function() Actions.split_window( lDebug, "v", lSWidth, lBHeight, lEveryTime, "Debug" ) end,
48-
DebugH = function() Actions.split_window( lDebug, "h", lSWidth, lBHeight, lEveryTime, "Debug" ) end,
49-
DebugBG = function() Actions.new_window( lDebug, aConfig.build_run_window_title, "Debug" ) end,
50-
51-
lazygit = function() Actions.lazygit( lOWidth, lOHeight ) end
29+
30+
local commands = {
31+
Run = { command = lRun, title = "Run" },
32+
Make = { command = lMake, title = "Make" },
33+
Debug = { command = lDebug, title = "Debug" }
5234
}
5335

54-
if lCommands[aOption] then
55-
lCommands[aOption]()
36+
local function execute_command( cmd, lOrientation, lBackground )
37+
local lCommandInfo = commands[cmd]
38+
if not lCommandInfo then
39+
print("Error: Invalid aOption.")
40+
return
41+
end
42+
43+
local action = lBackground and Actions.new_window or Actions.overlay
44+
if lOrientation then
45+
action = Actions.split_window
46+
end
47+
48+
if lBackground then
49+
action(
50+
lCommandInfo.command,
51+
aConfig.build_run_window_title,
52+
lCommandInfo.title
53+
)
54+
elseif lOrientation then
55+
action(
56+
lCommandInfo.command,
57+
lOrientation,
58+
aConfig.side_width_percent,
59+
aConfig.bottom_height_percent,
60+
aConfig.new_pane_everytime,
61+
lCommandInfo.title
62+
)
63+
else
64+
action(
65+
lCommandInfo.command,
66+
aConfig.overlay_sleep,
67+
aConfig.overlay_width_percent,
68+
aConfig.overlay_height_percent,
69+
lCommandInfo.title
70+
)
71+
end
72+
end
73+
74+
if aOption == "lazygit" then
75+
Actions.lazygit( aConfig.overlay_width_percent, aConfig.overlay_height_percent )
5676
else
57-
print( "Error: Invalid aOption." )
77+
local lOrientation = nil
78+
local lBackground = false
79+
80+
if aOption:sub(-1) == "V" then
81+
lOrientation = "v"
82+
aOption = aOption:sub(1, -2)
83+
84+
elseif aOption:sub(-1) == "H" then
85+
lOrientation = "h"
86+
aOption = aOption:sub(1, -2)
87+
88+
elseif aOption:sub(-2) == "BG" then
89+
lBackground = true
90+
aOption = aOption:sub(1, -3)
91+
end
92+
93+
execute_command( aOption, lOrientation, lBackground )
5894
end
5995
end
6096

6197
return Commands
6298

99+

0 commit comments

Comments
 (0)