Class: Archsight::Import::Progress
- Inherits:
-
Object
- Object
- Archsight::Import::Progress
- Defined in:
- lib/archsight/import/progress.rb
Overview
Progress reporter for import operations
In TTY mode: updates a single line with r In non-TTY mode (CI): prints each update on a new line
Instance Method Summary collapse
-
#complete(message = nil) ⇒ Object
Complete the current operation (moves to new line in TTY mode).
-
#context=(name) ⇒ Object
Set the current context (e.g., “Import:GitLab”).
-
#error(message) ⇒ Object
Report an error.
-
#initialize(output: $stdout) ⇒ Progress
constructor
A new instance of Progress.
- #tty? ⇒ Boolean
-
#update(message, current: nil, total: nil) ⇒ Object
Report progress with optional sub-progress Examples: update(“Fetching projects…”) update(“Cloning”, current: 5, total: 100).
-
#warn(message) ⇒ Object
Report a warning.
Constructor Details
#initialize(output: $stdout) ⇒ Progress
Returns a new instance of Progress.
8 9 10 11 12 13 |
# File 'lib/archsight/import/progress.rb', line 8 def initialize(output: $stdout) @output = output @tty = output.respond_to?(:tty?) && output.tty? @last_line_length = 0 @current_context = nil end |
Instance Method Details
#complete(message = nil) ⇒ Object
Complete the current operation (moves to new line in TTY mode)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/archsight/import/progress.rb', line 43 def complete( = nil) if line = build_line() if @tty clear = "\r#{" " * @last_line_length}\r" @output.puts "#{clear}#{line}" else @output.puts line end elsif @tty @output.puts end @last_line_length = 0 end |
#context=(name) ⇒ Object
Set the current context (e.g., “Import:GitLab”)
20 21 22 |
# File 'lib/archsight/import/progress.rb', line 20 def context=(name) @current_context = name end |
#error(message) ⇒ Object
Report an error
59 60 61 62 |
# File 'lib/archsight/import/progress.rb', line 59 def error() complete if @tty && @last_line_length.positive? @output.puts " Error: #{}" end |
#tty? ⇒ Boolean
15 16 17 |
# File 'lib/archsight/import/progress.rb', line 15 def tty? @tty end |
#update(message, current: nil, total: nil) ⇒ Object
Report progress with optional sub-progress Examples:
update("Fetching projects...")
update("Cloning", current: 5, total: 100)
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/archsight/import/progress.rb', line 28 def update(, current: nil, total: nil) line = build_line(, current, total) if @tty # Clear previous line and write new one clear = "\r#{" " * @last_line_length}\r" @output.print "#{clear}#{line}" @output.flush @last_line_length = line.length else @output.puts line end end |
#warn(message) ⇒ Object
Report a warning
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/archsight/import/progress.rb', line 65 def warn() if @tty && @last_line_length.positive? # Save current line, print warning, restore @output.puts @output.puts " Warning: #{}" @last_line_length = 0 else @output.puts " Warning: #{}" end end |