Class: CLI::UI::Progress

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/ui/progress.rb

Constant Summary collapse

FILLED_BAR =

A Cyan filled block

"\e[46m"
UNFILLED_BAR =

A bright white block

"\e[1;47m"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title = nil, width: Terminal.width, reporter: nil) ⇒ Progress

Initialize a progress bar. Typically used in a Progress.progress block

Options

  • :title - The title of the progress bar

  • :width - The width of the terminal

  • :reporter - The progress reporter instance

: (?String? title, ?width: Integer, ?reporter: ProgressReporter::Reporter?) -> void



65
66
67
68
69
70
# File 'lib/cli/ui/progress.rb', line 65

def initialize(title = nil, width: Terminal.width, reporter: nil)
  @percent_done = 0 #: Numeric
  @title = title
  @max_width = width
  @reporter = reporter
end

Class Method Details

.progress(title = nil, width: Terminal.width, &block) ⇒ Object

Add a progress bar to the terminal output

Example Usage:

Set the percent to X

CLI::UI::Progress.progress do |bar|
  bar.tick(set_percent: percent)
end

Increase the percent by 1 percent

CLI::UI::Progress.progress do |bar|
  bar.tick
end

Increase the percent by X

CLI::UI::Progress.progress do |bar|
  bar.tick(percent: 0.05)
end

Update the title

CLI::UI::Progress.progress('Title') do |bar|
  bar.tick(percent: 0.05)
  bar.update_title('New title')
end

: [T] (?String? title, ?width: Integer) { (Progress bar) -> T } -> T



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cli/ui/progress.rb', line 40

def progress(title = nil, width: Terminal.width, &block)
  bar = nil #: Progress?
  CLI::UI::ProgressReporter.with_progress(mode: :progress) do |reporter|
    bar = Progress.new(title, width: width, reporter: reporter)
    print(CLI::UI::ANSI.hide_cursor)
    yield(bar)
  end
ensure
  puts(bar) if bar

  CLI::UI.raw do
    print(ANSI.show_cursor)
  end
end

Instance Method Details

#tick(percent: nil, set_percent: nil) ⇒ Object

Set the progress of the bar. Typically used in a Progress.progress block

Options

One of the follow can be used, but not both together

  • :percent - Increment progress by a specific percent amount

  • :set_percent - Set progress to a specific percent

Note: The :percent and +:set_percent must be between 0.00 and 1.0

: (?percent: Numeric?, ?set_percent: Numeric?) -> void

Raises:

  • (ArgumentError)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cli/ui/progress.rb', line 83

def tick(percent: nil, set_percent: nil)
  raise ArgumentError, 'percent and set_percent cannot both be specified' if percent && set_percent

  @percent_done += percent || 0.01
  @percent_done = set_percent if set_percent
  @percent_done = [@percent_done, 1.0].min # Make sure we can't go above 1.0

  # Update terminal progress reporter with current percentage
  @reporter&.set_progress((@percent_done * 100).floor)

  print(self)

  printed_lines = @title ? 2 : 1
  print(CLI::UI::ANSI.previous_lines(printed_lines) + "\n")
end

#to_sObject

Format the progress bar to be printed to terminal

: -> String



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cli/ui/progress.rb', line 113

def to_s
  suffix = " #{(@percent_done * 100).floor}%".ljust(5)
  workable_width = @max_width - Frame.prefix_width - suffix.size
  filled = [(@percent_done * workable_width.to_f).ceil, 0].max
  unfilled = [workable_width - filled, 0].max

  title = CLI::UI.resolve_text(@title, truncate_to: @max_width - Frame.prefix_width) if @title
  bar = CLI::UI.resolve_text([
    FILLED_BAR + ' ' * filled,
    UNFILLED_BAR + ' ' * unfilled,
    CLI::UI::Color::RESET.code + suffix,
  ].join)

  [title, bar].compact.join("\n")
end

#update_title(new_title) ⇒ Object

Update the progress bar title

Attributes

  • new_title - title to change the progress bar to

: (String new_title) -> void



106
107
108
# File 'lib/cli/ui/progress.rb', line 106

def update_title(new_title)
  @title = new_title
end