@bastani/atomic-tui 0.9.89 → 0.9.90-alpha.1

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 (44) hide show
  1. package/dist/builtin-command-names.d.ts +4 -0
  2. package/dist/builtin-command-names.d.ts.map +1 -0
  3. package/dist/builtin-command-names.js +42 -0
  4. package/dist/builtin-command-names.js.map +1 -0
  5. package/dist/clickable-hints.d.ts +84 -0
  6. package/dist/clickable-hints.d.ts.map +1 -0
  7. package/dist/clickable-hints.js +267 -0
  8. package/dist/clickable-hints.js.map +1 -0
  9. package/dist/components/editor.d.ts +7 -1
  10. package/dist/components/editor.d.ts.map +1 -1
  11. package/dist/components/editor.js +63 -25
  12. package/dist/components/editor.js.map +1 -1
  13. package/dist/components/settings-list.d.ts.map +1 -1
  14. package/dist/components/settings-list.js +3 -2
  15. package/dist/components/settings-list.js.map +1 -1
  16. package/dist/components/windowed-container.d.ts +2 -0
  17. package/dist/components/windowed-container.d.ts.map +1 -1
  18. package/dist/components/windowed-container.js +4 -0
  19. package/dist/components/windowed-container.js.map +1 -1
  20. package/dist/index.d.ts +3 -2
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +4 -2
  23. package/dist/index.js.map +1 -1
  24. package/dist/keys.d.ts +5 -0
  25. package/dist/keys.d.ts.map +1 -1
  26. package/dist/keys.js +115 -0
  27. package/dist/keys.js.map +1 -1
  28. package/dist/terminal.d.ts +3 -0
  29. package/dist/terminal.d.ts.map +1 -1
  30. package/dist/terminal.js +16 -0
  31. package/dist/terminal.js.map +1 -1
  32. package/dist/tui-alt-screen.d.ts +24 -3
  33. package/dist/tui-alt-screen.d.ts.map +1 -1
  34. package/dist/tui-alt-screen.js +205 -58
  35. package/dist/tui-alt-screen.js.map +1 -1
  36. package/dist/tui.d.ts +23 -0
  37. package/dist/tui.d.ts.map +1 -1
  38. package/dist/tui.js +48 -5
  39. package/dist/tui.js.map +1 -1
  40. package/dist/utils.d.ts +4 -0
  41. package/dist/utils.d.ts.map +1 -1
  42. package/dist/utils.js +79 -11
  43. package/dist/utils.js.map +1 -1
  44. package/package.json +1 -1
package/dist/utils.js CHANGED
@@ -102,7 +102,7 @@ function truncateFragmentToWidth(text, maxWidth) {
102
102
  }
