Class: MonoclePrint::Progress

Inherits:
OutputDevice show all
Defined in:
lib/monocle-print/progress.rb

Constant Summary

Constants inherited from OutputDevice

OutputDevice::DEFAULT_SIZE, OutputDevice::IO_PRINT_METHODS, OutputDevice::SIZE_IOCTL, OutputDevice::SIZE_STRUCT

Constants included from TerminalEscapes

TerminalEscapes::ANSI_COLORS, TerminalEscapes::ANSI_COLOR_NAMES, TerminalEscapes::ANSI_MODIFIERS, TerminalEscapes::ANSI_MODIFIER_NAMES

Constants included from MonoclePrint

COLOR_ESCAPE, FOUR_BYTES, MULTIBYTE_CHARACTER, ONE_BYTE, THREE_BYTES, TWO_BYTES, VERSION

Instance Attribute Summary collapse

Attributes inherited from OutputDevice

#device, #style

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OutputDevice

#background, buffer, #clear, #clear_line, #close, #color_code, #colors, #column, #column_layout, #fill, #foreground, #full_width, #height, #indent, #left_margin, #left_margin=, #leger, #line, #list, #margin=, #modifier, #newline!, open, #outdent, #print, #printf, #put, #put!, #puts, #putsf, #reset!, #return!, #right_margin, #right_margin=, #space, stderr, stdout, #table, #use_color, #use_color=, #width

Methods included from TerminalEscapes

ansi_color, ansi_modifier, blink, bold, clear_attr, clear_down, clear_left, clear_line, clear_right, clear_screen, clear_up, conceal, cursor_backward, cursor_down, cursor_forward, cursor_up, dobule_height_bottom, double_height_top, double_width, konsole_color, restore_cursor, reverse, save_cursor, set_cursor, single_width, underline, xterm_color

Methods included from MonoclePrint

Line, Output, Rectangle, Style, Text, buffer, included, library_path, stderr, stdout, version

Constructor Details

#initialize(total, options = {}) ⇒ Progress

Returns a new instance of Progress.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/monocle-print/progress.rb', line 45

def initialize( total, options = {} )
  @total      = Utils.at_least( total.to_i, 1 )
  @position   = 0
  @title      = Line( options[ :title ].to_s )
  @limit      = @width = @time = @next_time = nil
  @bar_color  = options.fetch( :bar_color, :red )
  @text_color = options.fetch( :text_color, :black )
  @progress   = -1
  @draw       = true
  super( options.fetch( :output, $stderr ), options )
end

Instance Attribute Details

#bar_colorObject

Returns the value of attribute bar_color.



7
8
9
# File 'lib/monocle-print/progress.rb', line 7

def bar_color
  @bar_color
end

#outputObject (readonly)

Returns the value of attribute output.



8
9
10
# File 'lib/monocle-print/progress.rb', line 8

def output
  @output
end

#positionObject

Returns the value of attribute position.



7
8
9
# File 'lib/monocle-print/progress.rb', line 7

def position
  @position
end

#titleObject

Returns the value of attribute title.



8
9
10
# File 'lib/monocle-print/progress.rb', line 8

def title
  @title
end

#totalObject

Returns the value of attribute total.



7
8
9
# File 'lib/monocle-print/progress.rb', line 7

def total
  @total
end

#width=(value) ⇒ Object (writeonly)

Sets the attribute width

Parameters:

  • value

    the value to set the attribute width to.



9
10
11
# File 'lib/monocle-print/progress.rb', line 9

def width=(value)
  @width = value
end

Class Method Details

