terminal-progress 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/terminal_progress.rb +93 -0
  3. metadata +118 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 32e8754fe2c67746573adb545e22c71981f695c5f2b7466a3aa2ea51e411219e
4
+ data.tar.gz: 75a64bf6b8e27f9b7a74d61cecf7fc70f733d0afadb8a5ce28a43e8cafbff182
5
+ SHA512:
6
+ metadata.gz: a246a923a0ad593c3186a0fb76ad08dc186d162dc104723926cbe89bd2d29781af058f9f58b32eb1299bcc1258de9de08eeb6d09d568aee0182c7d499439728c
7
+ data.tar.gz: 2513d8249d45f48676a8bbe499258d6f458e12e287c65dba38b5dbb02ad2ac58d88a2a0a3605561dcc08dc2b2069fdd9fc5f22128f6757a000c18e6a281956d5
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'curses'
4
+ require 'colorize'
5
+
6
+ ##
7
+ # Class for managing progress bars in the terminal
8
+ class TerminalProgress
9
+ # Initialize a TermProg instance with a maximum value.
10
+ #
11
+ # @param max [Integer] The maximum value of the progress bar.
12
+ def initialize(max)
13
+ Curses.raw
14
+ instance_variable_set(:@cycle, '⣷⣯⣟⡿⢿⣻⣽⣾'.split(//).cycle)
15
+ instance_variable_set(:@i, 0)
16
+ instance_variable_set(:@max, max)
17
+ instance_variable_set(:@stop, '⣾')
18
+ instance_variable_set(:@loop, loop_thread)
19
+ end
20
+
21
+ def loop_thread
22
+ Thread.new do
23
+ loop do
24
+ instance_variable_set(:@stop, @cycle.next)
25
+ printf " #{@stop}\r"
26
+ sleep 0.0625
27
+ end
28
+ end
29
+ end
30
+
31
+ ##
32
+ # Increment the progress bar and print a message if present.
33
+ #
34
+ # @param message [String, nil] Optional message to display above the progress bar.
35
+ def print_progress(message = nil)
36
+ instance_variable_set(:@i, @max) if @i > @max
37
+ printf "#{' ' * Curses.cols}\r#{message}\r\n" unless message.nil?
38
+ print_line
39
+ instance_variable_set(:@i, @i + 1)
40
+ end
41
+
42
+ ##
43
+ # Print a single line of the progress bar.
44
+ def print_line
45
+ printf "#{prefix}#{'='.light_cyan * width}#{' ' * blank}#{suffix}\r"
46
+ end
47
+
48
+ ##
49
+ # This is the last call to terminate the progress loop and finish rendering the bar.
50
+ def print_complete
51
+ printf " #{@max}/#{@max}: [#{'='.light_yellow * width}#{suffix}\r\n"
52
+ kill
53
+ end
54
+
55
+ # Terminate the progress loop.
56
+ def kill
57
+ Thread.kill @loop
58
+ end
59
+
60
+ # Check if the progress loop is still alive.
61
+ #
62
+ # @return [Boolean] Returns true if the progress loop is alive, otherwise false.
63
+ def alive?
64
+ @loop.alive?
65
+ end
66
+
67
+ private
68
+
69
+ # Generate the prefix of the progress bar.
70
+ def prefix
71
+ " #{@stop} #{@i.to_s.rjust(@max.to_s.length)}/#{@max}: ["
72
+ end
73
+
74
+ # Calculate the maximum width for the progress bar.
75
+ def max_width
76
+ Curses.cols - prefix.length - suffix.length
77
+ end
78
+
79
+ # Calculate the current width of the progress bar.
80
+ def width
81
+ ((@i / @max.to_f) * max_width).to_i
82
+ end
83
+
84
+ # Calculate the number of blank spaces in the progress bar.
85
+ def blank
86
+ max_width - width
87
+ end
88
+
89
+ # Define the suffix of the progress bar.
90
+ def suffix
91
+ ']'
92
+ end
93
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: terminal-progress
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lee Whittaker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-10-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: curses
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.4.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.4.4
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 5.20.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 5.20.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.56.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 1.56.3
69
+ - !ruby/object:Gem::Dependency
70
+ name: stringio
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 3.0.8
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 3.0.8
83
+ description: |2
84
+ Terminal Progress is a progress bar manager for Ruby applications. It
85
+ provides a simple way to display progress updates in the terminal, making
86
+ it ideal for tasks like seed data eneration, file processing, or any
87
+ operation where progress tracking is ideal.
88
+ email: whittakerlee81@gmail.com
89
+ executables: []
90
+ extensions: []
91
+ extra_rdoc_files: []
92
+ files:
93
+ - lib/terminal_progress.rb
94
+ homepage: https://github.com/Okomikeruko/terminal-progress
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '3.1'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubygems_version: 3.3.26
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: terminal-progress - Simplify progress tracking in your Ruby applications
117
+ with this simple progress bar manager.
118
+ test_files: []