Socket
Socket
Sign inDemoInstall

ansi-escapes

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ansi-escapes - npm Package Compare versions

Comparing version 6.2.1 to 7.0.0

base.d.ts

274

index.d.ts

@@ -1,272 +0,2 @@

/* eslint-disable @typescript-eslint/member-ordering */
import type {Buffer} from 'node:buffer';
// From https://github.com/sindresorhus/type-fest
type Primitive =
| null // eslint-disable-line @typescript-eslint/ban-types
| undefined
| string
| number
| boolean
| symbol
| bigint;
type LiteralUnion<
LiteralType,
BaseType extends Primitive,
> = LiteralType | (BaseType & Record<never, never>);
// -
export type ImageOptions = {
/**
The width is given as a number followed by a unit, or the word `'auto'`.
- `N`: N character cells.
- `Npx`: N pixels.
- `N%`: N percent of the session's width or height.
- `auto`: The image's inherent size will be used to determine an appropriate dimension.
*/
readonly width?: LiteralUnion<'auto', number | string>;
/**
The height is given as a number followed by a unit, or the word `'auto'`.
- `N`: N character cells.
- `Npx`: N pixels.
- `N%`: N percent of the session's width or height.
- `auto`: The image's inherent size will be used to determine an appropriate dimension.
*/
readonly height?: LiteralUnion<'auto', number | string>;
/**
@default true
*/
readonly preserveAspectRatio?: boolean;
};
export type AnnotationOptions = {
/**
Nonzero number of columns to annotate.
Default: The remainder of the line.
*/
readonly length?: number;
/**
Starting X coordinate.
Must be used with `y` and `length`.
Default: The cursor position
*/
readonly x?: number;
/**
Starting Y coordinate.
Must be used with `x` and `length`.
Default: Cursor position.
*/
readonly y?: number;
/**
Create a "hidden" annotation.
Annotations created this way can be shown using the "Show Annotations" iTerm command.
*/
readonly isHidden?: boolean;
};
declare const ansiEscapes: {
/**
Set the absolute position of the cursor. `x0` `y0` is the top left of the screen.
*/
cursorTo(x: number, y?: number): string;
/**
Set the position of the cursor relative to its current position.
*/
cursorMove(x: number, y?: number): string;
/**
Move cursor up a specific amount of rows.
@param count - Count of rows to move up. Default is `1`.
*/
cursorUp(count?: number): string;
/**
Move cursor down a specific amount of rows.
@param count - Count of rows to move down. Default is `1`.
*/
cursorDown(count?: number): string;
/**
Move cursor forward a specific amount of rows.
@param count - Count of rows to move forward. Default is `1`.
*/
cursorForward(count?: number): string;
/**
Move cursor backward a specific amount of rows.
@param count - Count of rows to move backward. Default is `1`.
*/
cursorBackward(count?: number): string;
/**
Move cursor to the left side.
*/
cursorLeft: string;
/**
Save cursor position.
*/
cursorSavePosition: string;
/**
Restore saved cursor position.
*/
cursorRestorePosition: string;
/**
Get cursor position.
*/
cursorGetPosition: string;
/**
Move cursor to the next line.
*/
cursorNextLine: string;
/**
Move cursor to the previous line.
*/
cursorPrevLine: string;
/**
Hide cursor.
*/
cursorHide: string;
/**
Show cursor.
*/
cursorShow: string;
/**
Erase from the current cursor position up the specified amount of rows.
@param count - Count of rows to erase.
*/
eraseLines(count: number): string;
/**
Erase from the current cursor position to the end of the current line.
*/
eraseEndLine: string;
/**
Erase from the current cursor position to the start of the current line.
*/
eraseStartLine: string;
/**
Erase the entire current line.
*/
eraseLine: string;
/**
Erase the screen from the current line down to the bottom of the screen.
*/
eraseDown: string;
/**
Erase the screen from the current line up to the top of the screen.
*/
eraseUp: string;
/**
Erase the screen and move the cursor the top left position.
*/
eraseScreen: string;
/**
Scroll display up one line.
*/
scrollUp: string;
/**
Scroll display down one line.
*/
scrollDown: string;
/**
Clear the terminal screen. (Viewport)
*/
clearScreen: string;
/**
Clear the whole terminal, including scrollback buffer. (Not just the visible part of it)
*/
clearTerminal: string;
/**
Enter the [alternative screen](https://terminalguide.namepad.de/mode/p47/).
*/
enterAlternativeScreen: string;
/**
Exit the [alternative screen](https://terminalguide.namepad.de/mode/p47/), assuming `enterAlternativeScreen` was called before.
*/
exitAlternativeScreen: string;
/**
Output a beeping sound.
*/
beep: string;
/**
Create a clickable link.
[Supported terminals.](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda) Use [`supports-hyperlinks`](https://github.com/jamestalmage/supports-hyperlinks) to detect link support.
*/
link(text: string, url: string): string;
/**
Display an image.
_Currently only supported on iTerm2 >=3_
See [term-img](https://github.com/sindresorhus/term-img) for a higher-level module.
@param buffer - Buffer of an image. Usually read in with `fs.readFile()`.
*/
image(buffer: Buffer, options?: ImageOptions): string;
iTerm: {
/**
[Inform iTerm2](https://www.iterm2.com/documentation-escape-codes.html) of the current directory to help semantic history and enable [Cmd-clicking relative paths](https://coderwall.com/p/b7e82q/quickly-open-files-in-iterm-with-cmd-click).
@param cwd - Current directory. Default: `process.cwd()`.
*/
setCwd(cwd?: string): string;
/**
An annotation looks like this when shown:
![screenshot of iTerm annotation](https://user-images.githubusercontent.com/924465/64382136-b60ac700-cfe9-11e9-8a35-9682e8dc4b72.png)
See the [iTerm Proprietary Escape Codes documentation](https://iterm2.com/documentation-escape-codes.html) for more information.
@param message - The message to display within the annotation. The `|` character is disallowed and will be stripped.
@returns An escape code which will create an annotation when printed in iTerm2.
*/
annotation(message: string, options?: AnnotationOptions): string;
};
};
export default ansiEscapes;
export * from './base.js';
export * as default from './base.js';

