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
147
148
|
"use strict"
var test = require("tap").test
var ProgressBar = require("../progress-bar.js")
var cursor = []
var C
var bar = new ProgressBar({theme: ProgressBar.ascii}, C = {
show: function () {
cursor.push(["show"])
return C
},
hide: function () {
cursor.push(["hide"])
return C
},
up: function (lines) {
cursor.push(["up",lines])
return C
},
horizontalAbsolute: function (col) {
cursor.push(["horizontalAbsolute", col])
return C
},
eraseLine: function () {
cursor.push(["eraseLine"])
return C
},
write: function (line) {
cursor.push(["write", line])
return C
}
})
function isOutput(t, msg, output) {
var tests = []
for (var ii = 0; ii<output.length; ++ii) {
for (var jj = 0; jj<output[ii].length; ++jj) {
tests.push({cmd: ii, arg: jj})
}
}
tests.forEach(function(test) {
t.is(cursor[test.cmd] ? cursor[test.cmd][test.arg] : null,
output[test.cmd][test.arg],
msg + ": " + output[test.cmd] + (test.arg ? " arg #"+test.arg : ""))
})
}
test("hide", function (t) {
t.plan(11)
process.stderr.isTTY = false
bar.hide()
t.is(cursor.length, 0, "We don't progress bar without a tty")
cursor = []
process.stderr.isTTY = true
bar.hide()
isOutput(t, "hide while not showing",[
["show"], // cursor
["horizontalAbsolute",0],
["eraseLine"]])
cursor = []
bar.showing = true
bar.hide()
isOutput(t, "hide while showing",[
["show"], // cursor
["up", 1],
["horizontalAbsolute",0],
["eraseLine"]])
})
test("renderTemplate", function (t) {
t.plan(16)
process.stderr.columns = 11
var result = bar.renderTemplate(ProgressBar.ascii,[{type: "name"}],{name: "NAME"})
t.is(result, "NAME", "name substitution")
var result = bar.renderTemplate(ProgressBar.ascii,[{type: "completionbar"}],{completed: 0})
t.is(result, "----------", "0% bar")
var result = bar.renderTemplate(ProgressBar.ascii,[{type: "completionbar"}],{completed: 0.5})
t.is(result, "#####-----", "50% bar")
var result = bar.renderTemplate(ProgressBar.ascii,[{type: "completionbar"}],{completed: 1})
t.is(result, "##########", "100% bar")
var result = bar.renderTemplate(ProgressBar.ascii,[{type: "completionbar"}],{completed: -100})
t.is(result, "----------", "0% underflow bar")
var result = bar.renderTemplate(ProgressBar.ascii,[{type: "completionbar"}],{completed: 100})
t.is(result, "##########", "100% overflow bar")
var result = bar.renderTemplate(ProgressBar.ascii,[{type: "name"},{type: "completionbar"}],{name: "NAME", completed: 0.5})
t.is(result, "NAME###---", "name + 50%")
var result = bar.renderTemplate(ProgressBar.ascii, ["static"], {})
t.is(result, "static", "static text")
var result = bar.renderTemplate(ProgressBar.ascii, ["static",{type: "name"}], {name: "NAME"})
t.is(result, "staticNAME", "static text + var")
var result = bar.renderTemplate(ProgressBar.ascii, ["static",{type: "name", separated: true}], {name: "NAME"})
t.is(result, "static NAME ", "pre-separated")
var result = bar.renderTemplate(ProgressBar.ascii, [{type: "name", separated: true}, "static"], {name: "NAME"})
t.is(result, "NAME static", "post-separated")
var result = bar.renderTemplate(ProgressBar.ascii, ["1",{type: "name", separated: true}, "2"], {name: ""})
t.is(result, "12", "separated no value")
var result = bar.renderTemplate(ProgressBar.ascii, ["1",{type: "name", separated: true}, "2"], {name: "NAME"})
t.is(result, "1 NAME 2", "separated value")
var result = bar.renderTemplate(ProgressBar.ascii, [{type: "spinner"}], {spinner: 0})
t.is(result, "", "No spinner")
var result = bar.renderTemplate(ProgressBar.ascii, [{type: "spinner"}], {spinner: 1})
t.is(result, "\\", "Spinner 1")
var result = bar.renderTemplate(ProgressBar.ascii, [{type: "spinner"}], {spinner: 10})
t.is(result, "|", "Spinner 10")
})
test("show & pulse", function (t) {
t.plan(23)
process.stderr.columns = 16
cursor = []
process.stderr.isTTY = false
bar.template[0].length = 6
bar.last = new Date(0)
bar.show("NAME", 0)
t.is(cursor.length, 0, "no tty, no progressbar")
cursor = []
process.stderr.isTTY = true
bar.last = new Date(0)
bar.show("NAME", 0.1)
isOutput(t, "tty, name, completion",
[ [ 'hide' ],
[ 'horizontalAbsolute', 0 ],
[ 'write', 'NAME |#-----|\n' ],
[ 'show' ] ])
bar.show("S")
cursor = []
bar.last = new Date(0)
bar.pulse()
isOutput(t, "pulsed spinner",
[ [ 'up', 1 ],
[ 'hide' ],
[ 'horizontalAbsolute', 0 ],
[ 'write', 'S \\ |----|\n' ],
[ 'show' ] ])
cursor = []
bar.last = new Date(0)
bar.pulse("P")
isOutput(t, "pulsed spinner with subsection",
[ [ 'up', 1 ],
[ 'hide' ],
[ 'horizontalAbsolute', 0 ],
[ 'write', 'S -> P | |----|\n' ],
[ 'show' ] ])
})
|