clio 0.0.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. data/CHANGES +66 -0
  2. data/MANIFEST +48 -169
  3. data/README +13 -17
  4. data/RELEASE +8 -0
  5. data/VERSION +1 -0
  6. data/lib/clio/buffer.rb +93 -0
  7. data/lib/clio/commandable.rb +71 -69
  8. data/lib/clio/commandline.rb +429 -230
  9. data/lib/clio/consoleutils.rb +0 -16
  10. data/lib/clio/facets/kernel.rb +13 -0
  11. data/lib/clio/facets/string.rb +25 -0
  12. data/lib/clio/layout.rb +14 -0
  13. data/lib/clio/layout/flow.rb +1 -0
  14. data/lib/clio/layout/line.rb +23 -0
  15. data/lib/clio/layout/list.rb +33 -0
  16. data/lib/clio/layout/split.rb +122 -0
  17. data/lib/clio/layout/stack.rb +17 -0
  18. data/lib/clio/layout/table.rb +46 -0
  19. data/lib/clio/progressbar.rb +189 -210
  20. data/lib/clio/string.rb +116 -109
  21. data/lib/clio/usage.rb +184 -0
  22. data/lib/clio/usage/argument.rb +84 -0
  23. data/lib/clio/usage/command.rb +440 -0
  24. data/lib/clio/usage/interface.rb +122 -0
  25. data/lib/clio/usage/main.rb +165 -0
  26. data/lib/clio/usage/option.rb +251 -0
  27. data/lib/clio/usage/parser.rb +191 -0
  28. data/lib/clio/usage/signature.rb +55 -0
  29. data/meta/abstract +3 -0
  30. data/meta/authors +1 -0
  31. data/meta/created +1 -0
  32. data/meta/homepage +1 -0
  33. data/meta/license +1 -0
  34. data/meta/repository +1 -0
  35. data/meta/summary +1 -0
  36. data/spec/commandline/autousage.rd +56 -0
  37. data/spec/commandline/bracket.rd +64 -0
  38. data/spec/commandline/completion.rd +38 -0
  39. data/spec/commandline/define.rd +44 -0
  40. data/spec/commandline/method.rd +60 -0
  41. data/spec/commandline/parse.rd +72 -0
  42. data/spec/commandline/scenario.rd +81 -0
  43. data/spec/commandline/subclass.rd +54 -0
  44. data/spec/string/unit.rd +58 -0
  45. data/spec/usage/define.rd +44 -0
  46. data/spec/usage/parse.rd +47 -0
  47. metadata +65 -196
  48. data/HISTORY +0 -11
  49. data/METADATA +0 -18
  50. data/NEWS +0 -10
  51. data/admin/config/reap.yaml +0 -30
  52. data/admin/depot/commandline.rb +0 -219
  53. data/admin/depot/multicommand.rb +0 -403
  54. data/admin/depot/test_multicommand.rb +0 -40
  55. data/admin/log/notes.xml +0 -28
  56. data/admin/log/stats.html +0 -25
  57. data/admin/log/syntax.log +0 -0
  58. data/admin/log/testunit.log +0 -16
  59. data/admin/pack/clio-0.0.1.gem +0 -0
  60. data/admin/share/reap/example.rb +0 -7
  61. data/admin/temps/lib/clio/about.rb.erb +0 -4
  62. data/lib/clio/command.rb +0 -296
  63. data/lib/clio/option.rb +0 -36
  64. data/lib/clio/runmode.rb +0 -126
  65. data/test/test_command.rb +0 -42
  66. data/test/test_commandline.rb +0 -83
  67. data/vendor/Console/Console.cpp +0 -1203
  68. data/vendor/Console/Console.rdoc +0 -690
  69. data/vendor/Console/Console_ANSI.rdoc +0 -302
  70. data/vendor/Console/HISTORY.txt +0 -7
  71. data/vendor/Console/INSTALL.txt +0 -18
  72. data/vendor/Console/Makefile +0 -162
  73. data/vendor/Console/README.txt +0 -26
  74. data/vendor/Console/doc/classes/Win32.html +0 -115
  75. data/vendor/Console/doc/classes/Win32/Console.html +0 -650
  76. data/vendor/Console/doc/classes/Win32/Console.src/M000001.html +0 -31
  77. data/vendor/Console/doc/classes/Win32/Console.src/M000002.html +0 -23
  78. data/vendor/Console/doc/classes/Win32/Console.src/M000003.html +0 -23
  79. data/vendor/Console/doc/classes/Win32/Console.src/M000004.html +0 -27
  80. data/vendor/Console/doc/classes/Win32/Console.src/M000005.html +0 -23
  81. data/vendor/Console/doc/classes/Win32/Console.src/M000006.html +0 -28
  82. data/vendor/Console/doc/classes/Win32/Console.src/M000007.html +0 -23
  83. data/vendor/Console/doc/classes/Win32/Console.src/M000008.html +0 -24
  84. data/vendor/Console/doc/classes/Win32/Console.src/M000009.html +0 -44
  85. data/vendor/Console/doc/classes/Win32/Console.src/M000010.html +0 -23
  86. data/vendor/Console/doc/classes/Win32/Console.src/M000011.html +0 -33
  87. data/vendor/Console/doc/classes/Win32/Console.src/M000012.html +0 -26
  88. data/vendor/Console/doc/classes/Win32/Console.src/M000013.html +0 -27
  89. data/vendor/Console/doc/classes/Win32/Console.src/M000014.html +0 -28
  90. data/vendor/Console/doc/classes/Win32/Console.src/M000015.html +0 -23
  91. data/vendor/Console/doc/classes/Win32/Console.src/M000016.html +0 -23
  92. data/vendor/Console/doc/classes/Win32/Console.src/M000017.html +0 -23
  93. data/vendor/Console/doc/classes/Win32/Console.src/M000018.html +0 -29
  94. data/vendor/Console/doc/classes/Win32/Console.src/M000019.html +0 -23
  95. data/vendor/Console/doc/classes/Win32/Console.src/M000020.html +0 -23
  96. data/vendor/Console/doc/classes/Win32/Console.src/M000021.html +0 -28
  97. data/vendor/Console/doc/classes/Win32/Console.src/M000022.html +0 -23
  98. data/vendor/Console/doc/classes/Win32/Console.src/M000023.html +0 -28
  99. data/vendor/Console/doc/classes/Win32/Console.src/M000024.html +0 -35
  100. data/vendor/Console/doc/classes/Win32/Console.src/M000025.html +0 -28
  101. data/vendor/Console/doc/classes/Win32/Console.src/M000026.html +0 -28
  102. data/vendor/Console/doc/classes/Win32/Console.src/M000027.html +0 -28
  103. data/vendor/Console/doc/classes/Win32/Console.src/M000028.html +0 -31
  104. data/vendor/Console/doc/classes/Win32/Console.src/M000029.html +0 -23
  105. data/vendor/Console/doc/classes/Win32/Console.src/M000030.html +0 -23
  106. data/vendor/Console/doc/classes/Win32/Console.src/M000031.html +0 -23
  107. data/vendor/Console/doc/classes/Win32/Console.src/M000032.html +0 -27
  108. data/vendor/Console/doc/classes/Win32/Console.src/M000033.html +0 -27
  109. data/vendor/Console/doc/classes/Win32/Console.src/M000034.html +0 -25
  110. data/vendor/Console/doc/classes/Win32/Console/ANSI.html +0 -103
  111. data/vendor/Console/doc/classes/Win32/Console/ANSI/IO.html +0 -220
  112. data/vendor/Console/doc/classes/Win32/Console/ANSI/IO.src/M000035.html +0 -32
  113. data/vendor/Console/doc/classes/Win32/Console/ANSI/IO.src/M000036.html +0 -205
  114. data/vendor/Console/doc/classes/Win32/Console/ANSI/IO.src/M000037.html +0 -40
  115. data/vendor/Console/doc/classes/Win32/Console/ANSI/IO.src/M000038.html +0 -25
  116. data/vendor/Console/doc/classes/Win32/Console/API.html +0 -758
  117. data/vendor/Console/doc/classes/Win32/Console/API.src/M000039.html +0 -27
  118. data/vendor/Console/doc/classes/Win32/Console/API.src/M000040.html +0 -27
  119. data/vendor/Console/doc/classes/Win32/Console/API.src/M000041.html +0 -27
  120. data/vendor/Console/doc/classes/Win32/Console/API.src/M000042.html +0 -32
  121. data/vendor/Console/doc/classes/Win32/Console/API.src/M000043.html +0 -32
  122. data/vendor/Console/doc/classes/Win32/Console/API.src/M000044.html +0 -28
  123. data/vendor/Console/doc/classes/Win32/Console/API.src/M000045.html +0 -26
  124. data/vendor/Console/doc/classes/Win32/Console/API.src/M000046.html +0 -26
  125. data/vendor/Console/doc/classes/Win32/Console/API.src/M000047.html +0 -27
  126. data/vendor/Console/doc/classes/Win32/Console/API.src/M000048.html +0 -30
  127. data/vendor/Console/doc/classes/Win32/Console/API.src/M000049.html +0 -29
  128. data/vendor/Console/doc/classes/Win32/Console/API.src/M000050.html +0 -27
  129. data/vendor/Console/doc/classes/Win32/Console/API.src/M000051.html +0 -28
  130. data/vendor/Console/doc/classes/Win32/Console/API.src/M000052.html +0 -30
  131. data/vendor/Console/doc/classes/Win32/Console/API.src/M000053.html +0 -27
  132. data/vendor/Console/doc/classes/Win32/Console/API.src/M000054.html +0 -29
  133. data/vendor/Console/doc/classes/Win32/Console/API.src/M000055.html +0 -29
  134. data/vendor/Console/doc/classes/Win32/Console/API.src/M000056.html +0 -28
  135. data/vendor/Console/doc/classes/Win32/Console/API.src/M000057.html +0 -27
  136. data/vendor/Console/doc/classes/Win32/Console/API.src/M000058.html +0 -47
  137. data/vendor/Console/doc/classes/Win32/Console/API.src/M000059.html +0 -32
  138. data/vendor/Console/doc/classes/Win32/Console/API.src/M000060.html +0 -47
  139. data/vendor/Console/doc/classes/Win32/Console/API.src/M000061.html +0 -34
  140. data/vendor/Console/doc/classes/Win32/Console/API.src/M000062.html +0 -32
  141. data/vendor/Console/doc/classes/Win32/Console/API.src/M000063.html +0 -32
  142. data/vendor/Console/doc/classes/Win32/Console/API.src/M000064.html +0 -35
  143. data/vendor/Console/doc/classes/Win32/Console/API.src/M000065.html +0 -26
  144. data/vendor/Console/doc/classes/Win32/Console/API.src/M000066.html +0 -27
  145. data/vendor/Console/doc/classes/Win32/Console/API.src/M000067.html +0 -29
  146. data/vendor/Console/doc/classes/Win32/Console/API.src/M000068.html +0 -27
  147. data/vendor/Console/doc/classes/Win32/Console/API.src/M000069.html +0 -27
  148. data/vendor/Console/doc/classes/Win32/Console/API.src/M000070.html +0 -28
  149. data/vendor/Console/doc/classes/Win32/Console/API.src/M000071.html +0 -27
  150. data/vendor/Console/doc/classes/Win32/Console/API.src/M000072.html +0 -26
  151. data/vendor/Console/doc/classes/Win32/Console/API.src/M000073.html +0 -27
  152. data/vendor/Console/doc/classes/Win32/Console/API.src/M000074.html +0 -31
  153. data/vendor/Console/doc/classes/Win32/Console/API.src/M000075.html +0 -27
  154. data/vendor/Console/doc/classes/Win32/Console/API.src/M000076.html +0 -32
  155. data/vendor/Console/doc/classes/Win32/Console/API.src/M000077.html +0 -27
  156. data/vendor/Console/doc/classes/Win32/Console/API.src/M000078.html +0 -32
  157. data/vendor/Console/doc/classes/Win32/Console/API.src/M000079.html +0 -32
  158. data/vendor/Console/doc/classes/Win32/Console/API.src/M000080.html +0 -32
  159. data/vendor/Console/doc/classes/Win32/Console/Constants.html +0 -360
  160. data/vendor/Console/doc/created.rid +0 -1
  161. data/vendor/Console/doc/files/Console_ANSI_rdoc.html +0 -407
  162. data/vendor/Console/doc/files/Console_cpp.html +0 -104
  163. data/vendor/Console/doc/files/Console_rdoc.html +0 -964
  164. data/vendor/Console/doc/files/lib/Win32/Console/ANSI_rb.html +0 -123
  165. data/vendor/Console/doc/files/lib/Win32/Console_rb.html +0 -297
  166. data/vendor/Console/doc/fr_class_index.html +0 -32
  167. data/vendor/Console/doc/fr_file_index.html +0 -31
  168. data/vendor/Console/doc/fr_method_index.html +0 -106
  169. data/vendor/Console/doc/index.html +0 -24
  170. data/vendor/Console/doc/rdoc-style.css +0 -172
  171. data/vendor/Console/extconf.rb +0 -18
  172. data/vendor/Console/lib/Term/ansicolor.rb +0 -76
  173. data/vendor/Console/lib/Win32/Console.rb +0 -970
  174. data/vendor/Console/lib/Win32/Console/ANSI.rb +0 -305
  175. data/vendor/Console/test/test_cursor.rb +0 -9
  176. data/vendor/Console/test/test_mouse.rb +0 -6
  177. data/vendor/Console/test/test_readinput.rb +0 -62
  178. data/vendor/Console/test/test_readoutput.rb +0 -52
  179. data/vendor/Console/test/test_sendevent.rb +0 -17
  180. data/vendor/Console/test/test_title.rb +0 -14
  181. data/vendor/Console/test/test_write.rb +0 -36
