clio 0.0.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +66 -0
- data/MANIFEST +48 -169
- data/README +13 -17
- data/RELEASE +8 -0
- data/VERSION +1 -0
- data/lib/clio/buffer.rb +93 -0
- data/lib/clio/commandable.rb +71 -69
- data/lib/clio/commandline.rb +429 -230
- data/lib/clio/consoleutils.rb +0 -16
- data/lib/clio/facets/kernel.rb +13 -0
- data/lib/clio/facets/string.rb +25 -0
- data/lib/clio/layout.rb +14 -0
- data/lib/clio/layout/flow.rb +1 -0
- data/lib/clio/layout/line.rb +23 -0
- data/lib/clio/layout/list.rb +33 -0
- data/lib/clio/layout/split.rb +122 -0
- data/lib/clio/layout/stack.rb +17 -0
- data/lib/clio/layout/table.rb +46 -0
- data/lib/clio/progressbar.rb +189 -210
- data/lib/clio/string.rb +116 -109
- data/lib/clio/usage.rb +184 -0
- data/lib/clio/usage/argument.rb +84 -0
- data/lib/clio/usage/command.rb +440 -0
- data/lib/clio/usage/interface.rb +122 -0
- data/lib/clio/usage/main.rb +165 -0
- data/lib/clio/usage/option.rb +251 -0
- data/lib/clio/usage/parser.rb +191 -0
- data/lib/clio/usage/signature.rb +55 -0
- data/meta/abstract +3 -0
- data/meta/authors +1 -0
- data/meta/created +1 -0
- data/meta/homepage +1 -0
- data/meta/license +1 -0
- data/meta/repository +1 -0
- data/meta/summary +1 -0
- data/spec/commandline/autousage.rd +56 -0
- data/spec/commandline/bracket.rd +64 -0
- data/spec/commandline/completion.rd +38 -0
- data/spec/commandline/define.rd +44 -0
- data/spec/commandline/method.rd +60 -0
- data/spec/commandline/parse.rd +72 -0
- data/spec/commandline/scenario.rd +81 -0
- data/spec/commandline/subclass.rd +54 -0
- data/spec/string/unit.rd +58 -0
- data/spec/usage/define.rd +44 -0
- data/spec/usage/parse.rd +47 -0
- metadata +65 -196
- data/HISTORY +0 -11
- data/METADATA +0 -18
- data/NEWS +0 -10
- data/admin/config/reap.yaml +0 -30
- data/admin/depot/commandline.rb +0 -219
- data/admin/depot/multicommand.rb +0 -403
- data/admin/depot/test_multicommand.rb +0 -40
- data/admin/log/notes.xml +0 -28
- data/admin/log/stats.html +0 -25
- data/admin/log/syntax.log +0 -0
- data/admin/log/testunit.log +0 -16
- data/admin/pack/clio-0.0.1.gem +0 -0
- data/admin/share/reap/example.rb +0 -7
- data/admin/temps/lib/clio/about.rb.erb +0 -4
- data/lib/clio/command.rb +0 -296
- data/lib/clio/option.rb +0 -36
- data/lib/clio/runmode.rb +0 -126
- data/test/test_command.rb +0 -42
- data/test/test_commandline.rb +0 -83
- data/vendor/Console/Console.cpp +0 -1203
- data/vendor/Console/Console.rdoc +0 -690
- data/vendor/Console/Console_ANSI.rdoc +0 -302
- data/vendor/Console/HISTORY.txt +0 -7
- data/vendor/Console/INSTALL.txt +0 -18
- data/vendor/Console/Makefile +0 -162
- data/vendor/Console/README.txt +0 -26
- data/vendor/Console/doc/classes/Win32.html +0 -115
- data/vendor/Console/doc/classes/Win32/Console.html +0 -650
- data/vendor/Console/doc/classes/Win32/Console.src/M000001.html +0 -31
- data/vendor/Console/doc/classes/Win32/Console.src/M000002.html +0 -23
- data/vendor/Console/doc/classes/Win32/Console.src/M000003.html +0 -23
- data/vendor/Console/doc/classes/Win32/Console.src/M000004.html +0 -27
- data/vendor/Console/doc/classes/Win32/Console.src/M000005.html +0 -23
- data/vendor/Console/doc/classes/Win32/Console.src/M000006.html +0 -28
- data/vendor/Console/doc/classes/Win32/Console.src/M000007.html +0 -23
- data/vendor/Console/doc/classes/Win32/Console.src/M000008.html +0 -24
- data/vendor/Console/doc/classes/Win32/Console.src/M000009.html +0 -44
- data/vendor/Console/doc/classes/Win32/Console.src/M000010.html +0 -23
- data/vendor/Console/doc/classes/Win32/Console.src/M000011.html +0 -33
- data/vendor/Console/doc/classes/Win32/Console.src/M000012.html +0 -26
- data/vendor/Console/doc/classes/Win32/Console.src/M000013.html +0 -27
- data/vendor/Console/doc/classes/Win32/Console.src/M000014.html +0 -28
- data/vendor/Console/doc/classes/Win32/Console.src/M000015.html +0 -23
- data/vendor/Console/doc/classes/Win32/Console.src/M000016.html +0 -23
- data/vendor/Console/doc/classes/Win32/Console.src/M000017.html +0 -23
- data/vendor/Console/doc/classes/Win32/Console.src/M000018.html +0 -29
- data/vendor/Console/doc/classes/Win32/Console.src/M000019.html +0 -23
- data/vendor/Console/doc/classes/Win32/Console.src/M000020.html +0 -23
- data/vendor/Console/doc/classes/Win32/Console.src/M000021.html +0 -28
- data/vendor/Console/doc/classes/Win32/Console.src/M000022.html +0 -23
- data/vendor/Console/doc/classes/Win32/Console.src/M000023.html +0 -28
- data/vendor/Console/doc/classes/Win32/Console.src/M000024.html +0 -35
- data/vendor/Console/doc/classes/Win32/Console.src/M000025.html +0 -28
- data/vendor/Console/doc/classes/Win32/Console.src/M000026.html +0 -28
- data/vendor/Console/doc/classes/Win32/Console.src/M000027.html +0 -28
- data/vendor/Console/doc/classes/Win32/Console.src/M000028.html +0 -31
- data/vendor/Console/doc/classes/Win32/Console.src/M000029.html +0 -23
- data/vendor/Console/doc/classes/Win32/Console.src/M000030.html +0 -23
- data/vendor/Console/doc/classes/Win32/Console.src/M000031.html +0 -23
- data/vendor/Console/doc/classes/Win32/Console.src/M000032.html +0 -27
- data/vendor/Console/doc/classes/Win32/Console.src/M000033.html +0 -27
- data/vendor/Console/doc/classes/Win32/Console.src/M000034.html +0 -25
- data/vendor/Console/doc/classes/Win32/Console/ANSI.html +0 -103
- data/vendor/Console/doc/classes/Win32/Console/ANSI/IO.html +0 -220
- data/vendor/Console/doc/classes/Win32/Console/ANSI/IO.src/M000035.html +0 -32
- data/vendor/Console/doc/classes/Win32/Console/ANSI/IO.src/M000036.html +0 -205
- data/vendor/Console/doc/classes/Win32/Console/ANSI/IO.src/M000037.html +0 -40
- data/vendor/Console/doc/classes/Win32/Console/ANSI/IO.src/M000038.html +0 -25
- data/vendor/Console/doc/classes/Win32/Console/API.html +0 -758
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000039.html +0 -27
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000040.html +0 -27
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000041.html +0 -27
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000042.html +0 -32
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000043.html +0 -32
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000044.html +0 -28
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000045.html +0 -26
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000046.html +0 -26
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000047.html +0 -27
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000048.html +0 -30
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000049.html +0 -29
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000050.html +0 -27
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000051.html +0 -28
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000052.html +0 -30
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000053.html +0 -27
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000054.html +0 -29
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000055.html +0 -29
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000056.html +0 -28
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000057.html +0 -27
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000058.html +0 -47
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000059.html +0 -32
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000060.html +0 -47
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000061.html +0 -34
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000062.html +0 -32
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000063.html +0 -32
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000064.html +0 -35
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000065.html +0 -26
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000066.html +0 -27
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000067.html +0 -29
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000068.html +0 -27
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000069.html +0 -27
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000070.html +0 -28
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000071.html +0 -27
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000072.html +0 -26
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000073.html +0 -27
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000074.html +0 -31
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000075.html +0 -27
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000076.html +0 -32
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000077.html +0 -27
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000078.html +0 -32
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000079.html +0 -32
- data/vendor/Console/doc/classes/Win32/Console/API.src/M000080.html +0 -32
- data/vendor/Console/doc/classes/Win32/Console/Constants.html +0 -360
- data/vendor/Console/doc/created.rid +0 -1
- data/vendor/Console/doc/files/Console_ANSI_rdoc.html +0 -407
- data/vendor/Console/doc/files/Console_cpp.html +0 -104
- data/vendor/Console/doc/files/Console_rdoc.html +0 -964
- data/vendor/Console/doc/files/lib/Win32/Console/ANSI_rb.html +0 -123
- data/vendor/Console/doc/files/lib/Win32/Console_rb.html +0 -297
- data/vendor/Console/doc/fr_class_index.html +0 -32
- data/vendor/Console/doc/fr_file_index.html +0 -31
- data/vendor/Console/doc/fr_method_index.html +0 -106
- data/vendor/Console/doc/index.html +0 -24
- data/vendor/Console/doc/rdoc-style.css +0 -172
- data/vendor/Console/extconf.rb +0 -18
- data/vendor/Console/lib/Term/ansicolor.rb +0 -76
- data/vendor/Console/lib/Win32/Console.rb +0 -970
- data/vendor/Console/lib/Win32/Console/ANSI.rb +0 -305
- data/vendor/Console/test/test_cursor.rb +0 -9
- data/vendor/Console/test/test_mouse.rb +0 -6
- data/vendor/Console/test/test_readinput.rb +0 -62
- data/vendor/Console/test/test_readoutput.rb +0 -52
- data/vendor/Console/test/test_sendevent.rb +0 -17
- data/vendor/Console/test/test_title.rb +0 -14
- data/vendor/Console/test/test_write.rb +0 -36
data/lib/clio/consoleutils.rb
CHANGED
@@ -1,19 +1,3 @@
|
|
1
|
-
# = Console Utilities
|
2
|
-
#
|
3
|
-
# ConsoleUtils provides methods that are
|
4
|
-
# generally useful in the context of
|
5
|
-
# creating console output.
|
6
|
-
#
|
7
|
-
# = Authors
|
8
|
-
#
|
9
|
-
# * Thomas Sawyer
|
10
|
-
#
|
11
|
-
# == Copying
|
12
|
-
#
|
13
|
-
# Copyright (c) 2006,2007 Thomas Sawyer
|
14
|
-
# Ruby/GPL
|
15
|
-
|
16
|
-
|
17
1
|
module Clio
|
18
2
|
|
19
3
|
# = Termninal
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class String
|
2
|
+
|
3
|
+
# Indent left or right by n spaces.
|
4
|
+
# (This used to be called #tab and aliased as #indent.)
|
5
|
+
|
6
|
+
def indent(n)
|
7
|
+
if n >= 0
|
8
|
+
gsub(/^/, ' ' * n)
|
9
|
+
else
|
10
|
+
gsub(/^ {0,#{-n}}/, "")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# Preserves relative tabbing.
|
15
|
+
# The first non-empty line ends up with n spaces before nonspace.
|
16
|
+
|
17
|
+
def tabto(n)
|
18
|
+
if self =~ /^( *)\S/
|
19
|
+
indent(n - $1.length)
|
20
|
+
else
|
21
|
+
self
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/lib/clio/layout.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'clio/layout'
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'clio/layout'
|
2
|
+
|
3
|
+
module Clio
|
4
|
+
|
5
|
+
class Layout
|
6
|
+
|
7
|
+
# List of items.
|
8
|
+
class List < Layout
|
9
|
+
|
10
|
+
attr :items
|
11
|
+
|
12
|
+
def initialize(*items)
|
13
|
+
options = Hash===items.last ? items.pop : {}
|
14
|
+
|
15
|
+
@items = items
|
16
|
+
@mark = options[:mark]
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_s
|
20
|
+
s = [""]
|
21
|
+
n = (items.size / 10).to_i + 1
|
22
|
+
items.each_with_index do |item, index|
|
23
|
+
s << "%#{n}s. %s" % [index+1, item]
|
24
|
+
end
|
25
|
+
s << ""
|
26
|
+
s.join("\n")
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'clio/consoleutils'
|
2
|
+
require 'clio/layout'
|
3
|
+
|
4
|
+
module Clio
|
5
|
+
|
6
|
+
class Layout
|
7
|
+
|
8
|
+
# TODO: name LeftRight instead?
|
9
|
+
class Split < Layout
|
10
|
+
|
11
|
+
PAD = ''
|
12
|
+
FILL = ' '
|
13
|
+
RATIO = 0.8
|
14
|
+
|
15
|
+
attr_accessor :left
|
16
|
+
|
17
|
+
attr_accessor :right
|
18
|
+
|
19
|
+
attr_accessor :fill
|
20
|
+
|
21
|
+
attr_accessor :ratio
|
22
|
+
|
23
|
+
attr_accessor :pad
|
24
|
+
|
25
|
+
###
|
26
|
+
def initialize(left, right, options={})
|
27
|
+
@left = left
|
28
|
+
@right = right
|
29
|
+
|
30
|
+
@fill = FILL
|
31
|
+
@ratio = RATIO
|
32
|
+
@pad = PAD
|
33
|
+
|
34
|
+
options.each do |k,v|
|
35
|
+
send("#{k}=",v) if respond_to?("#{k}=")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def print
|
40
|
+
print_justified(@left, @right)
|
41
|
+
end
|
42
|
+
|
43
|
+
def ratio=(value)
|
44
|
+
if value < 0
|
45
|
+
@ratio = 1 + value
|
46
|
+
else
|
47
|
+
@ratio = value
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def fill=(letter)
|
52
|
+
case letter
|
53
|
+
when '', nil
|
54
|
+
letter = ' '
|
55
|
+
else
|
56
|
+
letter = letter[0,1]
|
57
|
+
end
|
58
|
+
@fill = letter
|
59
|
+
end
|
60
|
+
|
61
|
+
def to_s
|
62
|
+
print_justified(left, right)
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
# Print a justified line with left and right entries.
|
68
|
+
#
|
69
|
+
# A fill option can be given to fill in any empty space
|
70
|
+
# between the two. And a ratio option can be given which defaults
|
71
|
+
# to 0.8 (eg. 80/20)
|
72
|
+
#
|
73
|
+
def print_justified(left, rite)
|
74
|
+
left_size = left.size
|
75
|
+
rite_size = rite.size
|
76
|
+
|
77
|
+
left = left.to_s
|
78
|
+
rite = rite.to_s
|
79
|
+
|
80
|
+
width = screen_width
|
81
|
+
|
82
|
+
l = (width * ratio).to_i
|
83
|
+
r = width - l
|
84
|
+
|
85
|
+
left = left[0,l]
|
86
|
+
rite = rite[0,r]
|
87
|
+
|
88
|
+
str = fill * width
|
89
|
+
|
90
|
+
str[0,pad.size] = pad
|
91
|
+
str[pad.size,left_size] = left
|
92
|
+
str[-(pad.size+rite_size), rite_size] = rite
|
93
|
+
str[-pad.size, pad.size] = pad
|
94
|
+
|
95
|
+
Kernel.print(str)
|
96
|
+
end
|
97
|
+
|
98
|
+
###
|
99
|
+
def screen_width
|
100
|
+
@screen_width ||= Terminal.screen_width
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
__END__
|
111
|
+
|
112
|
+
### demo ###
|
113
|
+
|
114
|
+
require 'clio/string2'
|
115
|
+
|
116
|
+
s1 = Clio.string("Left").color(:red)
|
117
|
+
s2 = Clio.string("Right").color(:green)
|
118
|
+
|
119
|
+
s = (s1 | s2)
|
120
|
+
|
121
|
+
s.print
|
122
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'clio/layout'
|
2
|
+
|
3
|
+
module Clio
|
4
|
+
|
5
|
+
class Layout
|
6
|
+
|
7
|
+
# = Table
|
8
|
+
#
|
9
|
+
# Currently the table layout class is very
|
10
|
+
# simplistic. Ultimately it will support
|
11
|
+
# headers, footers, and a varity of border
|
12
|
+
# options.
|
13
|
+
class Table < Layout
|
14
|
+
|
15
|
+
attr :rows
|
16
|
+
|
17
|
+
def initialize(*rows_of_cells, &block)
|
18
|
+
@rows = rows_of_cells
|
19
|
+
end
|
20
|
+
|
21
|
+
def row(*cells, &block)
|
22
|
+
@rows << cells
|
23
|
+
instance_eval(&block)
|
24
|
+
end
|
25
|
+
|
26
|
+
def cell(acell)
|
27
|
+
(@rows.last ||= []) << acell
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_s
|
31
|
+
#screen_width
|
32
|
+
rows.collect{ |cells|
|
33
|
+
cells.join(' ')
|
34
|
+
}.join("\n")
|
35
|
+
end
|
36
|
+
|
37
|
+
###
|
38
|
+
#class Row
|
39
|
+
#end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
data/lib/clio/progressbar.rb
CHANGED
@@ -1,253 +1,232 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
# pbar.
|
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
|
-
class ProgressBar
|
37
|
-
|
38
|
-
def initialize(title, total, out = STDERR)
|
39
|
-
@title = title
|
40
|
-
@total = total
|
41
|
-
@out = out
|
42
|
-
@bar_length = 80
|
43
|
-
@bar_mark = "o"
|
44
|
-
@total_overflow = true
|
45
|
-
@current = 0
|
46
|
-
@previous = 0
|
47
|
-
@is_finished = false
|
48
|
-
@start_time = Time.now
|
49
|
-
@format = "%-14s %3d%% %s %s"
|
50
|
-
@format_arguments = [:title, :percentage, :bar, :stat]
|
51
|
-
show_progress
|
52
|
-
end
|
53
|
-
|
54
|
-
private
|
55
|
-
def convert_bytes (bytes)
|
56
|
-
if bytes < 1024
|
57
|
-
sprintf("%6dB", bytes)
|
58
|
-
elsif bytes < 1024 * 1000 # 1000kb
|
59
|
-
sprintf("%5.1fKB", bytes.to_f / 1024)
|
60
|
-
elsif bytes < 1024 * 1024 * 1000 # 1000mb
|
61
|
-
sprintf("%5.1fMB", bytes.to_f / 1024 / 1024)
|
62
|
-
else
|
63
|
-
sprintf("%5.1fGB", bytes.to_f / 1024 / 1024 / 1024)
|
1
|
+
module Clio
|
2
|
+
|
3
|
+
# = Progressbar
|
4
|
+
#
|
5
|
+
# Progressbar is a console-based progress bar library.
|
6
|
+
#
|
7
|
+
# == Usage
|
8
|
+
#
|
9
|
+
# pbar = ProgressBar.new( "Demo", 100 )
|
10
|
+
# 100.times { pbar.inc }
|
11
|
+
# pbar.finish
|
12
|
+
#
|
13
|
+
# == Copying
|
14
|
+
#
|
15
|
+
# Based-on the original ProgressBar library by Satoru Takabayashi
|
16
|
+
#
|
17
|
+
# Copyright (C) 2001 Satoru Takabayashi
|
18
|
+
#
|
19
|
+
class Progressbar
|
20
|
+
|
21
|
+
def initialize(title, total, out = STDERR)
|
22
|
+
@title = title
|
23
|
+
@total = total
|
24
|
+
@out = out
|
25
|
+
@bar_length = 80
|
26
|
+
@bar_mark = "o"
|
27
|
+
@total_overflow = true
|
28
|
+
@current = 0
|
29
|
+
@previous = 0
|
30
|
+
@is_finished = false
|
31
|
+
@start_time = Time.now
|
32
|
+
@format = "%-14s %3d%% %s %s"
|
33
|
+
@format_arguments = [:title, :percentage, :bar, :stat]
|
34
|
+
show_progress
|
64
35
|
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def transfer_rate
|
68
|
-
bytes_per_second = @current.to_f / (Time.now - @start_time)
|
69
|
-
sprintf("%s/s", convert_bytes(bytes_per_second))
|
70
|
-
end
|
71
36
|
|
72
|
-
|
73
|
-
convert_bytes(
|
74
|
-
|
37
|
+
private
|
38
|
+
def convert_bytes (bytes)
|
39
|
+
if bytes < 1024
|
40
|
+
sprintf("%6dB", bytes)
|
41
|
+
elsif bytes < 1024 * 1000 # 1000kb
|
42
|
+
sprintf("%5.1fKB", bytes.to_f / 1024)
|
43
|
+
elsif bytes < 1024 * 1024 * 1000 # 1000mb
|
44
|
+
sprintf("%5.1fMB", bytes.to_f / 1024 / 1024)
|
45
|
+
else
|
46
|
+
sprintf("%5.1fGB", bytes.to_f / 1024 / 1024 / 1024)
|
47
|
+
end
|
48
|
+
end
|
75
49
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
hour = t / 3600
|
81
|
-
sprintf("%02d:%02d:%02d", hour, min, sec);
|
82
|
-
end
|
50
|
+
def transfer_rate
|
51
|
+
bytes_per_second = @current.to_f / (Time.now - @start_time)
|
52
|
+
sprintf("%s/s", convert_bytes(bytes_per_second))
|
53
|
+
end
|
83
54
|
|
84
|
-
|
85
|
-
|
86
|
-
if @current == 0
|
87
|
-
"ETA: --:--:--"
|
88
|
-
else
|
89
|
-
elapsed = Time.now - @start_time
|
90
|
-
eta = elapsed * @total / @current - elapsed;
|
91
|
-
sprintf("ETA: %s", format_time(eta))
|
55
|
+
def bytes
|
56
|
+
convert_bytes(@current)
|
92
57
|
end
|
93
|
-
end
|
94
58
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
59
|
+
def format_time(t)
|
60
|
+
t = t.to_i
|
61
|
+
sec = t % 60
|
62
|
+
min = (t / 60) % 60
|
63
|
+
hour = t / 3600
|
64
|
+
sprintf("%02d:%02d:%02d", hour, min, sec);
|
65
|
+
end
|
99
66
|
|
100
|
-
|
101
|
-
|
102
|
-
|
67
|
+
# ETA stands for Estimated Time of Arrival.
|
68
|
+
def eta
|
69
|
+
if @current == 0
|
70
|
+
"ETA: --:--:--"
|
71
|
+
else
|
72
|
+
elapsed = Time.now - @start_time
|
73
|
+
eta = elapsed * @total / @current - elapsed;
|
74
|
+
sprintf("ETA: %s", format_time(eta))
|
75
|
+
end
|
76
|
+
end
|
103
77
|
|
104
|
-
|
105
|
-
|
106
|
-
sprintf("
|
107
|
-
else
|
108
|
-
sprintf("%s %s %s", bytes, transfer_rate, eta)
|
78
|
+
def elapsed
|
79
|
+
elapsed = Time.now - @start_time
|
80
|
+
sprintf("Time: %s", format_time(elapsed))
|
109
81
|
end
|
110
|
-
end
|
111
82
|
|
112
|
-
|
113
|
-
|
114
|
-
|
83
|
+
def stat
|
84
|
+
if @is_finished then elapsed else eta end
|
85
|
+
end
|
115
86
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
87
|
+
def stat_for_file_transfer
|
88
|
+
if @is_finished then
|
89
|
+
sprintf("%s %s %s", bytes, transfer_rate, elapsed)
|
90
|
+
else
|
91
|
+
sprintf("%s %s %s", bytes, transfer_rate, eta)
|
92
|
+
end
|
93
|
+
end
|
120
94
|
|
121
|
-
|
122
|
-
|
123
|
-
100
|
124
|
-
else
|
125
|
-
@current * 100 / @total
|
95
|
+
def eol
|
96
|
+
if @is_finished then "\n" else "\r" end
|
126
97
|
end
|
127
|
-
end
|
128
98
|
|
129
|
-
|
130
|
-
|
131
|
-
|
99
|
+
def bar
|
100
|
+
len = percentage * @bar_length / 100
|
101
|
+
sprintf("|%s%s|", @bar_mark * len, " " * (@bar_length - len))
|
102
|
+
end
|
132
103
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
begin
|
137
|
-
tiocgwinsz = 0x5413
|
138
|
-
data = [0, 0, 0, 0].pack("SSSS")
|
139
|
-
if @out.ioctl(tiocgwinsz, data) >= 0 then
|
140
|
-
rows, cols, xpixels, ypixels = data.unpack("SSSS")
|
141
|
-
if cols >= 0 then cols else default_width end
|
104
|
+
def percentage
|
105
|
+
if @total.zero?
|
106
|
+
100
|
142
107
|
else
|
143
|
-
|
108
|
+
@current * 100 / @total
|
144
109
|
end
|
145
|
-
rescue Exception
|
146
|
-
default_width
|
147
110
|
end
|
148
|
-
end
|
149
111
|
|
150
|
-
|
151
|
-
|
152
|
-
|
112
|
+
def title
|
113
|
+
@title[0,13] + ":"
|
114
|
+
end
|
153
115
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
116
|
+
def get_width
|
117
|
+
# FIXME: I don't know how portable it is.
|
118
|
+
default_width = 80
|
119
|
+
begin
|
120
|
+
tiocgwinsz = 0x5413
|
121
|
+
data = [0, 0, 0, 0].pack("SSSS")
|
122
|
+
if @out.ioctl(tiocgwinsz, data) >= 0 then
|
123
|
+
rows, cols, xpixels, ypixels = data.unpack("SSSS")
|
124
|
+
if cols >= 0 then cols else default_width end
|
125
|
+
else
|
126
|
+
default_width
|
127
|
+
end
|
128
|
+
rescue Exception
|
129
|
+
default_width
|
130
|
+
end
|
163
131
|
end
|
164
|
-
end
|
165
132
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
133
|
+
def show
|
134
|
+
arguments = @format_arguments.map{|method| send(method)}
|
135
|
+
line = sprintf(@format, *arguments)
|
136
|
+
|
137
|
+
width = get_width
|
138
|
+
if line.length == width - 1
|
139
|
+
@out.print(line + eol)
|
140
|
+
elsif line.length >= width
|
141
|
+
@bar_length = [@bar_length - (line.length - width + 1), 0].max
|
142
|
+
if @bar_length == 0 then @out.print(line + eol) else show end
|
143
|
+
else #line.length < width - 1
|
144
|
+
@bar_length += width - line.length + 1
|
145
|
+
show
|
146
|
+
end
|
173
147
|
end
|
174
148
|
|
175
|
-
|
176
|
-
|
149
|
+
def show_progress
|
150
|
+
if @total.zero?
|
151
|
+
cur_percentage = 100
|
152
|
+
prev_percentage = 0
|
153
|
+
else
|
154
|
+
cur_percentage = (@current * 100 / @total).to_i
|
155
|
+
prev_percentage = (@previous * 100 / @total).to_i
|
156
|
+
end
|
157
|
+
|
158
|
+
if cur_percentage > prev_percentage || @is_finished
|
159
|
+
show
|
160
|
+
end
|
177
161
|
end
|
178
|
-
end
|
179
162
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
163
|
+
public
|
164
|
+
def file_transfer_mode
|
165
|
+
@format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
|
166
|
+
end
|
184
167
|
|
185
|
-
|
186
|
-
|
187
|
-
|
168
|
+
def title=(str)
|
169
|
+
@title = str
|
170
|
+
end
|
188
171
|
|
189
|
-
|
190
|
-
|
191
|
-
|
172
|
+
def bar_mark=(mark)
|
173
|
+
@bar_mark = String(mark)[0..0]
|
174
|
+
end
|
192
175
|
|
193
|
-
|
194
|
-
|
195
|
-
|
176
|
+
def total_overflow=(boolv)
|
177
|
+
@total_overflow = boolv ? true : false
|
178
|
+
end
|
196
179
|
|
197
|
-
|
198
|
-
|
199
|
-
|
180
|
+
def format=(format)
|
181
|
+
@format = format
|
182
|
+
end
|
200
183
|
|
201
|
-
|
202
|
-
|
203
|
-
|
184
|
+
def format_arguments=(arguments)
|
185
|
+
@format_arguments = arguments
|
186
|
+
end
|
204
187
|
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
188
|
+
def finish
|
189
|
+
@current = @total
|
190
|
+
@is_finished = true
|
191
|
+
show_progress
|
192
|
+
end
|
210
193
|
|
211
|
-
|
212
|
-
|
213
|
-
|
194
|
+
def flush
|
195
|
+
@out.flush
|
196
|
+
end
|
214
197
|
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
198
|
+
def halt
|
199
|
+
@is_finished = true
|
200
|
+
show_progress
|
201
|
+
end
|
219
202
|
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
203
|
+
def set(count)
|
204
|
+
if count < 0
|
205
|
+
raise "invalid count less than zero: #{count}"
|
206
|
+
elsif count > @total
|
207
|
+
if @total_overflow
|
208
|
+
@total = count + 1
|
209
|
+
else
|
210
|
+
raise "invalid count greater than total: #{count}"
|
211
|
+
end
|
228
212
|
end
|
213
|
+
@current = count
|
214
|
+
show_progress
|
215
|
+
@previous = @current
|
229
216
|
end
|
230
|
-
@current = count
|
231
|
-
show_progress
|
232
|
-
@previous = @current
|
233
|
-
end
|
234
|
-
|
235
|
-
def inc(step = 1)
|
236
|
-
@current += step
|
237
|
-
@current = @total if @current > @total
|
238
|
-
show_progress
|
239
|
-
@previous = @current
|
240
|
-
end
|
241
217
|
|
242
|
-
|
243
|
-
|
244
|
-
|
218
|
+
def inc(step = 1)
|
219
|
+
@current += step
|
220
|
+
@current = @total if @current > @total
|
221
|
+
show_progress
|
222
|
+
@previous = @current
|
223
|
+
end
|
245
224
|
|
246
|
-
|
225
|
+
def inspect
|
226
|
+
"(ProgressBar: #{@current}/#{@total})"
|
227
|
+
end
|
247
228
|
|
229
|
+
end
|
248
230
|
|
249
|
-
module Console #:nodoc:
|
250
|
-
# For backward compatibility
|
251
|
-
ProgressBar = ::ProgressBar
|
252
231
|
end
|
253
232
|
|