pax_global_header00006660000000000000000000000064135724036210014515gustar00rootroot0000000000000052 comment=2f422c146701b9e76ac93b2f88753a915be37301 cli-truncate-2.1.0/000077500000000000000000000000001357240362100141075ustar00rootroot00000000000000cli-truncate-2.1.0/.editorconfig000066400000000000000000000002571357240362100165700ustar00rootroot00000000000000root = 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 cli-truncate-2.1.0/.gitattributes000066400000000000000000000000231357240362100167750ustar00rootroot00000000000000* text=auto eol=lf cli-truncate-2.1.0/.github/000077500000000000000000000000001357240362100154475ustar00rootroot00000000000000cli-truncate-2.1.0/.github/funding.yml000066400000000000000000000001661357240362100176270ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus custom: https://sindresorhus.com/donate tidelift: npm/cli-truncate cli-truncate-2.1.0/.github/security.md000066400000000000000000000002631357240362100176410ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. cli-truncate-2.1.0/.gitignore000066400000000000000000000000271357240362100160760ustar00rootroot00000000000000node_modules yarn.lock cli-truncate-2.1.0/.npmrc000066400000000000000000000000231357240362100152220ustar00rootroot00000000000000package-lock=false cli-truncate-2.1.0/.travis.yml000066400000000000000000000000651357240362100162210ustar00rootroot00000000000000language: node_js node_js: - '12' - '10' - '8' cli-truncate-2.1.0/index.d.ts000066400000000000000000000043761357240362100160220ustar00rootroot00000000000000declare namespace cliTruncate { interface Options { /** Position to truncate the string. @default 'end' */ readonly position?: 'start' | 'middle' | 'end'; /** Add a space between the text and the ellipsis. @default false @example ``` cliTruncate('unicorns', 5, {position: 'end', space: true}); //=> 'uni …' cliTruncate('unicorns', 5, {position: 'end', space: false}); //=> 'unic…' cliTruncate('unicorns', 6, {position: 'start', space: true}); //=> '… orns' cliTruncate('unicorns', 7, {position: 'middle', space: true}); //=> 'uni … s' ``` */ readonly space?: boolean; /** Truncate the string from a whitespace if it is within 3 characters from the actual breaking point. @default false @example ``` cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true}); //=> '…rainbow dragons' cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncationOnSpace: true}); //=> 'unicorns…dragons' cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true}); //=> 'unico…' ```` */ readonly preferTruncationOnSpace?: boolean; } } /** Truncate a string to a specific width in the terminal. @param text - Text to truncate. @param columns - Columns to occupy in the terminal. @example ``` import cliTruncate = require('cli-truncate'); cliTruncate('unicorn', 4); //=> 'uni…' // Truncate at different positions cliTruncate('unicorn', 4, {position: 'start'}); //=> '…orn' cliTruncate('unicorn', 4, {position: 'middle'}); //=> 'un…n' cliTruncate('\u001B[31municorn\u001B[39m', 4); //=> '\u001B[31muni\u001B[39m…' // Truncate Unicode surrogate pairs cliTruncate('uni\uD83C\uDE00corn', 5); //=> 'uni\uD83C\uDE00…' // Truncate fullwidth characters cliTruncate('안녕하세요', 3); //=> '안…' // Truncate the paragraph to the terminal width const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.'; cliTruncate(paragraph, process.stdout.columns)); //=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…' ``` */ declare function cliTruncate( text: string, columns: number, options?: cliTruncate.Options ): string; export = cliTruncate; cli-truncate-2.1.0/index.js000066400000000000000000000047711357240362100155650ustar00rootroot00000000000000'use strict'; const sliceAnsi = require('slice-ansi'); const stringWidth = require('string-width'); function getIndexOfNearestSpace(string, index, shouldSearchRight) { if (string.charAt(index) === ' ') { return index; } for (let i = 1; i <= 3; i++) { if (shouldSearchRight) { if (string.charAt(index + i) === ' ') { return index + i; } } else if (string.charAt(index - i) === ' ') { return index - i; } } return index; } module.exports = (text, columns, options) => { options = { position: 'end', preferTruncationOnSpace: false, ...options }; const {position, space, preferTruncationOnSpace} = options; let ellipsis = '…'; let ellipsisWidth = 1; if (typeof text !== 'string') { throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`); } if (typeof columns !== 'number') { throw new TypeError(`Expected \`columns\` to be a number, got ${typeof columns}`); } if (columns < 1) { return ''; } if (columns === 1) { return ellipsis; } const length = stringWidth(text); if (length <= columns) { return text; } if (position === 'start') { if (preferTruncationOnSpace) { const nearestSpace = getIndexOfNearestSpace(text, length - columns + 1, true); return ellipsis + sliceAnsi(text, nearestSpace, length).trim(); } if (space === true) { ellipsis += ' '; ellipsisWidth = 2; } return ellipsis + sliceAnsi(text, length - columns + ellipsisWidth, length); } if (position === 'middle') { if (space === true) { ellipsis = ' ' + ellipsis + ' '; ellipsisWidth = 3; } const half = Math.floor(columns / 2); if (preferTruncationOnSpace) { const spaceNearFirstBreakPoint = getIndexOfNearestSpace(text, half); const spaceNearSecondBreakPoint = getIndexOfNearestSpace(text, length - (columns - half) + 1, true); return sliceAnsi(text, 0, spaceNearFirstBreakPoint) + ellipsis + sliceAnsi(text, spaceNearSecondBreakPoint, length).trim(); } return ( sliceAnsi(text, 0, half) + ellipsis + sliceAnsi(text, length - (columns - half) + ellipsisWidth, length) ); } if (position === 'end') { if (preferTruncationOnSpace) { const nearestSpace = getIndexOfNearestSpace(text, columns - 1); return sliceAnsi(text, 0, nearestSpace) + ellipsis; } if (space === true) { ellipsis = ' ' + ellipsis; ellipsisWidth = 2; } return sliceAnsi(text, 0, columns - ellipsisWidth) + ellipsis; } throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`); }; cli-truncate-2.1.0/index.test-d.ts000066400000000000000000000006371357240362100167730ustar00rootroot00000000000000import {expectType} from 'tsd'; import cliTruncate = require('.'); expectType(cliTruncate('unicorn', 4)); expectType(cliTruncate('unicorn', 4, {position: 'start'})); expectType(cliTruncate('unicorn', 4, {position: 'middle'})); expectType(cliTruncate('unicorn', 4, {position: 'end'})); expectType(cliTruncate('unicorn', 4, {position: 'end', preferTruncationOnSpace: true})); cli-truncate-2.1.0/license000066400000000000000000000021251357240362100154540ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (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. cli-truncate-2.1.0/package.json000066400000000000000000000014261357240362100164000ustar00rootroot00000000000000{ "name": "cli-truncate", "version": "2.1.0", "description": "Truncate a string to a specific width in the terminal", "license": "MIT", "repository": "sindresorhus/cli-truncate", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=8" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "truncate", "ellipsis", "text", "limit", "slice", "cli", "terminal", "term", "shell", "width", "ansi", "string" ], "dependencies": { "slice-ansi": "^3.0.0", "string-width": "^4.2.0" }, "devDependencies": { "ava": "^2.1.0", "tsd": "^0.11.0", "xo": "^0.25.3" } } cli-truncate-2.1.0/readme.md000066400000000000000000000070231357240362100156700ustar00rootroot00000000000000# cli-truncate [![Build Status](https://travis-ci.org/sindresorhus/cli-truncate.svg?branch=master)](https://travis-ci.org/sindresorhus/cli-truncate) > Truncate a string to a specific width in the terminal Gracefully handles [ANSI escapes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles). Like a string styled with [`chalk`](https://github.com/chalk/chalk). It also supports Unicode surrogate pairs and fullwidth characters. ## Install ``` $ npm install cli-truncate ``` ## Usage ```js const cliTruncate = require('cli-truncate'); cliTruncate('unicorn', 4); //=> 'uni…' // Truncate at different positions cliTruncate('unicorn', 4, {position: 'start'}); //=> '…orn' cliTruncate('unicorn', 4, {position: 'middle'}); //=> 'un…n' cliTruncate('unicorns rainbow dragons', 6, {position: 'end'}) //=> 'unico…' cliTruncate('\u001B[31municorn\u001B[39m', 4); //=> '\u001B[31muni\u001B[39m…' // Truncate Unicode surrogate pairs cliTruncate('uni\uD83C\uDE00corn', 5); //=> 'uni\uD83C\uDE00…' // Truncate fullwidth characters cliTruncate('안녕하세요', 3); //=> '안…' // Truncate the paragraph to the terminal width const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.'; cliTruncate(paragraph, process.stdout.columns)); //=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…' ``` ## API ### cliTruncate(text, columns, options?) #### text Type: `string` Text to truncate. #### columns Type: `number` Columns to occupy in the terminal. #### options Type: `object` ##### position Type: `string`\ Default: `'end'`\ Values: `'start'` `'middle'` `'end'` Position to truncate the string. ##### space Type: `boolean`\ Default: `false` Add a space between the text and the ellipsis. ```js cliTruncate('unicorns', 5, {space: false}); //=> 'unic…' cliTruncate('unicorns', 5, {space: true}); //=> 'uni …' cliTruncate('unicorns', 6, {position: 'start', space: true}); //=> '… orns' cliTruncate('unicorns', 7, {position: 'middle', space: true}); //=> 'uni … s' ``` ##### preferTruncationOnSpace Type: `boolean`\ Default: `false` Truncate the string from a whitespace if it is within 3 characters from the actual breaking point. ```js cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true}) //=> '…rainbow dragons' // without preferTruncationOnSpace cliTruncate('unicorns rainbow dragons', 20, {position: 'start'}) //=> '…rns rainbow dragons' cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncationOnSpace: true}) //=> 'unicorns…dragons' cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true}) //=> 'unico…' // preferTruncationOnSpace would have no effect if space isn't found within next 3 indexes cliTruncate('unicorns rainbow dragons', 6, {position: 'middle', preferTruncationOnSpace: true}) //=> 'uni…ns' ``` ## Related - [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes - [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes ---
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.
cli-truncate-2.1.0/test.js000066400000000000000000000056401357240362100154310ustar00rootroot00000000000000import test from 'ava'; import cliTruncate from '.'; test('main', t => { t.is(cliTruncate('unicorn', 4), 'uni…'); t.is(cliTruncate('unicorn', 4, {position: 'end'}), 'uni…'); t.is(cliTruncate('unicorn', 1), '…'); t.is(cliTruncate('unicorn', 0), ''); t.is(cliTruncate('unicorn', -4), ''); t.is(cliTruncate('unicorn', 20), 'unicorn'); t.is(cliTruncate('unicorn', 7), 'unicorn'); t.is(cliTruncate('unicorn', 6), 'unico…'); t.is(cliTruncate('\u001B[31municorn\u001B[39m', 7), '\u001B[31municorn\u001B[39m'); t.is(cliTruncate('\u001B[31municorn\u001B[39m', 1), '…'); t.is(cliTruncate('\u001B[31municorn\u001B[39m', 4), '\u001B[31muni\u001B[39m…'); t.is(cliTruncate('a\uD83C\uDE00b\uD83C\uDE00c', 5), 'a\uD83C\uDE00b\uD83C\uDE00…', 'surrogate pairs'); t.is(cliTruncate('안녕하세요', 3), '안…', 'wide char'); t.is(cliTruncate('unicorn', 5, {position: 'start'}), '…corn'); t.is(cliTruncate('unicorn', 6, {position: 'start'}), '…icorn'); t.is(cliTruncate('unicorn', 5, {position: 'middle'}), 'un…rn'); t.is(cliTruncate('unicorns', 6, {position: 'middle'}), 'uni…ns'); }); test('space option', t => { t.is(cliTruncate('unicorns', 5, {position: 'end', space: true}), 'uni …'); t.is(cliTruncate('unicorns', 6, {position: 'start', space: true}), '… orns'); t.is(cliTruncate('unicorns', 7, {position: 'middle', space: true}), 'uni … s'); t.is(cliTruncate('unicorns', 5, {position: 'end', space: false}), 'unic…'); t.is(cliTruncate('\u001B[31municorn\u001B[39m', 6, {space: true}), '\u001B[31munic\u001B[39m …'); t.is(cliTruncate('Plant a tree every day.', 14, {space: true}), 'Plant a tree …'); t.is(cliTruncate('안녕하세요', 4, {space: true}), '안 …', 'wide char'); t.is(cliTruncate('\u001B[31municorn\u001B[39m', 6, {position: 'start', space: true}), '… \u001B[31mcorn\u001B[39m'); t.is(cliTruncate('\u001B[31municornsareawesome\u001B[39m', 10, {position: 'middle', space: true}), '\u001B[31munico\u001B[39m … \u001B[31mme\u001B[39m'); t.is(cliTruncate('Plant a tree every day.', 14, {position: 'middle', space: true}), 'Plant a … day.'); t.is(cliTruncate('안녕하세요', 4, {position: 'start', space: true}), '… 요', 'wide char'); }); test('preferTruncationOnSpace option', t => { t.is(cliTruncate('unicorns are awesome', 15, {position: 'start', preferTruncationOnSpace: true}), '…are awesome'); t.is(cliTruncate('dragons are awesome', 15, {position: 'end', preferTruncationOnSpace: true}), 'dragons are…'); t.is(cliTruncate('unicorns rainbow dragons', 6, {position: 'start', preferTruncationOnSpace: true}), '…agons'); t.is(cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true}), 'unico…'); t.is(cliTruncate('unicorns rainbow dragons', 6, {position: 'middle', preferTruncationOnSpace: true}), 'uni…ns'); t.is(cliTruncate('unicorns partying with dragons', 20, {position: 'middle', preferTruncationOnSpace: true}), 'unicorns…dragons'); });