@dinhtungdu/watcher 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,58 @@
1
+ import { TmuxTarget } from './model.js';
2
+ import { tmuxTarget } from './terminalTarget.js';
3
+ import { CommandRunner, nodeCommandRunner } from './tmux.js';
4
+
5
+ const PANE_FORMAT = [
6
+ '#{pane_id}',
7
+ '#{session_name}',
8
+ '#{window_index}',
9
+ '#{pane_index}',
10
+ '#{pane_current_path}',
11
+ '#{pane_pid}',
12
+ '#{pane_current_command}',
13
+ '#{window_name}',
14
+ '#{pane_title}',
15
+ ].join('\t');
16
+
17
+ export async function listTmuxPanes(runner: CommandRunner = nodeCommandRunner): Promise<TmuxTarget[]> {
18
+ const result = await runner.execFile('tmux', ['list-panes', '-a', '-F', PANE_FORMAT], { timeout: 1000 });
19
+ return result.stdout.split('\n').filter(Boolean).map(parsePaneLine);
20
+ }
21
+
22
+ export async function getTmuxPane(paneId: string, runner: CommandRunner = nodeCommandRunner): Promise<TmuxTarget> {
23
+ try {
24
+ const panes = await listTmuxPanes(runner);
25
+ return panes.find((pane) => pane.paneId === paneId) ?? tmuxTarget({ paneId });
26
+ } catch {
27
+ return tmuxTarget({ paneId });
28
+ }
29
+ }
30
+
31
+ export async function captureTmuxPanePreview(paneId: string, runner: CommandRunner = nodeCommandRunner, lines = 40): Promise<string | undefined> {
32
+ try {
33
+ const result = await runner.execFile('tmux', ['capture-pane', '-p', '-t', paneId, '-S', `-${lines}`], { timeout: 1000 });
34
+ const preview = result.stdout
35
+ .split('\n')
36
+ .map((line) => line.replace(/\s+$/u, ''))
37
+ .join('\n')
38
+ .trim();
39
+ return preview ? preview.slice(-8000) : undefined;
40
+ } catch {
41
+ return undefined;
42
+ }
43
+ }
44
+
45
+ function parsePaneLine(line: string): TmuxTarget {
46
+ const [paneId, sessionName, windowIndex, paneIndex, paneCurrentPath, panePid, paneCurrentCommand, windowName, paneTitle] = line.split('\t');
47
+ return tmuxTarget({
48
+ paneId,
49
+ sessionName,
50
+ windowIndex,
51
+ paneIndex,
52
+ paneCurrentPath,
53
+ panePid: panePid ? Number(panePid) : undefined,
54
+ paneCurrentCommand,
55
+ windowName,
56
+ paneTitle,
57
+ });
58
+ }