You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

slice-ansi

Package Overview
Dependencies
Maintainers
2
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

slice-ansi

Slice a string with ANSI escape codes


Version published
Maintainers
2
Created

Package description

What is slice-ansi?

The slice-ansi npm package is designed for slicing strings with ANSI escape codes without breaking the visual appearance. This is particularly useful for terminal applications where text styling (like colors, bold, underline) is applied through ANSI escape codes. It allows developers to extract or truncate portions of the styled text without losing or corrupting the intended styles.

What are slice-ansi's main functionalities?

Slicing styled strings

This feature allows you to slice a string that contains ANSI escape codes, ensuring that the visual styling is preserved in the sliced string. The code sample demonstrates how to slice a string that starts with red-colored text and then resets to default color. The sliced result contains only the portion of the string within the specified range, along with the necessary ANSI codes to maintain the original styling.

const sliceAnsi = require('slice-ansi');
const input = '\u001B[31mThis is red\u001B[39m and this is not.';
const sliced = sliceAnsi(input, 0, 10);
console.log(sliced);

Other packages similar to slice-ansi

Readme

Source

slice-ansi XO: Linted

Slice a string with ANSI escape codes

Install

npm install slice-ansi

Usage

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.

startSlice

Type: number

Zero-based index at which to start the slice.

endSlice

Type: number

Zero-based index at which to end the slice.

  • wrap-ansi - Wordwrap a string with ANSI escape codes
  • cli-truncate - Truncate a string to a specific width in the terminal
  • chalk - Terminal string styling done right

Maintainers

Keywords

FAQs

Package last updated on 29 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc