@noldova/terminal 0.0.16 → 0.0.18

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 (52) 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 +271 -13
  12. package/src/runtime-part.js +1 -1
  13. package/src/services/agent-terminal.js +7 -5
  14. package/src/services/command-describer.js +35 -33
  15. package/src/services/command-runner.js +5 -3
  16. package/src/services/command-tracker.js +17 -2
  17. package/src/services/completion-source.js +62 -30
  18. package/src/services/escape-sequence-tracker.js +1 -1
  19. package/src/services/escape-stripper.js +10 -7
  20. package/src/services/file-system.reader.js +2 -1
  21. package/src/services/fish-shell-adapter.js +90 -0
  22. package/src/services/folder-change-line.js +2 -0
  23. package/src/services/line-process.js +3 -2
  24. package/src/services/log-file.js +4 -3
  25. package/src/services/mac-environment.js +5 -2
  26. package/src/services/mark-scanner.js +5 -5
  27. package/src/services/output-batcher.js +1 -1
  28. package/src/services/output-log.js +2 -1
  29. package/src/services/posix-shell-adapter.js +3 -2
  30. package/src/services/printed-text.js +3 -2
  31. package/src/services/pseudo-terminal.js +1 -1
  32. package/src/services/registry-export.js +5 -5
  33. package/src/services/resize-wakeup.js +1 -1
  34. package/src/services/saved-terminals.js +2 -1
  35. package/src/services/shell-adapters.js +3 -1
  36. package/src/services/shell-command.js +7 -2
  37. package/src/services/shell-integration.js +32 -14
  38. package/src/services/shell-locator.js +27 -13
  39. package/src/services/shell-words.js +5 -5
  40. package/src/services/terminal-history-segment.js +1 -1
  41. package/src/services/terminal-host.js +166 -45
  42. package/src/services/terminal-line-reader.js +9 -3
  43. package/src/services/terminal-mirror.js +12 -11
  44. package/src/services/terminal-session.js +24 -8
  45. package/src/services/wsl-distribution.reader.js +5 -2
  46. package/src/services/wsl-shell-adapter.js +6 -2
  47. package/src/tools/models/output-excerpt.js +2 -1
  48. package/src/tools/models/terminal-output-report.js +4 -2
  49. package/src/tools/models/tool-call-context.js +2 -1
  50. package/src/tools/services/terminal-access-policy.js +4 -2
  51. package/src/tools/services/terminal-text-escaper.js +4 -2
  52. package/src/tools/services/terminal-write-tool.js +5 -3
