Class: BetterTranslate::ProgressTracker
- Inherits:
-
Object
- Object
- BetterTranslate::ProgressTracker
- Defined in:
- lib/better_translate/progress_tracker.rb
Overview
Tracks and displays translation progress
Shows real-time progress updates with colored console output.
Instance Attribute Summary collapse
-
#enabled ⇒ Boolean
readonly
Whether to show progress.
Instance Method Summary collapse
-
#colorize(text, color) ⇒ String
private
private
Colorize text for terminal output.
-
#complete(language, total_strings) ⇒ void
Mark translation as complete for a language.
-
#error(language, error) ⇒ void
Display an error.
-
#format_time(seconds) ⇒ String
private
private
Format time in human-readable format.
-
#initialize(enabled: true) ⇒ ProgressTracker
constructor
Initialize progress tracker.
-
#reset ⇒ void
Reset the progress tracker.
-
#truncate(text, max_length) ⇒ String
private
private
Truncate text to max length.
-
#update(language:, current_key:, progress:) ⇒ void
Update progress.
Constructor Details
#initialize(enabled: true) ⇒ ProgressTracker
Initialize progress tracker
24 25 26 27 |
# File 'lib/better_translate/progress_tracker.rb', line 24 def initialize(enabled: true) @enabled = enabled @start_time = Time.now end |
Instance Attribute Details
#enabled ⇒ Boolean (readonly)
15 16 17 |
# File 'lib/better_translate/progress_tracker.rb', line 15 def enabled @enabled end |
Instance Method Details
#colorize(text, color) ⇒ String (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Colorize text for terminal output
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/better_translate/progress_tracker.rb', line 144 def colorize(text, color) return text unless $stdout.tty? colors = { red: "\e[31m", green: "\e[32m", cyan: "\e[36m", reset: "\e[0m" } "#{colors[color]}#{text}#{colors[:reset]}" end |
#complete(language, total_strings) ⇒ void
This method returns an undefined value.
Mark translation as complete for a language
70 71 72 73 74 75 |
# File 'lib/better_translate/progress_tracker.rb', line 70 def complete(language, total_strings) return unless enabled elapsed = Time.now - @start_time puts colorize("✓ #{language}: #{total_strings} strings translated in #{format_time(elapsed)}", :green) end |
#error(language, error) ⇒ void
This method returns an undefined value.
Display an error
86 87 88 89 90 |
# File 'lib/better_translate/progress_tracker.rb', line 86 def error(language, error) return unless enabled puts colorize("✗ #{language}: #{error.message}", :red) end |
#format_time(seconds) ⇒ String (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Format time in human-readable format
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/better_translate/progress_tracker.rb', line 111 def format_time(seconds) return "0s" if seconds <= 0 minutes = (seconds / 60).to_i secs = (seconds % 60).to_i if minutes.positive? "#{minutes}m #{secs}s" else "#{secs}s" end end |
#reset ⇒ void
This method returns an undefined value.
Reset the progress tracker
99 100 101 |
# File 'lib/better_translate/progress_tracker.rb', line 99 def reset @start_time = Time.now end |
#truncate(text, max_length) ⇒ String (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Truncate text to max length
131 132 133 134 135 |
# File 'lib/better_translate/progress_tracker.rb', line 131 def truncate(text, max_length) return text if text.length <= max_length "#{text[0...(max_length - 3)]}..." end |
#update(language:, current_key:, progress:) ⇒ void
This method returns an undefined value.
Update progress
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/better_translate/progress_tracker.rb', line 39 def update(language:, current_key:, progress:) return unless enabled elapsed = Time.now - @start_time estimated_total = progress.positive? ? elapsed / (progress / 100.0) : 0 remaining = estimated_total - elapsed = format( "\r[BetterTranslate] %s | %s | %.1f%% | Elapsed: %s | Remaining: ~%s", colorize(language, :cyan), truncate(current_key, 40), progress, format_time(elapsed), format_time(remaining) ) print $stdout.flush puts "" if progress >= 100.0 # New line when complete end |