package/package.json000644 000765 000024 0000001552 13130652215013016 0ustar00000000 000000 { "name": "slice-ansi", "version": "1.0.0", "description": "Slice a string with ANSI escape codes", "license": "MIT", "repository": "chalk/slice-ansi", "author": { "name": "David Caccavella", "email": "threedeecee@gmail.com" }, "engines": { "node": ">=4" }, "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": { "is-fullwidth-code-point": "^2.0.0" }, "devDependencies": { "ava": "*", "chalk": "^2.0.1", "random-item": "^1.0.0", "strip-ansi": "^4.0.0", "xo": "*" } } package/index.js000755 000765 000024 0000003203 13130651515012175 0ustar00000000 000000 'use strict'; const isFullwidthCodePoint = require('is-fullwidth-code-point'); const ESCAPES = [ '\u001B', '\u009B' ]; const END_CODE = 39; const ASTRAL_REGEX = /[\uD800-\uDBFF][\uDC00-\uDFFF]/; const ESCAPE_CODES = new Map([ [0, 0], [1, 22], [2, 22], [3, 23], [4, 24], [7, 27], [8, 28], [9, 29], [30, 39], [31, 39], [32, 39], [33, 39], [34, 39], [35, 39], [36, 39], [37, 39], [90, 39], [40, 49], [41, 49], [42, 49], [43, 49], [44, 49], [45, 49], [46, 49], [47, 49] ]); const wrapAnsi = code => `${ESCAPES[0]}[${code}m`; module.exports = (str, begin, end) => { const arr = Array.from(str.normalize()); end = typeof end === 'number' ? end : arr.length; let insideEscape = false; let escapeCode; let visible = 0; let output = ''; for (const item of arr.entries()) { const i = item[0]; const x = item[1]; let leftEscape = false; if (ESCAPES.indexOf(x) !== -1) { insideEscape = true; const code = /\d[^m]*/.exec(str.slice(i, i + 4)); escapeCode = code === END_CODE ? null : code; } else if (insideEscape && x === 'm') { insideEscape = false; leftEscape = true; } if (!insideEscape && !leftEscape) { ++visible; } if (!ASTRAL_REGEX.test(x) && isFullwidthCodePoint(x.codePointAt())) { ++visible; } if (visible > begin && visible <= end) { output += x; } else if (visible === begin && !insideEscape && escapeCode !== undefined && escapeCode !== END_CODE) { output += wrapAnsi(escapeCode); } else if (visible >= end) { if (escapeCode !== undefined) { output += wrapAnsi(ESCAPE_CODES.get(parseInt(escapeCode, 10)) || END_CODE); } break; } } return output; }; package/license000644 000765 000024 0000002066 13130651607012102 0ustar00000000 000000 MIT License Copyright (c) DC 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. package/readme.md000755 000765 000024 0000002621 13122207123012303 0ustar00000000 000000 # slice-ansi [![Build Status](https://travis-ci.org/chalk/slice-ansi.svg?branch=master)](https://travis-ci.org/chalk/slice-ansi) [![XO: Linted](https://img.shields.io/badge/xo-linted-blue.svg)](https://github.com/sindresorhus/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 const chalk = require('chalk'); const sliceAnsi = require('slice-ansi'); const input = 'The quick brown ' + chalk.red('fox jumped over ') + 'the lazy ' + chalk.green('dog and then ran away with the unicorn.'); console.log(sliceAnsi(input, 20, 30)); ``` ## API ### sliceAnsi(input, beginSlice, [endSlice]) #### input 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-) ## License MIT