Repository navigation
Expand file tree
/
Copy pathterminal.ts
More file actions
77 lines (71 loc) · 2.24 KB
/
Copy pathterminal.ts
File metadata and controls
77 lines (71 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Running a command in the user's terminal emulator (same detection as the Code Projects extension).
/** Tried in this order when no terminal is set in the preferences. */
export const TERMINALS = [
"kitty",
"alacritty",
"wezterm",
"foot",
"ghostty",
"ptyxis",
"kgx",
"gnome-terminal",
"konsole",
"xfce4-terminal",
"tilix",
"xterm",
];
const quote = (arg: string) => /^[\w@%+=:,./-]+$/.test(arg) ? arg : `'${arg.replace(/'/g, `'\\''`)}'`;
/** A command line as a shell would read it back. */
export const shellLine = (argv: string[]) => argv.map(quote).join(" ");
/** The arguments that make `terminal` run `argv` instead of a shell. */
export function terminalArgs(terminal: string, argv: string[]): string[] {
switch (terminal) {
case "wezterm":
return ["start", "--", ...argv];
case "kitty":
case "foot":
return argv;
case "ptyxis":
case "kgx":
case "gnome-terminal":
return ["--", ...argv];
case "xfce4-terminal":
return ["-x", ...argv];
case "tilix":
return ["-e", shellLine(argv)];
default: // alacritty, ghostty, konsole, xterm and most others
return ["-e", ...argv];
}
}
/** Starts `argv` in a new terminal window that outlives the command. */
export function runInTerminal(argv: string[], preferred?: string): string {
for (const term of preferred ? [preferred] : TERMINALS) {
try {
const child = new Deno.Command(term, {
args: terminalArgs(term, argv),
stdin: "null",
stdout: "null",
stderr: "null",
}).spawn();
child.unref();
return term;
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) throw err;
}
}
throw new Error(
preferred
? `Terminal "${preferred}" not found: change it in the extension's preferences`
: "No terminal found: set one in the extension's preferences",
);
}
/** Starts a desktop program that outlives the command; false when it is not installed. */
export function launch(program: string, args: string[] = []): boolean {
try {
new Deno.Command(program, { args, stdin: "null", stdout: "null", stderr: "null" }).spawn().unref();
return true;
} catch (err) {
if (err instanceof Deno.errors.NotFound) return false;
throw err;
}
}