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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rtui/progress.rb', line 55

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] || STDERR
  @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.



28
29
30
# File 'lib/rtui/progress.rb', line 28

def current
  @current
end

#start_timeObject

Returns the value of attribute start_time.



30
31
32
# File 'lib/rtui/progress.rb', line 30

def start_time
  @start_time
end

#titleObject (readonly)

Returns the value of attribute title.



27
28
29
# File 'lib/rtui/progress.rb', line 27

def title
  @title
end

#totalObject (readonly)

Returns the value of attribute total.



29
30
31
# File 'lib/rtui/progress.rb', line 29

def total
  @total
end

Instance Method Details

#clearObject



77
78
79
# File 'lib/rtui/progress.rb', line 77

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

#components=(arguments) ⇒ Object

def format= (format)

@format = format

end



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

def components= (arguments)
  @components = arguments
end

#file_transfer_modeObject



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

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

#finishObject



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

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

#finished?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/rtui/progress.rb', line 87

def finished?
  @finished
end

#haltObject



108
109
110
111
# File 'lib/rtui/progress.rb', line 108

def halt
  @finished = true
  show
end

#inc(step = 1) ⇒ Object



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

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

#inspectObject



129
130
131
# File 'lib/rtui/progress.rb', line 129

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

#set(count) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/rtui/progress.rb', line 120

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



104
105
106
# File 'lib/rtui/progress.rb', line 104

def subject=(subject)
  @subject = subject
end