@noldova/terminal 0.0.16 → 0.0.17

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.
Files changed (51) hide show
  1. package/module.json +2 -2
  2. package/package.json +9 -9
  3. package/src/enums/mark-kind.js +1 -0
  4. package/src/enums/shell-family.js +1 -0
  5. package/src/exceptions/command-exited.exception.js +2 -1
  6. package/src/models/hosted-terminal.js +20 -9
  7. package/src/models/one-shot-launch.js +7 -4
  8. package/src/models/right-prompt-span.js +14 -0
  9. package/src/models/terminal-prompt.js +5 -5
  10. package/src/models/typed-line.js +2 -2
  11. package/src/resources.js +263 -13
  12. package/src/services/agent-terminal.js +7 -5
  13. package/src/services/command-describer.js +35 -33
  14. package/src/services/command-runner.js +5 -3
  15. package/src/services/command-tracker.js +17 -2
  16. package/src/services/completion-source.js +62 -30
  17. package/src/services/escape-sequence-tracker.js +1 -1
  18. package/src/services/escape-stripper.js +10 -7
  19. package/src/services/file-system.reader.js +2 -1
  20. package/src/services/fish-shell-adapter.js +90 -0
  21. package/src/services/folder-change-line.js +2 -0
  22. package/src/services/line-process.js +3 -2
  23. package/src/services/log-file.js +4 -3
  24. package/src/services/mac-environment.js +5 -2
  25. package/src/services/mark-scanner.js +5 -5
  26. package/src/services/output-batcher.js +1 -1
  27. package/src/services/output-log.js +2 -1
  28. package/src/services/posix-shell-adapter.js +3 -2
  29. package/src/services/printed-text.js +3 -2
  30. package/src/services/pseudo-terminal.js +1 -1
  31. package/src/services/registry-export.js +5 -5
  32. package/src/services/resize-wakeup.js +1 -1
  33. package/src/services/saved-terminals.js +2 -1
  34. package/src/services/shell-adapters.js +3 -1
  35. package/src/services/shell-command.js +7 -2
  36. package/src/services/shell-integration.js +32 -14
  37. package/src/services/shell-locator.js +27 -13
  38. package/src/services/shell-words.js +5 -5
  39. package/src/services/terminal-history-segment.js +1 -1
  40. package/src/services/terminal-host.js +84 -43
  41. package/src/services/terminal-line-reader.js +9 -3
  42. package/src/services/terminal-mirror.js +12 -11
  43. package/src/services/terminal-session.js +24 -8
  44. package/src/services/wsl-distribution.reader.js +5 -2
  45. package/src/services/wsl-shell-adapter.js +6 -2
  46. package/src/tools/models/output-excerpt.js +2 -1
  47. package/src/tools/models/terminal-output-report.js +4 -2
  48. package/src/tools/models/tool-call-context.js +2 -1
  49. package/src/tools/services/terminal-access-policy.js +4 -2
  50. package/src/tools/services/terminal-text-escaper.js +4 -2
  51. package/src/tools/services/terminal-write-tool.js +5 -3
@@ -70,7 +70,7 @@ export class AgentTerminal {
70
70
  return this.isRunning && !this.session.commands.isAtPrompt && this.session.text.isAwaitingSecret;
71
71
  }
72
72
  get hasPendingLine() {
73
- return this.hosted.hasRecentTyping || (this.hasShellIntegration && (this.session.readPromptLine() ?? "").length > 0);
73
+ return this.hosted.hasRecentTyping || (this.hasShellIntegration && (this.session.readPromptLine() ?? String.empty).length > 0);
74
74
  }
