Skip to content

Commit 39a9b4b

Browse files
authored
Merge: pull request #1 from DaniloDjokic/main
* allow commands to be set on a directory level
2 parents 7513d0c + c304d35 commit 39a9b4b

File tree

6 files changed

+155
-108
lines changed

6 files changed

+155
-108
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ require('tmux-compile').setup({
5757
-- missing
5858
}
5959
}
60+
61+
-- Directory override config. [OPTIONAL]
62+
-- If you want to set Run, Build and Debug actions for a specific directory (per project basis)
63+
-- Note: do not use '~' for home directory, use full path
64+
project_override_config = {
65+
{
66+
project_base_dir = '/path/to/project',
67+
build = 'make',
68+
run = 'make run',
69+
debug = 'lldb',
70+
},
71+
{
72+
project_base_dir = '/path/to/another/project',
73+
build = 'cargo build',
74+
-- Only build will work for this path
75+
}
76+
}
6077
})
6178
```
6279

lua/tmux-compile/actions.lua

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,100 @@
1-
21
-- Actions.Lua
32

4-
local Helpers = require( "tmux-compile.helpers" )
3+
local Helpers = require("tmux-compile.helpers")
54

65
local Actions = {}
76

87
--
98
-- run command in a new or existing tmux window
10-
function Actions.new_window( aCmd, aWindowTitle, aErrorName )
9+
function Actions.new_window(aCmd, aWindowName, aErrorName)
1110
if not aCmd then
1211
local lExtension = Helpers.get_file_extension()
13-
print( "Error: " .. aErrorName .. " command not found for ." .. lExtension )
12+
print("Error: " .. aErrorName .. " command not found for ." .. lExtension)
1413

1514
return 1
1615
end
1716

18-
if tmux_window_exists( aWindowName ) then
19-
aCmd = Helpers.change_dir( aWindowName ) .. aCmd
17+
if Helpers.tmux_window_exists(aWindowName) then
18+
aCmd = Helpers.change_dir(aWindowName) .. aCmd
2019

21-
vim.fn.system( "tmux selectw -t " .. aWindowName .. " \\; send-keys '" .. aCmd .. "' C-m" )
20+
vim.fn.system("tmux selectw -t " .. aWindowName .. " \\; send-keys '" .. aCmd .. "' C-m")
2221
else
23-
local lProjectDir = vim.fn.trim(
24-
vim.fn.system("git rev-parse --show-toplevel 2>/dev/null || pwd")
25-
) .. " -n "
22+
local lProjectDir = vim.fn.trim(vim.fn.system("git rev-parse --show-toplevel 2>/dev/null || pwd")) .. " -n "
2623

27-
vim.fn.system( "tmux neww -c " .. lProjectDir .. aWindowName .. " '" .. aCmd .. "; zsh'")
24+
vim.fn.system("tmux neww -c " .. lProjectDir .. aWindowName .. " '" .. aCmd .. "; zsh'")
2825
end
2926
end
3027

31-
3228
--
3329
-- run command in an overlay pane
34-
function Actions.overlay( aCmd, aSleepDuration, aWidth, aHeight, aErrorName )
30+
function Actions.overlay(aCmd, aSleepDuration, aWidth, aHeight, aErrorName)
3531
if not aCmd then
3632
local lExtension = Helpers.get_file_extension()
37-
print( "Error: " .. aErrorName .. " command not found for ." .. lExtension )
33+
print("Error: " .. aErrorName .. " command not found for ." .. lExtension)
3834

3935
return 1
4036
end
4137

42-
local lProjectDir = vim.fn.trim(
43-
vim.fn.system("git rev-parse --show-toplevel 2>/dev/null || pwd")
44-
)
38+
local lProjectDir = vim.fn.trim(vim.fn.system("git rev-parse --show-toplevel 2>/dev/null || pwd"))
4539

46-
local aCmdHead = "tmux display-popup -E -d" .. lProjectDir
40+
local aCmdHead = "tmux display-popup -E -d" .. lProjectDir
4741
local lDimensions = " -w " .. aWidth .. "\\% -h " .. aHeight .. "\\% '"
4842

49-
local lSleep
50-
if aSleepDuration < 0 then
51-
lSleep = "; read'"
52-
else
53-
lSleep = "; sleep " .. aSleepDuration .. "'"
54-
end
43+
local lSleep
44+
if aSleepDuration < 0 then
45+
lSleep = "; read'"
46+
else
47+
lSleep = "; sleep " .. aSleepDuration .. "'"
48+
end
5549

56-
vim.fn.system( aCmdHead .. lDimensions .. aCmd .. lSleep )
50+
vim.fn.system(aCmdHead .. lDimensions .. aCmd .. lSleep)
5751
end
5852

59-
6053
--
6154
-- run command in same window on a new pane
62-
function Actions.split_window( aCmd, aSide, aWidth, aHeight, aNewPane, aErrorName )
55+
function Actions.split_window(aCmd, aSide, aWidth, aHeight, aNewPane, aErrorName)
6356
if not aCmd then
6457
local lExtension = Helpers.get_file_extension()
65-
print( "Error: " .. aErrorName .. " command not found for ." .. lExtension )
58+
print("Error: " .. aErrorName .. " command not found for ." .. lExtension)
6659

6760
return 1
6861
end
6962

7063
local lDirectionLookup = {
7164
v = "-D",
72-
h = "-R"
65+
h = "-R",
7366
}
7467

7568
local lLengthPercentage = {
7669
v = aHeight,
77-
h = aWidth
70+
h = aWidth,
7871
}
7972

80-
local lCurrentPane = vim.fn.system( "tmux display -p '#{pane_id}'" )
81-
vim.fn.system( "tmux selectp " .. lDirectionLookup[aSide] )
82-
local lMovedPane = vim.fn.system( "tmux display -p '#{pane_id}'" )
73+
local lCurrentPane = vim.fn.system("tmux display -p '#{pane_id}'")
74+
vim.fn.system("tmux selectp " .. lDirectionLookup[aSide])
75+
local lMovedPane = vim.fn.system("tmux display -p '#{pane_id}'")
8376

84-
aCmd = Helpers.change_dir( vim.trim(lMovedPane) ) .. aCmd
77+
aCmd = Helpers.change_dir(vim.trim(lMovedPane)) .. aCmd
8578

86-
if ( vim.trim(lCurrentPane) == vim.trim(lMovedPane) or aNewPane ) then
79+
if vim.trim(lCurrentPane) == vim.trim(lMovedPane) or aNewPane then
8780
local lParameters = aSide .. " -l " .. lLengthPercentage[aSide] .. "%"
8881
vim.fn.system("tmux splitw -" .. lParameters .. " '" .. aCmd .. "; zsh'")
8982
else
90-
vim.fn.system( "tmux send -t " .. vim.trim(lMovedPane) .. " '" .. aCmd .. "' C-m" )
83+
vim.fn.system("tmux send -t " .. vim.trim(lMovedPane) .. " '" .. aCmd .. "' C-m")
9184
end
9285

93-
-- return to nvim pane
94-
vim.fn.system( "tmux select-pane -l" )
86+
-- return to nvim pane
87+
vim.fn.system("tmux select-pane -l")
9588
end
9689

97-
9890
--
9991
-- run lazygit in an overlay pane
100-
function Actions.lazygit( aWidth, aHeight )
101-
if vim.fn.executable( "lazygit" ) == 1 then
102-
Actions.overlay( "lazygit", 0, aWidth, aHeight, aErrorName )
92+
function Actions.lazygit(aWidth, aHeight, aErrorName)
93+
if vim.fn.executable("lazygit") == 1 then
94+
Actions.overlay("lazygit", 0, aWidth, aHeight, aErrorName)
10395
else
104-
print( "Error: lazygit not installed." )
96+
print("Error: lazygit not installed.")
10597
end
10698
end
10799

108-
109100
return Actions
110-

lua/tmux-compile/commands.lua

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,77 @@
1-
21
-- Commands.Lua
32

4-
local Actions = require( "tmux-compile.actions" )
5-
local Helpers = require( "tmux-compile.helpers" )
6-
local Env = require( "tmux-compile.env" )
3+
local Actions = require("tmux-compile.actions")
4+
local Helpers = require("tmux-compile.helpers")
5+
local Env = require("tmux-compile.env")
76

87
local Commands = {}
98

109
--
1110
-- commands dispatch
12-
function Commands.dispatch( aOption, aConfig )
11+
function Commands.dispatch(aOption, aConfig)
1312
if not Env.is_tmux_installed() then
14-
print( "Error: install TMUX to use the plugin" )
13+
print("Error: install TMUX to use the plugin")
1514
return 1
1615
end
1716

1817
if not Env.is_tmux_running() then
19-
print( "Error: run session in TMUX" )
18+
print("Error: run session in TMUX")
2019
return 1
2120
end
2221

2322
if aConfig.save_session then
24-
vim.cmd( ":wall" )
23+
vim.cmd(":wall")
24+
end
25+
26+
local lMake, lRun, lDebug
27+
28+
local function load_from_extension()
29+
local lExtension = Helpers.get_file_extension()
30+
lMake, lRun, lDebug = Helpers.get_commands_for_extension(lExtension, aConfig)
2531
end
2632

27-
local lExtension = Helpers.get_file_extension()
28-
local lMake, lRun, lDebug = Helpers.get_commands_for_extension( lExtension, aConfig )
33+
local lIsDirectoryOverrideSet = aConfig.project_override_config ~= nil
34+
local lIsDirectoryOverrideFound = false
35+
36+
if lIsDirectoryOverrideSet then
37+
local lOverrideConfig = Helpers.get_matched_directory_override(aConfig)
38+
39+
if lOverrideConfig ~= nil then
40+
lMake, lRun, lDebug = lOverrideConfig.build, lOverrideConfig.run, lOverrideConfig.debug
41+
lIsDirectoryOverrideFound = true
42+
else
43+
load_from_extension()
44+
end
45+
else
46+
load_from_extension()
47+
end
2948

3049
local commands = {
31-
Run = { command = lRun, title = "Run" },
32-
Make = { command = lMake, title = "Make" },
33-
Debug = { command = lDebug, title = "Debug" }
50+
Run = { command = lRun, title = "Run" },
51+
Make = { command = lMake, title = "Make" },
52+
Debug = { command = lDebug, title = "Debug" },
3453
}
3554

36-
local function execute_command( cmd, lOrientation, lBackground )
55+
local function execute_command(cmd, lOrientation, lBackground)
3756
local lCommandInfo = commands[cmd]
57+
3858
if not lCommandInfo then
3959
print("Error: Invalid aOption.")
4060
return
4161
end
4262

63+
if lIsDirectoryOverrideFound and lCommandInfo.command == nil then
64+
print("Error: override for directory set but no command found.")
65+
return
66+
end
67+
4368
local action = lBackground and Actions.new_window or Actions.overlay
4469
if lOrientation then
4570
action = Actions.split_window
4671
end
4772

4873
if lBackground then
49-
action(
50-
lCommandInfo.command,
51-
aConfig.build_run_window_title,
52-
lCommandInfo.title
53-
)
74+
action(lCommandInfo.command, aConfig.build_run_window_title, lCommandInfo.title)
5475
elseif lOrientation then
5576
action(
5677
lCommandInfo.command,
@@ -62,7 +83,7 @@ function Commands.dispatch( aOption, aConfig )
6283
)
6384
else
6485
action(
65-
lCommandInfo.command,
86+
lCommandInfo.command,
6687
aConfig.overlay_sleep,
6788
aConfig.overlay_width_percent,
6889
aConfig.overlay_height_percent,
@@ -72,28 +93,24 @@ function Commands.dispatch( aOption, aConfig )
7293
end
7394

7495
if aOption == "lazygit" then
75-
Actions.lazygit( aConfig.overlay_width_percent, aConfig.overlay_height_percent )
96+
Actions.lazygit(aConfig.overlay_width_percent, aConfig.overlay_height_percent)
7697
else
7798
local lOrientation = nil
7899
local lBackground = false
79100

80101
if aOption:sub(-1) == "V" then
81102
lOrientation = "v"
82103
aOption = aOption:sub(1, -2)
83-
84104
elseif aOption:sub(-1) == "H" then
85105
lOrientation = "h"
86106
aOption = aOption:sub(1, -2)
87-
88107
elseif aOption:sub(-2) == "BG" then
89108
lBackground = true
90109
aOption = aOption:sub(1, -3)
91110
end
92111

93-
execute_command( aOption, lOrientation, lBackground )
112+
execute_command(aOption, lOrientation, lBackground)
94113
end
95114
end
96115

97116
return Commands
98-
99-

0 commit comments

Comments
 (0)