From 09656ff7798f2e330d2e424725c53ac7f497c23c Mon Sep 17 00:00:00 2001 From: Reverier-Xu Date: Mon, 27 Feb 2023 16:49:50 +0800 Subject: [PATCH 1/7] Support vite in-browser uses --- index.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index e26cde0..9f38598 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,24 @@ -import process from 'node:process'; - const ESC = '\u001B['; const OSC = '\u001B]'; const BEL = '\u0007'; const SEP = ';'; -const isTerminalApp = process.env.TERM_PROGRAM === 'Apple_Terminal'; +const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; + +let isTerminalApp_; +let platform_ = 'browser'; +let cwdFunc_ = () => {throw new Error('NodeJS.Process.cwd() doesn\'t work in Browser.');} +if (isBrowser) { + isTerminalApp_ = false; +} else { + const process = require('node:process'); + isTerminalApp_ = process.env.TERM_PROGRAM === 'Apple_Terminal'; + platform_ = process.platform; + cwdFunc_ = process.cwd; +} +const isTerminalApp = isTerminalApp_; +const platform = platform_; +const cwdFunc = cwdFunc_; const ansiEscapes = {}; @@ -82,7 +95,7 @@ ansiEscapes.scrollDown = ESC + 'T'; ansiEscapes.clearScreen = '\u001Bc'; -ansiEscapes.clearTerminal = process.platform === 'win32' +ansiEscapes.clearTerminal = platform === 'win32' ? `${ansiEscapes.eraseScreen}${ESC}0f` // 1. Erases the screen (Only done in case `2` is not supported) // 2. Erases the whole screen including scrollback buffer @@ -126,7 +139,7 @@ ansiEscapes.image = (buffer, options = {}) => { }; ansiEscapes.iTerm = { - setCwd: (cwd = process.cwd()) => `${OSC}50;CurrentDir=${cwd}${BEL}`, + setCwd: (cwd = cwdFunc()) => `${OSC}50;CurrentDir=${cwd}${BEL}`, annotation(message, options = {}) { let returnValue = `${OSC}1337;`; From 1f12bc182d0b8ed50938229fdbbd6cd2f4efd929 Mon Sep 17 00:00:00 2001 From: Reverier-Xu Date: Mon, 27 Feb 2023 17:12:45 +0800 Subject: [PATCH 2/7] Fix npm test --- index.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 9f38598..1a1141e 100644 --- a/index.js +++ b/index.js @@ -1,21 +1,26 @@ +import process from 'node:process'; + const ESC = '\u001B['; const OSC = '\u001B]'; const BEL = '\u0007'; const SEP = ';'; -const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"; +const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined'; let isTerminalApp_; let platform_ = 'browser'; -let cwdFunc_ = () => {throw new Error('NodeJS.Process.cwd() doesn\'t work in Browser.');} +let cwdFunc_ = () => { + throw new Error('NodeJS.Process.cwd() doesn\'t work in Browser.'); +}; + if (isBrowser) { - isTerminalApp_ = false; + isTerminalApp_ = false; } else { - const process = require('node:process'); isTerminalApp_ = process.env.TERM_PROGRAM === 'Apple_Terminal'; platform_ = process.platform; cwdFunc_ = process.cwd; } + const isTerminalApp = isTerminalApp_; const platform = platform_; const cwdFunc = cwdFunc_; From 9c0cf29b771ff7d510f111dc114535f637510204 Mon Sep 17 00:00:00 2001 From: Reverier-Xu Date: Mon, 27 Feb 2023 17:15:09 +0800 Subject: [PATCH 3/7] Fix npm test --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index 1a1141e..f2c7199 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ const OSC = '\u001B]'; const BEL = '\u0007'; const SEP = ';'; +/* global window */ const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined'; let isTerminalApp_; From dd07b1fb9964db2b06fe2ec00a13408e6f36eee5 Mon Sep 17 00:00:00 2001 From: Reverier-Xu Date: Thu, 16 Mar 2023 14:40:46 +0800 Subject: [PATCH 4/7] refactor & add xterm usage --- index.js | 24 ++++++------------------ readme.md | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index f2c7199..5dca66d 100644 --- a/index.js +++ b/index.js @@ -8,23 +8,11 @@ const SEP = ';'; /* global window */ const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined'; -let isTerminalApp_; -let platform_ = 'browser'; -let cwdFunc_ = () => { - throw new Error('NodeJS.Process.cwd() doesn\'t work in Browser.'); -}; - -if (isBrowser) { - isTerminalApp_ = false; -} else { - isTerminalApp_ = process.env.TERM_PROGRAM === 'Apple_Terminal'; - platform_ = process.platform; - cwdFunc_ = process.cwd; -} - -const isTerminalApp = isTerminalApp_; -const platform = platform_; -const cwdFunc = cwdFunc_; +const isTerminalApp = isBrowser && process.env.TERM_PROGRAM === 'Apple_Terminal'; +const isWindows = isBrowser && process.platform === 'win32'; +const cwdFunc = isBrowser ? () => { + throw new Error('`process.cwd()` only works in Node.js, not the browser.'); +} : process.cwd; const ansiEscapes = {}; @@ -101,7 +89,7 @@ ansiEscapes.scrollDown = ESC + 'T'; ansiEscapes.clearScreen = '\u001Bc'; -ansiEscapes.clearTerminal = platform === 'win32' +ansiEscapes.clearTerminal = isWindows ? `${ansiEscapes.eraseScreen}${ESC}0f` // 1. Erases the screen (Only done in case `2` is not supported) // 2. Erases the whole screen including scrollback buffer diff --git a/readme.md b/readme.md index 9467a24..bbf7b89 100644 --- a/readme.md +++ b/readme.md @@ -10,6 +10,8 @@ npm install ansi-escapes ## Usage +### Use within terminal + ```js import ansiEscapes from 'ansi-escapes'; @@ -18,6 +20,21 @@ process.stdout.write(ansiEscapes.cursorUp(2) + ansiEscapes.cursorLeft); //=> '\u001B[2A\u001B[1000D' ``` +### Use with xterm.js + +```js +import ansiEscapes from 'ansi-escapes'; +import { Terminal } from 'xterm'; +import 'xterm/css/xterm.css'; + +... +const term = new Terminal({...}); +... +// Moves the cursor two rows up and to the left +term.write(ansiEscapes.cursorUp(2) + ansiEscapes.cursorLeft); +//=> '\u001B[2A\u001B[1000D' +``` + ## API ### cursorTo(x, y?) From 21e49f72ddc2b7c1409c89cf19700c04122f20a2 Mon Sep 17 00:00:00 2001 From: Reverier-Xu Date: Thu, 16 Mar 2023 14:44:22 +0800 Subject: [PATCH 5/7] func -> function --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 5dca66d..887fcbb 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,7 @@ const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'u const isTerminalApp = isBrowser && process.env.TERM_PROGRAM === 'Apple_Terminal'; const isWindows = isBrowser && process.platform === 'win32'; -const cwdFunc = isBrowser ? () => { +const cwdFunction = isBrowser ? () => { throw new Error('`process.cwd()` only works in Node.js, not the browser.'); } : process.cwd; @@ -133,7 +133,7 @@ ansiEscapes.image = (buffer, options = {}) => { }; ansiEscapes.iTerm = { - setCwd: (cwd = cwdFunc()) => `${OSC}50;CurrentDir=${cwd}${BEL}`, + setCwd: (cwd = cwdFunction()) => `${OSC}50;CurrentDir=${cwd}${BEL}`, annotation(message, options = {}) { let returnValue = `${OSC}1337;`; From b359b3056abe68487833b659ea0f0cadb41b3a9d Mon Sep 17 00:00:00 2001 From: Reverier-Xu Date: Thu, 16 Mar 2023 14:57:12 +0800 Subject: [PATCH 6/7] fix condition construct mistakes --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 887fcbb..9307dfc 100644 --- a/index.js +++ b/index.js @@ -8,8 +8,8 @@ const SEP = ';'; /* global window */ const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined'; -const isTerminalApp = isBrowser && process.env.TERM_PROGRAM === 'Apple_Terminal'; -const isWindows = isBrowser && process.platform === 'win32'; +const isTerminalApp = !isBrowser && process.env.TERM_PROGRAM === 'Apple_Terminal'; +const isWindows = !isBrowser && process.platform === 'win32'; const cwdFunction = isBrowser ? () => { throw new Error('`process.cwd()` only works in Node.js, not the browser.'); } : process.cwd; From fa3d1b3a865bd4bd0f8821a9d23a09197fcd83e2 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 16 Mar 2023 15:02:17 +0700 Subject: [PATCH 7/7] Update readme.md --- readme.md | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/readme.md b/readme.md index bbf7b89..30f73e7 100644 --- a/readme.md +++ b/readme.md @@ -10,8 +10,6 @@ npm install ansi-escapes ## Usage -### Use within terminal - ```js import ansiEscapes from 'ansi-escapes'; @@ -20,18 +18,17 @@ process.stdout.write(ansiEscapes.cursorUp(2) + ansiEscapes.cursorLeft); //=> '\u001B[2A\u001B[1000D' ``` -### Use with xterm.js +**You can also use it in the browser with Xterm.js:** ```js import ansiEscapes from 'ansi-escapes'; -import { Terminal } from 'xterm'; +import {Terminal} from 'xterm'; import 'xterm/css/xterm.css'; -... -const term = new Terminal({...}); -... +const terminal = new Terminal({…}); + // Moves the cursor two rows up and to the left -term.write(ansiEscapes.cursorUp(2) + ansiEscapes.cursorLeft); +terminal.write(ansiEscapes.cursorUp(2) + ansiEscapes.cursorLeft); //=> '\u001B[2A\u001B[1000D' ```