Skip to content

Commit e7dfe31

Browse files
authored
Merge pull request #4 from DaniloDjokic/main
Support for defining project override configs in the project directory (instead of the nvim config directory)
2 parents 41ae834 + 22b9105 commit e7dfe31

File tree

5 files changed

+200
-128
lines changed

5 files changed

+200
-128
lines changed

README.md

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ within current Neovim session on an overlay terminal.
1515

1616
Install using your favorite plugin manager. For example, using
1717
[lazy.nvim](https://github.com/folke/lazy.nvim):
18+
1819
```lua
1920
{ 'karshPrime/tmux-compile.nvim', event = 'VeryLazy' },
2021
```
22+
2123
And setup it with:
24+
2225
```lua
2326
require('tmux-compile').setup({
2427
-- Overriding Default Configurations. [OPTIONAL]
@@ -58,7 +61,7 @@ require('tmux-compile').setup({
5861
}
5962
},
6063

61-
-- Directory override config. [OPTIONAL]
64+
-- Directory override config. [OPTIONAL]
6265
-- Set actions for a specific directory (per project basis)
6366
project_override_config = {
6467
{
@@ -74,6 +77,31 @@ require('tmux-compile').setup({
7477
-- Only build will work for this path
7578
}
7679
}
80+
81+
82+
-- Defining project overrides locally.
83+
-- Along with the previously defined 'project_override_config' it is also possible to define build/run/debug actions
84+
-- locally inside the project working directory.
85+
-- The plugin will look for a configuratino file called 'tmux-compile.lua' inside the following directories
86+
-- RELATIVE to the vim current owrking directory:
87+
-- ./.nvim/
88+
-- ./nvim/
89+
-- ./
90+
-- example tmux-compile.lua file
91+
92+
return {
93+
build = "make",
94+
run = "make run"
95+
}
96+
97+
-- IMPORTANT: configuration precedence
98+
-- When starting, this plugin will load and apply the build/run/debug commands in the following order
99+
100+
-- 1. tmux-compile.lua
101+
-- 2. project_override_config
102+
-- 3. build_run_config
103+
104+
-- If there is no tmux-compile.lua file defined in the current working directory, the plugin will load the commands from the 'project_override_config' table inside the main config. If that is also not defined for the current working directory, then the plugin will default to the commands defined by the file extension of the current buffer
77105
})
78106

79107
```
@@ -85,29 +113,31 @@ Create keybindings for any command by adding the following to Neovim config:
85113
```lua
86114
vim.keymap.set('n', 'KEYBIND', 'COMMAND<CR>', {silent=true})
87115
```
116+
88117
Example: to set F5 to compile and run current project in an overlay terminal
89118
window-
119+
90120
```lua
91121
vim.keymap.set('n','<F5>', ':TMUXcompile Run<CR>', {silent=true})
92122
```
93123

94124
### List of all supported commands
95125

96-
| Action / Purpose | Command |
97-
|---------------------------------------------------------|-----------------------|
98-
| Compile program in an overlay terminal window | `:TMUXcompile Make` |
99-
| Compile program in a new tmux window | `:TMUXcompile MakeBG` |
100-
| Compile program in a new pane next to current nvim pane | `:TMUXcompile MakeV` |
101-
| Compile program in a new pane bellow current nvim pane | `:TMUXcompile MakeH` |
102-
| Run program in an overlay terminal window | `:TMUXcompile Run` |
103-
| Run program in a tmux new window | `:TMUXcompile RunBG` |
104-
| Run program in a new pane next to current nvim pane | `:TMUXcompile RunV` |
105-
| Run program in a new pane bellow current nvim pane | `:TMUXcompile RunH` |
106-
| Start debugger in an overlay terminal window | `:TMUXcompile Debug` |
107-
| Start debugger in a tmux new window | `:TMUXcompile DebugBG`|
108-
| Start debugger in a new pane next to current nvim pane | `:TMUXcompile DebugV` |
109-
| Start debugger in a new pane bellow current nvim pane | `:TMUXcompile DebugH` |
110-
| Open lazygit in overlay | `:TMUXcompile lazygit`|
126+
| Action / Purpose | Command |
127+
| ------------------------------------------------------- | ---------------------- |
128+
| Compile program in an overlay terminal window | `:TMUXcompile Make` |
129+
| Compile program in a new tmux window | `:TMUXcompile MakeBG` |
130+
| Compile program in a new pane next to current nvim pane | `:TMUXcompile MakeV` |
131+
| Compile program in a new pane bellow current nvim pane | `:TMUXcompile MakeH` |
132+
| Run program in an overlay terminal window | `:TMUXcompile Run` |
133+
| Run program in a tmux new window | `:TMUXcompile RunBG` |
134+
| Run program in a new pane next to current nvim pane | `:TMUXcompile RunV` |
135+
| Run program in a new pane bellow current nvim pane | `:TMUXcompile RunH` |
136+
| Start debugger in an overlay terminal window | `:TMUXcompile Debug` |
137+
| Start debugger in a tmux new window | `:TMUXcompile DebugBG` |
138+
| Start debugger in a new pane next to current nvim pane | `:TMUXcompile DebugV` |
139+
| Start debugger in a new pane bellow current nvim pane | `:TMUXcompile DebugH` |
140+
| Open lazygit in overlay | `:TMUXcompile lazygit` |
111141

112142
\* **Run** here includes both compiling and running the program, depending on the
113143
run command specified for the file extension.
@@ -124,4 +154,3 @@ previously unnamed list, resulting in incompatibility with older configurations.
124154
Apologies for any inconvenience this may cause. From version 2, the plugin has been
125155
designed with future-proofing in mind to ensure that such issues do not recur.
126156
</details>
127-

lua/tmux-compile/actions.lua

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,41 @@
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, aWindowName, 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+
vim.notify("Error: " .. aErrorName .. " command not found for ." .. lExtension, vim.log.levels.ERROR)
1413

1514
return 1
1615
end
1716

18-
if Helpers.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

3128
--
3229
-- run command in an overlay pane
33-
function Actions.overlay( aCmd, aSleepDuration, aWidth, aHeight, aErrorName )
30+
function Actions.overlay(aCmd, aSleepDuration, aWidth, aHeight, aErrorName)
3431
if not aCmd then
3532
local lExtension = Helpers.get_file_extension()
36-
print( "Error: " .. aErrorName .. " command not found for ." .. lExtension )
33+
vim.notify("Error: " .. aErrorName .. " command not found for ." .. lExtension, vim.log.levels.ERROR)
3734

3835
return 1
3936
end
4037

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

4540
local aCmdHead = "tmux display-popup -E -d" .. lProjectDir
4641
local lDimensions = " -w " .. aWidth .. "\\% -h " .. aHeight .. "\\% '"
@@ -52,15 +47,15 @@ function Actions.overlay( aCmd, aSleepDuration, aWidth, aHeight, aErrorName )
5247
lSleep = "; sleep " .. aSleepDuration .. "'"
5348
end
5449

55-
vim.fn.system( aCmdHead .. lDimensions .. aCmd .. lSleep )
50+
vim.fn.system(aCmdHead .. lDimensions .. aCmd .. lSleep)
5651
end
5752

5853
--
5954
-- run command in same window on a new pane
60-
function Actions.split_window( aCmd, aSide, aWidth, aHeight, aNewPane, aErrorName )
55+
function Actions.split_window(aCmd, aSide, aWidth, aHeight, aNewPane, aErrorName)
6156
if not aCmd then
6257
local lExtension = Helpers.get_file_extension()
63-
print( "Error: " .. aErrorName .. " command not found for ." .. lExtension )
58+
vim.notify("Error: " .. aErrorName .. " command not found for ." .. lExtension, vim.log.levels.ERROR)
6459

6560
return 1
6661
end
@@ -75,32 +70,40 @@ function Actions.split_window( aCmd, aSide, aWidth, aHeight, aNewPane, aErrorNam
7570
h = aWidth,
7671
}
7772

78-
local lCurrentPane = vim.fn.system( "tmux display -p '#{pane_id}'" )
79-
vim.fn.system( "tmux selectp " .. lDirectionLookup[aSide] )
80-
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}'")
8176

82-
aCmd = Helpers.change_dir( vim.trim(lMovedPane) ) .. aCmd
77+
aCmd = Helpers.change_dir(vim.trim(lMovedPane)) .. aCmd
8378

84-
if vim.trim( lCurrentPane ) == vim.trim( lMovedPane ) or aNewPane then
79+
if vim.trim(lCurrentPane) == vim.trim(lMovedPane) or aNewPane then
8580
local lParameters = aSide .. " -l " .. lLengthPercentage[aSide] .. "%"
86-
vim.fn.system( "tmux splitw -" .. lParameters .. " '" .. aCmd .. "; zsh'" )
81+
vim.fn.system("tmux splitw -" .. lParameters .. " '" .. aCmd .. "; zsh'")
8782
else
88-
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")
8984
end
9085

9186
-- return to nvim pane
92-
vim.fn.system( "tmux select-pane -l" )
87+
vim.fn.system("tmux select-pane -l")
9388
end
9489

90+
--
91+
-- run yazi in an overlay pane
92+
function Actions.yazi(aWidth, aHeight, aErrorName)
93+
if vim.fn.executable("yazi") == 1 then
94+
Actions.overlay("yazi", 0, aWidth, aHeight, aErrorName)
95+
else
96+
vim.notify("Error: yazi not installed.", vim.log.levels.ERROR)
97+
end
98+
end
9599
--
96100
-- run lazygit in an overlay pane
97-
function Actions.lazygit( aWidth, aHeight, aErrorName )
98-
if vim.fn.executable( "lazygit" ) == 1 then
99-
Actions.overlay( "lazygit", 0, aWidth, aHeight, aErrorName )
101+
function Actions.lazygit(aWidth, aHeight, aErrorName)
102+
if vim.fn.executable("lazygit") == 1 then
103+
Actions.overlay("lazygit", 0, aWidth, aHeight, aErrorName)
100104
else
101-
print( "Error: lazygit not installed." )
105+
vim.notify("Error: lazygit not installed.", vim.log.levels.ERROR)
102106
end
103107
end
104108

