Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support in-browser uses #31

Merged
merged 7 commits into from Mar 16, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions index.js
Expand Up @@ -5,7 +5,26 @@ const OSC = '\u001B]';
const BEL = '\u0007';
const SEP = ';';

const isTerminalApp = process.env.TERM_PROGRAM === 'Apple_Terminal';
/* global window */
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';

let isTerminalApp_;
let platform_ = 'browser';
Reverier-Xu marked this conversation as resolved.
Show resolved Hide resolved
let cwdFunc_ = () => {
Reverier-Xu marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('NodeJS.Process.cwd() doesn\'t work in Browser.');
Reverier-Xu marked this conversation as resolved.
Show resolved Hide resolved
};

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_;
Reverier-Xu marked this conversation as resolved.
Show resolved Hide resolved

const ansiEscapes = {};

Expand Down Expand Up @@ -82,7 +101,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
Expand Down Expand Up @@ -126,7 +145,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;`;
Expand Down