vis-config
lua scripts to configure vis editor
git clone https://9o.is/git/vis-config.git
tmux.lua
(1985B)
1 local M = {
2 enabled = true,
3 status = {
4 left = '',
5 right = '',
6 },
7 }
8
9 local function write(cmd)
10 if not M.running() then
11 vis:info('tmux client is not running')
12 return false
13 end
14 M.fd:write(cmd .. '\n')
15 M.fd:flush()
16 return true
17 end
18
19 M.init = function()
20 M.id = os.getenv('TMUX_PANE')
21
22 if not M.id then
23 return
24 end
25
26 M.fd = vis:communicate('tmux',
27 string.format("exec env -u TMUX tmux -C attach -t %s", M.id))
28
29 vis.events.subscribe(vis.events.PROCESS_RESPONSE, function(name, event, code, msg)
30 if name ~= 'tmux' then
31 return
32 end
33
34 if event == 'EXIT' then
35 vis:info('tmux client exited: ' .. code)
36 elseif event == 'SIGNAL' then
37 vis:info('tmux client received signal: ' .. code)
38 end
39 end)
40 end
41
42 M.running = function()
43 return io.type(M.fd) == 'file'
44 end
45
46 M.navigate = function(path, cmd)
47 cmd = cmd and "+'"..cmd.."'" or ''
48 return write(string.format('new-window -b vis %s %s', cmd, path))
49 end
50
51 M.tabnew = function(argv)
52 local command = table.concat(argv, ' ')
53 return write('new-window -b vis ' .. command)
54 end
55
56 M.terminal = function(argv)
57 local command = table.concat(argv, ' '):gsub('"', '\\"')
58 return write(string.format('new-window -b "%s"', command))
59 end
60
61 M.set_title = function(title)
62 return write(string.format("select-pane -t %s -T '%s'", M.id, title))
63 end
64
65 M.set_status = function(left, right)
66 if left and left ~= M.status.left then
67 M.status.left = left
68 write(string.format("set -pt %s @pane_left_status '%s'", M.id, right))
69 end
70
71 if right and right ~= M.status.right then
72 M.status.right = right
73 write(string.format("set -pt %s @pane_right_status '%s'", M.id, right))
74 end
75 end
76
77 vis.events.subscribe(vis.events.INIT, function()
78 if M.enabled then
79 M.init()
80 end
81 end)
82
83 vis.events.subscribe(vis.events.WIN_OPEN, function()
84 if M.running() then
85 vis:command_register('tabnew', M.tabnew, 'Run vis command in a new tab page')
86 vis:command_register('terminal', M.terminal, 'Run shell command in a new tab page')
87 end
88 end)
89
90 return M