103
103
  function finalizeTruncatedResult(prefix, prefixWidth, ellipsis, ellipsisWidth, maxWidth, pad) {
104
104
  const reset = "\x1b[0m";
105
- const hyperlinkClose = getActiveOsc8Close(prefix);
105
+ const hyperlinkClose = getActiveOsc8Close(prefix) + getActiveHintClose(prefix);
106
106
  const visibleWidth = prefixWidth + ellipsisWidth;
107
107
  let result;
108
108
  if (ellipsis.length > 0) {
@@ -525,6 +525,15 @@ function formatOsc8Hyperlink(hyperlink) {
525
525
  function formatOsc8Close(terminator) {
526
526
  return `\x1b]8;;${terminator}`;
527
527
  }
528
+ /** The opening marker of a clickable key hint, which its action and a BEL follow; see clickable-hints.ts. */
529
+ export const HINT_OPEN = "\x1b_pi:h:";
530
+ /** The closing marker of a clickable key hint. */
531
+ export const HINT_CLOSE = "\x1b_pi:/h\x07";
532
+ /** The closing marker for a key hint `prefix` leaves open, or "" when it leaves none open. */
533
+ function getActiveHintClose(prefix) {
534
+ const open = prefix.lastIndexOf(HINT_OPEN);
535
+ return open !== -1 && prefix.indexOf(HINT_CLOSE, open) === -1 ? HINT_CLOSE : "";
536
+ }
528
537
  function getActiveOsc8Close(prefix) {
529
538
  if (!prefix.includes("\x1b]8;")) {
530
539
  return "";
@@ -562,7 +571,17 @@ class AnsiCodeTracker {
562
571
  fgColor = null; // Stores the full code like "31" or "38;5;240"
563
572
  bgColor = null; // Stores the full code like "41" or "48;5;240"
564
573
  activeHyperlink = null;
574
+ /** The opening marker of the key hint the text is inside, closed at a line's end and re-opened on the next. */
575
+ activeHint = null;
565
576
  process(ansiCode) {
577
+ if (ansiCode.startsWith(HINT_OPEN)) {
578
+ this.activeHint = ansiCode;
579
+ return;
580
+ }
581
+ if (ansiCode === HINT_CLOSE) {
582
+ this.activeHint = null;
583
+ return;
584
+ }
566
585
  // OSC 8 hyperlink: \x1b]8;;<url>\x1b\\ (open) or \x1b]8;;\x1b\\ (close).
567
586
  // Preserve the original terminator because some terminals only make BEL-terminated
568
587
  // links clickable. OAuth login URLs use BEL, so reopening wrapped lines with ST
@@ -704,12 +723,17 @@ class AnsiCodeTracker {
704
723
  this.strikethrough = false;
705
724
  this.fgColor = null;
706
725
  this.bgColor = null;
707
- // SGR reset does not affect OSC 8 hyperlink state
726
+ // SGR reset does not affect OSC 8 hyperlink or key hint state
708
727
  }
709
728
  /** Clear all state for reuse. */
710
729
  clear() {
711
730
  this.reset();
712
731
  this.activeHyperlink = null;
732
+ this.activeHint = null;
733
+ }
734
+ /** The closing marker of the key hint the text is inside, or "" outside one. */
735
+ getHintClose() {
736
+ return this.activeHint === null ? "" : HINT_CLOSE;
713
737
  }
714
738
  getActiveCodes() {
715
739
  const codes = [];
@@ -737,6 +761,9 @@ class AnsiCodeTracker {
737
761
  if (this.activeHyperlink) {
738
762
  result += formatOsc8Hyperlink(this.activeHyperlink);
739
763
  }
764
+ if (this.activeHint) {
765
+ result += this.activeHint;
766
+ }
740
767
  return result;
741
768
  }
742
769
  getActiveBackgroundCode() {
@@ -753,12 +780,13 @@ class AnsiCodeTracker {
753
780
  this.strikethrough ||
754
781
  this.fgColor !== null ||
755
782
  this.bgColor !== null ||
756
- this.activeHyperlink !== null);
783
+ this.activeHyperlink !== null ||
784
+ this.activeHint !== null);
757
785
  }
758
786
  /**
759
787
  * Get reset codes for attributes that need to be turned off at line end.
760
788
  * Underline and strikethrough must be closed to prevent bleeding into padding.
761
- * Active OSC 8 hyperlinks must be closed and re-opened on the next line.
789
+ * Active OSC 8 hyperlinks and key hints must be closed and re-opened on the next line.
762
790
  * Returns empty string if no attributes need closing.
763
791
  */
764
792
  getLineEndReset() {
@@ -772,7 +800,7 @@ class AnsiCodeTracker {
772
800
  if (this.activeHyperlink) {
773
801
  result += formatOsc8Close(this.activeHyperlink.terminator); // Re-opened at line start via getActiveCodes()
774
802
  }
775
- return result;
803
+ return result + this.getHintClose(); // Re-opened at line start via getActiveCodes()
776
804
  }
777
805
  }
778
806
  function updateTrackerFromText(text, tracker) {
@@ -901,6 +929,10 @@ export function wrapTextWithAnsi(text, width) {
901
929
  for (const inputLine of inputLines) {
902
930
  // Prepend active ANSI codes from previous lines (except for first line)
903
931
  const prefix = result.length > 0 ? tracker.getActiveCodes() : "";
932
+ // A key hint open across the newline ends with this line's text, so padding or a border after it is not part
933
+ // of the hint; the next line re-opens it.
934
+ if (result.length > 0)
935
+ result[result.length - 1] += tracker.getHintClose();
904
936
  const wrappedLines = wrapSingleLine(prefix + inputLine, width);
905
937
  for (const wrappedLine of wrappedLines) {
906
938
  result.push(wrappedLine);
@@ -910,6 +942,39 @@ export function wrapTextWithAnsi(text, width) {
910
942
  }
911
943
  return result.length > 0 ? result : [""];
912
944
  }
945
+ function hintMarkerLength(line, index) {
946
+ if (line.startsWith(HINT_CLOSE, index))
947
+ return HINT_CLOSE.length;
948
+ return line.startsWith(HINT_OPEN, index) ? ansiCodeLength(line, index) : 0;
949
+ }
950
+ /** Hint markers carry the active action across a wrap but do not by themselves make a row. */
951
+ function hasWrapContent(line) {
952
+ for (let i = 0; i < line.length;) {
953
+ const length = hintMarkerLength(line, i);
954
+ if (!length)
955
+ return true;
956
+ i += length;
957
+ }
958
+ return false;
959
+ }
960
+ /** Trim the same spaces as the unmarked row, retaining any hint markers among them. */
961
+ function trimWrapEnd(line) {
962
+ const trimmed = line.trimEnd();
963
+ if (!trimmed.endsWith("\x07"))
964
+ return trimmed;
965
+ let end = trimmed.length;
966
+ const markers = [];
967
+ while (end > 0 && trimmed.charCodeAt(end - 1) === 7) {
968
+ const start = trimmed.lastIndexOf("\x1b", end - 1);
969
+ if (start < 0 || hintMarkerLength(trimmed, start) !== end - start)
970
+ break;
971
+ markers.push(trimmed.slice(start, end));
972
+ end = start;
973
+ while (end > 0 && WHITESPACE_REGEX.test(trimmed[end - 1]))
974
+ end--;
975
+ }
976
+ return markers.length ? trimmed.slice(0, end) + markers.reverse().join("") : trimmed;
977
+ }
913
978
  function wrapSingleLine(line, width) {
914
979
  if (!line) {
915
980
  return [""];
@@ -928,9 +993,9 @@ function wrapSingleLine(line, width) {
928
993
  const isWhitespace = isWhitespaceToken(token);
929
994
  // Token itself is too long - break it character by character
930
995
  if (tokenVisibleLength > width && !isWhitespace) {
931
- if (currentLine) {
996
+ if (hasWrapContent(currentLine)) {
932
997
  // Trim before adding the reset, which would otherwise block the final trimEnd()
933
- currentLine = currentLine.trimEnd();
998
+ currentLine = trimWrapEnd(currentLine);
934
999
  // Add specific reset for underline only (preserves background)
935
1000
  const lineEndReset = tracker.getLineEndReset();
936
1001
  if (lineEndReset) {
@@ -953,7 +1018,7 @@ function wrapSingleLine(line, width) {
953
1018
  const totalNeeded = currentVisibleLength + tokenVisibleLength;
954
1019
  if (totalNeeded > width && currentVisibleLength > 0) {
955
1020
  // Trim trailing whitespace, then add underline reset (not full reset, to preserve background)
956
- let lineToWrap = currentLine.trimEnd();
1021
+ let lineToWrap = trimWrapEnd(currentLine);
957
1022
  if (isWhitespace) {
958
1023
  // The dropped space may carry codes (e.g. closing a styled word). Keep them on the wrapped line so
959
1024
  // the style ends there and is not re-opened on the next line.
@@ -974,7 +1039,10 @@ function wrapSingleLine(line, width) {
974
1039
  currentLine = tracker.getActiveCodes() + token;
975
1040
  currentVisibleLength = tokenVisibleLength;
976
1041
  }
977
- else if (isWhitespace && totalNeeded > width && token.includes("\x1b")) {
1042
+ else if (isWhitespace &&
1043
+ totalNeeded > width &&
1044
+ token.includes("\x1b") &&
1045
+ hasWrapContent(extractAnsiCodes(token))) {
978
1046
  // Whitespace wider than an empty line. Plain spaces are removed by the final trimEnd(), but codes after
979
1047
  // them (e.g. a trailing reset) would block that and leave the line wider than width. Keep only the codes.
980
1048
  currentLine += extractAnsiCodes(token);
@@ -986,12 +1054,12 @@ function wrapSingleLine(line, width) {
986
1054
  }
987
1055
  updateTrackerFromText(token, tracker);
988
1056
  }
989
- if (currentLine) {
1057
+ if (hasWrapContent(currentLine)) {
990
1058
  // No reset at end of final line - let caller handle it
991
1059
  wrapped.push(currentLine);
992
1060
  }
993
1061
  // Trailing whitespace can cause lines to exceed the requested width
994
- return wrapped.length > 0 ? wrapped.map((line) => line.trimEnd()) : [""];
1062
+ return wrapped.length > 0 ? wrapped.map(trimWrapEnd) : [""];
995
1063
  }
996
1064
  export const PUNCTUATION_REGEX = /[(){}[\]<>.,;:'"!?+\-=*/\\|&%^$#@~`]/;
997
1065
  /**