@gesslar/toolkit 3.38.0 → 3.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "name": "gesslar",
6
6
  "url": "https://gesslar.dev"
7
7
  },
8
- "version": "3.38.0",
8
+ "version": "3.40.0",
9
9
  "license": "Unlicense",
10
10
  "homepage": "https://github.com/gesslar/toolkit#readme",
11
11
  "repository": {
@@ -1,10 +1,12 @@
1
+ import c from "@gesslar/colours"
1
2
  import console, {Console} from "node:console"
2
3
  import process from "node:process"
3
4
  import {Writable} from "node:stream"
4
- import supportsColor from "supports-color"
5
5
  import {stripVTControlCharacters} from "node:util"
6
- import c from "@gesslar/colours"
6
+ import supportsColor from "supports-color"
7
7
 
8
+ import Promised from "../../browser/lib/Promised.js"
9
+ import Time from "../../browser/lib/Time.js"
8
10
  import Sass from "./Sass.js"
9
11
 
10
12
  c.alias.set("success", "{F035}")
@@ -488,13 +490,15 @@ export default class Term {
488
490
  * If in Char Mode, it resolves on Enter, Ctrl+D, or the ANSI 'R' terminator.
489
491
  *
490
492
  * @param {(text: string) => boolean} [terminator] - Optional callback to check if input is complete.
491
- * @returns {Promise<string>} Resolves with the input data.
493
+ * @param {number} [timeoutMs=0] - Optional timeout in milliseconds. Resolves with empty string if exceeded.
494
+ * @returns {Promise<string>} Resolves with the input data, or empty string on timeout.
492
495
  */
493
- static data(terminator = () => false) {
496
+ static data(terminator = () => false, timeoutMs = 0) {
494
497
  process.stdin.resume()
495
498
 
496
499
  return new Promise((resolve, reject) => {
497
500
  const chunks = []
501
+ let timer = null
498
502
 
499
503
  function onData(chunk) {
500
504
  const s = chunk.toString()
@@ -532,6 +536,7 @@ export default class Term {
532
536
  }
533
537
 
534
538
  function cleanup() {
539
+ clearTimeout(timer)
535
540
  process.stdin.off("data", onData)
536
541
  process.stdin.off("end", onEnd)
537
542
  process.stdin.off("error", onError)
@@ -544,11 +549,15 @@ export default class Term {
544
549
  process.stdin.on("data", onData)
545
550
  process.stdin.once("end", onEnd)
546
551
  process.stdin.once("error", onError)
552
+
553
+ if(timeoutMs > 0)
554
+ timer = setTimeout(onEnd, timeoutMs)
547
555
  })
548
556
  }
549
557
 
550
558
  /**
551
559
  * Gets the current cursor position in the terminal.
560
+ * Returns [0, 0] for non-interactive terminals or if the terminal does not respond within the timeout.
552
561
  *
553
562
  * @returns {Promise<[number, number]>} Resolves with [x, y] cursor position.
554
563
  */
@@ -567,13 +576,16 @@ export default class Term {
567
576
  // 2. Start the listener FIRST (do not await yet)
568
577
  const dataPromise = this.data((text => {
569
578
  return this.isCharMode && text.endsWith("R")
570
- }).bind(this))
579
+ }).bind(this), 25)
571
580
 
572
581
  // 3. Write to stdout AFTER the listener is ready
573
582
  this.write("\x1b[6n")
574
583
 
575
584
  // 4. Now await the response
576
- const positionData = await dataPromise
585
+ const positionData = await Promised.race([
586
+ Time.after(25),
587
+ dataPromise
588
+ ])
577
589
 
578
590
  // 5. Restore the previous mode
579
591
  prevRawMode ? this.setCharMode() : this.setLineMode()
@@ -601,8 +613,11 @@ export default class Term {
601
613
  })
602
614
  }
603
615
 
604
- // Spinner frames - using Braille patterns (widely supported)
605
- // Falls back to ASCII when spinimate is implemented with proper detection
616
+ /**
617
+ * Spinner animation frames using Braille patterns (widely supported).
618
+ *
619
+ * @type {readonly string[]}
620
+ */
606
621
  static spinFrames = Object.freeze(["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"])
607
622
 
608
623
  // static async spinimate(delay=300, options = {position: {x: 0,y: 0}}) {
@@ -648,4 +663,98 @@ export default class Term {
648
663
 
649
664
  return this
650
665
  }
666
+
667
+ /**
668
+ * Switch to the alternate screen buffer.
669
+ *
670
+ * @returns {typeof Term} The Term class for chaining.
671
+ */
672
+ static altScreen() {
673
+ this.write("\x1b[?1049h")
674
+
675
+ return this
676
+ }
677
+
678
+ /**
679
+ * Switch back to the main screen buffer.
680
+ *
681
+ * @returns {typeof Term} The Term class for chaining.
682
+ */
683
+ static mainScreen() {
684
+ this.write("\x1b[?1049l")
685
+
686
+ return this
687
+ }
688
+
689
+ /**
690
+ * Queries the terminal to determine whether the alternate screen buffer is currently active.
691
+ * Returns undefined for non-interactive terminals or if the terminal does not respond within the timeout.
692
+ *
693
+ * @returns {Promise<boolean|undefined>} true if in alt screen, false if in main screen, undefined if unknown.
694
+ */
695
+ static async isAltScreen() {
696
+ if(!this.isInteractive)
697
+ return undefined
698
+
699
+ const prevRawMode = this.isCharMode
700
+
701
+ // 1. Force Raw Mode so the terminal sends the report immediately
702
+ this.setCharMode()
703
+ process.stdin.setEncoding("utf8")
704
+
705
+ // 2. Start the listener FIRST (do not await yet)
706
+ const dataPromise = this.data((text => {
707
+ return this.isCharMode && text.endsWith("$y")
708
+ }).bind(this), 25)
709
+
710
+ this.write("\x1b[?1049$p")
711
+
712
+ const response = await Promised.race([
713
+ Time.after(25),
714
+ dataPromise
715
+ ])
716
+
717
+ prevRawMode ? this.setCharMode() : this.setLineMode()
718
+
719
+ if(response === "\x1b[?1049;1$y")
720
+ return true
721
+
722
+ if(response === "\x1b[?1049;2$y")
723
+ return false
724
+
725
+ return undefined
726
+ }
727
+
728
+ /**
729
+ * Save the current screen contents.
730
+ *
731
+ * @returns {typeof Term} The Term class for chaining.
732
+ */
733
+ static saveScreen() {
734
+ this.write("\x1b[?47h")
735
+
736
+ return this
737
+ }
738
+
739
+ /**
740
+ * Restore previously saved screen contents.
741
+ *
742
+ * @returns {typeof Term} The Term class for chaining.
743
+ */
744
+ static restoreScreen() {
745
+ this.write("\x1b[?47l")
746
+
747
+ return this
748
+ }
749
+
750
+ /**
751
+ * Clear the entire screen and move cursor to the home position.
752
+ *
753
+ * @returns {typeof Term} The Term class for chaining.
754
+ */
755
+ static cls() {
756
+ this.write("\x1b[2J\x1b[H")
757
+
758
+ return this
759
+ }
651
760
  }
@@ -266,11 +266,13 @@ export default class Term {
266
266
  * If in Char Mode, it resolves on Enter, Ctrl+D, or the ANSI 'R' terminator.
267
267
  *
268
268
  * @param {(text: string) => boolean} [terminator] - Optional callback to check if input is complete.
269
- * @returns {Promise<string>} Resolves with the input data.
269
+ * @param {number} [timeoutMs=0] - Optional timeout in milliseconds. Resolves with empty string if exceeded.
270
+ * @returns {Promise<string>} Resolves with the input data, or empty string on timeout.
270
271
  */
271
- static data(terminator?: (text: string) => boolean): Promise<string>;
272
+ static data(terminator?: (text: string) => boolean, timeoutMs?: number): Promise<string>;
272
273
  /**
273
274
  * Gets the current cursor position in the terminal.
275
+ * Returns [0, 0] for non-interactive terminals or if the terminal does not respond within the timeout.
274
276
  *
275
277
  * @returns {Promise<[number, number]>} Resolves with [x, y] cursor position.
276
278
  */
@@ -282,6 +284,11 @@ export default class Term {
282
284
  * @returns {Promise<void>} Resolves when write completes.
283
285
  */
284
286
  static directWrite(output: string): Promise<void>;
287
+ /**
288
+ * Spinner animation frames using Braille patterns (widely supported).
289
+ *
290
+ * @type {readonly string[]}
291
+ */
285
292
  static spinFrames: readonly string[];
286
293
  /**
287
294
  * Pause stdin, preventing it from emitting data events.
@@ -301,5 +308,42 @@ export default class Term {
301
308
  * @returns {typeof Term} The Term class for chaining.
302
309
  */
303
310
  static utf8(): typeof Term;
311
+ /**
312
+ * Switch to the alternate screen buffer.
313
+ *
314
+ * @returns {typeof Term} The Term class for chaining.
315
+ */
316
+ static altScreen(): typeof Term;
317
+ /**
318
+ * Switch back to the main screen buffer.
319
+ *
320
+ * @returns {typeof Term} The Term class for chaining.
321
+ */
322
+ static mainScreen(): typeof Term;
323
+ /**
324
+ * Queries the terminal to determine whether the alternate screen buffer is currently active.
325
+ * Returns undefined for non-interactive terminals or if the terminal does not respond within the timeout.
326
+ *
327
+ * @returns {Promise<boolean|undefined>} true if in alt screen, false if in main screen, undefined if unknown.
328
+ */
329
+ static isAltScreen(): Promise<boolean | undefined>;
330
+ /**
331
+ * Save the current screen contents.
332
+ *
333
+ * @returns {typeof Term} The Term class for chaining.
334
+ */
335
+ static saveScreen(): typeof Term;
336
+ /**
337
+ * Restore previously saved screen contents.
338
+ *
339
+ * @returns {typeof Term} The Term class for chaining.
340
+ */
341
+ static restoreScreen(): typeof Term;
342
+ /**
343
+ * Clear the entire screen and move cursor to the home position.
344
+ *
345
+ * @returns {typeof Term} The Term class for chaining.
346
+ */
347
+ static cls(): typeof Term;
304
348
  }
305
349
  //# sourceMappingURL=Term.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Term.d.ts","sourceRoot":"","sources":["../../../src/node/lib/Term.js"],"names":[],"mappings":"AAeA;;;;;;;;;;;;GAYG;AACH;IACE,0CAAyB;IAEzB,+CAIC;IAED;;;;OAIG;IACH,sBAFU,MAAM,GAAG,SAAS,CAI3B;IAED;;;;OAIG;IACH,mBAFU,MAAM,GAAG,SAAS,CAI3B;IAED;;;;OAIG;IACH,kBAFU;QAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;QAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAA;KAAC,CAIhE;IAED;;;;OAIG;IACH,4BAFU,OAAO,CAMhB;IAED;;;;OAIG;IACH,uBAFU,OAAO,CAMhB;IAED;;;;OAIG;IACH,oBAFc,OAAO,EAAA,QAIpB;IAED;;;;OAIG;IACH,qBAFc,OAAO,EAAA,QAIpB;IAED;;;;OAIG;IACH,qBAFc,OAAO,EAAA,QAIpB;IAED;;;;OAIG;IACH,sBAFc,OAAO,EAAA,QAIpB;IAED;;;;OAIG;IACH,sBAFc,OAAO,EAAA,QAIpB;IAED;;;;OAIG;IACH,sBAFc,OAAO,EAAA,QAIpB;IAED;;OAEG;IACH,wBAEC;IAED;;;;;;;;OAQG;IACH,0BANW,MAAM,QAAQ,YAEtB;QAAgC,UAAU,GAAlC,KAAK,CAAC,MAAM,CAAC;QACK,UAAU,GAA5B,OAAO;QACW,aAAa,GAA/B,OAAO;KACjB,QA2DA;IAED;;;;;;;;;;;;;;OAcG;IACH,oBALW,MAAM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