Class: BetterTranslate::ProgressTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/better_translate/progress_tracker.rb

Overview

Tracks and displays translation progress

Shows real-time progress updates with colored console output.

Examples:

Basic usage

tracker = ProgressTracker.new(enabled: true)
tracker.update(language: "Italian", current_key: "greeting", progress: 50.0)
tracker.complete("Italian", 100)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enabled: true) ⇒ ProgressTracker

Initialize progress tracker

Examples:

tracker = ProgressTracker.new(enabled: true)

Parameters:

  • enabled (Boolean) (defaults to: true)

    Whether to show progress (default: true)



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

#enabledBoolean (readonly)

Returns Whether to show progress.

Returns:

  • (Boolean)

    Whether to show progress



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

Parameters:

  • text (String)

    Text to colorize

  • color (Symbol)

    Color name (:red, :green, :cyan)

Returns:

  • (String)

    Colorized text



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

Examples:

tracker.complete("Italian", 150)

Parameters:

  • language (String)

    Language name

  • total_strings (Integer)

    Total number of strings translated



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

Examples:

tracker.error("Italian", StandardError.new("API error"))

Parameters:

  • language (String)

    Language name

  • error (StandardError)

    The error that occurred



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

Parameters:

  • seconds (Float)

    Seconds

Returns:

  • (String)

    Formatted time



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

#resetvoid

This method returns an undefined value.

Reset the progress tracker

Examples:

tracker.reset


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

Parameters:

  • text (String)

    Text to truncate

  • max_length (Integer)

    Maximum length

Returns:

  • (String)

    Truncated text



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

Examples:

tracker.update(language: "Italian", current_key: "nav.home", progress: 75.5)

Parameters:

  • language (String)

    Current language being translated

  • current_key (String)

    Current translation key

  • progress (Float)

    Progress percentage (0-100)



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

  message = 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 message
  $stdout.flush

  puts "" if progress >= 100.0 # New line when complete
end