105109
return Actions
106-

lua/tmux-compile/commands.lua

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,74 @@
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")
2524
end
2625

2726
local lMake, lRun, lDebug
2827

2928
local function load_from_extension()
30-
local lExtension = Helpers.get_file_extension()
31-
lMake, lRun, lDebug = Helpers.get_commands_for_extension( lExtension, aConfig )
29+
local lExtension = Helpers.get_file_extension()
30+
lMake, lRun, lDebug = Helpers.get_commands_for_extension(lExtension, aConfig)
3231
end
3332

34-
local lIsDirectoryOverrideSet = aConfig.project_override_config ~= nil
3533
local lIsDirectoryOverrideFound = false
3634

37-
if lIsDirectoryOverrideSet then
38-
local lOverrideConfig = Helpers.get_matched_directory_override( aConfig )
35+
if aConfig.override_config_from_project then
36+
local lOverrideConfig = aConfig.project_override_config
37+
lMake, lRun, lDebug = lOverrideConfig.build, lOverrideConfig.run, lOverrideConfig.debug
38+
lIsDirectoryOverrideFound = true
39+
else
40+
local lIsDirectoryOverrideSet = aConfig.project_override_config ~= nil
41+
42+
if lIsDirectoryOverrideSet then
43+
local lOverrideConfig = Helpers.get_matched_directory_override(aConfig)
3944

40-
if lOverrideConfig ~= nil then
41-
lMake, lRun, lDebug = lOverrideConfig.build, lOverrideConfig.run, lOverrideConfig.debug
42-
lIsDirectoryOverrideFound = true
45+
if lOverrideConfig ~= nil then
46+
lMake, lRun, lDebug = lOverrideConfig.build, lOverrideConfig.run, lOverrideConfig.debug
47+
lIsDirectoryOverrideFound = true
48+
else
49+
load_from_extension()
50+
end
4351
else
4452
load_from_extension()
4553
end
46-
else
47-
load_from_extension()
4854
end
4955

5056
local commands = {
51-
Run = { command = lRun, title = "Run" },
52-
Make = { command = lMake, title = "Make" },
57+
Run = { command = lRun, title = "Run" },
58+
Make = { command = lMake, title = "Make" },
5359
Debug = { command = lDebug, title = "Debug" },
5460
}
5561

56-
local function execute_command( cmd, lOrientation, lBackground )
62+
local function execute_command(cmd, lOrientation, lBackground)
5763
local lCommandInfo = commands[cmd]
5864

5965
if not lCommandInfo then
60-
print( "Error: Invalid aOption." )
66+
print("Error: Invalid aOption.")
6167
return
6268
end
6369

6470
if lIsDirectoryOverrideFound and lCommandInfo.command == nil then
65-
print( "Error: override for directory set but no command found." )
71+
print("Error: override for directory set but no command found.")
6672
return
6773
end
6874

@@ -72,7 +78,7 @@ function Commands.dispatch( aOption, aConfig )
7278
end
7379

7480
if lBackground then
75-
action( lCommandInfo.command, aConfig.build_run_window_title, lCommandInfo.title )
81+
action(lCommandInfo.command, aConfig.build_run_window_title, lCommandInfo.title)
7682
elseif lOrientation then
7783
action(
7884
lCommandInfo.command,
@@ -94,25 +100,26 @@ function Commands.dispatch( aOption, aConfig )
94100
end
95101

96102
if aOption == "lazygit" then
97-
Actions.lazygit( aConfig.overlay_width_percent, aConfig.overlay_height_percent )
103+
Actions.lazygit(aConfig.overlay_width_percent, aConfig.overlay_height_percent)
104+
elseif aOption == "yazi" then
105+
Actions.yazi(aConfig.overlay_width_percent, aConfig.overlay_height_percent)
98106
else
99107
local lOrientation = nil
100108
local lBackground = false
101109

102-
if aOption:sub( -1 ) == "V" then
110+
if aOption:sub(-1) == "V" then
103111
lOrientation = "v"
104-
aOption = aOption:sub( 1, -2 )
105-
elseif aOption:sub( -1 ) == "H" then
112+
aOption = aOption:sub(1, -2)
113+
elseif aOption:sub(-1) == "H" then
106114
lOrientation = "h"
107-
aOption = aOption:sub( 1, -2 )
108-
elseif aOption:sub( -2 ) == "BG" then
115+
aOption = aOption:sub(1, -2)
116+
elseif aOption:sub(-2) == "BG" then
109117
lBackground = true
110-
aOption = aOption:sub( 1, -3 )
118+
aOption = aOption:sub(1, -3)
111119
end
112120

113-
execute_command( aOption, lOrientation, lBackground )
121+
execute_command(aOption, lOrientation, lBackground)
114122
end
115123
end
116124

117125
return Commands
118-

0 commit comments

Comments
 (0)