@retrovm/terminal 0.1.1 → 0.1.3

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/dist/node.js CHANGED
@@ -311,6 +311,7 @@ import { format } from "node:util";
311
311
  class TerminalCore {
312
312
  writer;
313
313
  plain;
314
+ _altScreen = false;
314
315
  constructor(writer, options = {}) {
315
316
  this.writer = writer;
316
317
  const envNoColor = typeof process !== "undefined" && !!process?.env?.NO_COLOR;
@@ -341,9 +342,12 @@ class TerminalCore {
341
342
  const color = typeof c === "string" ? new Color(c) : c;
342
343
  return this.style(color.toAnsiBackgroundRGB(), fmt, args);
343
344
  }
344
- reset(fmt = "", ...args) {
345
+ resetText(fmt = "", ...args) {
345
346
  return this.style("\x1B[0m", fmt, args);
346
347
  }
348
+ reset(fmt = "", ...args) {
349
+ return this.cursor(true).alt(false).style("\x1B[0m", fmt, args);
350
+ }
347
351
  resetInk(fmt = "", ...args) {
348
352
  return this.style("\x1B[39m", fmt, args);
349
353
  }
@@ -399,7 +403,10 @@ class TerminalCore {
399
403
  return this;
400
404
  }
401
405
  alt(b = true) {
402
- this.emit(`\x1B[?1049${b ? "h" : "l"}`);
406
+ if (b !== this._altScreen) {
407
+ this.emit(`\x1B[?1049${b ? "h" : "l"}`);
408
+ this._altScreen = b;
409
+ }
403
410
  return this;
404
411
  }
405
412
  autoWrap(b = true) {
@@ -40,6 +40,7 @@ declare class TerminalCore {
40
40
  private readonly writer;
41
41
  /** ANSI is suppressed when true; styling/cursor methods become no-ops. */
42
42
  plain: boolean;
43
+ private _altScreen;
43
44
  [key: string]: unknown;
44
45
  constructor(writer: ITerminalWriter, options?: {
45
46
  plain?: boolean;
@@ -56,7 +57,24 @@ declare class TerminalCore {
56
57
  ink(c: string | Color, fmt?: string, ...args: unknown[]): this;
57
58
  /** Sets the background color and optionally writes formatted text. */
58
59
  paper(c: string | Color, fmt?: string, ...args: unknown[]): this;
59
- /** Resets all attributes (color, background, intensity, …). */
60
+ /**
61
+ * Resets all text attributes (color, background, bold, dim, italic,
62
+ * underline, blink, reverse, …) by emitting `\x1b[0m`.
63
+ *
64
+ * Unlike {@link reset}, this does **not** touch the cursor visibility
65
+ * or the alternate screen buffer.
66
+ */
67
+ resetText(fmt?: string, ...args: unknown[]): this;
68
+ /**
69
+ * Fully resets the terminal to a clean state.
70
+ *
71
+ * In order:
72
+ * 1. Shows the cursor (`cursor(true)`)
73
+ * 2. Exits the alternate screen buffer (`alt(false)`)
74
+ * 3. Resets all text attributes — color, background, intensity, etc. (`\x1b[0m`)
75
+ *
76
+ * Safe to call as a teardown step after any TUI or interactive session.
77
+ */
60
78
  reset(fmt?: string, ...args: unknown[]): this;
61
79
  /** Resets only the foreground color. */
62
80
  resetInk(fmt?: string, ...args: unknown[]): this;
package/dist/terminal.js CHANGED
@@ -311,6 +311,7 @@ import { format } from "node:util";
311
311
  class TerminalCore {
312
312
  writer;
313
313
  plain;
314
+ _altScreen = false;
314
315
  constructor(writer, options = {}) {
315
316
  this.writer = writer;
316
317
  const envNoColor = typeof process !== "undefined" && !!process?.env?.NO_COLOR;
@@ -341,9 +342,12 @@ class TerminalCore {
341
342
  const color = typeof c === "string" ? new Color(c) : c;
342
343
  return this.style(color.toAnsiBackgroundRGB(), fmt, args);
343
344
  }
344
- reset(fmt = "", ...args) {
345
+ resetText(fmt = "", ...args) {
345
346
  return this.style("\x1B[0m", fmt, args);
346
347
  }
348
+ reset(fmt = "", ...args) {
349
+ return this.cursor(true).alt(false).style("\x1B[0m", fmt, args);
350
+ }
347
351
  resetInk(fmt = "", ...args) {
348
352
  return this.style("\x1B[39m", fmt, args);
349
353
  }
@@ -399,7 +403,10 @@ class TerminalCore {
399
403
  return this;
400
404
  }
401
405
  alt(b = true) {
402
- this.emit(`\x1B[?1049${b ? "h" : "l"}`);
406
+ if (b !== this._altScreen) {
407
+ this.emit(`\x1B[?1049${b ? "h" : "l"}`);
408
+ this._altScreen = b;
409
+ }
403
410
  return this;
404
411
  }
405
412
  autoWrap(b = true) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@retrovm/terminal",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Fluent ANSI terminal library — 24-bit color, cursor control and screen management for Bun and Node.js",
5
5
  "author": "Juan Carlos González Amestoy",
6
6
  "license": "MIT",
package/src/terminal.ts CHANGED
@@ -62,6 +62,8 @@ class TerminalCore {
62
62
  /** ANSI is suppressed when true; styling/cursor methods become no-ops. */
63
63
  public plain: boolean
64
64
 
65
+ private _altScreen = false;
66
+
65
67
  // Allows the auto-attached color methods to typecheck on `this`.
66
68
  [key: string]: unknown
67
69
 
@@ -116,11 +118,31 @@ class TerminalCore {
116
118
  return this.style(color.toAnsiBackgroundRGB(), fmt, args)
117
119
  }
118
120
 
119
- /** Resets all attributes (color, background, intensity, …). */
120
- reset(fmt: string = '', ...args: unknown[]): this {
121
+ /**
122
+ * Resets all text attributes (color, background, bold, dim, italic,
123
+ * underline, blink, reverse, …) by emitting `\x1b[0m`.
124
+ *
125
+ * Unlike {@link reset}, this does **not** touch the cursor visibility
126
+ * or the alternate screen buffer.
127
+ */
128
+ resetText(fmt: string = '', ...args: unknown[]): this {
121
129
  return this.style('\x1b[0m', fmt, args)
122
130
  }
123
131
 
132
+ /**
133
+ * Fully resets the terminal to a clean state.
134
+ *
135
+ * In order:
136
+ * 1. Shows the cursor (`cursor(true)`)
137
+ * 2. Exits the alternate screen buffer (`alt(false)`)
138
+ * 3. Resets all text attributes — color, background, intensity, etc. (`\x1b[0m`)
139
+ *
140
+ * Safe to call as a teardown step after any TUI or interactive session.
141
+ */
142
+ reset(fmt: string = '', ...args: unknown[]): this {
143
+ return this.cursor(true).alt(false).style('\x1b[0m', fmt, args)
144
+ }
145
+
124
146
  /** Resets only the foreground color. */
125
147
  resetInk(fmt: string = '', ...args: unknown[]): this {
126
148
  return this.style('\x1b[39m', fmt, args)
@@ -192,7 +214,10 @@ class TerminalCore {
192
214
 
193
215
  /** Enables or disables the alternate screen buffer. */
194
216
  alt(b: boolean = true): this {
195
- this.emit(`\x1b[?1049${b ? 'h' : 'l'}`)
217
+ if (b !== this._altScreen) {
218
+ this.emit(`\x1b[?1049${b ? 'h' : 'l'}`)
219
+ this._altScreen = b
220
+ }
196
221
  return this
197
222
  }
198
223