diff --git a/base.d.ts b/base.d.ts index 8055d0d..e81d37d 100644 --- a/base.d.ts +++ b/base.d.ts @@ -1,6 +1,3 @@ -/* 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 @@ -237,31 +234,29 @@ export function 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()`. +@param data - Image data. Usually read in with `fs.readFile()`. */ -export function image(buffer: Buffer, options?: ImageOptions): string; +export function image(data: Uint8Array, options?: ImageOptions): string; -export namespace iTerm { +export const 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). + [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()`. - */ - function setCwd(cwd?: string): string; + @param cwd - Current directory. Default: `process.cwd()`. + */ + setCwd(cwd?: string): string, /** - An annotation looks like this when shown: + An annotation looks like this when shown: - ![screenshot of iTerm annotation](https://user-images.githubusercontent.com/924465/64382136-b60ac700-cfe9-11e9-8a35-9682e8dc4b72.png) + ![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. + 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. - */ - function annotation(message: string, options?: AnnotationOptions): string; -} + @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 +}; diff --git a/base.js b/base.js index f7fb047..d4e64d8 100644 --- a/base.js +++ b/base.js @@ -1,15 +1,14 @@ import process from 'node:process'; +import {isBrowser} from 'environment'; 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; @@ -115,7 +114,7 @@ export const link = (text, url) => [ BEL, ].join(''); -export const image = (buffer, options = {}) => { +export const image = (data, options = {}) => { let returnValue = `${OSC}1337;File=inline=1`; if (options.width) { @@ -130,7 +129,7 @@ export const image = (buffer, options = {}) => { returnValue += ';preserveAspectRatio=0'; } - return returnValue + ':' + buffer.toString('base64') + BEL; + return returnValue + ':' + Buffer.from(data).toString('base64') + BEL; }; export const iTerm = { @@ -139,13 +138,13 @@ export const iTerm = { 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')) { + 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'); } - message = message.replace(/\|/g, ''); + message = message.replaceAll('|', ''); returnValue += options.isHidden ? 'AddHiddenAnnotation=' : 'AddAnnotation='; diff --git a/index.d.ts b/index.d.ts index 4857fb0..4ba092b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,2 +1,2 @@ -export * from './base.d.js'; -export * as default from './base.d.js'; +export * from './base.js'; +export * as default from './base.js'; diff --git a/index.js b/index.js index 6126130..4ba092b 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,2 @@ -import * as ansiEscapes from './base.js'; - -export default ansiEscapes; -export * from './base.js'; \ No newline at end of file +export * from './base.js'; +export * as default from './base.js'; diff --git a/package.json b/package.json index 1f927ae..94b7483 100644 --- a/package.json +++ b/package.json @@ -11,11 +11,13 @@ "url": "https://sindresorhus.com" }, "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" }, "scripts": { "test": "ava && tsd", @@ -23,7 +25,9 @@ }, "files": [ "index.js", - "index.d.ts" + "index.d.ts", + "base.js", + "base.d.ts" ], "keywords": [ "ansi", @@ -48,12 +52,19 @@ "codes", "cursor", "iterm", - "iterm2" + "iterm2", + "clear", + "screen", + "erase", + "scrollback" ], + "dependencies": { + "environment": "^1.0.0" + }, "devDependencies": { - "@types/node": "20.12.7", - "ava": "^4.3.3", + "@types/node": "20.12.8", + "ava": "^6.1.2", "tsd": "0.31.0", - "xo": "^0.52.3" + "xo": "^0.58.0" } } diff --git a/readme.md b/readme.md index 1ea4c61..ffaa021 100644 --- a/readme.md +++ b/readme.md @@ -21,7 +21,7 @@ process.stdout.write(ansiEscapes.cursorUp(2) + ansiEscapes.cursorLeft); Or use named exports... ```js -import {cursorUp, cursorLeft} from `ansi-escapes`; +import {cursorUp, cursorLeft} from 'ansi-escapes'; // etc, as above... ``` @@ -164,8 +164,6 @@ Create a clickable link. Display an image. -*Currently only supported on iTerm2 >=3* - See [term-img](https://github.com/sindresorhus/term-img) for a higher-level module. #### input diff --git a/test.js b/test.js index e352229..9453142 100644 --- a/test.js +++ b/test.js @@ -1,5 +1,5 @@ import test from 'ava'; -import ansiEscapes, { cursorTo } from './index.js'; +import ansiEscapes, {cursorTo} from './index.js'; test('default export', t => { t.true(Object.keys(ansiEscapes).length > 0);