-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·78 lines (70 loc) · 2.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env node
process.stdin.setEncoding('utf8')
// code copyright michael keating 2014, and may be licensed under the
// "MIT" or BSD 2 clause licenses.
var wrap = require('word-wrap')
, path = require('path')
, fs = require('fs')
, help = 'Usage: "sailor blah blah...", or piped: "textprogram | sailor"\n\n'
+ ' Art is copyright by Project Anime Online! This derivative work is '
+ 'for\n educational purposes only, and is protected by fair use. '
+ 'Sailor moon belongs to\n its creators, I hold no copyright! '
+ '\n\nYou can find the original art here:\n '
+ 'http://projectanime.com/sailormoon/sm-ascii.htm\n\n'
, art = [ 'chibi', 'haruka', 'jupiter', 'luna', 'makoto', 'mars1', 'mars2'
, 'mercury', 'michiru', 'moon1', 'moon2', 'neptune', 'rei', 'scout'
, 'uranus', 'usagi1', 'usagi2', 'usagi3', 'venus']
// implemented as a pure function, for fun and profit
process.stdin.on('readable', function () {
process.stdout.write(
process.argv.length > 2 ? ( // are there cli args
( process.argv[2] === '-h'
|| process.argv[2] === '-help'
|| process.argv[2] === '--help' ) ?
help : (
process.argv[2] === '-a' ? (
process.argv.length > 4 ?
format(
process.argv.slice(4, process.argv.length).join(' ')
, process.argv[3]
) : format(process.stdin.read(), process.argv[3])
) : format(
process.argv.slice(2, process.argv.length).join(' ')
, art[Math.floor(Math.random() * art.length)]
)
)
) : ( // or a pipe
format(process.stdin.read(), art[Math.floor(Math.random() * art.length)])
)
)
})
// formats text into array of text lines and parses it thru "style"
// "null" is process.stdin.read's EOF value, run a newline with it!
function format (text, art) {
return text === null ? '\n' : style(
[Array(46).join('_')].concat(
wrap(text.replace(/\s+/g, ' '), { width: 40 })
.split('\n')
.map(function(string){
return '| ' + string.replace(/^(\s*)|(\s*)$/g, '')
+ Array(44 - string.length).join(' ') + ' |'
})
, Array(46).join('-')
)
, load(art).split('\n')
)
}
// takes array of lines and inserts it into ultra cute text art
function style (lines, art) {
return art.map(function (line, index) {
return line
+ Array(80 - line.length).join(' ')
+ (lines[index - 5] !== undefined ? lines[index - 5] : '')
}).join('\n')
}
function load (art) {
return fs.readFileSync(
path.join(path.dirname(process.mainModule.filename), 'art', art)
, { encoding: 'utf8' }
)
}