it(`should use correct colors for seperator creation (${JSON.stringify(data)})`, function() { var segment = new SimpleBaseSegment("Some content", {foreground: "cyan", background: "blue", separator: data.name}); var nextSegment = new MockedSegment("Some other content", "red", "green"); var output = segment.render(nextSegment); var formatter = crayon.foreground(data.fg).background(data.bg); expect(output.substr(-31, 20).match(ansiRegex())).toEqual(formatter("").substr(0, 20).match(ansiRegex())); });
const toHumanReadableAnsi = text => { return text.replace(ansiRegex(), (match, offset, string) => { switch (match) { case style.red.close: case style.green.close: case style.cyan.close: case style.bgRed.close: case style.bgGreen.close: case style.bgCyan.close: case style.reset.open: case style.reset.close: return '</>'; case style.red.open: return '<red>'; case style.green.open: return '<green>'; case style.cyan.open: return '<cyan>'; case style.bgRed.open: return '<bgRed>'; case style.bgGreen.open: return '<bgGreen>'; case style.bgCyan.open: return '<bgCyan>'; case style.dim.open: return '<dim>'; case style.bold.open: return '<bold>'; default: return ''; } }); };
module.exports = function(str){ var r = []; var curr = str; var curr_style = []; var code; var e; while(e = ansiRegEx().exec(curr)){ if(e.index > 0){ r.push({ styles: curr_style.slice(0), text: curr.substr(0, e.index) }); } code = e[0]; if(escape_codes[code]){ each(escape_codes[code], function(o, code){ removeElm(curr_style, code); }); } if(code_to_style[code]){ curr_style.push(code_to_style[code]); } curr = curr.substr(e.index + e[0].length); } if(curr.length > 0){ r.push({ styles: curr_style, text: curr }); } return r; };
function divide(data) { let tester = ansiRegex(); let matcher = /.{1,139}/g; return data .split(/\n|\[K|\[[0-9]B|\[[1-9][0-9]B/) .reduce((arr, line) => { if (line.length < 140 || line.match(tester)) { arr.push(line); } else { arr.push(...line.match(matcher)); } return arr; }, []); }
*/const toHumanReadableAnsi = text => {const style = require('ansi-styles');return text.replace(ansiRegex(), (match, offset, string) => {switch (match) {case style.red.close:case style.green.close:case style.reset.open: case style.reset.close: return '</>'; case style.red.open: return '<red>'; case style.green.open: return '<green>'; case style.dim.open: return '<dim>'; case style.bold.open: return '<bold>'; default: return '';} }); };
const toHumanReadableAnsi = text => text.replace(ansiRegex(), (match, offset, string) => { switch (match) { case style.red.close: case style.green.close: case style.cyan.close: case style.gray.close: case style.white.close: case style.yellow.close: case style.bgRed.close: case style.bgGreen.close: case style.bgYellow.close: case style.inverse.close: case style.dim.close: case style.bold.close: case style.reset.open: case style.reset.close: return '</>'; case style.red.open: return '<red>'; case style.green.open: return '<green>'; case style.cyan.open: return '<cyan>'; case style.gray.open: return '<gray>'; case style.white.open: return '<white>'; case style.yellow.open: return '<yellow>'; case style.bgRed.open: return '<bgRed>'; case style.bgGreen.open: return '<bgGreen>'; case style.bgYellow.open: return '<bgYellow>'; case style.inverse.open: return '<inverse>'; case style.dim.open: return '<dim>'; case style.bold.open: return '<bold>'; default: return ''; } });
* @param {String} [optionnal] String to concatenate to the prefix * @return {String} prompt prefix */ Prompt.prototype.prefix = function (str) { str || (str = ''); return chalk.green('?') + ' ' + str; }; /** * Return the prompt line suffix * @param {String} [optionnal] String to concatenate to the suffix * @return {String} prompt suffix */ var reStrEnd = new RegExp('(?:' + ansiRegex().source + ')$|$'); Prompt.prototype.suffix = function (str) { str || (str = ''); // make sure we get the `:` inside the styles if (str.length < 1 || /[a-z1-9]$/i.test(chalk.stripColor(str))) { str = str.replace(reStrEnd, ':$&'); } return str.trim() + ' '; }; /** * Generate the prompt question string * @return {String} prompt question string
/* */ 'use strict'; var ansiRegex = require('ansi-regex'); var re = new RegExp(ansiRegex().source); // remove the `g` flag module.exports = re.test.bind(re);
it('should be false boolean', () => { const bool: boolean = ansiRegex().test('cake'); // => false });
it('should be false boolean 2', () => { const bool: boolean = ansiRegex({ onlyFirst: true }).test('cake'); // => false });
it(`should render output with given background and foreground color (${JSON.stringify(data)})`, function() { var segment = new SimpleBaseSegment("Some content", {foreground: data.fg, background: data.bg}); var output = segment.render(null); var expected = crayon.foreground(data.fg).background(data.bg)(""); expect(output.match(ansiRegex())).toEqual(expected.match(ansiRegex())); });
* @return {String} prompt prefix */ Prompt.prototype.prefix = function( str ) { str || (str = ""); return chalk.green("?") + " " + str; }; /** * Return the prompt line suffix * @param {String} [optionnal] String to concatenate to the suffix * @return {String} prompt suffix */ var reStrEnd = new RegExp("(?:" + ansiRegex().source + ")$|$"); Prompt.prototype.suffix = function( str ) { str || (str = ""); return (str.length < 1 || /[a-z1-9]$/i.test(chalk.stripColor(str)) ? // make sure we get the `:` inside the styles str.replace(reStrEnd, ":$&") : str).trim() + " "; }; /** * Generate the prompt question string * @return {String} prompt question string */ Prompt.prototype.getQuestion = function() {
const test = (value: any) => typeof value === 'string' && value.match(ansiRegex());
"use strict";var ansiRegex=require("ansi-regex");var re=new RegExp(ansiRegex().source);module.exports=re.test.bind(re);
module.exports = input => typeof input === 'string' ? input.replace(ansiRegex(), '') : input;
it('should be boolean', () => { // From the ansi-regex README.md const bool: boolean = ansiRegex().test('\u001B[4mcake\u001B[0m'); // => true });
it('should be RegExp', () => { const regexp: RegExp = ansiRegex(); });
it('should be RegExpMatchArray | null', () => { const array: ?Array<string> = '\u001B[4mcake\u001B[0m'.match(ansiRegex()); // => ['\u001B[4m', '\u001B[0m'] });
export const test = (val: any) => typeof val === 'string' && val.match(ansiRegex());
test('`fn` prints given string with correct ansi code', t => { t.true(str.match(ansiRegex()).indexOf(ec) !== -1); t.true(hasAnsi(str)); })