Comparing version 0.2.1 to 0.3.0
67
cli.js
#!/usr/bin/env node | ||
'use strict'; | ||
var yosay = require('./index.js'); | ||
var pkg = require('./package.json'); | ||
var yosay = require('./index'); | ||
var argv = require('minimist')(process.argv.slice(2)); | ||
var input = argv._[0]; | ||
function stdin(cb) { | ||
var ret = ''; | ||
process.stdin.setEncoding('utf8'); | ||
process.stdin.on('data', function (data) { ret += data; }); | ||
process.stdin.on('end', function () { cb(ret); }).resume(); | ||
} | ||
require('taketalk')({ | ||
init: function (input, options) { | ||
console.log(yosay(input, options)); | ||
}, | ||
function help() { | ||
console.log(pkg.description); | ||
console.log(''); | ||
console.log('Usage'); | ||
console.log(' $ yosay <string>'); | ||
console.log(' $ yosay <string> --maxLength 8'); | ||
console.log(' $ echo <string> | yosay'); | ||
console.log(''); | ||
console.log('Example'); | ||
console.log(' $ yosay "Sindre is a horse"'); | ||
console.log(yosay('Sindre is a horse')); | ||
} | ||
help: function () { | ||
console.log([ | ||
pkg.description, | ||
'', | ||
'Usage', | ||
' $ yosay <string>', | ||
' $ yosay <string> --maxLength 8', | ||
' $ echo <string> | yosay', | ||
'', | ||
'Example', | ||
' $ yosay "Sindre is a horse"', | ||
yosay('Sindre is a horse') | ||
].join('\n')); | ||
}, | ||
function logMessage(message) { | ||
console.log(yosay(message, argv)); | ||
} | ||
if (argv.h || argv.help) { | ||
help(); | ||
return; | ||
} | ||
if (argv.v || argv.version) { | ||
console.log(pkg.version); | ||
return; | ||
} | ||
if (process.stdin.isTTY) { | ||
if (!input) { | ||
help(); | ||
return; | ||
} | ||
logMessage(input); | ||
} else { | ||
stdin(logMessage); | ||
} | ||
version: pkg.version | ||
}); |
90
index.js
@@ -6,2 +6,6 @@ 'use strict'; | ||
var wrap = require('word-wrap'); | ||
var stringLength = require('string-length'); | ||
var stripAnsi = require('strip-ansi'); | ||
var ansiStyles = require('ansi-styles'); | ||
var ansiRegex = require('ansi-regex')(); | ||
@@ -25,7 +29,27 @@ var topOffset = 3; | ||
/* | ||
* What you're about to see may confuse you. And rightfully so. Here's an | ||
* explanation. | ||
* | ||
* When yosay is given a string, we create a duplicate with the ansi styling | ||
* sucked out. This way, the true length of the string is read by `pad` and | ||
* `wrap`, so they can correctly do their job without getting tripped up by | ||
* the "invisible" ansi. Along with the duplicated, non-ansi string, we store | ||
* the character position of where the ansi was, so that when we go back over | ||
* each line that will be printed out in the message box, we check the | ||
* character position to see if it needs any styling, then re-insert it if | ||
* necessary. | ||
* | ||
* Better implementations welcome :) | ||
*/ | ||
var maxLength = 24; | ||
var frame; | ||
var unstyledMessage; | ||
var styledIndexes = {}; | ||
var completedString = ''; | ||
var regExNewLine; | ||
if (options.maxLength) { | ||
maxLength = message.toLowerCase().split(' ').sort()[0].length; | ||
maxLength = stripAnsi(message).toLowerCase().split(' ').sort()[0].length; | ||
@@ -37,13 +61,67 @@ if (maxLength < options.maxLength) { | ||
regExNewLine = new RegExp('\\s{' + maxLength + '}'); | ||
frame = { | ||
top: '.' + pad('', maxLength + 2, '-') + '.', | ||
side: '|', | ||
bottom: '\'' + pad('', maxLength + 2, '-') + '\'' | ||
side: ansiStyles.reset.open + '|' + ansiStyles.reset.open, | ||
bottom: ansiStyles.reset.open + '\'' + pad('', maxLength + 2, '-') + '\'' | ||
}; | ||
return wrap(message, { width: maxLength }) | ||
message.replace(ansiRegex, function (match, offset) { | ||
Object.keys(styledIndexes).forEach(function (key) { | ||
offset -= styledIndexes[key].length; | ||
}); | ||
styledIndexes[offset] = styledIndexes[offset] ? styledIndexes[offset] + match : match; | ||
}); | ||
return wrap(stripAnsi(message), { width: maxLength }) | ||
.split(/\n/) | ||
.reduce(function (greeting, str, index, array) { | ||
str = pad(str.trim(), maxLength); | ||
var paddedString; | ||
if (!regExNewLine.test(str)) { | ||
str = str.trim(); | ||
} | ||
completedString += str; | ||
str = completedString | ||
.substr(completedString.length - str.length) | ||
.replace(/./g, function (char, charIndex) { | ||
if (index > 0) { | ||
charIndex += completedString.length - str.length + index; | ||
} | ||
var hasContinuedStyle = 0; | ||
var continuedStyle; | ||
Object.keys(styledIndexes).forEach(function (offset) { | ||
if (charIndex > offset) { | ||
hasContinuedStyle++; | ||
continuedStyle = styledIndexes[offset]; | ||
} | ||
if (hasContinuedStyle === 1 && charIndex < offset) { | ||
hasContinuedStyle++; | ||
} | ||
}); | ||
if (styledIndexes[charIndex]) { | ||
return styledIndexes[charIndex] + char; | ||
} else if (hasContinuedStyle >= 2) { | ||
return continuedStyle + char; | ||
} else { | ||
return char; | ||
} | ||
}) | ||
.trim(); | ||
paddedString = pad({ | ||
length: stringLength(str), | ||
valueOf: function () { | ||
return ansiStyles.reset.open + str + ansiStyles.reset.open; | ||
} | ||
}, maxLength); | ||
if (index === 0) { | ||
@@ -55,3 +133,3 @@ greeting[topOffset - 1] += frame.top; | ||
(greeting[index + topOffset] || pad.left('', leftOffset)) + | ||
frame.side + ' ' + str + ' ' + frame.side; | ||
frame.side + ' ' + paddedString + ' ' + frame.side; | ||
@@ -58,0 +136,0 @@ if (!array[index + 1]) { |
{ | ||
"name": "yosay", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Tell Yeoman what to say", | ||
@@ -30,6 +30,11 @@ "license": "BSD", | ||
"dependencies": { | ||
"ansi-regex": "^0.2.1", | ||
"ansi-styles": "^1.1.0", | ||
"chalk": "^0.4.0", | ||
"minimist": "^0.2.0", | ||
"pad-component": "0.0.1", | ||
"word-wrap": "^0.1.2", | ||
"chalk": "^0.4.0", | ||
"minimist": "0.0.9" | ||
"string-length": "^0.1.2", | ||
"strip-ansi": "^0.2.1", | ||
"taketalk": "^0.1.0", | ||
"word-wrap": "^0.1.2" | ||
}, | ||
@@ -36,0 +41,0 @@ "devDependencies": { |
@@ -37,6 +37,8 @@ # yosay [![Build Status](https://travis-ci.org/yeoman/yosay.svg?branch=master)](https://travis-ci.org/yeoman/yosay) | ||
*You can style your text with [chalk](https://github.com/sindresorhus/chalk) before passing it to `yosay`.* | ||
## CLI | ||
```sh | ||
``` | ||
$ npm install --global yosay | ||
@@ -51,3 +53,3 @@ $ yosay --help | ||
Example | ||
$ yosay "Sindre is a horse" | ||
$ yosay 'Sindre is a horse' | ||
@@ -54,0 +56,0 @@ _-----_ |
7075
141
69
9
+ Addedansi-regex@^0.2.1
+ Addedansi-styles@^1.1.0
+ Addedstring-length@^0.1.2
+ Addedstrip-ansi@^0.2.1
+ Addedtaketalk@^0.1.0
+ Addedansi-regex@0.1.00.2.1(transitive)
+ Addedansi-styles@1.1.0(transitive)
+ Addedget-stdin@0.1.0(transitive)
+ Addedminimist@0.1.00.2.4(transitive)
+ Addedstring-length@0.1.2(transitive)
+ Addedstrip-ansi@0.2.2(transitive)
+ Addedtaketalk@0.1.1(transitive)
- Removedminimist@0.0.9(transitive)
Updatedminimist@^0.2.0