pax_global_header00006660000000000000000000000064131351777670014533gustar00rootroot0000000000000052 comment=165504b9d6a88f5b1295df6afccc16592ed6fb80 wrap-ansi-3.0.1/000077500000000000000000000000001313517776700134355ustar00rootroot00000000000000wrap-ansi-3.0.1/.editorconfig000066400000000000000000000002571313517776700161160ustar00rootroot00000000000000root = 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 wrap-ansi-3.0.1/.gitattributes000066400000000000000000000000351313517776700163260ustar00rootroot00000000000000* text=auto *.js text eol=lf wrap-ansi-3.0.1/.gitignore000066400000000000000000000000431313517776700154220ustar00rootroot00000000000000node_modules yarn.lock .nyc_output wrap-ansi-3.0.1/.npmrc000066400000000000000000000000231313517776700145500ustar00rootroot00000000000000package-lock=false wrap-ansi-3.0.1/.travis.yml000066400000000000000000000001241313517776700155430ustar00rootroot00000000000000language: node_js node_js: - '8' - '6' - '4' after_success: npm run coveralls wrap-ansi-3.0.1/index.js000077500000000000000000000100501313517776700151010ustar00rootroot00000000000000'use strict'; const stringWidth = require('string-width'); const stripAnsi = require('strip-ansi'); const ESCAPES = new Set([ '\u001B', '\u009B' ]); const END_CODE = 39; const ESCAPE_CODES = new Map([ [0, 0], [1, 22], [2, 22], [3, 23], [4, 24], [7, 27], [8, 28], [9, 29], [30, 39], [31, 39], [32, 39], [33, 39], [34, 39], [35, 39], [36, 39], [37, 39], [90, 39], [40, 49], [41, 49], [42, 49], [43, 49], [44, 49], [45, 49], [46, 49], [47, 49] ]); const wrapAnsi = code => `${ESCAPES.values().next().value}[${code}m`; // Calculate the length of words split on ' ', ignoring // the extra characters added by ansi escape codes const wordLengths = str => str.split(' ').map(s => stringWidth(s)); // Wrap a long word across multiple rows // Ansi escape codes do not count towards length const wrapWord = (rows, word, cols) => { const arr = Array.from(word); let insideEscape = false; let visible = stringWidth(stripAnsi(rows[rows.length - 1])); for (const item of arr.entries()) { const i = item[0]; const char = item[1]; const charLength = stringWidth(char); if (visible + charLength <= cols) { rows[rows.length - 1] += char; } else { rows.push(char); visible = 0; } if (ESCAPES.has(char)) { insideEscape = true; } else if (insideEscape && char === 'm') { insideEscape = false; continue; } if (insideEscape) { continue; } visible += charLength; if (visible === cols && i < arr.length - 1) { rows.push(''); visible = 0; } } // It's possible that the last row we copy over is only // ansi escape characters, handle this edge-case if (!visible && rows[rows.length - 1].length > 0 && rows.length > 1) { rows[rows.length - 2] += rows.pop(); } }; // The wrap-ansi module can be invoked // in either 'hard' or 'soft' wrap mode // // 'hard' will never allow a string to take up more // than cols characters // // 'soft' allows long words to expand past the column length const exec = (str, cols, opts) => { const options = opts || {}; if (str.trim() === '') { return options.trim === false ? str : str.trim(); } let pre = ''; let ret = ''; let escapeCode; const lengths = wordLengths(str); const words = str.split(' '); const rows = ['']; for (const item of Array.from(words).entries()) { const i = item[0]; const word = item[1]; rows[rows.length - 1] = options.trim === false ? rows[rows.length - 1] : rows[rows.length - 1].trim(); let rowLength = stringWidth(rows[rows.length - 1]); if (rowLength || word === '') { if (rowLength === cols && options.wordWrap === false) { // If we start with a new word but the current row length equals the length of the columns, add a new row rows.push(''); rowLength = 0; } rows[rows.length - 1] += ' '; rowLength++; } // In 'hard' wrap mode, the length of a line is // never allowed to extend past 'cols' if (lengths[i] > cols && options.hard) { if (rowLength) { rows.push(''); } wrapWord(rows, word, cols); continue; } if (rowLength + lengths[i] > cols && rowLength > 0) { if (options.wordWrap === false && rowLength < cols) { wrapWord(rows, word, cols); continue; } rows.push(''); } if (rowLength + lengths[i] > cols && options.wordWrap === false) { wrapWord(rows, word, cols); continue; } rows[rows.length - 1] += word; } pre = rows.map(r => options.trim === false ? r : r.trim()).join('\n'); for (const item of Array.from(pre).entries()) { const i = item[0]; const char = item[1]; ret += char; if (ESCAPES.has(char)) { const code = parseFloat(/\d[^m]*/.exec(pre.slice(i, i + 4))); escapeCode = code === END_CODE ? null : code; } const code = ESCAPE_CODES.get(Number(escapeCode)); if (escapeCode && code) { if (pre[i + 1] === '\n') { ret += wrapAnsi(code); } else if (char === '\n') { ret += wrapAnsi(escapeCode); } } } return ret; }; // For each newline, invoke the method separately module.exports = (str, cols, opts) => { return String(str) .normalize() .split('\n') .map(line => exec(line, cols, opts)) .join('\n'); }; wrap-ansi-3.0.1/license000066400000000000000000000021251313517776700150020ustar00rootroot00000000000000MIT 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. wrap-ansi-3.0.1/package.json000066400000000000000000000021251313517776700157230ustar00rootroot00000000000000{ "name": "wrap-ansi", "version": "3.0.1", "description": "Wordwrap a string with ANSI escape codes", "license": "MIT", "repository": "chalk/wrap-ansi", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=4" }, "scripts": { "test": "xo && nyc ava", "coveralls": "nyc report --reporter=text-lcov | coveralls" }, "files": [ "index.js" ], "keywords": [ "wrap", "break", "wordwrap", "wordbreak", "linewrap", "ansi", "styles", "color", "colour", "colors", "terminal", "console", "cli", "string", "tty", "escape", "formatting", "rgb", "256", "shell", "xterm", "log", "logging", "command-line", "text" ], "dependencies": { "string-width": "^2.1.1", "strip-ansi": "^4.0.0" }, "devDependencies": { "ava": "^0.21.0", "chalk": "^2.0.1", "coveralls": "^2.11.4", "has-ansi": "^3.0.0", "nyc": "^11.0.3", "strip-ansi": "^4.0.0", "xo": "^0.18.2" } } wrap-ansi-3.0.1/readme.md000066400000000000000000000043651313517776700152240ustar00rootroot00000000000000# wrap-ansi [![Build Status](https://travis-ci.org/chalk/wrap-ansi.svg?branch=master)](https://travis-ci.org/chalk/wrap-ansi) [![Coverage Status](https://coveralls.io/repos/github/chalk/wrap-ansi/badge.svg?branch=master)](https://coveralls.io/github/chalk/wrap-ansi?branch=master) > Wordwrap a string with [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles) ## Install ``` $ npm install wrap-ansi ``` ## Usage ```js const chalk = require('chalk'); const wrapAnsi = require('wrap-ansi'); const input = 'The quick brown ' + chalk.red('fox jumped over ') + 'the lazy ' + chalk.green('dog and then ran away with the unicorn.'); console.log(wrapAnsi(input, 20)); ``` ## API ### wrapAnsi(input, columns, [options]) Wrap words to the specified column width. #### input Type: `string` String with ANSI escape codes. Like one styled by [`chalk`](https://github.com/chalk/chalk). #### columns Type: `number` Number of columns to wrap the text to. #### options Type: `Object` ##### hard Type: `boolean`
Default: `false` By default the wrap is soft, meaning long words may extend past the column width. Setting this to `true` will make it hard wrap at the column width. ##### wordWrap Type: `boolean`
Default: `true` By default, an attempt is made to split words at spaces, ensuring that they don't extend past the configured columns. If wordWrap is `false`, each column will instead be completely filled splitting words as necessary. ##### trim Type: `boolean`
Default: `true` Whitespace on all lines is removed by default. Set this option to `false` if you don't want to trim. ## Related - [slice-ansi](https://github.com/chalk/slice-ansi) - Slice 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 - [jsesc](https://github.com/mathiasbynens/jsesc) - Generate ASCII-only output from Unicode strings. Useful for creating test fixtures. ## Maintainers - [Sindre Sorhus](https://github.com/sindresorhus) - [Josh Junon](https://github.com/qix-) - [Benjamin Coe](https://github.com/bcoe) ## License MIT wrap-ansi-3.0.1/screenshot.png000066400000000000000000000261031313517776700163220ustar00rootroot00000000000000PNG  IHDRzB\}PLTE----,J+Hxͳr缓ܼ-,PלLָ-,DgM-,+Eʢ,&eY(C' ZTfϺbPxղQ?.J@!}|O>Vh+֒JWn֒L}ЀW%g眦ݤLG9u}Z|CȚgWPp`'ZZҘY\GK/0g1't0޺])۫Au҈gNcGu}#Z҃4Y9퟉="Iu%U;ӽ~޻j%"#A2vYˆKΈGĈqAQu}Zz6]Wg|. X0D)潤e%~WV[VZmBUkI9tW#m>˴eY9\@K253#>H:ǾKgjg Tܷ宷lJ$9:gq$;),T-Q'WhJ~t ΕsWi5}s {n/˟[Cc|n=_KTCZC8t[wh Gh΁j=7ZXeg}摖ZKPUKRe([mбAY')8YH==j>a2(L-y7rDѠZr~Aa*S&eLky-9\2-e6Jo"t#m)ZV< _~kS6'7PNgyT7{@͌@O;kJ-]_-S,5N֞{WFK.>y0H-jYw4;Mb#Zrr5O⩇hak޽>)Z*z<k$C a%%F=osQ] }6w#hYu3Me$3 @urM%WAPfq>_,G`rGUZjhI7[Z~Di*Dq35:pq]{g;;r.H9+7y tÉ "k8>Z10z2jE)-ar ] q-$0-7RcrY;:_L],Xy9d HK::zZ p%x7% JVUHOzׯV׿WNVOŹw 6NHKZh vx?j-1wD> fI_@%:>x1__ۚW&QL%kǼL4GђN*YYGK`͛̽B[%;@C64`t:Wy9h;H"4;XC9A;)3'ZsET<嫞ג6Yٴ9uKeXߵ䱐EGZ䉵j# Hu8hpH)6P-0'Gj' 5b,Z-&Au }-唇ma~u- k9o~TZ26<߬ )_dINK(8dZJ$גsk1҃h9}k'TFXˌc2:Ӳn4,>ZRNKѪ#6-+2asxYKU?W]9G:Z[| W24@ex-t:hYUKPP>Gkҳ}2LKa=y 8ThO G:ZM| SK~~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ޙ|i?':^Ty^qo(fXhǀWzbk|Б,p\vD-wxs"~$YL}5d0!rpB-E1aBD&(䩛!(fCU1ppƒ,F:­7/f0ÕJFKZQjTs" Xpv!ʑǹ%sC!@R%.okK*5W L:,]Q#e8zb}` 7Wy)QpWk?K*eH~WPs4NCG] #Hm >h=:ѣ|65N73uj=tyhZJ"0F:,5Vm!.- ! Hf_ @Ci%UVuݴ#LE@(-ˡ4d(čG, hYmM9tB,}K2Lypqz%GM=yAΕ3_FTٚuykӭTL6-rpv2e?ʋ{IGޕ2 Dl kw蒮U*v F%@.%)eNFt:}Ǧevz&Y|/7YGir\LǚԂlck560u}h;bnELZV$hu-}'H7 ׍֭^NzTy2|qJz"S >SrzHsk}k'&=TGX-CiQ:p؋GV{%PPP)o{[s a ծVpu}9BǡewE݇C j?;L2qܡPe:> |qhY)ۡj Q[\i>]yp$2JVB#4(>I>F-%jiv.׶"R"܂lOFoO+µޜ:"-yكE>J}U jYPh(CZ -[nݙFZ-ޑS H]A#4(%IqhQ]ڦ~?{nc|*Z:py2|@-hflkG8IN֪#;?9Vtq+z2z0K9n6QkGFK&yh-k&^3R"ؒާO2_KQ#NGO`")޳rz+;,p 2~B!B!B!B!B!B!|lͲi#IR!$f?K2*ȊHZL6OdfSB!BNN >rcD^[ދZ~|dz@&$&ujꙒ/)sk"`G&b },t CϔlŲm]eN=c¦+gZ6,e&>u\=x(,' }3%[eO&ғZ|>ڞJ CcbBe9]:;\$)N-G1&SG'Fx˱}-i.!虒-Բ_Nh2L?J$8ZVP3=SZ]QK6[01L6j)v8Sh Z>j2=SZZG4ic a3%[eE(-ʌ4 @Z`C jٰd|?ئd꒙|%2Mnڋ"KXLrYTȁ?B!B!B!B!B!Q>6IENDB`wrap-ansi-3.0.1/test.js000077500000000000000000000120511313517776700147540ustar00rootroot00000000000000import test from 'ava'; import chalk from 'chalk'; import hasAnsi from 'has-ansi'; import stripAnsi from 'strip-ansi'; import m from '.'; chalk.enabled = true; // When "hard" is false const fixture = 'The quick brown ' + chalk.red('fox jumped over ') + 'the lazy ' + chalk.green('dog and then ran away with the unicorn.'); const fixture2 = '12345678\n901234567890'; const fixture3 = '12345678\n901234567890 12345'; const fixture4 = '12345678\n'; const fixture5 = '12345678\n '; test('wraps string at 20 characters', t => { const res20 = m(fixture, 20); t.is(res20, 'The quick brown \u001B[31mfox\u001B[39m\n\u001B[31mjumped over \u001B[39mthe lazy\n\u001B[32mdog and then ran\u001B[39m\n\u001B[32maway with the\u001B[39m\n\u001B[32municorn.\u001B[39m'); t.true(stripAnsi(res20).split('\n').every(x => x.length <= 20)); }); test('wraps string at 30 characters', t => { const res30 = m(fixture, 30); t.is(res30, 'The quick brown \u001B[31mfox jumped\u001B[39m\n\u001B[31mover \u001B[39mthe lazy \u001B[32mdog and then ran\u001B[39m\n\u001B[32maway with the unicorn.\u001B[39m'); t.true(stripAnsi(res30).split('\n').every(x => x.length <= 30)); }); test('does not break strings longer than "cols" characters', t => { const res5 = m(fixture, 5, {hard: false}); t.is(res5, 'The\nquick\nbrown\n\u001B[31mfox\u001B[39m\n\u001B[31mjumped\u001B[39m\n\u001B[31mover\u001B[39m\n\u001B[31m\u001B[39mthe\nlazy\n\u001B[32mdog\u001B[39m\n\u001B[32mand\u001B[39m\n\u001B[32mthen\u001B[39m\n\u001B[32mran\u001B[39m\n\u001B[32maway\u001B[39m\n\u001B[32mwith\u001B[39m\n\u001B[32mthe\u001B[39m\n\u001B[32municorn.\u001B[39m'); t.true(stripAnsi(res5).split('\n').filter(x => x.length > 5).length > 0); }); test('handles colored string that wraps on to multiple lines', t => { const res = m(chalk.green('hello world') + ' hey!', 5, {hard: false}); const lines = res.split('\n'); t.true(hasAnsi(lines[0])); t.true(hasAnsi(lines[1])); t.false(hasAnsi(lines[2])); }); test('does not prepend newline if first string is greater than "cols"', t => { const res = m(chalk.green('hello') + '-world', 5, {hard: false}); t.is(res.split('\n').length, 1); }); // When "hard" is true test('breaks strings longer than "cols" characters', t => { const res5 = m(fixture, 5, {hard: true}); t.is(res5, 'The\nquick\nbrown\n\u001B[31mfox\u001B[39m\n\u001B[31mjumpe\u001B[39m\n\u001B[31md\u001B[39m\n\u001B[31mover\u001B[39m\n\u001B[31m\u001B[39mthe\nlazy\n\u001B[32mdog\u001B[39m\n\u001B[32mand\u001B[39m\n\u001B[32mthen\u001B[39m\n\u001B[32mran\u001B[39m\n\u001B[32maway\u001B[39m\n\u001B[32mwith\u001B[39m\n\u001B[32mthe\u001B[39m\n\u001B[32munico\u001B[39m\n\u001B[32mrn.\u001B[39m'); t.true(stripAnsi(res5).split('\n').every(x => x.length <= 5)); }); test('removes last row if it contained only ansi escape codes', t => { const res = m(chalk.green('helloworld'), 2, {hard: true}); t.true(stripAnsi(res).split('\n').every(x => x.length === 2)); }); test('does not prepend newline if first word is split', t => { const res = m(chalk.green('hello') + 'world', 5, {hard: true}); t.is(res.split('\n').length, 2); }); test('takes into account line returns inside input', t => { const res20 = m(fixture2, 10, {hard: true}); t.is(res20, '12345678\n9012345678\n90'); }); test('word wrapping', t => { const res = m(fixture3, 15); t.is(res, '12345678\n901234567890\n12345'); }); test('no word-wrapping', t => { const res = m(fixture3, 15, {wordWrap: false}); t.is(res, '12345678\n901234567890 12\n345'); const res2 = m(fixture3, 5, {wordWrap: false}); t.is(res2, '12345\n678\n90123\n45678\n90 12\n345'); const res3 = m(fixture5, 5, {wordWrap: false}); t.is(res3, '12345\n678\n'); const res4 = m(fixture, 5, {wordWrap: false}); t.is(res4, 'The q\nuick\nbrown\n\u001B[31mfox j\u001B[39m\n\u001B[31mumped\u001B[39m\n\u001B[31mover\u001B[39m\n\u001B[31m\u001B[39mthe l\nazy \u001B[32md\u001B[39m\n\u001B[32mog an\u001B[39m\n\u001B[32md the\u001B[39m\n\u001B[32mn ran\u001B[39m\n\u001B[32maway\u001B[39m\n\u001B[32mwith\u001B[39m\n\u001B[32mthe u\u001B[39m\n\u001B[32mnicor\u001B[39m\n\u001B[32mn.\u001B[39m'); }); test('no word-wrapping and no trimming', t => { const res = m(fixture3, 13, {wordWrap: false, trim: false}); t.is(res, '12345678\n901234567890 \n12345'); const res2 = m(fixture4, 5, {wordWrap: false, trim: false}); t.is(res2, '12345\n678\n'); const res3 = m(fixture5, 5, {wordWrap: false, trim: false}); t.is(res3, '12345\n678\n '); const res4 = m(fixture, 5, {wordWrap: false, trim: false}); t.is(res4, 'The q\nuick \nbrown\n \u001B[31mfox \u001B[39m\njumpe\nd ove\nr \u001B[39mthe\n lazy\n \u001B[32mdog \u001B[39m\nand t\nhen r\nan aw\nay wi\nth th\ne uni\ncorn.\u001B[39m'); }); test('supports fullwidth characters', t => { t.is(m('안녕하세', 4, {hard: true}), '안녕\n하세'); }); test('supports unicode surrogate pairs', t => { t.is(m('a\uD83C\uDE00bc', 2, {hard: true}), 'a\n\uD83C\uDE00\nbc'); t.is(m('a\uD83C\uDE00bc\uD83C\uDE00d\uD83C\uDE00', 2, {hard: true}), 'a\n\uD83C\uDE00\nbc\n\uD83C\uDE00\nd\n\uD83C\uDE00'); });