-
Notifications
You must be signed in to change notification settings - Fork 10
/
wrapper.js
70 lines (62 loc) · 1.84 KB
/
wrapper.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
// Wraps text at word boundaries for output to columnar displays.
// Manages levels of indentation, bullets, and margin text.
'use strict';
module.exports = class Wrapper {
constructor(target, width) {
this.target = target;
this.width = width || 60;
this.indents = [''];
this.leads = [''];
this.index = 0;
this.flush = false;
}
words(words) {
var array = words.split(' ');
for (var i = 0; i < array.length; i++) {
this.word(array[i]);
}
}
push(indent, lead) {
var prefix = this.indents[this.indents.length - 1];
this.indents.push(prefix + indent);
this.leads.push(prefix + lead);
}
pop() {
this.indents.pop();
this.leads.pop();
}
word(word) {
var indent = this.indents[this.indents.length - 1];
if (this.index === 0) {
this.target.write(indent);
this.index += indent.length;
this.flush = true;
}
if (this.index + word.length + 1 > this.width) {
this.break();
this.target.write(indent + word);
this.index += indent.length + word.length + 1;
this.flush = false;
} else if (this.flush) {
this.target.write(word);
this.index += word.length;
this.flush = false;
} else {
this.target.write(' ' + word);
this.index += word.length + 1;
this.flush = false;
}
}
break() {
this.target.write('\n');
this.index = 0;
this.flush = true;
}
// Bring your own break, if you need it.
bullet() {
var lead = this.leads[this.leads.length - 1];
this.target.write(lead);
this.index = lead.length;
this.flush = true;
}
}