@travetto/terminal 3.0.0-rc.6 → 3.0.0-rc.8

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/terminal",
3
- "version": "3.0.0-rc.6",
3
+ "version": "3.0.0-rc.8",
4
4
  "description": "General terminal support",
5
5
  "keywords": [
6
6
  "terminal",
package/src/operation.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { IterableUtil } from './iterable';
2
2
  import { TerminalWriter } from './writer';
3
- import { Indexed, TerminalProgressRender, TerminalWaitingConfig, TermLinePosition, TermState } from './types';
3
+ import { Indexed, TermCoord, TerminalProgressRender, TerminalWaitingConfig, TermLinePosition, TermState } from './types';
4
4
  import { ColorOutputUtil, TermStyleInput } from './color-output';
5
5
 
6
6
  const STD_WAIT_STATES = '⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'.split('');
@@ -10,8 +10,8 @@ export class TerminalOperation {
10
10
  /**
11
11
  * Allows for writing at top, bottom, or current position while new text is added
12
12
  */
13
- static async streamToPosition(term: TermState, source: AsyncIterable<string>, pos: TermLinePosition = 'inline'): Promise<void> {
14
- const curPos = { ...await term.getCursorPosition() };
13
+ static async streamToPosition(term: TermState, source: AsyncIterable<string>, pos: TermLinePosition = 'inline', curPos?: TermCoord): Promise<void> {
14
+ curPos ??= { ...await term.getCursorPosition() };
15
15
  const writePos = pos === 'inline' ?
16
16
  { ...curPos, x: 0 } :
17
17
  { x: 0, y: pos === 'top' ? 0 : -1 };
@@ -59,7 +59,7 @@ export class TerminalOperation {
59
59
  /**
60
60
  * Waiting indicator, streamed to a specific position, can be canceled
61
61
  */
62
- static streamWaiting(term: TermState, message: string, config: TerminalWaitingConfig = {}): () => Promise<void> {
62
+ static streamWaiting(term: TermState, message: string, config: TerminalWaitingConfig = {}, curPos?: TermCoord): () => Promise<void> {
63
63
  const { stop, stream } = IterableUtil.cycle(STD_WAIT_STATES);
64
64
  const indicator = IterableUtil.map(
65
65
  stream,
@@ -67,7 +67,7 @@ export class TerminalOperation {
67
67
  (ch, i) => config.end ? `${message} ${ch}` : (i === 0 ? `${ch} ${message}` : ch)
68
68
  );
69
69
 
70
- const final = this.streamToPosition(term, indicator, config.position ?? 'inline');
70
+ const final = this.streamToPosition(term, indicator, config.position ?? 'inline', curPos);
71
71
  return async () => { stop(); return final; };
72
72
  }
73
73
 
@@ -96,23 +96,33 @@ export class TerminalOperation {
96
96
  /**
97
97
  * Stream lines with a waiting indicator
98
98
  */
99
- static async streamLinesWithWaiting(term: TermState, lines: AsyncIterable<string>, cfg: TerminalWaitingConfig = {}): Promise<void> {
99
+ static async streamLinesWithWaiting(term: TermState, lines: AsyncIterable<string | undefined>, cfg: TerminalWaitingConfig = {}): Promise<void> {
100
100
  let writer: (() => Promise<unknown>) | undefined;
101
101
  let line: string | undefined;
102
102
 
103
103
  const commitLine = async (): Promise<void> => {
104
104
  await writer?.();
105
105
  if (line) {
106
- await TerminalWriter.for(term).restoreOnCommit().changePosition({ y: -1 }).changePosition({ x: 0 }).writeLine(line).commit();
106
+ const msg = cfg.commitedPrefix ? `${cfg.commitedPrefix} ${line}` : line;
107
+ if (cfg.position === 'inline') {
108
+ await TerminalWriter.for(term).setPosition({ x: 0 }).changePosition({ y: -1 }).writeLine(msg).commit();
109
+ } else {
110
+ await TerminalWriter.for(term).writeLine(msg).commit();
111
+ }
112
+ line = undefined;
107
113
  }
108
114
  };
109
115
 
110
- for await (const msg of lines) {
116
+ const pos = await term.getCursorPosition();
117
+
118
+ for await (let msg of lines) {
111
119
  await commitLine();
112
- line = `${String.fromCharCode(171)} ${msg}`;
113
- writer = this.streamWaiting(term, line, cfg);
120
+ if (msg !== undefined) {
121
+ msg = msg.replace(/\n$/, '');
122
+ writer = this.streamWaiting(term, msg, cfg, pos);
123
+ line = msg;
124
+ }
114
125
  }
115
-
116
126
  await commitLine();
117
127
  }
118
128
  }
package/src/query.ts CHANGED
@@ -50,7 +50,16 @@ export class TerminalQuerier {
50
50
  }
51
51
 
52
52
  async #readInput(query: string): Promise<Buffer> {
53
+ const isRaw = this.#input.isRaw;
54
+ const isPaused = this.#input.isPaused();
55
+ const data = this.#input.listeners('data');
53
56
  try {
57
+ this.#input.removeAllListeners('data');
58
+
59
+ if (isPaused) {
60
+ this.#input.resume();
61
+ }
62
+
54
63
  this.#input.setRawMode(true);
55
64
  // Send data, but do not wait on it
56
65
  this.#output.write(query);
@@ -58,7 +67,14 @@ export class TerminalQuerier {
58
67
  const val: Buffer | string = this.#input.read();
59
68
  return typeof val === 'string' ? Buffer.from(val, 'utf8') : val;
60
69
  } finally {
61
- this.#input.setRawMode(false);
70
+ if (isPaused) {
71
+ this.#input.pause();
72
+ }
73
+ this.#input.setRawMode(isRaw);
74
+ for (const fn of data) {
75
+ // @ts-ignore
76
+ this.#input.on('data', fn);
77
+ }
62
78
  }
63
79
  }
64
80
 
package/src/terminal.ts CHANGED
@@ -128,13 +128,17 @@ export class Terminal implements TermState {
128
128
  * @param config
129
129
  * @returns
130
130
  */
131
- async streamLinesWithWaiting(lines: AsyncIterable<string>, config: TerminalWaitingConfig = {}): Promise<void> {
131
+ async streamLinesWithWaiting(lines: AsyncIterable<string | undefined>, config: TerminalWaitingConfig): Promise<void> {
132
132
  if (!this.interactive) {
133
133
  for await (const line of lines) {
134
- await this.writeLines(line);
134
+ if (line !== undefined) {
135
+ const out = config.commitedPrefix ? `${config.commitedPrefix} ${line}` : line;
136
+ await this.writeLines(out);
137
+ }
135
138
  }
139
+ } else {
140
+ return TerminalOperation.streamLinesWithWaiting(this, lines, { position: 'bottom', ...config });
136
141
  }
137
- return TerminalOperation.streamLinesWithWaiting(this, lines, config);
138
142
  }
139
143
 
140
144
  /**
package/src/types.ts CHANGED
@@ -13,7 +13,7 @@ export type TerminalTableEvent = { idx: number, text: string, done?: boolean };
13
13
  export type TerminalTableConfig = { header?: string[], forceNonInteractiveOrder?: boolean };
14
14
  export type TerminalProgressEvent = { idx: number, total?: number, text?: string };
15
15
  export type TerminalProgressRender = (ev: TerminalProgressEvent) => string;
16
- export type TerminalWaitingConfig = { position?: TermLinePosition, end?: boolean } & DelayedConfig;
16
+ export type TerminalWaitingConfig = { position?: TermLinePosition, end?: boolean, commitedPrefix?: string } & DelayedConfig;
17
17
 
18
18
  export type TermColorLevel = 0 | 1 | 2 | 3;
19
19
  export type TermColorScheme = 'dark' | 'light';