75
75
  get hasUnechoedInput() {
76
76
  const received = this.hosted.inputReceivedLength;
@@ -78,13 +78,16 @@ export class AgentTerminal {
78
78
  return false;
79
79
  return !this.hasShellIntegration || this.hosted.hasUnreadInput;
80
80
  }
81
+ get isIntegrationReady() {
82
+ return !this.session.hasIntegration || this.session.hasExited || !Object.isNull(this.session.commands.promptEndOffset);
83
+ }
81
84
  get session() {
82
85
  return this.hosted.session;
83
86
  }
84
87
  readPrompt() {
85
88
  if (!this.hasShellIntegration)
86
89
  return new TerminalPrompt(this.isRunning && this.oneShot?.isRunning !== true && !this.hasUnechoedInput, null, null, this.isAwaitingSecret, this.hosted.inputCount);
87
- const isIdle = this.isRunning && !this.hasUnechoedInput && this.session.readPromptLine() === "";
90
+ const isIdle = this.isRunning && !this.hasUnechoedInput && this.session.readPromptLine() === String.empty;
88
91
  return new TerminalPrompt(isIdle, this.session.commands.cwd, this.session.commands.promptIdentity, this.isAwaitingSecret, this.hosted.inputCount);
89
92
  }
90
93
  readOneShotLine(command) {
@@ -118,10 +121,9 @@ export class AgentTerminal {
118
121
  return this.session.changes.waitUntilAsync(() => this.session.hasExited || this.session.text.endOffset > offset, timeoutMilliseconds, signal);
119
122
  }
120
123
  async waitForIntegrationAsync(signal) {
121
- const isReady = () => !this.session.hasIntegration || this.session.hasExited || !Object.isNull(this.session.commands.promptEndOffset);
122
- await this.session.waitForStartAsync(() => isReady() || this.session.commands.isLoaded, signal);
124
+ await this.session.waitForStartAsync(() => this.isIntegrationReady || this.session.commands.isLoaded, signal);
123
125
  if (this.session.commands.isLoaded)
124
- await this.session.changes.waitUntilAsync(isReady, Resources.shellStartMilliseconds, signal);
126
+ await this.session.changes.waitUntilAsync(() => this.isIntegrationReady, Resources.shellStartMilliseconds, signal);
125
127
  }
126
128
  send(data) {
127
129
  if (this.oneShot?.isRunning === true)
@@ -5,6 +5,7 @@
5
5
  * This source code is licensed under the license found in the
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
+ import "@noldova/context.core";
8
9
  import { TerminalCommandDescription, TerminalCommandDescriptions, TerminalShellKind } from "@noldova/terminal.protocol";
9
10
  import { CommandFailedException } from "../exceptions/command-failed.exception.js";
10
11
  import { DescribeGroup } from "../models/describe-group.js";
@@ -21,39 +22,12 @@ export class CommandDescriber {
21
22
  async describeAsync(kind, program, names, environment) {
22
23
  const asked = [...new Set(names)].filter(t => Resources.describedNamePattern.test(t));
23
24
  const isPowerShell = kind === TerminalShellKind.PowerShell;
24
- if (!isPowerShell && kind !== TerminalShellKind.Bash && kind !== TerminalShellKind.Zsh)
25
+ if (!isPowerShell && kind !== TerminalShellKind.Bash && kind !== TerminalShellKind.Zsh && kind !== TerminalShellKind.Fish)
25
26
  return new TerminalCommandDescriptions([]);
26
- const perRun = isPowerShell ? Number.POSITIVE_INFINITY : Resources.whatisNamesPerRun;
27
27
  const now = Date.now();
28
28
  const groups = [];
29
- const describe = name => {
30
- const own = isPowerShell ? Resources.powerShellDescriptions.get(name.toLowerCase()) : undefined;
31
- if (!Object.isUndefined(own))
32
- return Promise.resolve(own);
33
- const key = JSON.stringify([kind, program, isPowerShell ? name.toLowerCase() : name]);
34
- const known = this.known.get(key);
35
- if (!Object.isUndefined(known) && known.isFresh(now))
36
- return known.description;
37
- let group = groups.at(-1);
38
- if (Object.isUndefined(group) || group.names.length === perRun) {
39
- group = new DescribeGroup();
40
- groups.push(group);
41
- }
42
- group.names.push(name);
43
- const entry = new KnownDescription(group.answer.promise.then(t => {
44
- if (!Object.isNull(t))
45
- return t.get(name) ?? null;
46
- entry.failedAt = Date.now();
47
- return null;
48
- }, (error) => {
49
- this.known.delete(key);
50
- throw error;
51
- }));
52
- this.known.set(key, entry);
53
- return entry.description;
54
- };
55
29
  const pending = asked.map(async (name) => {
56
- const description = await describe(name);
30
+ const description = await this.describeOneAsync(kind, program, now, groups, name);
57
31
  return Object.isNull(description) ? null : new TerminalCommandDescription(name, description);
58
32
  });
59
33
  const shellEnvironment = Object.fromEntries(Object.entries(environment)
@@ -63,6 +37,34 @@ export class CommandDescriber {
63
37
  const items = await Promise.all(pending);
64
38
  return new TerminalCommandDescriptions(items.flatMap(t => Object.isNull(t) ? [] : [t]));
65
39
  }
40
+ describeOneAsync(kind, program, now, groups, name) {
41
+ const isPowerShell = kind === TerminalShellKind.PowerShell;
42
+ const perRun = isPowerShell ? Number.POSITIVE_INFINITY : Resources.whatisNamesPerRun;
43
+ const own = isPowerShell ? Resources.powerShellDescriptions.get(name.toLowerCase()) : undefined;
44
+ if (!Object.isUndefined(own))
45
+ return Promise.resolve(own);
46
+ const key = JSON.stringify([kind, program, isPowerShell ? name.toLowerCase() : name]);
47
+ const known = this.known.get(key);
48
+ if (!Object.isUndefined(known) && known.isFresh(now))
49
+ return known.description;
50
+ let group = groups.at(-1);
51
+ if (Object.isUndefined(group) || group.names.length === perRun) {
52
+ group = new DescribeGroup();
53
+ groups.push(group);
54
+ }
55
+ group.names.push(name);
56
+ const entry = new KnownDescription(group.answer.promise.then(t => {
57
+ if (!Object.isNull(t))
58
+ return t.get(name) ?? null;
59
+ entry.failedAt = Date.now();
60
+ return null;
61
+ }, (error) => {
62
+ this.known.delete(key);
63
+ throw error;
64
+ }));
65
+ this.known.set(key, entry);
66
+ return entry.description;
67
+ }
66
68
  async runAsync(groups, kind, program, environment) {
67
69
  for (const group of groups) {
68
70
  try {
@@ -80,7 +82,7 @@ export class CommandDescriber {
80
82
  Buffer.from(Resources.formatPowerShellDescribeScript(names), Resources.utf16Encoding).toString(Resources.base64Encoding)], Resources.describeMilliseconds, environment);
81
83
  return CommandDescriber.readPowerShell(output, names);
82
84
  }
83
- const output = await this.runner.runAsync(program, [...kind === TerminalShellKind.Zsh ? [Resources.zshNoStartupArgument] : [],
85
+ const output = await this.runner.runAsync(kind === TerminalShellKind.Fish ? Resources.describeShellProgram : program, [...kind === TerminalShellKind.Zsh ? [Resources.zshNoStartupArgument] : [],
84
86
  ...Resources.posixOneShotArguments, Resources.whatisScript, Resources.whatisScriptName, ...names], Resources.describeMilliseconds, environment);
85
87
  return CommandDescriber.readWhatis(output, names);
86
88
  }
@@ -114,8 +116,8 @@ export class CommandDescriber {
114
116
  continue;
115
117
  for (const [index, entry] of line.slice(0, dash.index).split(Resources.whatisEntrySeparator).entries()) {
116
118
  const page = Resources.whatisEntry.exec(entry);
117
- const name = page?.[1] ?? "";
118
- const section = page?.[2] ?? "";
119
+ const name = page?.[1] ?? String.empty;
120
+ const section = page?.[2] ?? String.empty;
119
121
  const rank = Resources.whatisSections.findIndex(t => section.startsWith(t));
120
122
  const order = (rank < 0 ? Resources.whatisSections.length : rank) * 2 + (index === 0 ? 0 : 1);
121
123
  if (asked.has(name) && order < (ranks.get(name) ?? Number.POSITIVE_INFINITY)) {
@@ -128,6 +130,6 @@ export class CommandDescriber {
128
130
  }
129
131
  static toLine(text) {
130
132
  const line = text.replace(Resources.controlCharacters, " ").replace(Resources.whitespaceRuns, " ").trim();
131
- return line.length === 0 ? null : [...line].slice(0, Resources.maximumDescribedLength).join("");
133
+ return line.length === 0 ? null : [...line].slice(0, Resources.maximumDescribedLength).join(String.empty);
132
134
  }
133
135
  }
@@ -5,6 +5,7 @@
5
5
  * This source code is licensed under the license found in the
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
+ import "@noldova/context.core";
8
9
  import { addAbortListener } from "node:events";
9
10
  import path from "node:path";
10
11
  import { ProcessRequest, ProcessStartException } from "@noldova/desktop.runtime";
@@ -29,7 +30,8 @@ export class CommandRunner {
29
30
  const answer = Promise.withResolvers();
30
31
  const late = setTimeout(() => answer.reject(new CommandTimedOutException(name, timeoutMilliseconds)), timeoutMilliseconds);
31
32
  const ending = setTimeout(() => stopping.abort(), Math.max(timeoutMilliseconds, endMilliseconds));
32
- const ended = this.readAsync(name, request, stopping, timeoutMilliseconds, () => clearTimeout(late)).then(answer.resolve, answer.reject)
33
+ const ended = this.readAsync(name, request, stopping, timeoutMilliseconds, () => clearTimeout(late))
34
+ .then(answer.resolve, answer.reject)
33
35
  .finally(() => {
34
36
  clearTimeout(late);
35
37
  clearTimeout(ending);
@@ -53,7 +55,7 @@ export class CommandRunner {
53
55
  process.errors.destroy();
54
56
  });
55
57
  let isTooLong = false;
56
- let output = "";
58
+ let output = String.empty;
57
59
  process.output.setEncoding(Resources.utf8Encoding);
58
60
  process.output.on(Resources.dataEvent, (text) => {
59
61
  output += text;
@@ -62,7 +64,7 @@ export class CommandRunner {
62
64
  stopping.abort();
63
65
  }
64
66
  });
65
- let errors = "";
67
+ let errors = String.empty;
66
68
  process.errors.setEncoding(Resources.utf8Encoding);
67
69
  process.errors.on(Resources.dataEvent, (text) => {
68
70
  errors = (errors + text).slice(-Resources.maximumCommandErrorLength);
@@ -7,6 +7,7 @@
7
7
  */
8
8
  import "@noldova/context.core";
9
9
  import { MarkKind } from "../enums/mark-kind.js";
10
+ import { RightPromptSpan } from "../models/right-prompt-span.js";
10
11
  import { TrackedCommand } from "../models/tracked-command.js";
11
12
  import { Resources } from "../resources.js";
12
13
  export class CommandTracker {
@@ -19,6 +20,7 @@ export class CommandTracker {
19
20
  redrawFrom = null;
20
21
  current = null;
21
22
  hasTypedAhead = false;
23
+ rightPromptSpans = [];
22
24
  hasMarks = false;
23
25
  isAtPrompt = false;
24
26
  isDrawingPrompt = false;
@@ -48,6 +50,9 @@ export class CommandTracker {
48
50
  get promptEndOffset() {
49
51
  return this.isAtPrompt ? this.lineOffset : null;
50
52
  }
53
+ get rightPrompts() {
54
+ return this.rightPromptSpans;
55
+ }
51
56
  get promptIdentity() {
52
57
  return this.isAtPrompt ? this.promptOffset : null;
53
58
  }
@@ -73,8 +78,18 @@ export class CommandTracker {
73
78
  this.isAtPrompt = false;
74
79
  this.isDrawingPrompt = true;
75
80
  this.promptStart = mark.offset;
81
+ this.rightPromptSpans = [];
76
82
  this.prompts++;
77
83
  break;
84
+ case MarkKind.RightPrompt:
85
+ this.rightPromptSpans.push(new RightPromptSpan(mark.offset));
86
+ break;
87
+ case MarkKind.RightPromptEnd: {
88
+ const last = this.rightPromptSpans.at(-1);
89
+ if (!Object.isUndefined(last) && Object.isNull(last.end))
90
+ last.end = mark.offset;
91
+ break;
92
+ }
78
93
  case MarkKind.PromptEnd:
79
94
  this.isDrawingPrompt = false;
80
95
  this.endPrompt(mark.offset);
@@ -121,7 +136,7 @@ export class CommandTracker {
121
136
  this.isModulePathReset = true;
122
137
  }
123
138
  else if (mark.value?.startsWith(Resources.namesProperty) === true) {
124
- this.names = new Set(mark.value.slice(Resources.namesProperty.length).split(Resources.namesSeparator).filter(t => t !== ""));
139
+ this.names = new Set(mark.value.slice(Resources.namesProperty.length).split(Resources.namesSeparator).filter(t => t !== String.empty));
125
140
  this.isNamesReset = false;
126
141
  }
127
142
  else if (mark.value === Resources.namesReset) {
@@ -129,7 +144,7 @@ export class CommandTracker {
129
144
  this.isNamesReset = true;
130
145
  }
131
146
  else if (mark.value?.startsWith(Resources.builtinsProperty) === true)
132
- this.builtins = new Set(mark.value.slice(Resources.builtinsProperty.length).split(Resources.namesSeparator).filter(t => t !== ""));
147
+ this.builtins = new Set(mark.value.slice(Resources.builtinsProperty.length).split(Resources.namesSeparator).filter(t => t !== String.empty));
133
148
  else if (mark.value === Resources.builtinsReset)
134
149
  this.builtins = null;
135
150
  else if (mark.value?.startsWith(Resources.optionsProperty) === true)
@@ -61,12 +61,14 @@ export class CompletionSource {
61
61
  }
62
62
  addOptions(completion, target, line, cursor, options) {
63
63
  const ignoresCase = this.adapters.of(target)?.ignoresCase === true;
64
- const toKey = (text) => ignoresCase ? text.toLowerCase() : text;
65
- const prefix = toKey(line.slice(completion.replaceFrom, cursor));
64
+ const prefix = CompletionSource.keyOf(line.slice(completion.replaceFrom, cursor), ignoresCase);
66
65
  const labels = new Set(completion.items.map(t => t.label));
67
66
  const added = [...new Set(options)]
68
- .filter(t => t.trim().length > 0 && t.length <= ProtocolResources.maximumPathLength && !ProtocolResources.controlCharacters.test(t) && !labels.has(t)
69
- && toKey(t).startsWith(prefix))
67
+ .filter(t => t.trim().length > 0 &&
68
+ t.length <= ProtocolResources.maximumPathLength &&
69
+ !ProtocolResources.controlCharacters.test(t) &&
70
+ !labels.has(t) &&
71
+ CompletionSource.keyOf(t, ignoresCase).startsWith(prefix))
70
72
  .map(t => new TerminalCompletionItem(t, t, TerminalCompletionKind.Option));
71
73
  if (added.length === 0)
72
74
  return completion;
@@ -89,17 +91,23 @@ export class CompletionSource {
89
91
  this.listed.set(shell, new CommandListing(prompt, search, commands));
90
92
  const found = [...shellNames, ...commands];
91
93
  const known = new Set(adapter.takesWindowsNames ? found.map(t => t.toLowerCase()) : found);
92
- const unknown = names.filter(t => !this.isPath(adapter, t) && !t.startsWith(Resources.helperNamePrefix)
93
- && !known.has(this.toCommandName(adapter, t)) && !this.hasSuffixAlias(t, shellNames));
94
+ const unknown = names.filter(t => !this.isPath(adapter, t) &&
95
+ !t.startsWith(Resources.helperNamePrefix) &&
96
+ !known.has(this.toCommandName(adapter, t)) &&
97
+ !this.hasSuffixAlias(t, shellNames));
94
98
  const missing = unknown.length > 0 && shellNames.has(Resources.autocdName)
95
- ? await this.dropFoldersAsync(reads, adapter, folder, environment, unknown) : unknown;
99
+ ? await this.dropFoldersAsync(reads, adapter, folder, environment, unknown)
100
+ : unknown;
96
101
  return new TerminalMissingCommands(reads.wasCutShort ? [] : missing);
97
102
  });
98
103
  }
99
104
  async findMissingPowerShellAsync(shell, program, names, shellNames, environment) {
100
105
  if (Object.isNull(shellNames)) {
101
- const reason = shell.isPathReset ? Resources.powerShellLookupPathReset : shell.isModulePathReset ? Resources.powerShellLookupModulePathReset
102
- : shell.isNamesReset ? Resources.powerShellLookupNamesTooLong : Resources.powerShellLookupNamesUnreported;
106
+ const reason = shell.isPathReset
107
+ ? Resources.powerShellLookupPathReset
108
+ : shell.isModulePathReset
109
+ ? Resources.powerShellLookupModulePathReset
110
+ : shell.isNamesReset ? Resources.powerShellLookupNamesTooLong : Resources.powerShellLookupNamesUnreported;
103
111
  if (this.skipped.get(shell) !== reason)
104
112
  this.log.write(Resources.formatPowerShellLookupSkipped(reason));
105
113
  this.skipped.set(shell, reason);
@@ -107,7 +115,8 @@ export class CompletionSource {
107
115
  }
108
116
  this.skipped.delete(shell);
109
117
  const variables = await this.toLongModulePathsAsync(Object.fromEntries([Resources.pathVariable,
110
- Resources.pathExtensionsVariable, Resources.modulePathVariable].flatMap(t => {
118
+ Resources.pathExtensionsVariable, Resources.modulePathVariable]
119
+ .flatMap(t => {
111
120
  const value = this.readVariable(environment, this.isWindows ? t.toUpperCase() : t);
112
121
  return Object.isUndefined(value) ? [] : [[t, value]];
113
122
  })));
@@ -116,8 +125,10 @@ export class CompletionSource {
116
125
  const answers = !Object.isUndefined(last) && last.shellNames === shellNames && last.search === search ? last.answers : new Map();
117
126
  this.looked.set(shell, new PowerShellLookup(shellNames, search, answers));
118
127
  const known = new Set([...shellNames].map(t => t.toLowerCase()));
119
- const asked = [...new Set(names)].filter(t => !this.isPath(null, t) && Resources.powerShellLookupNamePattern.test(t)
120
- && !known.has(t.toLowerCase()) && !known.has(`${Resources.powerShellImplicitVerb}${t}`.toLowerCase()));
128
+ const asked = [...new Set(names)].filter(t => !this.isPath(null, t) &&
129
+ Resources.powerShellLookupNamePattern.test(t) &&
130
+ !known.has(t.toLowerCase()) &&
131
+ !known.has(`${Resources.powerShellImplicitVerb}${t}`.toLowerCase()));
121
132
  const unasked = asked.filter(t => !answers.has(t));
122
133
  if (unasked.length > 0 && !this.lookups.has(shell)) {
123
134
  const run = this.runner.start(program, [...Resources.environmentReadArguments,
@@ -149,7 +160,8 @@ export class CompletionSource {
149
160
  const workFolder = shell.toLocalPath(folder, this.readVariable(environment, Resources.temporaryFolderVariable) ?? null);
150
161
  if (Object.isNull(workFolder))
151
162
  return names;
152
- const folders = new Set((await reads.readEntriesAsync(workFolder)).filter(t => t.isFolder)
163
+ const folders = new Set((await reads.readEntriesAsync(workFolder))
164
+ .filter(t => t.isFolder)
153
165
  .map(t => shell.takesWindowsNames ? t.name.toLowerCase() : t.name));
154
166
  return names.filter(t => !folders.has(shell.takesWindowsNames ? t.toLowerCase() : t));
155
167
  }
@@ -194,9 +206,15 @@ export class CompletionSource {
194
206
  return this.listCommandsAsync(reads, shell, builtins, environment);
195
207
  const temporaryFolder = this.readVariable(environment, Resources.temporaryFolderVariable) ?? null;
196
208
  const workFolder = shell.toLocalPath(folder, temporaryFolder);
197
- if (!Object.isNull(workFolder) && words.length === Resources.subcommandWordCount && words[0] === Resources.gitCommand && Resources.gitBranchCommands.some(t => shellLine.words[1] === t))
209
+ if (!Object.isNull(workFolder) &&
210
+ words.length === Resources.subcommandWordCount &&
211
+ words[0] === Resources.gitCommand &&
212
+ Resources.gitBranchCommands.some(t => shellLine.words[1] === t))
198
213
  return this.listBranchesAsync(reads, shell, workFolder, environment);
199
- if (!Object.isNull(workFolder) && words.length === Resources.subcommandWordCount && words[0] === Resources.npmCommand && Resources.npmRunCommands.some(t => shellLine.words[1] === t))
214
+ if (!Object.isNull(workFolder) &&
215
+ words.length === Resources.subcommandWordCount &&
216
+ words[0] === Resources.npmCommand &&
217
+ Resources.npmRunCommands.some(t => shellLine.words[1] === t))
200
218
  return this.listScriptsAsync(reads, workFolder);
201
219
  const home = this.homeOf(shell, environment);
202
220
  const paths = this.listPathsAsync(reads, shell, shellLine.value, workFolder, expandsHome ? home : null, temporaryFolder);
@@ -208,9 +226,11 @@ export class CompletionSource {
208
226
  async listToolWordsAsync(reads, shell, command, workFolder, paths) {
209
227
  const tool = Resources.toolCommands.find(t => t.name === command);
210
228
  if (!Object.isUndefined(tool))
211
- return [...tool.words.map(t => new CompletionCandidate(t.name, t.name, TerminalCompletionKind.Command, true, t.description)), ...(await paths).map(t => t.ranked(1))];
229
+ return [...tool.words.map(t => new CompletionCandidate(t.name, t.name, TerminalCompletionKind.Command, true, t.description)), ...(await paths)
230
+ .map(t => t.ranked(1))];
212
231
  if (command === Resources.nodeCommand)
213
- return (await paths).map(t => t.kind === TerminalCompletionKind.File && Resources.nodeScriptExtensions.includes(this.paths.extname(t.label).toLowerCase()) ? t : t.ranked(1));
232
+ return (await paths)
233
+ .map(t => t.kind === TerminalCompletionKind.File && Resources.nodeScriptExtensions.includes(this.paths.extname(t.label).toLowerCase()) ? t : t.ranked(1));
214
234
  if (command === Resources.npxCommand && !Object.isNull(workFolder))
215
235
  return [...await this.listPackageBinsAsync(reads, shell, workFolder), ...(await paths).map(t => t.ranked(1))];
216
236
  return paths;
@@ -219,7 +239,9 @@ export class CompletionSource {
219
239
  let current = folder;
220
240
  for (;;) {
221
241
  const entries = await reads.readEntriesAsync(this.paths.join(current, ...Resources.packageBinFolder));
222
- const names = [...new Set(entries.filter(t => !t.isFolder).map(t => shell.takesWindowsNames ? t.name.replace(Resources.packageBinExtension, "") : t.name))];
242
+ const names = [...new Set(entries
243
+ .filter(t => !t.isFolder)
244
+ .map(t => shell.takesWindowsNames ? t.name.replace(Resources.packageBinExtension, String.empty) : t.name))];
223
245
  if (names.length > 0 || this.paths.dirname(current) === current)
224
246
  return names.map(t => new CompletionCandidate(t, t, TerminalCompletionKind.Command, true));
225
247
  current = this.paths.dirname(current);
@@ -241,14 +263,16 @@ export class CompletionSource {
241
263
  }
242
264
  async listCommandsAsync(reads, shell, builtins, environment, takesBareNames = false) {
243
265
  const extensions = shell.takesWindowsNames
244
- ? (this.readVariable(environment, Resources.pathExtensionsVariable) ?? Resources.defaultPathExtensions).toLowerCase()
245
- .split(path.win32.delimiter).filter(t => t !== "")
266
+ ? (this.readVariable(environment, Resources.pathExtensionsVariable) ?? Resources.defaultPathExtensions)
267
+ .toLowerCase()
268
+ .split(path.win32.delimiter)
269
+ .filter(t => t !== String.empty)
246
270
  : null;
247
271
  const entries = await Promise.all(this.searchFolders(shell, environment).map(t => reads.readEntriesAsync(t)));
248
272
  const names = [...builtins];
249
273
  for (const entry of entries.flat()) {
250
274
  const extension = this.paths.extname(entry.name).toLowerCase();
251
- if (!entry.isFolder && (Object.isNull(extensions) || extensions.includes(extension) || takesBareNames && extension === ""))
275
+ if (!entry.isFolder && (Object.isNull(extensions) || extensions.includes(extension) || takesBareNames && extension === String.empty))
252
276
  names.push(Object.isNull(extensions) ? entry.name : entry.name.slice(0, entry.name.length - extension.length));
253
277
  }
254
278
  return names.map(t => new CompletionCandidate(t, t, TerminalCompletionKind.Command, true));
@@ -267,11 +291,13 @@ export class CompletionSource {
267
291
  return [];
268
292
  throw error;
269
293
  }
270
- return output.split(Resources.lineBreaks)
294
+ return output
295
+ .split(Resources.lineBreaks)
271
296
  .filter(t => !t.endsWith(Resources.remoteHeadSuffix))
272
- .map(t => t.startsWith(Resources.headsPrefix) ? t.slice(Resources.headsPrefix.length)
273
- : t.startsWith(Resources.remotesPrefix) ? t.slice(Resources.remotesPrefix.length) : "")
274
- .filter(t => t !== "")
297
+ .map(t => t.startsWith(Resources.headsPrefix)
298
+ ? t.slice(Resources.headsPrefix.length)
299
+ : t.startsWith(Resources.remotesPrefix) ? t.slice(Resources.remotesPrefix.length) : String.empty)
300
+ .filter(t => t !== String.empty)
275
301
  .map(t => new CompletionCandidate(t, t, TerminalCompletionKind.Branch, true));
276
302
  }
277
303
  async listScriptsAsync(reads, folder) {
@@ -308,7 +334,7 @@ export class CompletionSource {
308
334
  return null;
309
335
  }
310
336
  searchFolders(shell, environment) {
311
- return [...new Set(shell.readSearchFolders(this.readVariable(environment, Resources.pathVariable) ?? ""))];
337
+ return [...new Set(shell.readSearchFolders(this.readVariable(environment, Resources.pathVariable) ?? String.empty))];
312
338
  }
313
339
  homeOf(shell, environment) {
314
340
  if (shell.takesWindowsNames)
@@ -323,7 +349,8 @@ export class CompletionSource {
323
349
  return variables;
324
350
  const reads = new BoundedReads(this.shared, Resources.completionReadMilliseconds);
325
351
  try {
326
- const entries = await Promise.all(modulePath.split(Resources.windowsPathListSeparator)
352
+ const entries = await Promise.all(modulePath
353
+ .split(Resources.windowsPathListSeparator)
327
354
  .map(async (t) => t.includes(Resources.shortNameMark) ? await reads.readRealPathOrNullAsync(t) ?? t : t));
328
355
  return { ...variables, [Resources.modulePathVariable]: entries.join(Resources.windowsPathListSeparator) };
329
356
  }
@@ -340,13 +367,18 @@ export class CompletionSource {
340
367
  }
341
368
  toCommandName(shell, word) {
342
369
  const name = (shell.takesWindowsNames ? path.win32 : path.posix).basename(word);
343
- return shell.takesWindowsNames ? name.toLowerCase().replace(Resources.windowsProgramExtension, "") : name;
370
+ return shell.takesWindowsNames ? name.toLowerCase().replace(Resources.windowsProgramExtension, String.empty) : name;
344
371
  }
345
372
  isHome(shell, value) {
346
373
  return value.startsWith(Resources.homePrefix) || (shell.takesWindowsNames && value.startsWith(Resources.windowsHomePrefix));
347
374
  }
348
375
  isPath(shell, value) {
349
- return value.includes(Resources.posixSeparator) || ((shell?.takesWindowsNames ?? this.isWindows) && value.includes(Resources.windowsSeparator))
350
- || value.startsWith(Resources.hiddenPrefix) || value.startsWith(Resources.homeWord);
376
+ return value.includes(Resources.posixSeparator) ||
377
+ ((shell?.takesWindowsNames ?? this.isWindows) && value.includes(Resources.windowsSeparator)) ||
378
+ value.startsWith(Resources.hiddenPrefix) ||
379
+ value.startsWith(Resources.homeWord);
380
+ }
381
+ static keyOf(text, ignoresCase) {
382
+ return ignoresCase ? text.toLowerCase() : text;
351
383
  }
352
384
  }
@@ -14,7 +14,7 @@ export class EscapeSequenceTracker {
14
14
  length = 0;
15
15
  isOverflowed = false;
16
16
  get pending() {
17
- return this.isOverflowed ? "" : this.parts.join("");
17
+ return this.isOverflowed ? String.empty : this.parts.join(String.empty);
18
18
  }
19
19
  track(data) {
20
20
  let start = this.state === EscapeState.Ground ? -1 : 0;
@@ -5,19 +5,20 @@
5
5
  * This source code is licensed under the license found in the
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
+ import "@noldova/context.core";
8
9
  import { EscapeStripperState } from "../enums/escape-stripper-state.js";
9
10
  import { Resources } from "../resources.js";
10
11
  export class EscapeStripper {
11
12
  state = EscapeStripperState.Ground;
12
13
  hasCarriageReturn = false;
13
14
  strip(data) {
14
- let text = "";
15
+ let text = String.empty;
15
16
  let index = 0;
16
17
  while (index < data.length) {
17
18
  if (this.state === EscapeStripperState.Ground) {
18
19
  const end = EscapeStripper.findSpecial(data, index);
19
20
  if (end > index) {
20
- text += (this.hasCarriageReturn ? Resources.lineFeed : "") + data.slice(index, end);
21
+ text += (this.hasCarriageReturn ? Resources.lineFeed : String.empty) + data.slice(index, end);
21
22
  this.hasCarriageReturn = false;
22
23
  index = end;
23
24
  continue;
@@ -61,12 +62,12 @@ export class EscapeStripper {
61
62
  this.state = character === Resources.escapeStripperStringTerminatorFinal ? EscapeStripperState.Ground : EscapeStripperState.String;
62
63
  break;
63
64
  }
64
- return "";
65
+ return String.empty;
65
66
  }
66
67
  readControl(code) {
67
68
  if (code === Resources.escapeStripperEscape) {
68
69
  this.state = EscapeStripperState.Escape;
69
- return "";
70
+ return String.empty;
70
71
  }
71
72
  this.hasCarriageReturn = false;
72
73
  return Resources.lineFeed;
@@ -82,9 +83,11 @@ export class EscapeStripper {
82
83
  static afterEscape(character, code) {
83
84
  if (character === Resources.escapeStripperControlSequenceIntroducer)
84
85
  return EscapeStripperState.Control;
85
- if (character === Resources.escapeStripperOperatingSystemCommandIntroducer || character === Resources.escapeStripperDeviceControlIntroducer
86
- || character === Resources.escapeStripperStartOfStringIntroducer || character === Resources.escapeStripperPrivacyMessageIntroducer
87
- || character === Resources.escapeStripperApplicationCommandIntroducer)
86
+ if (character === Resources.escapeStripperOperatingSystemCommandIntroducer ||
87
+ character === Resources.escapeStripperDeviceControlIntroducer ||
88
+ character === Resources.escapeStripperStartOfStringIntroducer ||
89
+ character === Resources.escapeStripperPrivacyMessageIntroducer ||
90
+ character === Resources.escapeStripperApplicationCommandIntroducer)
88
91
  return EscapeStripperState.String;
89
92
  return code >= Resources.escapeStripperFirstIntermediate && code <= Resources.escapeStripperLastIntermediate ? EscapeStripperState.Intermediate : EscapeStripperState.Ground;
90
93
  }
@@ -29,7 +29,8 @@ export class FileSystemReader {
29
29
  }
30
30
  async readFolderNamesAsync(path) {
31
31
  return (await FileSystemReader.readOrAsync(() => readdir(path, { withFileTypes: true }), [], Resources.unreadablePathCodes))
32
- .filter(t => t.isDirectory()).map(t => t.name);
32
+ .filter(t => t.isDirectory())
33
+ .map(t => t.name);
33
34
  }
34
35
  async readEntriesAsync(path) {
35
36
  const entries = await FileSystemReader.readOrAsync(() => readdir(path, { withFileTypes: true }), [], Resources.unreadablePathCodes);
@@ -0,0 +1,90 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) Noldova.
4
+ *
5
+ * This source code is licensed under the license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import "@noldova/context.core";
9
+ import path from "node:path";
10
+ import { ProgramCall } from "../models/program-call.js";
11
+ import { ShellLine } from "../models/shell-line.js";
12
+ import { Resources } from "../resources.js";
13
+ export class FishShellAdapter {
14
+ ignoresCase = false;
15
+ separator = Resources.posixSeparator;
16
+ takesBareNames = false;
17
+ optionsKeys = Resources.fishOptionsKeys;
18
+ folderKeys = null;
19
+ looksUpMissing = false;
20
+ takesWindowsNames = false;
21
+ readWords(line) {
22
+ let words = [];
23
+ let value = String.empty;
24
+ let start = line.length;
25
+ let isInWord = false;
26
+ let quote = null;
27
+ let startsQuoted = false;
28
+ for (let index = 0; index < line.length; index++) {
29
+ const character = line.charAt(index);
30
+ if (!isInWord && Object.isNull(quote) && !Resources.wordBreaks.includes(character) && !Resources.fishCommandBreaks.includes(character)) {
31
+ isInWord = true;
32
+ start = index;
33
+ startsQuoted = character === Resources.posixEscape || character === Resources.singleQuote || character === Resources.doubleQuote;
34
+ }
35
+ const next = line.charAt(index + 1);
36
+ if (character === Resources.posixEscape &&
37
+ index + 1 < line.length &&
38
+ (Object.isNull(quote) || (quote === Resources.singleQuote ? Resources.fishSingleQuotedEscapes : Resources.fishDoubleQuotedEscapes).includes(next))) {
39
+ value += next;
40
+ index++;
41
+ }
42
+ else if (!Object.isNull(quote)) {
43
+ if (character === quote)
44
+ quote = null;
45
+ else
46
+ value += character;
47
+ }
48
+ else if (character === Resources.singleQuote || character === Resources.doubleQuote)
49
+ quote = character;
50
+ else if (Resources.wordBreaks.includes(character)) {
51
+ if (isInWord)
52
+ words = words.length === 0 && Resources.fishCommandKeywords.includes(value) ? [] : [...words, value];
53
+ value = String.empty;
54
+ isInWord = false;
55
+ }
56
+ else if (Resources.fishCommandBreaks.includes(character)) {
57
+ words = [];
58
+ value = String.empty;
59
+ isInWord = false;
60
+ }
61
+ else
62
+ value += character;
63
+ }
64
+ return new ShellLine(words, isInWord ? start : line.length, value, quote, isInWord && startsQuoted);
65
+ }
66
+ writeWord(value, quote, closesQuote, _escapesWildcards, keepsHome) {
67
+ if (Object.isNull(quote))
68
+ return `${!keepsHome && value.startsWith(Resources.homeWord) ? Resources.posixEscape : String.empty}${value.replace(Resources.fishSpecial, Resources.shellWordsPosixEscapeReplacement)}`;
69
+ const body = value.replace(quote === Resources.singleQuote ? Resources.fishSingleQuotedSpecial : Resources.fishDoubleQuotedSpecial, Resources.shellWordsPosixEscapeReplacement);
70
+ return `${quote}${body}${closesQuote ? quote : String.empty}`;
71
+ }
72
+ escapesWildcards() {
73
+ return false;
74
+ }
75
+ expandsHome(startsQuoted) {
76
+ return !startsQuoted;
77
+ }
78
+ listBuiltins(shellNames, shellBuiltins) {
79
+ return [...new Set([...shellBuiltins ?? [], ...shellNames ?? []])].filter(t => !t.startsWith(Resources.helperNamePrefix));
80
+ }
81
+ toLocalPath(value) {
82
+ return value;
83
+ }
84
+ readSearchFolders(value) {
85
+ return value.split(path.posix.delimiter).filter(t => path.posix.isAbsolute(t));
86
+ }
87
+ toProgramCall(file, args) {
88
+ return new ProgramCall(file, args);
89
+ }
90
+ }
@@ -16,6 +16,8 @@ export class FolderChangeLine {
16
16
  case TerminalShellKind.Bash:
17
17
  case TerminalShellKind.Zsh:
18
18
  return `${Resources.posixChangeFolder}${Resources.singleQuote}${folder.replaceAll(Resources.singleQuote, Resources.posixQuotedSingleQuote)}${Resources.singleQuote}`;
19
+ case TerminalShellKind.Fish:
20
+ return `${Resources.posixChangeFolder}${Resources.singleQuote}${folder.replace(Resources.fishSingleQuotedSpecial, Resources.shellWordsPosixEscapeReplacement)}${Resources.singleQuote}`;
19
21
  default:
20
22
  return null;
21
23
  }
@@ -5,6 +5,7 @@
5
5
  * This source code is licensed under the license found in the
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
+ import "@noldova/context.core";
8
9
  import path from "node:path";
9
10
  import { ProcessRequest, ProcessStartException } from "@noldova/desktop.runtime";
10
11
  import { Resources } from "../resources.js";
@@ -15,7 +16,7 @@ export class LineProcess {
15
16
  name;
16
17
  stopping = new AbortController();
17
18
  started;
18
- output = "";
19
+ output = String.empty;
19
20
  constructor(context, file, args, listener) {
20
21
  this.name = path.basename(file);
21
22
  this.listener = listener;
@@ -65,7 +66,7 @@ export class LineProcess {
65
66
  const end = output.lastIndexOf(Resources.lineFeed) + 1;
66
67
  this.output = output.slice(end);
67
68
  for (const line of output.slice(0, end).split(Resources.lineFeed).slice(0, -1))
68
- this.listener.onLine(line.replace(Resources.lineProcessTrailingCarriageReturn, ""));
69
+ this.listener.onLine(line.replace(Resources.lineProcessTrailingCarriageReturn, String.empty));
69
70
  if (this.output.length > Resources.maximumLineLength)
70
71
  this.kill();
71
72
  }