@@ -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,13 @@
1
+ module Kernel
2
+
3
+ # Anything that can be marshaled can be copied in totality.
4
+ # This is also commonly called a deep_copy.
5
+ #
6
+ # "ABC".copy #=> "ABC"
7
+ #
8
+ def deep_copy
9
+ Marshal::load(Marshal::dump(self))
10
+ end
11
+
12
+ end
13
+
@@ -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
@@ -0,0 +1,14 @@
1
+ require 'clio/consoleutils'
2
+
3
+ module Clio
4
+
5
+ class Layout
6
+
7
+ def screen_width
8
+ @screen_width ||= Terminal.screen_width
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+
@@ -0,0 +1 @@
1
+ require 'clio/layout'
@@ -0,0 +1,23 @@
1
+ module Clio
2
+
3
+ class Layout
4
+
5
+ # Draws a horizonal line.
6
+ class Line < Layout
7
+
8
+ attr :fill
9
+
10
+ def initialize(fill='-')
11
+ @fill = '-'
12
+ end
13
+
14
+ def to_s
15
+ fill * screen_width
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+
@@ -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,17 @@
1
+ require 'clio/layout'
2
+
3
+ module Clio
4
+
5
+ class Layout
6
+
7
+ class Stack < Layout
8
+
9
+ def initialize(&block)
10
+ instance_eval(&block)
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -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
+
@@ -1,253 +1,232 @@
1
- # = Progressbar
2
- #
3
- # ProgressBar is a text-based progressbar library.
4
- #
5
- # == Usage
6
- #
7
- # pbar = ProgressBar.new( "Demo", 100 )
8
- # 100.times { pbar.inc }
9
- # pbar.finish
10
- #
11
- # == Copying
12
- #
13
- # Copyright (C) 2001 Satoru Takabayashi
14
- #
15
- # Ruby License
16
- #
17
- # This module is free software. You may use, modify, and/or redistribute this
18
- # software under the same terms as Ruby.
19
- #
20
- # This program is distributed in the hope that it will be useful, but WITHOUT
21
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22
- # FOR A PARTICULAR PURPOSE.
23
- #
24
- # == Author
25
- #
26
- # * Satoru Takabayashi
27
-
28
- # = ProgressBar
29
- #
30
- # ProgressBar is a text-based progressbar library.
31
- #
32
- # pbar = ProgressBar.new( "Demo", 100 )
33
- # 100.times { pbar.inc }
34
- # pbar.finish
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
- def bytes
73
- convert_bytes(@current)
74
- end
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
- def format_time(t)
77
- t = t.to_i
78
- sec = t % 60
79
- min = (t / 60) % 60
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
- # ETA stands for Estimated Time of Arrival.
85
- def eta
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
- def elapsed
96
- elapsed = Time.now - @start_time
97
- sprintf("Time: %s", format_time(elapsed))
98
- end
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
- def stat
101
- if @is_finished then elapsed else eta end
102
- end
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
- def stat_for_file_transfer
105
- if @is_finished then
106
- sprintf("%s %s %s", bytes, transfer_rate, elapsed)
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
- def eol
113
- if @is_finished then "\n" else "\r" end
114
- end
83
+ def stat
84
+ if @is_finished then elapsed else eta end
85
+ end
115
86
 
116
- def bar
117
- len = percentage * @bar_length / 100
118
- sprintf("|%s%s|", @bar_mark * len, " " * (@bar_length - len))
119
- end
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
- def percentage
122
- if @total.zero?
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
- def title
130
- @title[0,13] + ":"
131
- end
99
+ def bar
100
+ len = percentage * @bar_length / 100
101
+ sprintf("|%s%s|", @bar_mark * len, " " * (@bar_length - len))
102
+ end
132
103
 
133
- def get_width
134
- # FIXME: I don't know how portable it is.
135
- default_width = 80
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
- default_width
108
+ @current * 100 / @total
144
109
  end
145
- rescue Exception
146
- default_width
147
110
  end
148
- end
149
111
 
150
- def show
151
- arguments = @format_arguments.map{|method| send(method)}
152
- line = sprintf(@format, *arguments)
112
+ def title
113
+ @title[0,13] + ":"
114
+ end
153
115
 
154
- width = get_width
155
- if line.length == width - 1
156
- @out.print(line + eol)
157
- elsif line.length >= width
158
- @bar_length = [@bar_length - (line.length - width + 1), 0].max
159
- if @bar_length == 0 then @out.print(line + eol) else show end
160
- else #line.length < width - 1
161
- @bar_length += width - line.length + 1
162
- show
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
- def show_progress
167
- if @total.zero?
168
- cur_percentage = 100
169
- prev_percentage = 0
170
- else
171
- cur_percentage = (@current * 100 / @total).to_i
172
- prev_percentage = (@previous * 100 / @total).to_i
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
- if cur_percentage > prev_percentage || @is_finished
176
- show
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
- public
181
- def file_transfer_mode
182
- @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
183
- end
163
+ public
164
+ def file_transfer_mode
165
+ @format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
166
+ end
184
167
 
