| 1 |
#!/usr/bin/env ruby |
| 2 |
|
| 3 |
require 'rubygems' |
| 4 |
require 'powerbar' |
| 5 |
require 'logger' |
| 6 |
|
| 7 |
# DEMO 1 |
| 8 |
def demo_1 |
| 9 |
total = 100000 |
| 10 |
step = 1000 |
| 11 |
|
| 12 |
p = PowerBar.new |
| 13 |
|
| 14 |
# This is how you alter the settings. |
| 15 |
# (see the source-code for all available settings) |
| 16 |
p.settings.tty.finite.show_eta = true |
| 17 |
|
| 18 |
(0..total).step(step).each do |i| |
| 19 |
p.show({:msg => 'DEMO 1 - Ten seconds of progress', :done => i, :total => total}) |
| 20 |
sleep 0.1 |
| 21 |
end |
| 22 |
|
| 23 |
# close(true) will set the bar to 100% before closing. |
| 24 |
# use this when you are sure your operation has finished but |
| 25 |
# for some reason 'done' may still be smaller than 'total'. |
| 26 |
# |
| 27 |
# this is not necessary in this example, and usually a bad idea, |
| 28 |
# but demonstrated here so you know it exists. |
| 29 |
p.close(true) |
| 30 |
end |
| 31 |
|
| 32 |
# DEMO 2 |
| 33 |
def demo_2 |
| 34 |
total = 100000 |
| 35 |
step = 1000 |
| 36 |
text = " "*30 + "Need to change text in mid-progress? No Problemo!" + |
| 37 |
" "*5 + "As seen in: DEMO 2 - Scrolling madness" |
| 38 |
p = PowerBar.new |
| 39 |
j = 0 |
| 40 |
(0..total).step(step).each do |i| |
| 41 |
woah = sprintf('%-26s', text[j%text.length..j%text.length+25]) |
| 42 |
p.show({:msg => woah, :done => i, :total => total}) |
| 43 |
j += 1 |
| 44 |
sleep 0.1 |
| 45 |
end |
| 46 |
p.close |
| 47 |
end |
| 48 |
|
| 49 |
# DEMO 3 |
| 50 |
def demo_3 |
| 51 |
total = 100000 |
| 52 |
step = 1000 |
| 53 |
text = "DEMO 3 - Don't sweat your message width too much, we can squeeze it some" |
| 54 |
p = PowerBar.new |
| 55 |
j = 0 |
| 56 |
(0..total).step(step).each do |i| |
| 57 |
woah = text[0..j] |
| 58 |
p.show({:msg => woah, :done => i, :total => total}) |
| 59 |
j += 1 |
| 60 |
sleep 0.1 |
| 61 |
end |
| 62 |
p.close |
| 63 |
end |
| 64 |
|
| 65 |
# DEMO 4 |
| 66 |
def demo_4 |
| 67 |
total = 100000 |
| 68 |
step = 1000 |
| 69 |
text = "\e[0mDEMO 4 - Colors!" |
| 70 |
interstitial = [ |
| 71 |
"Oh, btw, know what?\n", |
| 72 |
"You may use PowerBar#print to print messages while the bar is running.\n", |
| 73 |
"Awesome, right?\n" |
| 74 |
] |
| 75 |
p = PowerBar.new |
| 76 |
p.settings.tty.finite.template.main = \ |
| 77 |
"${<msg>} ${<bar> }\e[0m${<rate>/s} \e[33;1m${<percent>%} " + |
| 78 |
"\e[36;1m${<elapsed>}\e[31;1m${ ETA: <eta>}" |
| 79 |
p.settings.tty.finite.template.padchar = "\e[30;1m#{p.settings.tty.finite.template.padchar}" |
| 80 |
p.settings.tty.finite.template.barchar = "\e[34;1m#{p.settings.tty.finite.template.barchar}" |
| 81 |
p.settings.tty.finite.template.exit = "\e[?25h\e[0m" # clean up after us |
| 82 |
p.settings.tty.finite.template.close = "\e[?25h\e[0m\n" # clean up after us |
| 83 |
p.settings.tty.finite.output = Proc.new{ |s| |
| 84 |
# The default output-function truncates our |
| 85 |
# string to enable the "squeezing" as seen in the |
| 86 |
# previous demo. This doesn't mix so well with ANSI-colors, |
| 87 |
# thus if you want to use colors you'll have to make the |
| 88 |
# output-function a little more naive. Like this: |
| 89 |
$stderr.print s |
| 90 |
} |
| 91 |
j = 0 |
| 92 |
(0..total).step(step).each do |i| |
| 93 |
p.show({:msg => text, :done => i, :total => total}) |
| 94 |
j += 1 |
| 95 |
if 0 == j % 25 |
| 96 |
p.print interstitial.shift |
| 97 |
end |
| 98 |
sleep 0.1 |
| 99 |
end |
| 100 |
p.close |
| 101 |
end |
| 102 |
|
| 103 |
# DEMO 5 |
| 104 |
def demo_5 |
| 105 |
total = 100000 |
| 106 |
step = 1000 |
| 107 |
text = "DEMO 5 - When total is :unknown then we only display what we know" |
| 108 |
p = PowerBar.new |
| 109 |
j = 0 |
| 110 |
(0..total).step(step).each do |i| |
| 111 |
p.show({:msg => text, :done => i, :total => :unknown}) |
| 112 |
j += 1 |
| 113 |
sleep 0.1 |
| 114 |
end |
| 115 |
p.close |
| 116 |
end |
| 117 |
|
| 118 |
# DEMO 6 |
| 119 |
def demo_6 |
| 120 |
total = 100000 |
| 121 |
step = 1000 |
| 122 |
text = "DEMO 6 - Still :unknown, now with a different template" |
| 123 |
p = PowerBar.new |
| 124 |
p.settings.tty.infinite.template.main = \ |
| 125 |
'${<msg>} > ${Rate: <rate>/s, }elapsed: ${<elapsed>}' |
| 126 |
j = 0 |
| 127 |
(0..total).step(step).each do |i| |
| 128 |
p.show({:msg => text, :done => i, :total => :unknown}) |
| 129 |
j += 1 |
| 130 |
sleep 0.1 |
| 131 |
end |
| 132 |
p.close |
| 133 |
end |
| 134 |
|
| 135 |
|
| 136 |
# DEMO 7 |
| 137 |
def demo_7 |
| 138 |
total = 100000 |
| 139 |
step = 1000 |
| 140 |
text = "DEMO 7 - Forcing notty mode" |
| 141 |
p = PowerBar.new |
| 142 |
p.settings.force_mode = :notty |
| 143 |
j = 0 |
| 144 |
(0..total).step(step).each do |i| |
| 145 |
p.show({:msg => text, :done => i, :total => total}) |
| 146 |
j += 1 |
| 147 |
if j == 60 |
| 148 |
p.print "Hint: Of course PowerBar#print also works when in :notty-mode\n" |
| 149 |
end |
| 150 |
sleep 0.1 |
| 151 |
end |
| 152 |
p.close |
| 153 |
end |
| 154 |
|
| 155 |
# DEMO 8 |
| 156 |
def demo_8 |
| 157 |
total = 100000 |
| 158 |
step = 1000 |
| 159 |
log = Logger.new(STDOUT) |
| 160 |
text = "DEMO 8 - Forcing notty mode and using a logger" |
| 161 |
p = PowerBar.new |
| 162 |
p.settings.force_mode = :notty |
| 163 |
p.settings.notty.finite.output = Proc.new { |s| |
| 164 |
log.debug(s.chomp) unless s == '' |
| 165 |
} |
| 166 |
j = 0 |
| 167 |
(0..total).step(step).each do |i| |
| 168 |
p.show({:msg => text, :done => i, :total => total}) |
| 169 |
j += 1 |
| 170 |
if j == 60 |
| 171 |
p.print "Hint: Yes, PowerBar#print also works here. ;)\n" |
| 172 |
end |
| 173 |
sleep 0.1 |
| 174 |
end |
| 175 |
p.close |
| 176 |
end |
| 177 |
|
| 178 |
# DEMO 9 |
| 179 |
def demo_9 |
| 180 |
total = 100000 |
| 181 |
step = 1000 |
| 182 |
|
| 183 |
puts "DEMO 9 - No output by PowerBar." |
| 184 |
puts "Here we use it to collect stats silently, and then we roll our own output:\n-" |
| 185 |
p = PowerBar.new |
| 186 |
j = 0 |
| 187 |
(0..total).step(step).each do |i| |
| 188 |
# update() does not output anything, it only updates (who would've guessed!) |
| 189 |
p.update({:done => i, :total => total}) |
| 190 |
j += 1 |
| 191 |
if 0 == j % 50 |
| 192 |
puts "elapsed is: #{p.elapsed}, humanized elapsed is: #{p.h_elapsed}" |
| 193 |
puts "percent is: #{p.percent}, humanized percent is: #{p.h_percent}" |
| 194 |
puts "eta is: #{p.eta}, humanized eta is: #{p.h_eta}" |
| 195 |
puts "done is: #{p.done}, humanized done is: #{p.h_done}" |
| 196 |
puts "total is: #{p.total}, humanized done is: #{p.h_total}" |
| 197 |
puts "total is: #{p.total}, humanized done is: #{p.h_total}" |
| 198 |
puts "bar is: #{p.bar}" |
| 199 |
puts "-" |
| 200 |
end |
| 201 |
sleep 0.1 |
| 202 |
end |
| 203 |
#p.close # no need to close this one |
| 204 |
end |
| 205 |
|
| 206 |
# DEMO 10 |
| 207 |
def demo_10 |
| 208 |
total = 100000 |
| 209 |
step = 1000 |
| 210 |
text = " " * 30 + "DEMO 10 - OMGWTFBBQ!" |
| 211 |
p = PowerBar.new |
| 212 |
p.settings.tty.finite.template.padchar = "\e[30;1m#{p.settings.tty.finite.template.padchar}" |
| 213 |
p.settings.tty.finite.template.barchar = "\e[34;1m#{p.settings.tty.finite.template.barchar}" |
| 214 |
p.settings.tty.finite.output = Proc.new{ |s| $stderr.print s } |
| 215 |
j = 0 |
| 216 |
total = 300000000 |
| 217 |
step = 1000000 |
| 218 |
|
| 219 |
spin = ')|(|' |
| 220 |
spun = ',.oO^`^Oo' |
| 221 |
|
| 222 |
(0..total).step(step).each do |i| |
| 223 |
p.send(:state).scope = nil # omghax, don't try this at home! |
| 224 |
wtf_range = (j % text.length)..((j % text.length) + 25) |
| 225 |
wtf = sprintf('%-26s', text[wtf_range]) |
| 226 |
wtf.gsub!(/./) { |char| rand(2).zero? ? char.downcase : char.upcase } |
| 227 |
p.show( |
| 228 |
:msg => wtf, |
| 229 |
:done => i, |
| 230 |
:total => total, |
| 231 |
:settings => { |
| 232 |
:tty => { |
| 233 |
:finite => { |
| 234 |
:template => { |
| 235 |
:barchar => "\e[36;1m#{spin[j%spin.length..j%spin.length]}", |
| 236 |
:padchar => "\e[0;34m#{spun[j%spun.length..j%spun.length]}", |
| 237 |
:main => "\e[#{rand(1)};3#{rand(7)}m${<msg>}\e[0m "+ |
| 238 |
"\e[0m${<bar> }\e[0m${<rate>/s} "+ |
| 239 |
"\e[33;1m${<percent>%} " + |
| 240 |
"\e[36;1m${<elapsed>}\e[31;1m${ ETA: <eta>}", |
| 241 |
:exit => "\e[?25h\e[0m", |
| 242 |
:close => "\e[?25h\e[0m\n" |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
) |
| 248 |
j += 1 |
| 249 |
sleep 0.1 |
| 250 |
end |
| 251 |
p.wipe |
| 252 |
puts "Thanks for watching! ;-)" |
| 253 |
end |
| 254 |
|
| 255 |
begin |
| 256 |
method("demo_#{ARGV[0]}".to_sym).call |
| 257 |
rescue |
| 258 |
(1..10).each do |i| |
| 259 |
method("demo_#{i}".to_sym).call |
| 260 |
puts "---" |
| 261 |
sleep 1 |
| 262 |
end |
| 263 |
end |