Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ require('tmux-compile').setup({
-- missing
}
}

-- Directory override config. [OPTIONAL]
-- If you want to set Run, Build and Debug actions for a specific directory (per project basis)
-- Note: do not use '~' for home directory, use full path
project_override_config = {
{
project_base_dir = '/path/to/project',
build = 'make',
run = 'make run',
debug = 'lldb',
},
{
project_base_dir = '/path/to/another/project',
build = 'cargo build',
-- Only build will work for this path
}
}
})
```

Expand Down
80 changes: 35 additions & 45 deletions lua/tmux-compile/actions.lua
Original file line number Diff line number Diff line change
@@ -1,110 +1,100 @@

-- Actions.Lua

local Helpers = require( "tmux-compile.helpers" )
local Helpers = require("tmux-compile.helpers")

local Actions = {}

--
-- run command in a new or existing tmux window
function Actions.new_window( aCmd, aWindowTitle, aErrorName )
function Actions.new_window(aCmd, aWindowName, aErrorName)
if not aCmd then
local lExtension = Helpers.get_file_extension()
print( "Error: " .. aErrorName .. " command not found for ." .. lExtension )
print("Error: " .. aErrorName .. " command not found for ." .. lExtension)

return 1
end

if tmux_window_exists( aWindowName ) then
aCmd = Helpers.change_dir( aWindowName ) .. aCmd
if Helpers.tmux_window_exists(aWindowName) then
aCmd = Helpers.change_dir(aWindowName) .. aCmd

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

vim.fn.system( "tmux neww -c " .. lProjectDir .. aWindowName .. " '" .. aCmd .. "; zsh'")
vim.fn.system("tmux neww -c " .. lProjectDir .. aWindowName .. " '" .. aCmd .. "; zsh'")
end
end


--
-- run command in an overlay pane
function Actions.overlay( aCmd, aSleepDuration, aWidth, aHeight, aErrorName )
function Actions.overlay(aCmd, aSleepDuration, aWidth, aHeight, aErrorName)
if not aCmd then
local lExtension = Helpers.get_file_extension()
print( "Error: " .. aErrorName .. " command not found for ." .. lExtension )
print("Error: " .. aErrorName .. " command not found for ." .. lExtension)

return 1
end

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

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

local lSleep
if aSleepDuration < 0 then
lSleep = "; read'"
else
lSleep = "; sleep " .. aSleepDuration .. "'"
end
local lSleep
if aSleepDuration < 0 then
lSleep = "; read'"
else
lSleep = "; sleep " .. aSleepDuration .. "'"
end

vim.fn.system( aCmdHead .. lDimensions .. aCmd .. lSleep )
vim.fn.system(aCmdHead .. lDimensions .. aCmd .. lSleep)
end


--
-- run command in same window on a new pane
function Actions.split_window( aCmd, aSide, aWidth, aHeight, aNewPane, aErrorName )
function Actions.split_window(aCmd, aSide, aWidth, aHeight, aNewPane, aErrorName)
if not aCmd then
local lExtension = Helpers.get_file_extension()
print( "Error: " .. aErrorName .. " command not found for ." .. lExtension )
print("Error: " .. aErrorName .. " command not found for ." .. lExtension)

return 1
end

local lDirectionLookup = {
v = "-D",
h = "-R"
h = "-R",
}

local lLengthPercentage = {
v = aHeight,
h = aWidth
h = aWidth,
}

local lCurrentPane = vim.fn.system( "tmux display -p '#{pane_id}'" )
vim.fn.system( "tmux selectp " .. lDirectionLookup[aSide] )
local lMovedPane = vim.fn.system( "tmux display -p '#{pane_id}'" )
local lCurrentPane = vim.fn.system("tmux display -p '#{pane_id}'")
vim.fn.system("tmux selectp " .. lDirectionLookup[aSide])
local lMovedPane = vim.fn.system("tmux display -p '#{pane_id}'")

aCmd = Helpers.change_dir( vim.trim(lMovedPane) ) .. aCmd
aCmd = Helpers.change_dir(vim.trim(lMovedPane)) .. aCmd

if ( vim.trim(lCurrentPane) == vim.trim(lMovedPane) or aNewPane ) then
if vim.trim(lCurrentPane) == vim.trim(lMovedPane) or aNewPane then
local lParameters = aSide .. " -l " .. lLengthPercentage[aSide] .. "%"
vim.fn.system("tmux splitw -" .. lParameters .. " '" .. aCmd .. "; zsh'")
else
vim.fn.system( "tmux send -t " .. vim.trim(lMovedPane) .. " '" .. aCmd .. "' C-m" )
vim.fn.system("tmux send -t " .. vim.trim(lMovedPane) .. " '" .. aCmd .. "' C-m")
end

-- return to nvim pane
vim.fn.system( "tmux select-pane -l" )
-- return to nvim pane
vim.fn.system("tmux select-pane -l")
end


--
-- run lazygit in an overlay pane
function Actions.lazygit( aWidth, aHeight )
if vim.fn.executable( "lazygit" ) == 1 then
Actions.overlay( "lazygit", 0, aWidth, aHeight, aErrorName )
function Actions.lazygit(aWidth, aHeight, aErrorName)
if vim.fn.executable("lazygit") == 1 then
Actions.overlay("lazygit", 0, aWidth, aHeight, aErrorName)
else
print( "Error: lazygit not installed." )
print("Error: lazygit not installed.")
end
end


return Actions

69 changes: 43 additions & 26 deletions lua/tmux-compile/commands.lua
Original file line number Diff line number Diff line change
@@ -1,56 +1,77 @@

-- Commands.Lua

local Actions = require( "tmux-compile.actions" )
local Helpers = require( "tmux-compile.helpers" )
local Env = require( "tmux-compile.env" )
local Actions = require("tmux-compile.actions")
local Helpers = require("tmux-compile.helpers")
local Env = require("tmux-compile.env")

local Commands = {}

--
-- commands dispatch
function Commands.dispatch( aOption, aConfig )
function Commands.dispatch(aOption, aConfig)
if not Env.is_tmux_installed() then
print( "Error: install TMUX to use the plugin" )
print("Error: install TMUX to use the plugin")
return 1
end

if not Env.is_tmux_running() then
print( "Error: run session in TMUX" )
print("Error: run session in TMUX")
return 1
end

if aConfig.save_session then
vim.cmd( ":wall" )
vim.cmd(":wall")
end

local lMake, lRun, lDebug

local function load_from_extension()
local lExtension = Helpers.get_file_extension()
lMake, lRun, lDebug = Helpers.get_commands_for_extension(lExtension, aConfig)
end

local lExtension = Helpers.get_file_extension()
local lMake, lRun, lDebug = Helpers.get_commands_for_extension( lExtension, aConfig )
local lIsDirectoryOverrideSet = aConfig.project_override_config ~= nil
local lIsDirectoryOverrideFound = false

if lIsDirectoryOverrideSet then
local lOverrideConfig = Helpers.get_matched_directory_override(aConfig)

if lOverrideConfig ~= nil then
lMake, lRun, lDebug = lOverrideConfig.build, lOverrideConfig.run, lOverrideConfig.debug
lIsDirectoryOverrideFound = true
else
load_from_extension()
end
else
load_from_extension()
end

local commands = {
Run = { command = lRun, title = "Run" },
Make = { command = lMake, title = "Make" },
Debug = { command = lDebug, title = "Debug" }
Run = { command = lRun, title = "Run" },
Make = { command = lMake, title = "Make" },
Debug = { command = lDebug, title = "Debug" },
}

local function execute_command( cmd, lOrientation, lBackground )
local function execute_command(cmd, lOrientation, lBackground)
local lCommandInfo = commands[cmd]

if not lCommandInfo then
print("Error: Invalid aOption.")
return
end

if lIsDirectoryOverrideFound and lCommandInfo.command == nil then
print("Error: override for directory set but no command found.")
return
end

local action = lBackground and Actions.new_window or Actions.overlay
if lOrientation then
action = Actions.split_window
end

if lBackground then
action(
lCommandInfo.command,
aConfig.build_run_window_title,
lCommandInfo.title
)
action(lCommandInfo.command, aConfig.build_run_window_title, lCommandInfo.title)
elseif lOrientation then
action(
lCommandInfo.command,
Expand All @@ -62,7 +83,7 @@ function Commands.dispatch( aOption, aConfig )
)
else
action(
lCommandInfo.command,
lCommandInfo.command,
aConfig.overlay_sleep,
aConfig.overlay_width_percent,
aConfig.overlay_height_percent,
Expand All @@ -72,28 +93,24 @@ function Commands.dispatch( aOption, aConfig )
end

if aOption == "lazygit" then
Actions.lazygit( aConfig.overlay_width_percent, aConfig.overlay_height_percent )
Actions.lazygit(aConfig.overlay_width_percent, aConfig.overlay_height_percent)
else
local lOrientation = nil
local lBackground = false

if aOption:sub(-1) == "V" then
lOrientation = "v"
aOption = aOption:sub(1, -2)

elseif aOption:sub(-1) == "H" then
lOrientation = "h"
aOption = aOption:sub(1, -2)

elseif aOption:sub(-2) == "BG" then
lBackground = true
aOption = aOption:sub(1, -3)
end

execute_command( aOption, lOrientation, lBackground )
execute_command(aOption, lOrientation, lBackground)
end
end

return Commands


Loading