185
- def title=(str)
186
- @title = str
187
- end
168
+ def title=(str)
169
+ @title = str
170
+ end
188
171
 
189
- def bar_mark=(mark)
190
- @bar_mark = String(mark)[0..0]
191
- end
172
+ def bar_mark=(mark)
173
+ @bar_mark = String(mark)[0..0]
174
+ end
192
175
 
193
- def total_overflow=(boolv)
194
- @total_overflow = boolv ? true : false
195
- end
176
+ def total_overflow=(boolv)
177
+ @total_overflow = boolv ? true : false
178
+ end
196
179
 
197
- def format=(format)
198
- @format = format
199
- end
180
+ def format=(format)
181
+ @format = format
182
+ end
200
183
 
201
- def format_arguments=(arguments)
202
- @format_arguments = arguments
203
- end
184
+ def format_arguments=(arguments)
185
+ @format_arguments = arguments
186
+ end
204
187
 
205
- def finish
206
- @current = @total
207
- @is_finished = true
208
- show_progress
209
- end
188
+ def finish
189
+ @current = @total
190
+ @is_finished = true
191
+ show_progress
192
+ end
210
193
 
211
- def flush
212
- @out.flush
213
- end
194
+ def flush
195
+ @out.flush
196
+ end
214
197
 
215
- def halt
216
- @is_finished = true
217
- show_progress
218
- end
198
+ def halt
199
+ @is_finished = true
200
+ show_progress
201
+ end
219
202
 
220
- def set(count)
221
- if count < 0
222
- raise "invalid count less than zero: #{count}"
223
- elsif count > @total
224
- if @total_overflow
225
- @total = count + 1
226
- else
227
- raise "invalid count greater than total: #{count}"
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
- def inspect
243
- "(ProgressBar: #{@current}/#{@total})"
244
- end
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
- end
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