Class: Archsight::Import::Progress

Inherits:
Object
  • Object
show all
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

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(message = nil)
  if message
    line = build_line(message)
    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(message)
  complete if @tty && @last_line_length.positive?
  @output.puts "  Error: #{message}"
end

#tty?Boolean

Returns:

  • (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(message, current: nil, total: nil)
  line = build_line(message, 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(message)
  if @tty && @last_line_length.positive?
    # Save current line, print warning, restore
    @output.puts
    @output.puts "  Warning: #{message}"
    @last_line_length = 0
  else
    @output.puts "  Warning: #{message}"
  end
end