UNPKG

@teaui/core

Version:

A high-level terminal UI library for Node

54 lines 1.74 kB
import { colorToHex } from './Color.js'; /** * Sets iTerm2 proprietary ANSI codes */ export class iTerm2 { static _restoreBg; /** * Returns a promise in case you really want to do flow control here, but it's not * necessary; you can fire and forget this as part of `Screen.start()` * * @example * Screen.start(async (program) => { * await iTerm2.setBackground(program, [23, 23, 23]) * return new Box({ … }) * }) */ static setBackground(program, bg) { process.on('exit', () => { iTerm2.restoreBg(program); }); return new Promise(resolve => { const hex = colorToHex(bg).slice(1); program.onceRawData((input) => { const response = typeof input === 'string' ? input : new TextDecoder().decode(input); iTerm2._restoreBg = parseBackgroundResponse(response); program.write(setBackgroundCommand(hex)); setTimeout(resolve, 5); }); setTimeout(resolve, 10); program.write(getBackgroundColorCommand()); }); } static restoreBg(program) { if (iTerm2._restoreBg) { program.write(setBackgroundCommand(iTerm2._restoreBg)); } } } function getBackgroundColorCommand() { return '\u001b]4;-2;?\x07'; } function parseBackgroundResponse(response) { const match = response.match(/\x1b\]4;-2;rgb:(\w{2})\w*\/(\w{2})\w*\/(\w{2})/); if (match) { return match[1] + match[2] + match[3]; } } /** * @param rgb should not include the '#' symbol */ function setBackgroundCommand(rgb) { return `\u001b]Ph${rgb.replace('#', '')}\u001b\\`; } //# sourceMappingURL=iTerm2.js.map