Skip to content

Commit

Permalink
working version and testscript
Browse files Browse the repository at this point in the history
  • Loading branch information
m42e committed Sep 15, 2023
1 parent b0e7688 commit b0bc912
Show file tree
Hide file tree
Showing 11 changed files with 425 additions and 310 deletions.
1 change: 1 addition & 0 deletions clean.sh
@@ -0,0 +1 @@
rm -rf test/state/*
7 changes: 5 additions & 2 deletions lua/nvimux/config.lua
Expand Up @@ -5,12 +5,15 @@ M.default_opts = {
height = 20,
orientation = "v",
use_nearest = true,
reset_sequence = "q C-u",
reset_mode_sequence = {
["copy-mode"] = "q",
},
reset_cmdline_sequence = "C-u",
prompt_string = "Command? ",
runner_type = "pane",
runner_name = "",
tmux_command = "tmux",
open_extra_args = "",
open_extra_args = {},
expand_command = false,
close_on_exit = false,
command_shell = true,
Expand Down
206 changes: 159 additions & 47 deletions lua/nvimux/init.lua
Expand Up @@ -3,82 +3,194 @@ local tmux = require("nvimux.tmux")
local utils = require("nvimux.utils")

local M = {
last_command = {},
}

M.setup = function (user_opts)
config.setup(user_opts)
M.setup = function(user_opts)
config.setup(user_opts)
utils.check()
M.setup_commands()
end

M.prompt_command = function (...)
local completion = if config.get('command_shell') then 'shellcmd' else nil end
local command = nil
if #arg > 0 then
command = ''
for _,v in ipairs(arg) do
command = command .. tostring(v) .. " "
end
end

opts = {
prompt = config.get('prompt_string'),
completion = completion,
default = command,
}

vim.ui.input(opts, function(command)
if config.get('expand_command') then
local expanded_command = {}
for value in string.gmatch(command, "%S+") do
table.insert(expanded_command, vim.fn.expand(value))
end
command = ''
for _, v in ipairs(expanded_command) do
command = command .. ' ' .. v
end
M.prompt_command = function(prefix)
local completion = ""
if config.get("command_shell") then
completion = "shellcmd"
end
local prompt = config.get("prompt_string")
prefix = prefix or ""

local opts = {
prompt = prompt .. prefix,
completion = completion,
default = command,
}

vim.ui.input(opts, function(command)
if command ~= nil then
M.run(prefix .. command)
end
M.run(command)
end)
end)
end

M.run = function(command, autoreturn)
if not utils.runner_exists() then
utils.open()
end
autoreturn = autoreturn or true
table.insert(M.last_command, command)
utils.reset_runner()
utils.send_text(utils.expand_command(command))
if autoreturn then
utils.send_keys("Enter")
end
end

M.run_last = function()
if #M.last_command > 0 then
M.run(M.last_command[#M.last_command])
end
end

M.clear_history = function()
utils.clear_history()
utils.clear_history()
end

M.clear_terminal_screen = function()
utils.send_keys('C-l')
utils.send_keys("C-l")
end

M.interrupt_runner = function()
utils.send_keys('^c')
utils.send_keys("^c")
end

M.inspect_runner = function()
utils.select()
utils.copy_mode()
utils.select()
utils.copy_mode()
end

M.inspect_scroll_up = function()
M.inspect_runner()
utils.last()
utils.send_keys('C-u')
M.inspect_runner()
utils.last()
utils.send_keys("C-u")
end

M.inspect_scroll_down = function()
M.inspect_runner()
utils.last()
utils.send_keys('C-d')
M.inspect_runner()
utils.last()
utils.send_keys("C-d")
end

M.zoom_runner = function()
utils.zoom()
end

M.zoom_runner = function ()
utils.zoom()
M.close_runner = function()
utils.close()
end

M.close_runner = function ()
utils.close()
M.toggle = function()
utils.toggle()
end

M.toggle = function ()
utils.toggle()
local CMDS = {
{
name = "VimuxRunCommand",
opts = { desc = "nvimux: run command", nargs = "*", complete = "shellcmd" },
command = function(c)
M.run(c.args)
end,
},
{
name = "VimuxRunLastCommand",
opts = { desc = "nvimux: rerun last command", bar = true },
command = function()
M.run_last()
end,
},
{
name = "VimuxOpenRunner",
opts = { desc = "nvimux: open runner", bar = true },
command = function()
utils.open()
end,
},
{
name = "VimuxCloseRunner",
opts = { desc = "nvimux: close runner", bar = true },
command = function()
M.close_runner()
end,
},
{
name = "VimuxZoomRunner",
opts = { desc = "nvimux: zoom runner", bar = true },
command = function()
M.zoom_runner()
end,
},
{
name = "VimuxInspectRunner",
opts = { desc = "nvimux: inspect runner", bar = true },
command = function()
M.inspect_runner()
end,
},
{
name = "VimuxScrollUpInspect",
opts = { desc = "nvimux: scroll runner up", bar = true },
command = function()
M.inspect_scroll_down()
end,
},
{
name = "VimuxScrollDownInspect",
opts = { desc = "nvimux: scroll runner down", bar = true },
command = function()
M.inspect_scroll_down()
end,
},
{
name = "VimuxInterruptRunner",
opts = { desc = "nvimux: interrupt running", bar = true },
command = function()
M.interrupt_runner()
end,
},
{
name = "VimuxPromptCommand",
opts = { desc = "nvimux: interrupt running", nargs = "*" },
command = function(c)
M.prompt_command(c.args)
end,
},
{
name = "VimuxClearTerminalScreen",
opts = { desc = "nvimux: interrupt running", bar = true },
command = function()
M.clear_terminal_screen()
end,
},
{
name = "VimuxClearRunnerHistory",
opts = { desc = "nvimux: interrupt running", bar = true },
command = function()
M.clear_history()
end,
},
{
name = "VimuxTogglePane",
opts = { desc = "nvimux: interrupt running", bar = true },
command = function()
M.toggle()
end,
},
}

M.setup_commands = function()
for _, cmd in ipairs(CMDS) do
local opts = vim.tbl_extend("force", cmd.opts, { force = true })
vim.api.nvim_create_user_command(cmd.name, cmd.command, opts)
end
end
return M
60 changes: 42 additions & 18 deletions lua/nvimux/tmux.lua
@@ -1,30 +1,54 @@
local config = require("nvimux.config")

local M = { }
local M = {}

M.exe = function (...)
local command = config.user_opts.tmux
for _,v in ipairs(arg) do
command = command .. tostring(v) .. " "
end
if config.user_opts.debug then
print('[vimux] Run command "' .. command .. '"')
end
if vim.env.TMUX == nil then
vim.message("Not in a tmux session (TMUX environment variable not found)")
else
vim.system(command)
end
local function all_trim(s)
return s:match( "^%s*(.-)%s*$" )
end

M.send_keys = function(keys)
M.exe('send-keys -t '.. M.runner_index .. ' ' .. keys)
M.is_executable = function()
local command = config.get("tmux_command")
return vim.fn.executable(command) == 1
end

M.get_property = function(name)
return M.exe("display -p '".. name .."'")
M.exe = function(cmd)
local command = { config.get("tmux_command") }
local dbg_command = command[1]

if type(cmd) == "string" then
cmd = { cmd }
end

for _, v in ipairs(cmd) do
if type(v) == "table" then
vim.tbl_extend("force", command, v)
else
table.insert(command, tostring(v))
end
dbg_command = dbg_command .. " " .. tostring(v)
end
if vim.env.TMUX == nil then
vim.message("Not in a tmux session (TMUX environment variable not found)")
else
local result = vim.system(command):wait()
return all_trim(result.stdout)
end
return ""
end

M.send_keys = function(runner, keys)
M.exe({ "send-keys", "-t", runner, keys })
end

M.get_property = function(name, runner)
command = { "display" }
if runner ~= nil then
table.insert(command, '-t')
table.insert(command, runner)
end
table.insert(command, '-p')
table.insert(command, name)
return M.exe(command)
end

return M

0 comments on commit b0bc912

Please sign in to comment.