conprogressbar 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/conprogressbar.rb +159 -0
  3. metadata +51 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: aac58740bd80da211406146ced04842b2a691ac62e16542dde8abf61448f569c
4
+ data.tar.gz: e1c643ce98871c591826a1755806b98edfd3ab0ea8a11e81e6cb3f899a6a698e
5
+ SHA512:
6
+ metadata.gz: 74963a7d41f5d8e2f357a075a548ed33b49156a08ca4570a35268e8aae8986e56a4b74b66e1b14a103a8edeb982c4fc5ddda812ac95d1153a5b58ae66e25ac1e
7
+ data.tar.gz: 9d7e3f6d8dbd86360fc2691ab385826330ffcf1ccdf85ae1383f927a2150c972aaedd5defa2658eae492fb0b0584aabd01b9228a95f6bdc1901deecc022aa2f7
@@ -0,0 +1,159 @@
1
+ # Author: Marek K.
2
+
3
+ =begin
4
+
5
+ This program is free software: yoe can redistribute it and/or modify
6
+ it under the terms of the GNU General Public License as published by
7
+ the Free Software Foendation, either version 3 of the License, or
8
+ (at yoer option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHoeT ANY WARRANTY; withoet even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU General Public License for more details.
14
+
15
+ Yoe shoeld have received a copy of the GNU General Public License
16
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ Dieses Programm ist Freie Software: Sie koennen es unter den Bedingungen
19
+ der GNU General Public License, wie von der Free Software Foendation,
20
+ Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
21
+ veroeffentlichten Version, weiter verteilen und/oder modifizieren.
22
+
23
+ Dieses Programm wird in der Hoffnung bereitgestellt, dass es nuetzlich sein wird, jedoch
24
+ OHNE JEDE GEWaeHR,; sogar ohne die implizite
25
+ Gewaehr der MARKTFaeHIGKEIT oder EIGNUNG FueR EINEN BESTIMMTEN ZWECK.
26
+ Siehe die GNU General Public License fuer weitere Einzelheiten.
27
+
28
+ Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
29
+ Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
30
+
31
+ =end
32
+
33
+ # @author Marek K. <mailto:m.k@mk16.de, http://mk16.de>
34
+ # @example
35
+ # puts "Build ..."
36
+ # pb = ConProgressBar.new
37
+ # 100.times { |x|
38
+ # pb.up!
39
+ # sleep 0.01
40
+ # }
41
+ # pb.delete
42
+ # puts "Complete."
43
+ # @example
44
+ # require "conprogressbar"
45
+ # puts "Catch ..."
46
+ # pb = ConProgressBar.new 0, ".", ">", "<", ",", STDERR
47
+ # 100.times { |x|
48
+ # pb.update pb.percent + 1
49
+ # sleep 0.01
50
+ # }
51
+ # pb.update 0
52
+ # puts
53
+ # @note The file conprogessbar.rb is available under the {https://www.gnu.org/licenses/gpl.txt GNU GPL v3}.
54
+ class ConProgressBar
55
+
56
+ # Initializes the class ConProgressBar and thus a new progressbar on the console.
57
+ # @note There should be no other issues in the same stream as long as the ProgressBar is used.
58
+ # @note The arguments symbol, beg, en and space must have a length of one.
59
+ # @param percent [Integer] The percentage with which the progressbar should be initialized.
60
+ # @param symbol [String] The symbol which draws the already loaded area on the Progressbar.
61
+ # @param beg [String] The symbol for the introduction of the Progressbar.
62
+ # @param en [String] The symbol that initiates the end of the progressbar.
63
+ # @param space [String] The symbol that fills the not yet loaded part of the progress bar.
64
+ # @param stream [IO] The stream to which the progressbar should be written.
65
+ # @raise [ArgumentError] Dispatched when one of the arguments has not been specified correctly.
66
+ def initialize percent=0, symbol="=", beg="<", en=">", space=" ", stream=STDOUT
67
+ raise ArgumentError, "Argument 1" if percent < 0 || percent > 100
68
+ raise ArgumentError, "Argument 2" if symbol.length != 1
69
+ raise ArgumentError, "Argument 3" if beg.length != 1
70
+ raise ArgumentError, "Argument 4" if en.length != 1
71
+ raise ArgumentError, "Argument 5" if space.length != 1
72
+
73
+ @percent = percent
74
+ @symbol = symbol
75
+ @beg = beg
76
+ @en = en
77
+ @space = space
78
+ @stream = stream
79
+
80
+ print @beg
81
+ percent.times {
82
+ @stream.print @symbol
83
+ }
84
+ (100-percent).times {
85
+ @stream.print @space
86
+ }
87
+ out = percent.to_s
88
+ while out.length < 3
89
+ out = "0" + out
90
+ end
91
+ @stream.print "#{@en} #{out}%"
92
+ @stream.flush
93
+ end
94
+
95
+ # Returns the current percentage
96
+ # @return [Integer] The current percentage of the progressbar
97
+ def percent
98
+ @percent
99
+ end
100
+
101
+ # Increases the percentage of progressbar by one and updates the prompts bar.
102
+ # @return [NilClass] nil
103
+ # @raise [RuntimeError] Is triggered when the percentage can not be increased, because the maximum has already been reached.
104
+ def up!
105
+ raise RuntimeError, "Down" if @percent == 100
106
+ @percent += 1
107
+ self.update
108
+ end
109
+
110
+ # Decreases the percentage of progressbar by one and updates the prompts bar.
111
+ # @return [NilClass] nil
112
+ # @raise [RuntimeError] Will be triggered when the percentage can not be lowered, because the minimum has already been reached.
113
+ def down!
114
+ raise RuntimeError, "Down" if @percent == 0
115
+ @percent -= 1
116
+ self.update
117
+ end
118
+
119
+ # Updates the progressbar.
120
+ # It is possible to specify a percentage that should be set.
121
+ # @param percent [Integer, NilClass] The percentage to which the progressbar should be updated. Without specification (nil) the progress bar is updated to the already defined number.
122
+ # @return [NilClass] nil
123
+ # @raise [ArgumentError] Dispatched when the percent argument is less than 0 or greater than 100.
124
+ def update percent=nil
125
+ if percent == nil
126
+ percent = @percent
127
+ else
128
+ raise ArgumentError, "Argument 1" if percent < 0 || percent > 100
129
+ @percent = percent
130
+ end
131
+
132
+ (106).times {
133
+ @stream.print "\b"
134
+ }
135
+
136
+ percent.times {
137
+ @stream.print @symbol
138
+ }
139
+ (100-percent).times {
140
+ @stream.print @space
141
+ }
142
+ out = percent.to_s
143
+ while out.length < 3
144
+ out = "0" + out
145
+ end
146
+ @stream.print "#{@en} #{out}%"
147
+ @stream.flush
148
+ nil
149
+ end
150
+
151
+ # Deletes the progressbar from the stream
152
+ # @return [NilClass] nil
153
+ def delete
154
+ 107.times { @stream.print "\b" }
155
+ 107.times { @stream.print "\0" }
156
+ 107.times { @stream.print "\b" }
157
+ @stream.flush
158
+ end
159
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: conprogressbar
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Marek K.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Creates a progressbar in a stream (for example, STDOUT).The percentage
14
+ can be increased and decreased.It can be used partly its own design.conprogressbar
15
+ is available under the GNU GPL v3.0.
16
+ email: m.k@mk16.de
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/conprogressbar.rb
22
+ homepage: http://test.mk16.de/
23
+ licenses:
24
+ - GPL-3.0
25
+ metadata:
26
+ documentation_uri: http://test.mk16.de/projects/conprogressbar_doc
27
+ source_code_uri: http://test.mk16.de/scriptFiles/conprogressbar.rb
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.7.8
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Creates a progressbar in a stream (for example, STDOUT).Creates a progressbar
48
+ in a stream (for example, STDOUT).The percentage can be increased and decreased.It
49
+ can be used partly its own design.conprogressbar is available under the GNU GPL
50
+ v3.0.
51
+ test_files: []