-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstarfish.js
executable file
·146 lines (128 loc) · 4.26 KB
/
starfish.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env node
let Starfish = require('./lib/sf.js').starfish;
let PNG = require('node-png').PNG;
let fs = require('fs');
let util = require('util');
let path = require('path');
let opts = parseOptions();
let png = new PNG({width: opts.size[0], height: opts.size[1], filterType: -1});
let outFile = fs.createWriteStream(opts.output)
outFile.on('error', handleError);
outFile.on('open', startRender);
// Done.
function verbose(msg) {
if (opts.verbose) console.error(msg);
}
function verboseRaw(msg) {
if (opts.verbose) process.stderr.write(msg);
}
function handleError(e) {
error(e.message);
}
function error(msg) {
console.error(`${process.argv0}: ${msg}`);
let code = arguments.length > 1? arguments[1] : 1;
process.exit(code);
}
function parseOptions() {
let opts = require('yargs')
.help()
.options({
output: {
alias: 'o'
, description: 'Path of the image file to create'
, 'default': 'output.png'
, 'type': 'string'
}
, size: {
alias: 's'
, description: 'Proportions of the output image'
, 'default': '384x384'
, 'type': 'string'
, coerce: parseSize
}
, verbose: {
alias: 'v'
, description: 'Display progress'
, 'default': false
, 'type': 'boolean'
}
})
.version()
.options({
wallpaper: {
alias: 'w'
, description: '[EXPERIMENTAL] Set the finished image as the desktop background'
, 'default': false
, 'type': 'boolean'
}
})
.alias('help', 'h')
.alias('version', 'V')
.strict()
.example('$0 -v -s 720x720', '(Or just -s 720.) The -v option is recommended for interactive use in a command console.')
.example(
'$0 -o ~/wallpaper.png -w'
, 'Generate a wallpaper at ~/wallpaper.png, and set it as the current desktop wallpaper. Currently, there are a number of known issues with this feature, including that it can\'t set tiled mode (so it\'ll be either centered or stretched - ugly if it\'s much smaller than the desktop); and doesn\'t set all desktop backgrounds, only the current one.'
)
.argv;
if (opts._.length != 0) {
error("program arguments are not accepted.", 2);
}
return opts;
}
function parseSize(value) {
const re = /^([0-9]+)(?:x([0-9]+))?$/;
let match = re.exec(value);
if (match === null) {
error('--size must be expressed as a number N, or NxN. Ex: 384x384');
}
else {
let retval = match.slice(1,3);
if (retval[1] === undefined) {
retval[1] = retval[0];
}
return retval;
}
}
function startRender() {
loadGenerators();
let inst = new Starfish.Instance;
inst.render(png, handleRenderProgress, writeToImageFile);
}
function loadGenerators() {
let dir = fs.readdirSync(path.join(__dirname, 'lib', 'generators'));
let genRe = /^sf-.*\.js$/;
for (let entry of dir) {
if (genRe.test(entry)) {
let mod = require(`./lib/generators/${entry}`);
mod.registerLayer(Starfish);
}
}
}
function handleRenderProgress(kw) {
// Issue progress messages, overwriting previous lines (the \r).
// Does not advance to next line: need to do that at the end.
// If other messages occur before we've written \n, garbling will
// occur.
verboseRaw(`\rPROGRESS: Layer ${kw.curLayer} / ${kw.numLayers}: ${kw.percentDone} % `);
}
function writeToImageFile() {
// Write final progress update message
verboseRaw("\rPROGRESS: Done. \n");
png.on('end'
, () => {
verbose(`Finished writing ${util.inspect(opts.output)}`
+ ` [${opts.size[0]}x${opts.size[1]}].`);
maybeSetWallpaper();
}
);
png.on('error', handleError);
png.pack().pipe(outFile);
}
function maybeSetWallpaper() {
if (!opts.wallpaper) return;
verbose('Setting wallpaper...');
//require('wallpaper').set(opts.output, {scale: 'tile'});
require('wallpaper').set(opts.output);
}