@travetto/terminal
Advanced tools
Comparing version 3.0.0-rc.9 to 3.0.0-rc.10
{ | ||
"name": "@travetto/terminal", | ||
"version": "3.0.0-rc.9", | ||
"version": "3.0.0-rc.10", | ||
"description": "General terminal support", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
import { IterableUtil } from './iterable'; | ||
import { TerminalWriter } from './writer'; | ||
import { Indexed, TermCoord, TerminalProgressRender, TerminalWaitingConfig, TermLinePosition, TermState } from './types'; | ||
import { Indexed, TerminalProgressRender, TerminalStreamingConfig, TerminalWaitingConfig, TermState } from './types'; | ||
import { ColorOutputUtil, TermStyleInput } from './color-output'; | ||
@@ -13,4 +13,6 @@ | ||
*/ | ||
static async streamToPosition(term: TermState, source: AsyncIterable<string>, pos: TermLinePosition = 'inline', curPos?: TermCoord): Promise<void> { | ||
curPos ??= { ...await term.getCursorPosition() }; | ||
static async streamToPosition(term: TermState, source: AsyncIterable<string>, config: TerminalStreamingConfig = {}): Promise<void> { | ||
const curPos = config.at ?? { ...await term.getCursorPosition() }; | ||
const pos = config.position ?? 'inline'; | ||
const writePos = pos === 'inline' ? | ||
@@ -37,3 +39,5 @@ { ...curPos, x: 0 } : | ||
} | ||
await TerminalWriter.for(term).setPosition(writePos).clearLine().commit(true); | ||
if (config.clearOnFinish ?? true) { | ||
await TerminalWriter.for(term).setPosition(writePos).clearLine().commit(true); | ||
} | ||
} finally { | ||
@@ -64,3 +68,3 @@ const finalCursor = await term.getCursorPosition(); | ||
*/ | ||
static streamWaiting(term: TermState, message: string, config: TerminalWaitingConfig = {}, curPos?: TermCoord): () => Promise<void> { | ||
static streamWaiting(term: TermState, message: string, config: TerminalWaitingConfig = {}): () => Promise<void> { | ||
const { stop, stream } = IterableUtil.cycle(STD_WAIT_STATES); | ||
@@ -70,6 +74,6 @@ const indicator = IterableUtil.map( | ||
IterableUtil.DELAY(config), | ||
(ch, i) => config.end ? `${message} ${ch}` : (i === 0 ? `${ch} ${message}` : ch) | ||
(ch, i) => config.end ? `${message} ${ch}` : `${ch} ${message}` | ||
); | ||
const final = this.streamToPosition(term, indicator, config.position ?? 'inline', curPos); | ||
const final = this.streamToPosition(term, indicator, config); | ||
return async () => { stop(); return final; }; | ||
@@ -107,2 +111,5 @@ } | ||
let pos = await term.getCursorPosition(); | ||
const commitLine = async (): Promise<void> => { | ||
@@ -113,3 +120,3 @@ await writer?.(); | ||
if (cfg.position === 'inline') { | ||
await TerminalWriter.for(term).setPosition({ x: 0 }).changePosition({ y: -1 }).writeLine(msg).commit(); | ||
await TerminalWriter.for(term).setPosition(pos).write(msg).commit(true); | ||
} else { | ||
@@ -122,4 +129,2 @@ await TerminalWriter.for(term).writeLine(msg).commit(); | ||
const pos = await term.getCursorPosition(); | ||
for await (let msg of lines) { | ||
@@ -129,3 +134,4 @@ await commitLine(); | ||
msg = msg.replace(/\n$/, ''); | ||
writer = this.streamWaiting(term, msg, cfg, pos); | ||
pos = await term.getCursorPosition(); | ||
writer = this.streamWaiting(term, msg, { ...cfg, at: pos, clearOnFinish: false }); | ||
line = msg; | ||
@@ -132,0 +138,0 @@ } |
@@ -96,2 +96,3 @@ import tty from 'tty'; | ||
})(); | ||
process.on('exit', () => this.reset()); | ||
} | ||
@@ -178,3 +179,3 @@ return this.#init; | ||
} | ||
return TerminalOperation.streamToPosition(this, IterableUtil.map(source, resolve), config?.position); | ||
return TerminalOperation.streamToPosition(this, IterableUtil.map(source, resolve), config); | ||
} | ||
@@ -208,5 +209,2 @@ | ||
export const GlobalTerminal = new Terminal({ output: process.stdout }); | ||
// Trigger | ||
GlobalTerminal.init(); | ||
export const GlobalTerminal = new Terminal({ output: process.stdout }); |
@@ -16,3 +16,4 @@ import tty from 'tty'; | ||
export type TerminalProgressRender = (ev: TerminalProgressEvent) => string; | ||
export type TerminalWaitingConfig = { position?: TermLinePosition, end?: boolean, committedPrefix?: string } & DelayedConfig; | ||
export type TerminalStreamingConfig = { position?: TermLinePosition, clearOnFinish?: boolean, at?: TermCoord }; | ||
export type TerminalWaitingConfig = { end?: boolean, committedPrefix?: string } & TerminalStreamingConfig & DelayedConfig; | ||
@@ -19,0 +20,0 @@ export type TermColorLevel = 0 | 1 | 2 | 3; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41612
1092