@@ -1,168 +0,2 @@

import process from 'node:process';
const ESC = '\u001B[';
const OSC = '\u001B]';
const BEL = '\u0007';
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 cwdFunction = isBrowser ? () => {
throw new Error('`process.cwd()` only works in Node.js, not the browser.');
} : process.cwd;
const ansiEscapes = {};
ansiEscapes.cursorTo = (x, y) => {
if (typeof x !== 'number') {
throw new TypeError('The `x` argument is required');
}
if (typeof y !== 'number') {
return ESC + (x + 1) + 'G';
}
return ESC + (y + 1) + SEP + (x + 1) + 'H';
};
ansiEscapes.cursorMove = (x, y) => {
if (typeof x !== 'number') {
throw new TypeError('The `x` argument is required');
}
let returnValue = '';
if (x < 0) {
returnValue += ESC + (-x) + 'D';
} else if (x > 0) {
returnValue += ESC + x + 'C';
}
if (y < 0) {
returnValue += ESC + (-y) + 'A';
} else if (y > 0) {
returnValue += ESC + y + 'B';
}
return returnValue;
};
ansiEscapes.cursorUp = (count = 1) => ESC + count + 'A';
ansiEscapes.cursorDown = (count = 1) => ESC + count + 'B';
ansiEscapes.cursorForward = (count = 1) => ESC + count + 'C';
ansiEscapes.cursorBackward = (count = 1) => ESC + count + 'D';
ansiEscapes.cursorLeft = ESC + 'G';
ansiEscapes.cursorSavePosition = isTerminalApp ? '\u001B7' : ESC + 's';
ansiEscapes.cursorRestorePosition = isTerminalApp ? '\u001B8' : ESC + 'u';
ansiEscapes.cursorGetPosition = ESC + '6n';
ansiEscapes.cursorNextLine = ESC + 'E';
ansiEscapes.cursorPrevLine = ESC + 'F';
ansiEscapes.cursorHide = ESC + '?25l';
ansiEscapes.cursorShow = ESC + '?25h';
ansiEscapes.eraseLines = count => {
let clear = '';
for (let i = 0; i < count; i++) {
clear += ansiEscapes.eraseLine + (i < count - 1 ? ansiEscapes.cursorUp() : '');
}
if (count) {
clear += ansiEscapes.cursorLeft;
}
return clear;
};
ansiEscapes.eraseEndLine = ESC + 'K';
ansiEscapes.eraseStartLine = ESC + '1K';
ansiEscapes.eraseLine = ESC + '2K';
ansiEscapes.eraseDown = ESC + 'J';
ansiEscapes.eraseUp = ESC + '1J';
ansiEscapes.eraseScreen = ESC + '2J';
ansiEscapes.scrollUp = ESC + 'S';
ansiEscapes.scrollDown = ESC + 'T';
ansiEscapes.clearScreen = '\u001Bc';
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
// 3. Moves cursor to the top-left position
// More info: https://www.real-world-systems.com/docs/ANSIcode.html
: `${ansiEscapes.eraseScreen}${ESC}3J${ESC}H`;
ansiEscapes.enterAlternativeScreen = ESC + '?1049h';
ansiEscapes.exitAlternativeScreen = ESC + '?1049l';
ansiEscapes.beep = BEL;
ansiEscapes.link = (text, url) => [
OSC,
'8',
SEP,
SEP,
url,
BEL,
text,
OSC,
'8',
SEP,
SEP,
BEL,
].join('');
ansiEscapes.image = (buffer, options = {}) => {
let returnValue = `${OSC}1337;File=inline=1`;
if (options.width) {
returnValue += `;width=${options.width}`;
}
if (options.height) {
returnValue += `;height=${options.height}`;
}
if (options.preserveAspectRatio === false) {
returnValue += ';preserveAspectRatio=0';
}
return returnValue + ':' + buffer.toString('base64') + BEL;
};
ansiEscapes.iTerm = {
setCwd: (cwd = cwdFunction()) => `${OSC}50;CurrentDir=${cwd}${BEL}`,
annotation(message, options = {}) {
let returnValue = `${OSC}1337;`;
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.replace(/\|/g, '');
returnValue += options.isHidden ? 'AddHiddenAnnotation=' : 'AddAnnotation=';
if (options.length > 0) {
returnValue += (
hasX
? [message, options.length, options.x, options.y]
: [options.length, message]
).join('|');
} else {
returnValue += message;
}
return returnValue + BEL;
},
};
export default ansiEscapes;
export * from './base.js';
export * as default from './base.js';
{
"name": "ansi-escapes",
"version": "6.2.1",
"version": "7.0.0",
"description": "ANSI escape codes for manipulating the terminal",

@@ -14,7 +14,9 @@ "license": "MIT",

"type": "module",
"exports": "./index.js",
"types": "./index.d.ts",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"sideEffects": false,
"engines": {
"node": ">=14.16"
"node": ">=18"
},

@@ -27,3 +29,5 @@ "scripts": {

"index.js",
"index.d.ts"
"index.d.ts",
"base.js",
"base.d.ts"
],

@@ -53,10 +57,17 @@ "keywords": [

"iterm",
"iterm2"
"iterm2",
"clear",
"screen",
"erase",
"scrollback"
],
"dependencies": {
"environment": "^1.0.0"
},
"devDependencies": {
"@types/node": "^18.7.18",
"ava": "^4.3.3",
"tsd": "^0.24.1",
"xo": "^0.52.3"
"@types/node": "20.12.8",
"ava": "^6.1.2",
"tsd": "0.31.0",
"xo": "^0.58.0"
}
}

@@ -21,2 +21,10 @@ # ansi-escapes

Or use named exports...
```js
import {cursorUp, cursorLeft} from 'ansi-escapes';
// etc, as above...
```
**You can also use it in the browser with Xterm.js:**

@@ -160,4 +168,2 @@

*Currently only supported on iTerm2 >=3*
See [term-img](https://github.com/sindresorhus/term-img) for a higher-level module.

@@ -164,0 +170,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc