https://github.com/chalk/slice-ansi
Slice a string with ANSI escape codes
https://github.com/chalk/slice-ansi
Last synced: 5 months ago
JSON representation
Slice a string with ANSI escape codes
- Host: GitHub
- URL: https://github.com/chalk/slice-ansi
- Owner: chalk
- License: mit
- Created: 2015-09-26T04:21:17.000Z (almost 11 years ago)
- Default Branch: main
- Last Pushed: 2025-09-08T15:11:58.000Z (10 months ago)
- Last Synced: 2026-01-28T04:16:55.057Z (5 months ago)
- Language: JavaScript
- Size: 44.9 KB
- Stars: 50
- Watchers: 4
- Forks: 21
- Open Issues: 3
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license
- Security: .github/security.md
Awesome Lists containing this project
README
# slice-ansi [](https://github.com/xojs/xo)
> Slice a string with [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles)
## Install
```sh
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, startSlice, endSlice?)
#### string
Type: `string`
String with ANSI escape codes. Like one styled by [`chalk`](https://github.com/chalk/chalk).
#### startSlice
Type: `number`
Zero-based visible-column index at which to start the slice. Grapheme clusters (for example, emoji sequences and combining marks) are kept intact.
#### endSlice
Type: `number`
Zero-based visible-column 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-)