pax_global_header00006660000000000000000000000064131351003500014501gustar00rootroot0000000000000052 comment=94f26977e8f1603b306e8759147c8c9159ab3b39 cli-truncate-1.1.0/000077500000000000000000000000001313510035000140725ustar00rootroot00000000000000cli-truncate-1.1.0/.editorconfig000066400000000000000000000002571313510035000165530ustar00rootroot00000000000000root = 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-1.1.0/.gitattributes000066400000000000000000000000351313510035000167630ustar00rootroot00000000000000* text=auto *.js text eol=lf cli-truncate-1.1.0/.gitignore000066400000000000000000000000271313510035000160610ustar00rootroot00000000000000node_modules yarn.lock cli-truncate-1.1.0/.npmrc000066400000000000000000000000231313510035000152050ustar00rootroot00000000000000package-lock=false cli-truncate-1.1.0/.travis.yml000066400000000000000000000000631313510035000162020ustar00rootroot00000000000000language: node_js node_js: - '8' - '6' - '4' cli-truncate-1.1.0/index.js000066400000000000000000000022041313510035000155350ustar00rootroot00000000000000'use strict'; const sliceAnsi = require('slice-ansi'); const stringWidth = require('string-width'); module.exports = (input, columns, opts) => { opts = Object.assign({ position: 'end' }, opts); const position = opts.position; const ellipsis = '…'; if (typeof input !== 'string') { throw new TypeError(`Expected \`input\` to be a string, got ${typeof input}`); } 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(input); if (length <= columns) { return input; } if (position === 'start') { return ellipsis + sliceAnsi(input, length - columns + 1, length); } else if (position === 'middle') { const half = Math.floor(columns / 2); return sliceAnsi(input, 0, half) + ellipsis + sliceAnsi(input, length - (columns - half) + 1, length); } else if (position === 'end') { return sliceAnsi(input, 0, columns - 1) + ellipsis; } throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`); }; cli-truncate-1.1.0/license000066400000000000000000000021251313510035000154370ustar00rootroot00000000000000MIT 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-1.1.0/package.json000066400000000000000000000012341313510035000163600ustar00rootroot00000000000000{ "name": "cli-truncate", "version": "1.1.0", "description": "Truncate a string to a specific width in the terminal", "license": "MIT", "repository": "sindresorhus/cli-truncate", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=4" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js" ], "keywords": [ "truncate", "ellipsis", "text", "limit", "slice", "cli", "terminal", "term", "shell", "width", "ansi" ], "dependencies": { "slice-ansi": "^1.0.0", "string-width": "^2.0.0" }, "devDependencies": { "ava": "*", "xo": "*" } } cli-truncate-1.1.0/readme.md000066400000000000000000000035501313510035000156540ustar00rootroot00000000000000# 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('\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(input, columns, [options]) #### input 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. ## 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 ## License MIT © [Sindre Sorhus](https://sindresorhus.com) cli-truncate-1.1.0/test.js000066400000000000000000000016321313510035000154110ustar00rootroot00000000000000import test from 'ava'; import m from '.'; test(t => { t.is(m('unicorn', 4), 'uni…'); t.is(m('unicorn', 4, {position: 'end'}), 'uni…'); t.is(m('unicorn', 1), '…'); t.is(m('unicorn', 0), ''); t.is(m('unicorn', -4), ''); t.is(m('unicorn', 20), 'unicorn'); t.is(m('unicorn', 7), 'unicorn'); t.is(m('unicorn', 6), 'unico…'); t.is(m('\u001B[31municorn\u001B[39m', 7), '\u001B[31municorn\u001B[39m'); t.is(m('\u001B[31municorn\u001B[39m', 1), '…'); t.is(m('\u001B[31municorn\u001B[39m', 4), '\u001B[31muni\u001B[39m…'); t.is(m('a\uD83C\uDE00b\uD83C\uDE00c', 5), 'a\uD83C\uDE00b\uD83C\uDE00…', 'surrogate pairs'); t.is(m('안녕하세요', 3), '안…', 'wide char'); t.is(m('unicorn', 5, {position: 'start'}), '…corn'); t.is(m('unicorn', 6, {position: 'start'}), '…icorn'); t.is(m('unicorn', 5, {position: 'middle'}), 'un…rn'); t.is(m('unicorns', 6, {position: 'middle'}), 'uni…ns'); });