basant307/AI_Governance_Project
048
1import process from 'node:process';2import os from 'node:os';3import {isBrowser} from 'environment';4 5const ESC = '\u001B[';6const OSC = '\u001B]';7const BEL = '\u0007';8const SEP = ';';9 10const isTerminalApp = !isBrowser && process.env.TERM_PROGRAM === 'Apple_Terminal';11const isWindows = !isBrowser && process.platform === 'win32';12const isTmux = !isBrowser && (process.env.TERM?.startsWith('screen') || process.env.TERM?.startsWith('tmux') || process.env.TMUX !== undefined);13 14const cwdFunction = isBrowser ? () => {15 throw new Error('`process.cwd()` only works in Node.js, not the browser.');16} : process.cwd;17 18const wrapOsc = sequence => {19 if (isTmux) {20 // Tmux requires OSC sequences to be wrapped with DCS tmux; <sequence> ST21 // and all ESCs in <sequence> to be replaced with ESC ESC.22 // It only accepts ESC backslash for ST.23 return '\u001BPtmux;' + sequence.replaceAll('\u001B', '\u001B\u001B') + '\u001B\\';24 }25 26 return sequence;27};28 29export const cursorTo = (x, y) => {30 if (typeof x !== 'number') {31 throw new TypeError('The `x` argument is required');32 }33 34 if (typeof y !== 'number') {35 return ESC + (x + 1) + 'G';36 }37 38 return ESC + (y + 1) + SEP + (x + 1) + 'H';39};40 41export const cursorMove = (x, y) => {42 if (typeof x !== 'number') {43 throw new TypeError('The `x` argument is required');44 }45 46 let returnValue = '';47 48 if (x < 0) {49 returnValue += ESC + (-x) + 'D';50 } else if (x > 0) {51 returnValue += ESC + x + 'C';52 }53 54 if (y < 0) {55 returnValue += ESC + (-y) + 'A';56 } else if (y > 0) {57 returnValue += ESC + y + 'B';58 }59 60 return returnValue;61};62 63export const cursorUp = (count = 1) => ESC + count + 'A';64export const cursorDown = (count = 1) => ESC + count + 'B';65export const cursorForward = (count = 1) => ESC + count + 'C';66export const cursorBackward = (count = 1) => ESC + count + 'D';67 68export const cursorLeft = ESC + 'G';69export const cursorSavePosition = isTerminalApp ? '\u001B7' : ESC + 's';70export const cursorRestorePosition = isTerminalApp ? '\u001B8' : ESC + 'u';71export const cursorGetPosition = ESC + '6n';72export const cursorNextLine = ESC + 'E';73export const cursorPrevLine = ESC + 'F';74export const cursorHide = ESC + '?25l';75export const cursorShow = ESC + '?25h';76 77export const eraseLines = count => {78 let clear = '';79 80 for (let i = 0; i < count; i++) {81 clear += eraseLine + (i < count - 1 ? cursorUp() : '');82 }83 84 if (count) {85 clear += cursorLeft;86 }87 88 return clear;89};90 91export const eraseEndLine = ESC + 'K';92export const eraseStartLine = ESC + '1K';93export const eraseLine = ESC + '2K';94export const eraseDown = ESC + 'J';95export const eraseUp = ESC + '1J';96export const eraseScreen = ESC + '2J';97export const scrollUp = ESC + 'S';98export const scrollDown = ESC + 'T';99 100export const clearScreen = '\u001Bc';101 102export const clearViewport = `${eraseScreen}${ESC}H`;103 104const isOldWindows = () => {105 if (isBrowser || !isWindows) {106 return false;107 }108 109 const parts = os.release().split('.');110 const major = Number(parts[0]);111 const build = Number(parts[2] ?? 0);112 113 if (major < 10) {114 return true;115 }116 117 if (major === 10 && build < 10_586) {118 return true;119 }120 121 return false;122};123 124export const clearTerminal = isOldWindows()125 ? `${eraseScreen}${ESC}0f`126 // 1. Erases the screen (Only done in case `2` is not supported)127 // 2. Erases the whole screen including scrollback buffer128 // 3. Moves cursor to the top-left position129 // More info: https://www.real-world-systems.com/docs/ANSIcode.html130 : `${eraseScreen}${ESC}3J${ESC}H`;131 132export const enterAlternativeScreen = ESC + '?1049h';133export const exitAlternativeScreen = ESC + '?1049l';134 135export const beginSynchronizedOutput = ESC + '?2026h';136export const endSynchronizedOutput = ESC + '?2026l';137export const synchronizedOutput = text => beginSynchronizedOutput + text + endSynchronizedOutput;138 139export const beep = BEL;140 141export const link = (text, url) => {142 const openLink = wrapOsc(`${OSC}8${SEP}${SEP}${url}${BEL}`);143 const closeLink = wrapOsc(`${OSC}8${SEP}${SEP}${BEL}`);144 return openLink + text + closeLink;145};146 147export const image = (data, options = {}) => {148 let returnValue = `${OSC}1337;File=inline=1`;149 150 if (options.width) {151 returnValue += `;width=${options.width}`;152 }153 154 if (options.height) {155 returnValue += `;height=${options.height}`;156 }157 158 if (options.preserveAspectRatio === false) {159 returnValue += ';preserveAspectRatio=0';160 }161 162 const imageBuffer = Buffer.from(data);163 164 // `size` is optional in the spec, but xterm.js requires it.165 return wrapOsc(returnValue + `;size=${imageBuffer.byteLength}` + ':' + imageBuffer.toString('base64') + BEL);166};167 168export const iTerm = {169 setCwd: (cwd = cwdFunction()) => wrapOsc(`${OSC}50;CurrentDir=${cwd}${BEL}`),170 171 annotation(message, options = {}) {172 let returnValue = `${OSC}1337;`;173 174 const hasX = options.x !== undefined;175 const hasY = options.y !== undefined;176 if ((hasX || hasY) && !(hasX && hasY && options.length !== undefined)) {177 throw new Error('`x`, `y` and `length` must be defined when `x` or `y` is defined');178 }179 180 message = message.replaceAll('|', '');181 182 returnValue += options.isHidden ? 'AddHiddenAnnotation=' : 'AddAnnotation=';183 184 if (options.length > 0) {185 returnValue += (186 hasX187 ? [message, options.length, options.x, options.y]188 : [options.length, message]189 ).join('|');190 } else {191 returnValue += message;192 }193 194 return wrapOsc(returnValue + BEL);195 },196};197 198export const ConEmu = {199 setCwd: (cwd = cwdFunction()) => wrapOsc(`${OSC}9;9;${cwd}${BEL}`),200};201 202export const setCwd = (cwd = cwdFunction()) => iTerm.setCwd(cwd) + ConEmu.setCwd(cwd);203 