Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kjirou/slice-ansi-string
Slice a string included ANSI escape codes
https://github.com/kjirou/slice-ansi-string
ansi ansi-colors node-module nodejs
Last synced: 12 days ago
JSON representation
Slice a string included ANSI escape codes
- Host: GitHub
- URL: https://github.com/kjirou/slice-ansi-string
- Owner: kjirou
- License: mit
- Created: 2017-12-23T04:30:19.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-08-31T07:26:21.000Z (about 3 years ago)
- Last Synced: 2024-09-27T20:46:54.600Z (about 1 month ago)
- Topics: ansi, ansi-colors, node-module, nodejs
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/slice-ansi-string
- Size: 20.5 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# slice-ansi-string
[![npm version](https://badge.fury.io/js/slice-ansi-string.svg)](https://badge.fury.io/js/slice-ansi-string)
[![Build Status](https://travis-ci.org/kjirou/slice-ansi-string.svg?branch=master)](https://travis-ci.org/kjirou/slice-ansi-string)Slice a string included [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code)
In some use cases [slice-ansi](https://github.com/chalk/slice-ansi) did not work properly.
This is what I reworked with reference to [slice-ansi](https://github.com/chalk/slice-ansi).## Installation
```bash
npm install slice-ansi-string
```## Usage
### Basic usage```js
const sliceAnsiString = require('slice-ansi-string');const str = 'A\u001b[31mBC\u001b[39mD'; // Meaning like "ABCD"
console.log(sliceAnsiString(str, 0, 1)); // -> "A"
console.log(sliceAnsiString(str, 0, 2)); // -> "A\u001b[31mB\u001b[39m", meaning like "AB"
console.log(sliceAnsiString(str, 2, 3)); // -> "\u001b[31mC\u001b[39m", meaning like "C"
```### Nested ANSI escape codes
```js
const sliceAnsiString = require('slice-ansi-string');const str = 'A\u001b[31mB\u001b[4mCD\u001b[24m\u001b[39mE'; // Meaning like "ABCDE"
console.log(sliceAnsiString(str, 1, 3)); // -> "\u001b[31mB\u001b[4mC\u001b[24m\u001b[39m", meaning like "BC"
```## API
### sliceAnsiString(str, beginSlice, [endSlice])
#### Arguments- `str: string`
- `beginSlice: number`
- A zero-based index of where to begin the slice.
- Only 0 or more.
- `endSlice: (string|null|undefined)`
- A zero-based index of where to end the slice.
- Only 0 or more.
- The default is until the end.#### Supplements
- Multibyte characters and surrogate pairs are counted as one character.
- It may not be possible to correspond to the character string output by [chalk](https://github.com/chalk/chalk).
- For example, `chalk.dim.bold('A')` outputs `"\u001b[2m\u001b[1mA\u001b[2m\u001b[22m"`, but this library expects `"\u001b[2m\u001b[1mA\u001b[22m\u001b[22m"`.
- However, `chalk.bold.underline.red('A')` returns expected outputs, so it may be a specific problem of `dim.bold`.