.enumerate(collection, method = :each, *args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/monocle-print/progress.rb', line 11

def self.enumerate( collection, method = :each, *args )
  block_given? or return enum_for( :enumerate, collection, method, *args )
  #options = Hash === args.last ? args.pop : {}

  #method = options.fetch( :method, nil )
  #if method.nil? and Symbol === args.first
  #  method, *args = args
  #else
  #  method ||= :each
  #end

  #size = options[ :size ]
  #size ||= options.fetch( :length ) do
  #  collection.length rescue collection.size
  #end
  size = collection.length rescue collection.size

  enum = collection.enum_for( method, *args )
  run( size ) do | bar |
    for item in enum
      v = yield( item, bar )
      bar.step
      v
    end
  end
end

.run(total, options = {}) ⇒ Object



38
39
40
41
42
43
# File 'lib/monocle-print/progress.rb', line 38

def self.run( total, options = {} )
  bar = new( total, options )
  yield( bar )
ensure
  bar.clear_line
end

Instance Method Details

#displayObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/monocle-print/progress.rb', line 113

def display
  return!

  hour, r  = time_remaining.divmod( 3600 )
  min, sec = r.divmod( 60 )
  sec = sec.round
  eta = Line( ' %02i:%02i:%02i' % [ hour, min, sec ] )

  center_width = 6
  right_width  = ( width - center_width ) / 2
  left_width   = width - center_width - right_width

  center = ( @progress.to_s << '%' ).center( 6 ) # " ___% "
  left   = title.align( :left, left_width ).truncate!( left_width, '...' )
  right  = eta.align( :right, right_width )

  bar = left << center << right

  color_code = ''
  @bar_color  and color_code << ansi_color( ?b, @bar_color )
  @text_color and color_code << ansi_color( ?f, @text_color )

  unless color_code.empty?
    fill_point = bar.char_byte( width * @position / @total )
    bar.insert( fill_point, "\e[0m" )
    bar.insert( 0, color_code )
  end

  print( bar )
  @draw = false
  self
end

#draw?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
# File 'lib/monocle-print/progress.rb', line 71

def draw?
  progress = @position * 100 / @total
  if @progress != progress
    @progress = progress
    @draw = true
  end

  return @draw
end

#durationObject



101
102
103
# File 'lib/monocle-print/progress.rb', line 101

def duration
  Time.now - start_time
end

#hideObject



183
184
185
186
187
188
# File 'lib/monocle-print/progress.rb', line 183

def hide
  wipe
  return yield
ensure
  display
end

#limit(l = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/monocle-print/progress.rb', line 81

def limit( l = nil )
  unless l.nil?
    begin
      before, @limit = @limit, Utils.at_most( l.to_i, @total )
      yield( self )
    ensure
      self.limit = before
    end
  end
  return( @limit )
end

#limit=(l) ⇒ Object



93
94
95
# File 'lib/monocle-print/progress.rb', line 93

def limit=( l )
  @limit = Utils.at_most( l.to_i, @total )
end

#progressObject



171
172
173
# File 'lib/monocle-print/progress.rb', line 171

def progress
  @position * 100 / @total
end

#resetObject



150
151
152
153
154
# File 'lib/monocle-print/progress.rb', line 150

def reset
  @position = 0
  @limit = nil
  @start_time = nil
end

#stage(title, limit = @total - @position, absolute = false) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/monocle-print/progress.rb', line 57

def stage( title, limit = @total - @position, absolute = false )
  limit += @position unless absolute
  self.title = title
  limit( limit ) do
    return( yield( self ) )
  end
end

#start_timeObject



97
98
99
# File 'lib/monocle-print/progress.rb', line 97

def start_time
  @start_time ||= Time.now
end

#step(inc = 1) ⇒ Object



65
66
67
68
69
# File 'lib/monocle-print/progress.rb', line 65

def step( inc = 1 )
  limit = @limit || @total
  @position = Utils.at_most( @position + inc, limit )
  draw? and display
end

#time_remainingObject



175
176
177
178
179
# File 'lib/monocle-print/progress.rb', line 175

def time_remaining
  @position < 2 and return( 0 )
  sec_per_step = duration / @position
  ( @total - @position ) * sec_per_step
end

#title_widthObject



146
147
148
# File 'lib/monocle-print/progress.rb', line 146

def title_width
  width * 0.3
end

#to_sObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/monocle-print/progress.rb', line 156

def to_s
  title_width = ( width * 0.4 ).round
  title = @title.align( :center, title_width )[ 0, title_width ]

  hour, r  = time_remaining.divmod( 3600 )
  min, sec = r.divmod( 60 )
  sec = sec.round
  eta = '%02i:%02i:%02i' % [ hour, min, sec ]

  fill_width = width - title_width - eta.length - 9
  filled = ( fill_width * @position / @total ).round
  title << ' |' << ( '*' * filled ).ljust( fill_width ) <<
    '| ' << ( '%3i%% ' % progress ) << eta
end