Class: RTUI::Progress

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

Overview

Progress indicators

Direct Known Subclasses

ReversedProgress

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, total, *options) ⇒ Progress

Initializes a progress indicator.

Examples:

Just a bar and ETA: RTUI::Progress.new(“Foo”, 10, { :components => [:bar, :stat]})

A Spinner with just percentage: RTUI::Progress.new(“Foo”, 10, { :components => [:spinner, :percentage]})

Options:

  • bar => “=”

  • out => STDERR

Components:

  • title

  • spinner

  • percentage

  • stat

  • bar



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rtui/progress.rb', line 47

def initialize (title, total, *options)
  options = options.first || {}
  @title = title
  @total = total
  @terminal_width = 80
  @current = 0
  @previous = 0
  @finished = false
  @start_time = Time.now
  @previous_time = @start_time
  @title_width = 14
  @subject = ""

  @out        = options[:out] || STDOUT
  @bar_mark   = options[:bar] || "="
  @colors     = options[:colors] || false
  @components = options[:components] || [:title, :percentage, :bar, :stat]

  clear
  show
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



20
21
22
# File 'lib/rtui/progress.rb', line 20

def current
  @current
end

#start_timeObject

Returns the value of attribute start_time.



22
23
24
# File 'lib/rtui/progress.rb', line 22

def start_time
  @start_time
end

#titleObject (readonly)

Returns the value of attribute title.



19
20
21
# File 'lib/rtui/progress.rb', line 19

def title
  @title
end

#totalObject (readonly)

Returns the value of attribute total.



21
22
23
# File 'lib/rtui/progress.rb', line 21

def total
  @total
end

Instance Method Details

#clearObject



69
70
71
# File 'lib/rtui/progress.rb', line 69

def clear
  @out.print "\r#{(" " * (TTY.get_width - 1))}\r"
end

#components=(arguments) ⇒ Object

def format= (format)

@format = format

end



92
93
94
# File 'lib/rtui/progress.rb', line 92

def components= (arguments)
  @components = arguments
end

#file_transfer_modeObject



83
84
85
86
# File 'lib/rtui/progress.rb', line 83

def file_transfer_mode
  return unless @components.index(:stat)
  @components[@components.index(:stat)] = :stat_for_file_transfer
end

#finishObject



73
74
75
76
77
# File 'lib/rtui/progress.rb', line 73

def finish
  @current = @total
  @finished = true
  show
end

#finished?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/rtui/progress.rb', line 79

def finished?
  @finished
end

#haltObject



100
101
102
103
# File 'lib/rtui/progress.rb', line 100

def halt
  @finished = true
  show
end

#inc(step = 1) ⇒ Object



105
106
107
108
109
110
# File 'lib/rtui/progress.rb', line 105

def inc step = 1
  @current += step
  @current = @total if @current > @total
  show_if_needed
  @previous = @current
end

#inspectObject



121
122
123
# File 'lib/rtui/progress.rb', line 121

def inspect
  "#<RTUI::Progress:#{@current}/#{@total}>"
end

#set(count) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/rtui/progress.rb', line 112

def set (count)
  if count < 0 || count > @total
    raise "invalid count: #{count} (total: #{@total})"
  end
  @current = count
  show_if_needed
  @previous = @current
end

#subject=(subject) ⇒ Object



96
97
98
# File 'lib/rtui/progress.rb', line 96

def subject=(subject)
  @subject = subject
end