Skip to content

Commit

Permalink
minimize diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
broofa committed Apr 26, 2024
1 parent e72254e commit 1fa7ae0
Showing 1 changed file with 38 additions and 50 deletions.
88 changes: 38 additions & 50 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,15 @@ const BEL = '\u0007';
const SEP = ';';

/* global window */
const isBrowser
= typeof window !== 'undefined' && window.document !== undefined;
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';

const isTerminalApp
= !isBrowser && process.env.TERM_PROGRAM === 'Apple_Terminal';
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;

const cwdFunction = isBrowser
? () => {
throw new Error(
'`process.cwd()` only works in Node.js, not the browser.',
);
}
: process.cwd;

export function cursorTo(x, y) {
export const cursorTo = (x, y) => {
if (typeof x !== 'number') {
throw new TypeError('The `x` argument is required');
}
Expand All @@ -31,45 +24,34 @@ export function cursorTo(x, y) {
}

return ESC + (y + 1) + SEP + (x + 1) + 'H';
}
};

export function cursorMove(x, y) {
export const cursorMove = (x, y) => {
if (typeof x !== 'number') {
throw new TypeError('The `x` argument is required');
}

let returnValue = '';

if (x < 0) {
returnValue += ESC + -x + 'D';
returnValue += ESC + (-x) + 'D';
} else if (x > 0) {
returnValue += ESC + x + 'C';
}

if (y < 0) {
returnValue += ESC + -y + 'A';
returnValue += ESC + (-y) + 'A';
} else if (y > 0) {
returnValue += ESC + y + 'B';
}

return returnValue;
}

export function cursorUp(count = 1) {
return ESC + count + 'A';
}

export function cursorDown(count = 1) {
return ESC + count + 'B';
}
};

export function cursorForward(count = 1) {
return ESC + count + 'C';
}

export function cursorBackward(count = 1) {
return ESC + count + 'D';
}
export const cursorUp = (count = 1) => ESC + count + 'A';
export const cursorDown = (count = 1) => ESC + count + 'B';
export const cursorForward = (count = 1) => ESC + count + 'C';
export const cursorBackward = (count = 1) => ESC + count + 'D';

export const cursorLeft = ESC + 'G';
export const cursorSavePosition = isTerminalApp ? '\u001B7' : ESC + 's';
Expand Down Expand Up @@ -118,11 +100,22 @@ export const exitAlternativeScreen = ESC + '?1049l';

export const beep = BEL;

export function link(text, url) {
return [OSC, '8', SEP, SEP, url, BEL, text, OSC, '8', SEP, SEP, BEL].join('');
}

export function image(buffer, options = {}) {
export const link = (text, url) => [
OSC,
'8',
SEP,
SEP,
url,
BEL,
text,
OSC,
'8',
SEP,
SEP,
BEL,
].join('');

export const image = (buffer, options = {}) => {
let returnValue = `${OSC}1337;File=inline=1`;

if (options.width) {
Expand All @@ -138,26 +131,21 @@ export function image(buffer, options = {}) {
}

return returnValue + ':' + buffer.toString('base64') + BEL;
}
};

export const iTerm = {
setCwd: (cwd = cwdFunction()) => `${OSC}50;CurrentDir=${cwd}${BEL}`,

annotation(message, options = {}) {
let returnValue = `${OSC}1337;`;

const hasX = options.x !== undefined;
const hasY = options.y !== undefined;
if (
(hasX || hasY)
&& !(hasX && hasY && options.length !== undefined)
) {
throw new Error(
'`x`, `y` and `length` must be defined when `x` or `y` is defined',
);
const hasX = typeof options.x !== 'undefined';
const hasY = typeof options.y !== 'undefined';
if ((hasX || hasY) && !(hasX && hasY && typeof options.length !== 'undefined')) {
throw new Error('`x`, `y` and `length` must be defined when `x` or `y` is defined');
}

message = message.replaceAll('|', '');
message = message.replace(/\|/g, '');

returnValue += options.isHidden ? 'AddHiddenAnnotation=' : 'AddAnnotation=';

Expand Down Expand Up @@ -207,4 +195,4 @@ export default {
link,
scrollDown,
scrollUp,
}
}

0 comments on commit 1fa7ae0

Please sign in to comment.