@@ -49,6 +49,10 @@ export class TerminalHost {
49
49
  saved;
50
50
  completions;
51
51
  describer;
52
+ context;
53
+ commandWork = new Map();
54
+ foregroundWork = new Map();
55
+ foregroundWatches = new Map();
52
56
  folderKeys = new WeakMap();
53
57
  realFolderReads = new Map();
54
58
  sessions = new Map();
@@ -64,7 +68,7 @@ export class TerminalHost {
64
68
  appCloses = new Set();
65
69
  closings = new Map();
66
70
  closingConnections = new Set();
67
- constructor(spawner, fileSystem, integration, historyFolder, locator, platform, osRelease, environment, homeFolder, log, events, saved, completions, describer) {
71
+ constructor(spawner, fileSystem, integration, historyFolder, locator, platform, osRelease, environment, homeFolder, log, events, saved, completions, describer, context) {
68
72
  this.spawner = spawner;
69
73
  this.fileSystem = fileSystem;
70
74
  this.integration = integration;
@@ -84,6 +88,7 @@ export class TerminalHost {
84
88
  this.saved = saved;
85
89
  this.completions = completions;
86
90
  this.describer = describer;
91
+ this.context = context;
87
92
  }
88
93
  async handleAsync(method, payload, client) {
89
94
  this.audience.see(client);
@@ -99,8 +104,10 @@ export class TerminalHost {
99
104
  await this.restartAsync(TerminalRestartParams.fromJson(payload), client);
100
105
  return null;
101
106
  case TerminalNames.sessionsMethod:
102
- return new TerminalSessions(this.terminals.filter(t => t.isReachedBy(client.connection, client.isApp))
103
- .map(t => t.session.toSummary(t.folder, !Object.isNull(t.ownerSessionId), t.isPanelTerminal, t.owner, t.opensTab, t.name, t.shell.name))).toJson();
107
+ return new TerminalSessions(this.terminals
108
+ .filter(t => t.isReachedBy(client.connection, client.isApp))
109
+ .map(t => t.session.toSummary(t.folder, !Object.isNull(t.ownerSessionId), t.isPanelTerminal, t.owner, t.opensTab, t.name, t.shell.name)))
110
+ .toJson();
104
111
  case TerminalNames.attachMethod:
105
112
  return (await this.attachAsync(TerminalPaneReference.fromJson(payload), client)).toJson();
106
113
  case TerminalNames.inputMethod:
@@ -132,7 +139,8 @@ export class TerminalHost {
132
139
  await this.closeOwnerAsync(TerminalOwnerReference.fromJson(payload).owner, client);
133
140
  return null;
134
141
  case TerminalNames.launchMethod:
135
- return (await this.trackLaunchAsync(this.trackAsync(this.launchAsync(TerminalLaunchParams.fromJson(payload), client), new TerminalOpening(null, client.connection)), client)).toJson();
142
+ return (await this.trackLaunchAsync(this.trackAsync(this.launchAsync(TerminalLaunchParams.fromJson(payload), client), new TerminalOpening(null, client.connection)), client))
143
+ .toJson();
136
144
  case TerminalNames.runMethod: {
137
145
  const params = TerminalRunParams.fromJson(payload);
138
146
  return (await this.runAfterClosesAsync(params, client)).toJson();
@@ -209,8 +217,8 @@ export class TerminalHost {
209
217
  for (const [agentSessionId, bound] of this.agentSessionOwners)
210
218
  if (bound === owner)
211
219
  this.agentSessionOwners.delete(agentSessionId);
212
- const isOwned = (terminal) => terminal.owner === owner
213
- && (Object.isNull(client) || terminal.isReachedBy(client.connection, client.isApp));
220
+ const isOwned = (terminal) => terminal.owner === owner &&
221
+ (Object.isNull(client) || terminal.isReachedBy(client.connection, client.isApp));
214
222
  const closed = Promise.withResolvers();
215
223
  this.closings.set(isOwned, closed);
216
224
  try {
@@ -286,6 +294,7 @@ export class TerminalHost {
286
294
  const terminal = new HostedTerminal(new TerminalSession(sessionId, shell.id, size, started.process, started.nonce, this.windowsPty, historyPaths, storedLimit, {
287
295
  onOutput: t => publish(this.events.output, t.toJson()),
288
296
  onExited: t => {
297
+ this.endCommandWork(t.sessionId);
289
298
  this.endRun(terminal, TerminalRunEndedBy.TerminalEnded, null);
290
299
  if (terminal.isEndSent)
291
300
  return;
@@ -303,11 +312,13 @@ export class TerminalHost {
303
312
  publish(this.events.cwdChanged, t.toJson());
304
313
  },
305
314
  onCommandEnded: t => {
315
+ this.endCommandWork(t.sessionId);
306
316
  this.endLaunchCommand(terminal, t.exitCode);
307
317
  this.countRunEnd(terminal, t.exitCode, true);
308
318
  publish(this.events.commandEnded, t.toJson());
309
319
  },
310
320
  onPromptStarted: t => {
321
+ this.endCommandWork(t.sessionId);
311
322
  const offset = terminal.launchCommandOffset;
312
323
  if (!Object.isNull(offset) && terminal.session.text.readRange(offset, terminal.session.text.endOffset).includes(Resources.lineFeed))
313
324
  this.endLaunchCommand(terminal, null);
@@ -315,10 +326,15 @@ export class TerminalHost {
315
326
  publish(this.events.promptStarted, t.toJson());
316
327
  },
317
328
  onCommandStarted: t => {
329
+ if (this.commandWork.has(t.sessionId))
330
+ this.beginCommandWork(terminal, t.command);
318
331
  publish(this.events.commandStarted, t.toJson());
319
332
  this.addCommand(shell.id, t.command);
320
333
  },
321
- onOutputStarted: t => publish(this.events.outputStarted, t.toJson()),
334
+ onOutputStarted: t => {
335
+ this.beginCommandWork(terminal, t.command);
336
+ publish(this.events.outputStarted, t.toJson());
337
+ },
322
338
  onOutputPrinted: t => publish(this.events.outputPrinted, t.toJson()),
323
339
  onScreenChanged: t => publish(this.events.screenChanged, t.toJson()),
324
340
  onDetached: t => publish(this.events.detached, t.toJson()),
@@ -333,6 +349,7 @@ export class TerminalHost {
333
349
  terminal.session.writeNote(line);
334
350
  terminal.opensTab = opensTab;
335
351
  this.sessions.set(sessionId, terminal);
352
+ this.watchForeground(terminal);
336
353
  if (isKept)
337
354
  this.saved.save(kept ?? new SavedTerminal(sessionId, shell.id, folder, isPanelTerminal, owner, opensTab));
338
355
  else if (!Object.isNull(ownerSessionId) && !Object.isNull(owner))
@@ -352,7 +369,8 @@ export class TerminalHost {
352
369
  return launching;
353
370
  }
354
371
  async endConnectionAsync(connection, wasApp) {
355
- const isEnding = (terminal) => Object.isNull(terminal.connection) ? wasApp && !Object.isNull(terminal.owner)
372
+ const isEnding = (terminal) => Object.isNull(terminal.connection)
373
+ ? wasApp && !Object.isNull(terminal.owner)
356
374
  : terminal.connection === connection;
357
375
  const closed = Promise.withResolvers();
358
376
  this.closings.set(isEnding, closed);
@@ -420,16 +438,21 @@ export class TerminalHost {
420
438
  terminal.keepsAlive = params.keepsAlive;
421
439
  }
422
440
  if (!Object.isNull(params.command))
423
- void this.typeCommandAsync(terminal, params.command).then(t => {
441
+ void this.typeCommandAsync(terminal, params.command)
442
+ .then(t => {
424
443
  terminal.launchCommandOffset = terminal.session.text.endOffset;
425
- if (t !== TerminalRunOutcome.Typed || !terminal.session.commands.hasMarks || Object.isNull(terminal.session.commands.promptEndOffset) || terminal.keyCount !== 0)
444
+ if (t !== TerminalRunOutcome.Typed ||
445
+ !terminal.session.commands.hasMarks ||
446
+ Object.isNull(terminal.session.commands.promptEndOffset) ||
447
+ terminal.keyCount !== 0)
426
448
  this.endLaunchCommand(terminal, null);
427
- }).catch((error) => this.log.write(Resources.formatLaunchCommandFailed(terminal.session.sessionId, FailureText.read(error))));
449
+ })
450
+ .catch((error) => this.log.write(Resources.formatLaunchCommandFailed(terminal.session.sessionId, FailureText.read(error))));
428
451
  return new TerminalLaunched(terminal.session.sessionId, null, terminal.shell.kind, terminal.session.hasIntegration);
429
452
  }
430
453
  async typeCommandAsync(terminal, command, isPaste = false, waitsForFit = false, readRefusal = () => null, folderKeys = null) {
431
454
  const session = terminal.session;
432
- const typed = isPaste ? command.replace(Resources.leadingBlankLines, "") : command;
455
+ const typed = isPaste ? command.replace(Resources.leadingBlankLines, String.empty) : command;
433
456
  const pending = new AbortController();
434
457
  terminal.pendingCommand = pending;
435
458
  const signal = pending.signal;
@@ -456,16 +479,21 @@ export class TerminalHost {
456
479
  await session.changes.waitUntilAsync(() => session.hasExited || isReady(), Resources.shellStartMilliseconds, signal);
457
480
  const isEnded = () => signal.aborted || session.hasExited || this.sessions.get(session.sessionId) !== terminal || this.isClosing(terminal);
458
481
  const isBracketed = isPaste && !isEnded() && await session.isBracketedPasteAsync();
459
- const outcome = isEnded() ? TerminalRunOutcome.Ended
460
- : isPaste && ((waitsForFit ? terminal.keyCount !== keyCount : terminal.inputCount !== inputCount) || !Object.isNull(session.commands.promptEndOffset) && session.readPromptLine() !== "") ? TerminalRunOutcome.Input
461
- : isPaste && !isBracketed && typed.includes(Resources.tabKey) ? TerminalRunOutcome.Tab
482
+ const outcome = isEnded()
483
+ ? TerminalRunOutcome.Ended
484
+ : isPaste &&
485
+ ((waitsForFit ? terminal.keyCount !== keyCount : terminal.inputCount !== inputCount) ||
486
+ !Object.isNull(session.commands.promptEndOffset) && session.readPromptLine() !== String.empty)
487
+ ? TerminalRunOutcome.Input
488
+ : isPaste && !isBracketed && typed.includes(Resources.tabKey)
489
+ ? TerminalRunOutcome.Tab
462
490
  : readRefusal() ?? TerminalRunOutcome.Typed;
463
491
  if (outcome === TerminalRunOutcome.Typed && !Object.isNull(folderKeys))
464
492
  session.sendFolder(folderKeys, command);
465
493
  else if (outcome === TerminalRunOutcome.Typed) {
466
494
  const text = isPaste ? TerminalHost.toPaste(typed, isBracketed) : `${command}${Resources.enterKey}`;
467
495
  terminal.typedEnds = isBracketed || !isPaste ? 1 : text.split(Resources.enterKey).length - 1;
468
- terminal.paste = isBracketed ? typed.replace(Resources.whitespace, "") : "";
496
+ terminal.paste = isBracketed ? typed.replace(Resources.whitespace, String.empty) : String.empty;
469
497
  if (isPaste)
470
498
  terminal.recordRunInput();
471
499
  session.write(text);
@@ -481,7 +509,7 @@ export class TerminalHost {
481
509
  this.audience.publish(this.events.launchCommandEnded, terminal, new TerminalCommandEnded(terminal.session.sessionId, exitCode).toJson());
482
510
  }
483
511
  static findEchoEnd(output, paste) {
484
- const start = output.replace(Resources.whitespace, "").indexOf(paste);
512
+ const start = output.replace(Resources.whitespace, String.empty).indexOf(paste);
485
513
  if (start < 0)
486
514
  return null;
487
515
  let index = 0;
@@ -491,7 +519,7 @@ export class TerminalHost {
491
519
  return index;
492
520
  }
493
521
  static toPaste(command, isBracketed) {
494
- const text = command.replace(Resources.trailingLineBreaks, "").replace(Resources.pasteLineBreaks, Resources.enterKey);
522
+ const text = command.replace(Resources.trailingLineBreaks, String.empty).replace(Resources.pasteLineBreaks, Resources.enterKey);
495
523
  return isBracketed ? `${Resources.pasteStart}${text}${Resources.pasteEnd}${Resources.enterKey}` : `${text}${Resources.enterKey}`;
496
524
  }
497
525
  async runAfterClosesAsync(params, client) {
@@ -515,14 +543,17 @@ export class TerminalHost {
515
543
  this.ensureRunning();
516
544
  if (!Object.isNull(params.sessionId)) {
517
545
  const target = this.findRunTarget(params.sessionId, params, client);
518
- return Object.isNull(target) || this.isClosing(target) ? new TerminalRunResult(params.sessionId, TerminalRunOutcome.Ended)
546
+ return Object.isNull(target) || this.isClosing(target)
547
+ ? new TerminalRunResult(params.sessionId, TerminalRunOutcome.Ended)
519
548
  : this.runIntoAsync(params, target, client);
520
549
  }
521
550
  const place = new TerminalOpening(params.owner, client.connection);
522
551
  if (this.isClosing(place))
523
552
  return this.endForClose(params, client);
524
- const isOwned = (t) => t.owner === params.owner && t.isReachedBy(client.connection, client.isApp)
525
- && (params.shellKinds.length === 0 || params.shellKinds.includes(t.shell.kind)) && !t.isRunStarting;
553
+ const isOwned = (t) => t.owner === params.owner &&
554
+ t.isReachedBy(client.connection, client.isApp) &&
555
+ (params.shellKinds.length === 0 || params.shellKinds.includes(t.shell.kind)) &&
556
+ !t.isRunStarting;
526
557
  const wanted = Object.isNull(params.folder) ? null : await this.toRunFolderKeyAsync(params.folder);
527
558
  let inFolder = await this.findInFolderAsync(wanted, isOwned);
528
559
  const newest = this.terminals.findLast(t => isOwned(t) && inFolder?.has(t) !== false && (t.isFree || t.isDrawingPrompt));
@@ -533,7 +564,8 @@ export class TerminalHost {
533
564
  let terminal = this.terminals.findLast(t => isOwned(t) && inFolder?.has(t) !== false && t.isFree);
534
565
  const isNew = Object.isUndefined(terminal);
535
566
  if (Object.isUndefined(terminal)) {
536
- const shell = params.shellKinds.length === 0 ? await this.findFallbackShellAsync(params.shellId)
567
+ const shell = params.shellKinds.length === 0
568
+ ? await this.findFallbackShellAsync(params.shellId)
537
569
  : await this.findShellOfKindsAsync(params.shellKinds, params.shellId);
538
570
  if (Object.isUndefined(shell) && params.shellKinds.length > 0)
539
571
  return new TerminalRunResult(null, TerminalRunOutcome.NoShell);
@@ -564,8 +596,13 @@ export class TerminalHost {
564
596
  }
565
597
  findRunTarget(sessionId, params, client) {
566
598
  const terminal = this.sessions.get(sessionId);
567
- return Object.isUndefined(terminal) || !terminal.isReachedBy(client.connection, client.isApp) || !Object.isNull(terminal.ownerSessionId)
568
- || terminal.owner !== params.owner || params.shellKinds.length > 0 && !params.shellKinds.includes(terminal.shell.kind) ? null : terminal;
599
+ return Object.isUndefined(terminal) ||
600
+ !terminal.isReachedBy(client.connection, client.isApp) ||
601
+ !Object.isNull(terminal.ownerSessionId) ||
602
+ terminal.owner !== params.owner ||
603
+ params.shellKinds.length > 0 && !params.shellKinds.includes(terminal.shell.kind)
604
+ ? null
605
+ : terminal;
569
606
  }
570
607
  async runIntoAsync(params, terminal, client) {
571
608
  const session = terminal.session;
@@ -573,8 +610,10 @@ export class TerminalHost {
573
610
  return new TerminalRunResult(session.sessionId, TerminalRunOutcome.Busy);
574
611
  terminal.isRunStarting = true;
575
612
  try {
576
- const isBusy = () => !terminal.isFree && !session.hasExited && this.sessions.get(session.sessionId) === terminal
577
- && !this.isClosing(terminal);
613
+ const isBusy = () => !terminal.isFree &&
614
+ !session.hasExited &&
615
+ this.sessions.get(session.sessionId) === terminal &&
616
+ !this.isClosing(terminal);
578
617
  if (isBusy() && terminal.isDrawingPrompt) {
579
618
  await this.waitForPromptAsync(terminal);
580
619
  if (this.sessions.get(session.sessionId) !== terminal)
@@ -649,7 +688,7 @@ export class TerminalHost {
649
688
  run.isAfterEndMark = isEndMark;
650
689
  run.exitCode = exitCode;
651
690
  run.offset = text.endOffset;
652
- run.paste = "";
691
+ run.paste = String.empty;
653
692
  run.remainingEnds--;
654
693
  if (run.remainingEnds === 0)
655
694
  this.endRun(terminal, TerminalRunEndedBy.Command, exitCode);
@@ -670,8 +709,13 @@ export class TerminalHost {
670
709
  const runIds = Object.isUndefined(terminal) ? [] : [...terminal.runs.keys()];
671
710
  const index = runIds.indexOf(params.runId);
672
711
  const run = terminal?.runs.get(params.runId);
673
- if (Object.isUndefined(terminal) || Object.isUndefined(run) || !client.isApp || !terminal.isReachedBy(client.connection, true) || !Object.isNull(terminal.ownerSessionId)
674
- || terminal.session.hasExited || terminal.isRestarting)
712
+ if (Object.isUndefined(terminal) ||
713
+ Object.isUndefined(run) ||
714
+ !client.isApp ||
715
+ !terminal.isReachedBy(client.connection, true) ||
716
+ !Object.isNull(terminal.ownerSessionId) ||
717
+ terminal.session.hasExited ||
718
+ terminal.isRestarting)
675
719
  return null;
676
720
  const nextId = runIds[index + 1];
677
721
  const next = Object.isUndefined(nextId) ? undefined : terminal.runs.get(nextId);
@@ -742,8 +786,10 @@ export class TerminalHost {
742
786
  for (const terminal of saved) {
743
787
  if (this.sessions.has(terminal.sessionId))
744
788
  continue;
745
- if (terminal.isPanelTerminal && (terminal !== panel || this.panelOpenings.has(client.connection)
746
- || this.terminals.some(t => t.isPanelTerminal && t.connection === client.connection)))
789
+ if (terminal.isPanelTerminal &&
790
+ (terminal !== panel ||
791
+ this.panelOpenings.has(client.connection) ||
792
+ this.terminals.some(t => t.isPanelTerminal && t.connection === client.connection)))
747
793
  this.saved.forget(terminal.sessionId);
748
794
  else
749
795
  restoring.push(terminal);
@@ -777,7 +823,7 @@ export class TerminalHost {
777
823
  : hasFolder ? Resources.formatRestoredShellNote(saved.shellId) : Resources.formatRestoredShellAndFolderNote(saved.shellId, saved.folder);
778
824
  const terminal = await this.openTerminalAsync(hasShell ? saved.shellId : fallback, hasFolder ? saved.folder : this.homeFolder, params.size, null, params.storedLimit, note, saved.isPanelTerminal, saved.owner, client, saved.sessionId, null, false, saved.opensTab, saved);
779
825
  const name = saved.name;
780
- terminal.name = !Object.isNull(name) && name.trim() !== "" && name.length <= ProtocolResources.maximumTitleLength ? name : null;
826
+ terminal.name = !Object.isNull(name) && name.trim() !== String.empty && name.length <= ProtocolResources.maximumTitleLength ? name : null;
781
827
  if (!hasShell)
782
828
  this.log.write(Resources.formatRestoredShellMissing(saved.sessionId, saved.shellId));
783
829
  if (!hasFolder)
@@ -852,7 +898,7 @@ export class TerminalHost {
852
898
  }
853
899
  }
854
900
  toFolderKey(folder) {
855
- const trimmed = folder.replace(Resources.trailingFolderSeparators, "");
901
+ const trimmed = folder.replace(Resources.trailingFolderSeparators, String.empty);
856
902
  return Object.isNull(this.windowsPty) ? trimmed : trimmed.replaceAll(Resources.posixRoot, Resources.windowsSeparator).toLowerCase();
857
903
  }
858
904
  async closeAsync(sessionId, client) {
@@ -868,19 +914,22 @@ export class TerminalHost {
868
914
  async completeAsync(params, client) {
869
915
  const terminal = this.findHosted(params.sessionId, client);
870
916
  const keys = params.asksShell && this.completions.followsCommand(terminal.shell, params.line, params.cursor)
871
- ? this.completions.adapterOf(terminal.shell)?.optionsKeys ?? undefined : undefined;
917
+ ? this.completions.adapterOf(terminal.shell)?.optionsKeys ?? undefined
918
+ : undefined;
872
919
  const [completion, reply] = await Promise.all([
873
920
  this.completions.completeAsync(params.sessionId, terminal.shell, params.line, params.cursor, terminal.session.cwd ?? terminal.folder, this.readCompletionEnvironment(terminal), terminal.session.shellNames, terminal.session.shellBuiltins),
874
921
  Object.isUndefined(keys) ? Promise.resolve(ShellOptionsReply.none) : terminal.session.askOptionsAsync(keys)
875
922
  ]);
876
923
  if (reply.isTimedOut)
877
924
  return new TerminalCompletion(params.cursor, [], true);
878
- return Object.isNull(reply.options) ? completion
925
+ return Object.isNull(reply.options)
926
+ ? completion
879
927
  : this.completions.addOptions(completion, terminal.shell, params.line, params.cursor, reply.options);
880
928
  }
881
929
  async findCommandsAsync(params, client) {
882
930
  const terminal = this.findHosted(params.sessionId, client);
883
- const shellNames = terminal.session.isShellPathReset || terminal.session.isShellModulePathReset ? null
931
+ const shellNames = terminal.session.isShellPathReset || terminal.session.isShellModulePathReset
932
+ ? null
884
933
  : terminal.session.shellNames;
885
934
  const environment = this.readCompletionEnvironment(terminal);
886
935
  return this.completions.adapterOf(terminal.shell)?.looksUpMissing === true
@@ -935,6 +984,7 @@ export class TerminalHost {
935
984
  throw new SessionNotFoundException(sessionId);
936
985
  }
937
986
  session.restart(started.process, started.nonce, size);
987
+ this.watchForeground(terminal);
938
988
  terminal.startTemporaryFolder = started.temporaryFolder;
939
989
  terminal.clearRuns();
940
990
  terminal.heldExit = null;
@@ -981,7 +1031,8 @@ export class TerminalHost {
981
1031
  const terminal = this.findHosted(reference.sessionId, client);
982
1032
  terminal.paneClients.set(reference.paneId, client);
983
1033
  const attachment = await terminal.session.attachAsync(reference.paneId, Object.isNull(terminal.ownerSessionId) && this.isSuggesting()
984
- ? this.commandHistory.list(terminal.shell.id) : []);
1034
+ ? this.commandHistory.list(terminal.shell.id)
1035
+ : []);
985
1036
  if (!terminal.paneClients.has(reference.paneId))
986
1037
  terminal.session.detach(reference.paneId);
987
1038
  return attachment;
@@ -1000,11 +1051,11 @@ export class TerminalHost {
1000
1051
  async writeAsync(params, client) {
1001
1052
  const terminal = this.findHosted(params.sessionId, client);
1002
1053
  const session = terminal.session;
1003
- const text = params.text.replace(Resources.pasteMarkers, "").replace(Resources.pasteLineBreaks, Resources.enterKey);
1054
+ const text = params.text.replace(Resources.pasteMarkers, String.empty).replace(Resources.pasteLineBreaks, Resources.enterKey);
1004
1055
  const isBracketed = text.length > 0 && !session.hasExited && await session.isBracketedPasteAsync();
1005
1056
  if (session.hasExited || this.sessions.get(params.sessionId) !== terminal)
1006
1057
  throw new SessionNotFoundException(params.sessionId);
1007
- const written = `${isBracketed ? `${Resources.pasteStart}${text}${Resources.pasteEnd}` : text}${params.isEnter ? Resources.enterKey : ""}`;
1058
+ const written = `${isBracketed ? `${Resources.pasteStart}${text}${Resources.pasteEnd}` : text}${params.isEnter ? Resources.enterKey : String.empty}`;
1008
1059
  if (written.length === 0)
1009
1060
  return;
1010
1061
  terminal.recordInput(false, written, true);
@@ -1033,12 +1084,16 @@ export class TerminalHost {
1033
1084
  throw new SessionNotFoundException(params.sessionId);
1034
1085
  const typedAt = session.text.endOffset;
1035
1086
  const prompts = session.prompts;
1036
- const isMoved = () => Object.isNull(folderKeys) ? (session.commands.promptEndOffset ?? typedAt) > typedAt
1087
+ const isMoved = () => Object.isNull(folderKeys)
1088
+ ? (session.commands.promptEndOffset ?? typedAt) > typedAt
1037
1089
  : session.prompts > prompts && !Object.isNull(session.commands.promptEndOffset);
1038
1090
  await session.changes.waitUntilAsync(() => session.hasExited || this.sessions.get(params.sessionId) !== terminal || isMoved(), Resources.folderChangeWaitMilliseconds, new AbortController().signal);
1039
1091
  if (session.hasExited || this.sessions.get(params.sessionId) !== terminal)
1040
1092
  throw new SessionNotFoundException(params.sessionId);
1041
- const isChanged = Object.isNull(folderKeys) ? session.commands.lastCommand?.exitCode === 0 : session.cwd !== before || session.cwd === params.folder;
1093
+ const isChanged = Object.isNull(folderKeys)
1094
+ ? session.commands.lastCommand?.exitCode === 0
1095
+ : session.cwd !== before ||
1096
+ session.cwd === params.folder;
1042
1097
  return new TerminalFolderChanged(isMoved() && isChanged ? session.cwd : null, true);
1043
1098
  }
1044
1099
  static readMoveRefusal(terminal) {
@@ -1047,7 +1102,7 @@ export class TerminalHost {
1047
1102
  return TerminalRunOutcome.NoShell;
1048
1103
  if (Object.isNull(commands.promptEndOffset))
1049
1104
  return TerminalRunOutcome.Busy;
1050
- return terminal.hasUnreadInput || terminal.session.readPromptLine() !== "" ? TerminalRunOutcome.Input : null;
1105
+ return terminal.hasUnreadInput || terminal.session.readPromptLine() !== String.empty ? TerminalRunOutcome.Input : null;
1051
1106
  }
1052
1107
  resize(params, client) {
1053
1108
  const terminal = this.findHosted(params.sessionId, client);
@@ -1121,9 +1176,74 @@ export class TerminalHost {
1121
1176
  return hasEnded;
1122
1177
  }
1123
1178
  async disposeAsync(session) {
1179
+ this.stopForegroundWatch(session.sessionId);
1180
+ this.endCommandWork(session.sessionId);
1124
1181
  for (const failure of await session.disposeAsync())
1125
1182
  this.log.write(Resources.formatStoredLinesRemovalFailed(session.sessionId, FailureText.read(failure)));
1126
1183
  }
1184
+ beginCommandWork(terminal, command) {
1185
+ this.beginWork(terminal.session.sessionId, Resources.formatRunningCommand(command, terminal.name ?? terminal.shell.name));
1186
+ }
1187
+ beginWork(sessionId, description) {
1188
+ const previous = this.commandWork.get(sessionId);
1189
+ this.commandWork.set(sessionId, this.context.beginWork(description));
1190
+ previous?.[Symbol.dispose]();
1191
+ }
1192
+ endCommandWork(sessionId) {
1193
+ const item = this.commandWork.get(sessionId);
1194
+ this.commandWork.delete(sessionId);
1195
+ this.foregroundWork.delete(sessionId);
1196
+ item?.[Symbol.dispose]();
1197
+ }
1198
+ watchForeground(terminal) {
1199
+ const sessionId = terminal.session.sessionId;
1200
+ if (!Object.isNull(this.windowsPty) || this.foregroundWatches.has(sessionId))
1201
+ return;
1202
+ const startedAt = Date.now();
1203
+ const schedule = () => {
1204
+ const timer = setTimeout(() => void this.readForegroundWorkAsync(terminal, startedAt).then(t => {
1205
+ if (this.foregroundWatches.get(sessionId) !== timer)
1206
+ return;
1207
+ if (t)
1208
+ schedule();
1209
+ else
1210
+ this.stopForegroundWatch(sessionId);
1211
+ }), Resources.foregroundWorkPollMilliseconds);
1212
+ this.foregroundWatches.set(sessionId, timer);
1213
+ };
1214
+ schedule();
1215
+ }
1216
+ stopForegroundWatch(sessionId) {
1217
+ clearTimeout(this.foregroundWatches.get(sessionId));
1218
+ this.foregroundWatches.delete(sessionId);
1219
+ }
1220
+ async readForegroundWorkAsync(terminal, startedAt) {
1221
+ const session = terminal.session;
1222
+ if (session.hasExited || session.commands.hasMarks)
1223
+ return false;
1224
+ if (session.hasIntegration && Date.now() - startedAt < Resources.integrationWaitMilliseconds)
1225
+ return true;
1226
+ let foreground;
1227
+ try {
1228
+ foreground = await session.readForegroundProcessAsync();
1229
+ }
1230
+ catch (error) {
1231
+ this.log.write(Resources.formatForegroundUnread(session.sessionId, FailureText.read(error)));
1232
+ return true;
1233
+ }
1234
+ if (!this.foregroundWatches.has(session.sessionId))
1235
+ return false;
1236
+ if (Object.isNull(foreground) || foreground.group === session.pid || foreground.isTerminalProgram) {
1237
+ this.endCommandWork(session.sessionId);
1238
+ return true;
1239
+ }
1240
+ const description = Resources.formatRunningCommand(foreground.commandLine ?? foreground.program, terminal.name ?? (session.title === String.empty ? terminal.shell.name : session.title));
1241
+ if (this.foregroundWork.get(session.sessionId) !== description) {
1242
+ this.beginWork(session.sessionId, description);
1243
+ this.foregroundWork.set(session.sessionId, description);
1244
+ }
1245
+ return true;
1246
+ }
1127
1247
  ensureRunning() {
1128
1248
  if (this.hasEnded)
1129
1249
  throw new HostEndedException();
@@ -1139,9 +1259,10 @@ export class TerminalHost {
1139
1259
  }
1140
1260
  async startShellAsync(shell, folder, size, note, variables, deferredSessionId = null) {
1141
1261
  const environment = this.withVariables(await this.readEnvironmentAsync(), variables);
1142
- const printedNote = Object.isNull(note) || Object.isNull(this.windowsPty) ? null
1143
- : note.split(Resources.lineBreaks).map(t => TerminalSession.toNote(t)).join("");
1144
- const launch = await this.integration.prepareAsync(shell, randomUUID().replaceAll(Resources.terminalHostUuidSeparator, ""), environment, printedNote);
1262
+ const printedNote = Object.isNull(note) || Object.isNull(this.windowsPty)
1263
+ ? null
1264
+ : note.split(Resources.lineBreaks).map(t => TerminalSession.toNote(t)).join(String.empty);
1265
+ const launch = await this.integration.prepareAsync(shell, randomUUID().replaceAll(Resources.terminalHostUuidSeparator, String.empty), environment, printedNote);
1145
1266
  const inheritsCursor = !Object.isNull(note) && !launch.printsNote;
1146
1267
  let process;
1147
1268
  let deferred = null;
@@ -25,7 +25,7 @@ export class TerminalLineReader {
25
25
  }
26
26
  readRuns(line, end) {
27
27
  const runs = [];
28
- let text = "";
28
+ let text = String.empty;
29
29
  let length = 0;
30
30
  let foreground = ProtocolResources.defaultColor;
31
31
  let background = ProtocolResources.defaultColor;
@@ -64,8 +64,14 @@ export class TerminalLineReader {
64
64
  return end;
65
65
  }
66
66
  isBlank(cell) {
67
- return !Object.isUndefined(cell) && ((cell.getCode() === 0 && cell.isAttributeDefault()) || (cell.getChars().trim().length === 0 && cell.isBgDefault() &&
68
- !cell.isInverse() && !cell.isUnderline() && !cell.isStrikethrough() && !cell.isOverline()));
67
+ return !Object.isUndefined(cell) &&
68
+ ((cell.getCode() === 0 && cell.isAttributeDefault()) ||
69
+ (cell.getChars().trim().length === 0 &&
70
+ cell.isBgDefault() &&
71
+ !cell.isInverse() &&
72
+ !cell.isUnderline() &&
73
+ !cell.isStrikethrough() &&
74
+ !cell.isOverline()));
69
75
  }
70
76
  static foregroundOf(cell) {
71
77
  if (cell.isFgDefault())
@@ -24,7 +24,7 @@ export class TerminalMirror {
24
24
  anchors = new Map();
25
25
  writtenOffset = 0;
26
26
  removedLines = 0;
27
- currentTitle = "";
27
+ currentTitle = String.empty;
28
28
  oldest = null;
29
29
  oldestMarker = null;
30
30
  inputStart = undefined;
@@ -56,15 +56,15 @@ export class TerminalMirror {
56
56
  this.terminal.write(data, () => this.listener.onParsed(offset));
57
57
  }
58
58
  resize(size) {
59
- this.terminal.write("", () => this.resizeNow(size));
59
+ this.terminal.write(String.empty, () => this.resizeNow(size));
60
60
  }
61
61
  snapshotAsync() {
62
62
  const offset = this.writtenOffset;
63
63
  const pending = this.tracker.pending;
64
- return new Promise(resolve => this.terminal.write("", () => resolve(new MirrorSnapshot(this.serializer.serialize({ scrollback: Resources.scrollback }), pending, offset, this.readInputStart()))));
64
+ return new Promise(resolve => this.terminal.write(String.empty, () => resolve(new MirrorSnapshot(this.serializer.serialize({ scrollback: Resources.scrollback }), pending, offset, this.readInputStart()))));
65
65
  }
66
66
  startInput() {
67
- this.terminal.write("", () => {
67
+ this.terminal.write(String.empty, () => {
68
68
  this.inputStart?.dispose();
69
69
  const buffer = this.terminal.buffer.active;
70
70
  this.inputStart = buffer.type === Resources.normalBufferType ? this.terminal.registerMarker(0) : undefined;
@@ -73,7 +73,7 @@ export class TerminalMirror {
73
73
  });
74
74
  }
75
75
  markRightPrompt() {
76
- this.terminal.write("", () => {
76
+ this.terminal.write(String.empty, () => {
77
77
  const start = this.inputStart;
78
78
  const buffer = this.terminal.buffer.active;
79
79
  if (!Object.isUndefined(start) && !start.isDisposed && buffer.baseY + buffer.cursorY === start.line)
@@ -81,7 +81,7 @@ export class TerminalMirror {
81
81
  });
82
82
  }
83
83
  endInput() {
84
- return new Promise(resolve => this.terminal.write("", () => {
84
+ return new Promise(resolve => this.terminal.write(String.empty, () => {
85
85
  const line = this.isDisposed ? null : this.readInput();
86
86
  this.inputStart?.dispose();
87
87
  this.inputStart = undefined;
@@ -89,14 +89,15 @@ export class TerminalMirror {
89
89
  }));
90
90
  }
91
91
  isBracketedPasteAsync() {
92
- return new Promise(resolve => this.terminal.write("", () => resolve(this.terminal.modes.bracketedPasteMode)));
92
+ return new Promise(resolve => this.terminal.write(String.empty, () => resolve(this.terminal.modes.bracketedPasteMode)));
93
93
  }
94
94
  anchorAsync(at) {
95
- return new Promise(resolve => this.terminal.write("", () => {
95
+ return new Promise(resolve => this.terminal.write(String.empty, () => {
96
96
  const buffer = this.terminal.buffer.normal;
97
97
  const cursor = buffer.baseY + buffer.cursorY;
98
98
  const index = Object.isNull(at) ? cursor : at.index;
99
- const marker = Object.isNull(index) || this.terminal.buffer.active.type !== Resources.normalBufferType ? undefined
99
+ const marker = Object.isNull(index) || this.terminal.buffer.active.type !== Resources.normalBufferType
100
+ ? undefined
100
101
  : this.terminal.registerMarker(index - cursor);
101
102
  const line = new MirrorLine(at?.number ?? this.removedLines + cursor, marker);
102
103
  if (!Object.isUndefined(marker)) {
@@ -107,7 +108,7 @@ export class TerminalMirror {
107
108
  }));
108
109
  }
109
110
  readLinesAsync(start, end, maximum) {
110
- return new Promise(resolve => this.terminal.write("", () => resolve(this.readLines(start.number, end?.number ?? null, maximum))));
111
+ return new Promise(resolve => this.terminal.write(String.empty, () => resolve(this.readLines(start.number, end?.number ?? null, maximum))));
111
112
  }
112
113
  dispose() {
113
114
  this.isDisposed = true;
@@ -119,7 +120,7 @@ export class TerminalMirror {
119
120
  if (Object.isUndefined(start) || start.isDisposed)
120
121
  return null;
121
122
  const buffer = this.terminal.buffer.normal;
122
- let text = "";
123
+ let text = String.empty;
123
124
  let line = buffer.getLine(start.line);
124
125
  for (let index = start.line; !Object.isUndefined(line); index++) {
125
126
  const next = buffer.getLine(index + 1);
@@ -147,7 +147,7 @@ export class TerminalSession {
147
147
  if (isKey)
148
148
  this.options.cancel();
149
149
  this.beforeInput();
150
- for (let lines = data.replace(Resources.bracketedPastes, "").split(Resources.lineBreaks).length - 1; lines > 0; lines--) {
150
+ for (let lines = data.replace(Resources.bracketedPastes, String.empty).split(Resources.lineBreaks).length - 1; lines > 0; lines--) {
151
151
  this.commands.enterLine();
152
152
  this.enteredAt = Date.now();
153
153
  }
@@ -173,10 +173,14 @@ export class TerminalSession {
173
173
  return true;
174
174
  }
175
175
  async askOptionsAsync(keys) {
176
- if (!this.hasIntegration || !this.commands.hasOptionsBinding || !this.commands.isWaitingAtPrompt || !Object.isNull(this.commands.runningCommand)
177
- || !Object.isNull(this.exit))
176
+ if (!this.hasIntegration ||
177
+ !this.commands.hasOptionsBinding ||
178
+ !this.commands.isWaitingAtPrompt ||
179
+ !Object.isNull(this.commands.runningCommand) ||
180
+ !Object.isNull(this.exit))
178
181
  return ShellOptionsReply.none;
179
- const reply = this.options.askAsync(this.hasAnsweredOptions ? Resources.optionsMilliseconds
182
+ const reply = this.options.askAsync(this.hasAnsweredOptions
183
+ ? Resources.optionsMilliseconds
180
184
  : Resources.firstOptionsMilliseconds);
181
185
  this.beforeInput();
182
186
  this.terminal.write(keys);
@@ -192,7 +196,14 @@ export class TerminalSession {
192
196
  const promptEnd = this.commands.promptEndOffset;
193
197
  if (Object.isNull(promptEnd) || !Object.isNull(this.commands.runningCommand))
194
198
  return null;
195
- return this.text.readSince(promptEnd).text.trim();
199
+ let line = String.empty;
200
+ let from = promptEnd;
201
+ for (const span of this.commands.rightPrompts)
202
+ if (span.start >= promptEnd && !Object.isNull(span.end)) {
203
+ line += this.text.readRange(from, span.start);
204
+ from = span.end;
205
+ }
206
+ return (line + this.text.readRange(from, this.text.endOffset)).trim();
196
207
  }
197
208
  resize(size) {
198
209
  if (size.equals(this.size))
@@ -374,7 +385,8 @@ export class TerminalSession {
374
385
  this.listener.onCwdChanged(new TerminalCwd(this.sessionId, cwd));
375
386
  }
376
387
  settleTypedLines(mark) {
377
- const command = mark.kind === MarkKind.OutputStart ? this.commands.runningCommand
388
+ const command = mark.kind === MarkKind.OutputStart
389
+ ? this.commands.runningCommand
378
390
  : mark.kind === MarkKind.CommandEnd ? this.commands.lastCommand : null;
379
391
  for (const line of this.typedLines) {
380
392
  if (!Object.isNull(command))
@@ -429,10 +441,13 @@ export class TerminalSession {
429
441
  this.wakeup?.stop();
430
442
  }
431
443
  endInput(startsCommand, command) {
432
- void this.mirror.endInput().then(t => {
444
+ void this.mirror
445
+ .endInput()
446
+ .then(t => {
433
447
  if (startsCommand && !Object.isNull(t))
434
448
  this.listener.onCommandStarted(new TerminalCommandStarted(this.sessionId, TerminalSession.toCommand(command) ?? t));
435
- }).catch((error) => this.listener.onFailed(this.sessionId, FailureText.read(error)));
449
+ })
450
+ .catch((error) => this.listener.onFailed(this.sessionId, FailureText.read(error)));
436
451
  }
437
452
  readInputEdge(mark) {
438
453
  return this.marksInput ? TerminalSession.toInputEdge(mark.kind) : null;
@@ -455,6 +470,7 @@ export class TerminalSession {
455
470
  return TerminalInputEdge.Continuation;
456
471
  case MarkKind.Property:
457
472
  case MarkKind.Options:
473
+ case MarkKind.RightPromptEnd:
458
474
  return null;
459
475
  default:
460
476
  return TerminalInputEdge.End;