pax_global_header00006660000000000000000000000064140362276260014522gustar00rootroot0000000000000052 comment=d9f402cdbbccc399e6380f39fe7e6d800e9da1f3 slice-ansi-5.0.0/000077500000000000000000000000001403622762600135535ustar00rootroot00000000000000slice-ansi-5.0.0/.editorconfig000066400000000000000000000002571403622762600162340ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 slice-ansi-5.0.0/.gitattributes000066400000000000000000000000231403622762600164410ustar00rootroot00000000000000* text=auto eol=lf slice-ansi-5.0.0/.github/000077500000000000000000000000001403622762600151135ustar00rootroot00000000000000slice-ansi-5.0.0/.github/funding.yml000066400000000000000000000000661403622762600172720ustar00rootroot00000000000000github: [sindresorhus, Qix-] tidelift: npm/slice-ansi slice-ansi-5.0.0/.github/security.md000066400000000000000000000002631403622762600173050ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. slice-ansi-5.0.0/.github/workflows/000077500000000000000000000000001403622762600171505ustar00rootroot00000000000000slice-ansi-5.0.0/.github/workflows/main.yml000066400000000000000000000006451403622762600206240ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 14 - 12 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test slice-ansi-5.0.0/.gitignore000066400000000000000000000000271403622762600155420ustar00rootroot00000000000000node_modules yarn.lock slice-ansi-5.0.0/.npmrc000066400000000000000000000000231403622762600146660ustar00rootroot00000000000000package-lock=false slice-ansi-5.0.0/index.js000077500000000000000000000052021403622762600152220ustar00rootroot00000000000000import isFullwidthCodePoint from 'is-fullwidth-code-point'; import ansiStyles from 'ansi-styles'; const astralRegex = /^[\uD800-\uDBFF][\uDC00-\uDFFF]$/; const ESCAPES = [ '\u001B', '\u009B' ]; const wrapAnsi = code => `${ESCAPES[0]}[${code}m`; const checkAnsi = (ansiCodes, isEscapes, endAnsiCode) => { let output = []; ansiCodes = [...ansiCodes]; for (let ansiCode of ansiCodes) { const ansiCodeOrigin = ansiCode; if (ansiCode.includes(';')) { ansiCode = ansiCode.split(';')[0][0] + '0'; } const item = ansiStyles.codes.get(Number.parseInt(ansiCode, 10)); if (item) { const indexEscape = ansiCodes.indexOf(item.toString()); if (indexEscape === -1) { output.push(wrapAnsi(isEscapes ? item : ansiCodeOrigin)); } else { ansiCodes.splice(indexEscape, 1); } } else if (isEscapes) { output.push(wrapAnsi(0)); break; } else { output.push(wrapAnsi(ansiCodeOrigin)); } } if (isEscapes) { output = output.filter((element, index) => output.indexOf(element) === index); if (endAnsiCode !== undefined) { const fistEscapeCode = wrapAnsi(ansiStyles.codes.get(Number.parseInt(endAnsiCode, 10))); // TODO: Remove the use of `.reduce` here. // eslint-disable-next-line unicorn/no-array-reduce output = output.reduce((current, next) => next === fistEscapeCode ? [next, ...current] : [...current, next], []); } } return output.join(''); }; export default function sliceAnsi(string, begin, end) { const characters = [...string]; const ansiCodes = []; let stringEnd = typeof end === 'number' ? end : characters.length; let isInsideEscape = false; let ansiCode; let visible = 0; let output = ''; for (const [index, character] of characters.entries()) { let leftEscape = false; if (ESCAPES.includes(character)) { const code = /\d[^m]*/.exec(string.slice(index, index + 18)); ansiCode = code && code.length > 0 ? code[0] : undefined; if (visible < stringEnd) { isInsideEscape = true; if (ansiCode !== undefined) { ansiCodes.push(ansiCode); } } } else if (isInsideEscape && character === 'm') { isInsideEscape = false; leftEscape = true; } if (!isInsideEscape && !leftEscape) { visible++; } if (!astralRegex.test(character) && isFullwidthCodePoint(character.codePointAt())) { visible++; if (typeof end !== 'number') { stringEnd++; } } if (visible > begin && visible <= stringEnd) { output += character; } else if (visible === begin && !isInsideEscape && ansiCode !== undefined) { output = checkAnsi(ansiCodes); } else if (visible >= stringEnd) { output += checkAnsi(ansiCodes, true, ansiCode); break; } } return output; } slice-ansi-5.0.0/license000066400000000000000000000022061403622762600151200ustar00rootroot00000000000000MIT License Copyright (c) DC Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. slice-ansi-5.0.0/package.json000066400000000000000000000015271403622762600160460ustar00rootroot00000000000000{ "name": "slice-ansi", "version": "5.0.0", "description": "Slice a string with ANSI escape codes", "license": "MIT", "repository": "chalk/slice-ansi", "funding": "https://github.com/chalk/slice-ansi?sponsor=1", "type": "module", "exports": "./index.js", "engines": { "node": ">=12" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js" ], "keywords": [ "slice", "string", "ansi", "styles", "color", "colour", "colors", "terminal", "console", "cli", "tty", "escape", "formatting", "rgb", "256", "shell", "xterm", "log", "logging", "command-line", "text" ], "dependencies": { "ansi-styles": "^6.0.0", "is-fullwidth-code-point": "^4.0.0" }, "devDependencies": { "ava": "^3.15.0", "chalk": "^4.1.0", "random-item": "^4.0.0", "strip-ansi": "^7.0.0", "xo": "^0.38.2" } } slice-ansi-5.0.0/readme.md000066400000000000000000000032551403622762600153370ustar00rootroot00000000000000# slice-ansi [![XO: Linted](https://img.shields.io/badge/xo-linted-blue.svg)](https://github.com/xojs/xo) > Slice a string with [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles) ## Install ``` $ npm install slice-ansi ``` ## Usage ```js import chalk from 'chalk'; import sliceAnsi from 'slice-ansi'; const string = 'The quick brown ' + chalk.red('fox jumped over ') + 'the lazy ' + chalk.green('dog and then ran away with the unicorn.'); console.log(sliceAnsi(string, 20, 30)); ``` ## API ### sliceAnsi(string, beginSlice, endSlice?) #### string Type: `string` String with ANSI escape codes. Like one styled by [`chalk`](https://github.com/chalk/chalk). #### beginSlice Type: `number` Zero-based index at which to begin the slice. #### endSlice Type: `number` Zero-based index at which to end the slice. ## Related - [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes - [cli-truncate](https://github.com/sindresorhus/cli-truncate) - Truncate a string to a specific width in the terminal - [chalk](https://github.com/chalk/chalk) - Terminal string styling done right ## Maintainers - [Sindre Sorhus](https://github.com/sindresorhus) - [Josh Junon](https://github.com/qix-) ---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
slice-ansi-5.0.0/test.js000077500000000000000000000106701403622762600150770ustar00rootroot00000000000000import test from 'ava'; import chalk from 'chalk'; import stripAnsi from 'strip-ansi'; import randomItem from 'random-item'; import sliceAnsi from './index.js'; chalk.level = 1; const fixture = chalk.red('the ') + chalk.green('quick ') + chalk.blue('brown ') + chalk.cyan('fox ') + chalk.yellow('jumped '); const stripped = stripAnsi(fixture); function generate(string) { const random1 = randomItem(['rock', 'paper', 'scissors']); const random2 = randomItem(['blue', 'green', 'yellow', 'red']); return `${string}:${chalk[random2](random1)} `; } test('main', t => { // The slice should behave exactly as a regular JS slice behaves for (let index = 0; index < 20; index++) { for (let index2 = 19; index2 > index; index2--) { const nativeSlice = stripped.slice(index, index2); const ansiSlice = sliceAnsi(fixture, index, index2); t.is(nativeSlice, stripAnsi(ansiSlice)); } } const a = JSON.stringify('\u001B[31mthe \u001B[39m\u001B[32mquick \u001B[39m'); const b = JSON.stringify('\u001B[34mbrown \u001B[39m\u001B[36mfox \u001B[39m'); const c = JSON.stringify('\u001B[31m \u001B[39m\u001B[32mquick \u001B[39m\u001B[34mbrown \u001B[39m\u001B[36mfox \u001B[39m'); t.is(JSON.stringify(sliceAnsi(fixture, 0, 10)), a); t.is(JSON.stringify(sliceAnsi(fixture, 10, 20)), b); t.is(JSON.stringify(sliceAnsi(fixture, 3, 20)), c); const string = generate(1) + generate(2) + generate(3) + generate(4) + generate(5) + generate(6) + generate(7) + generate(8) + generate(9) + generate(10) + generate(11) + generate(12) + generate(13) + generate(14) + generate(15) + generate(1) + generate(2) + generate(3) + generate(4) + generate(5) + generate(6) + generate(7) + generate(8) + generate(9) + generate(10) + generate(11) + generate(12) + generate(13) + generate(14) + generate(15); const native = stripAnsi(string).slice(0, 55); const ansi = stripAnsi(sliceAnsi(string, 0, 55)); t.is(native, ansi); }); test('supports fullwidth characters', t => { t.is(sliceAnsi('안녕하세', 0, 4), '안녕'); }); test('supports unicode surrogate pairs', t => { t.is(sliceAnsi('a\uD83C\uDE00BC', 0, 2), 'a\uD83C\uDE00'); }); test('doesn\'t add unnecessary escape codes', t => { t.is(sliceAnsi('\u001B[31municorn\u001B[39m', 0, 3), '\u001B[31muni\u001B[39m'); }); test('can slice a normal character before a colored character', t => { t.is(sliceAnsi('a\u001B[31mb\u001B[39m', 0, 1), 'a'); }); test('can slice a normal character after a colored character', t => { t.is(sliceAnsi('\u001B[31ma\u001B[39mb', 1, 2), 'b'); }); // See https://github.com/chalk/slice-ansi/issues/22 test('can slice a string styled with both background and foreground', t => { // Test string: `chalk.bgGreen.black('test');` t.is(sliceAnsi('\u001B[42m\u001B[30mtest\u001B[39m\u001B[49m', 0, 1), '\u001B[42m\u001B[30mt\u001B[39m\u001B[49m'); }); test('can slice a string styled with modifier', t => { // Test string: `chalk.underline('test');` t.is(sliceAnsi('\u001B[4mtest\u001B[24m', 0, 1), '\u001B[4mt\u001B[24m'); }); test('can slice a string with unknown ANSI color', t => { t.is(sliceAnsi('\u001B[20mTEST\u001B[49m', 0, 4), '\u001B[20mTEST\u001B[0m'); t.is(sliceAnsi('\u001B[1001mTEST\u001B[49m', 0, 3), '\u001B[1001mTES\u001B[0m'); t.is(sliceAnsi('\u001B[1001mTEST\u001B[49m', 0, 2), '\u001B[1001mTE\u001B[0m'); }); test('weird null issue', t => { const s = '\u001B[1mautotune.flipCoin("easy as") ? 🎂 : 🍰 \u001B[33m★\u001B[39m\u001B[22m'; const result = sliceAnsi(s, 38); t.false(result.includes('null')); }); test('support true color escape sequences', t => { t.is(sliceAnsi('\u001B[1m\u001B[48;2;255;255;255m\u001B[38;2;255;0;0municorn\u001B[39m\u001B[49m\u001B[22m', 0, 3), '\u001B[1m\u001B[48;2;255;255;255m\u001B[38;2;255;0;0muni\u001B[22m\u001B[49m\u001B[39m'); }); // See https://github.com/chalk/slice-ansi/issues/24 test('doesn\'t add extra escapes', t => { const output = `${chalk.black.bgYellow(' RUNS ')} ${chalk.green('test')}`; t.is(sliceAnsi(output, 0, 7), `${chalk.black.bgYellow(' RUNS ')} `); t.is(sliceAnsi(output, 0, 8), `${chalk.black.bgYellow(' RUNS ')} `); t.is(JSON.stringify(sliceAnsi('\u001B[31m' + output, 0, 4)), JSON.stringify(`\u001B[31m${chalk.black.bgYellow(' RUN')}`)); }); // See https://github.com/chalk/slice-ansi/issues/26 test('does not lose fullwidth characters', t => { t.is(sliceAnsi('古古test', 0), '古古test'); }); test.failing('slice links', t => { const link = '\u001B]8;;https://google.com\u0007Google\u001B]8;;\u0007'; t.is(sliceAnsi(link, 0, 6), link); });