Class: Gloo::Objs::Bar

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/cli/bar.rb

Constant Summary collapse

KEYWORD =
'bar'.freeze
KEYWORD_SHORT =
'bar'.freeze
NAME =
'name'.freeze
TOTAL =
'total'.freeze

Constants inherited from Core::Baseo

Core::Baseo::NOT_IMPLEMENTED_ERR

Instance Attribute Summary

Attributes inherited from Core::Obj

#children, #parent, #value

Attributes inherited from Core::Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Obj

#add_child, can_create?, #can_receive_message?, #child_count, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, help, inherited, #initialize, #msg_reload, #msg_unload, #multiline_value?, #pn, #remove_child, #root?, #send_message, #set_parent, #set_value, #type_display, #value_display, #value_is_array?, #value_is_blank?, #value_string?

Methods inherited from Core::Baseo

#initialize, #type_display

Constructor Details

This class inherits a constructor from Gloo::Core::Obj

Class Method Details

.messagesObject

Get a list of message names that this object receives.



78
79
80
# File 'lib/gloo/objs/cli/bar.rb', line 78

def self.messages
  return super + %w[start advance stop run]
end

.short_typenameObject

The short name of the object type.



27
28
29
# File 'lib/gloo/objs/cli/bar.rb', line 27

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



20
21
22
# File 'lib/gloo/objs/cli/bar.rb', line 20

def self.typename
  return KEYWORD
end

Instance Method Details

#add_children_on_create?Boolean

Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.

Returns:



58
59
60
# File 'lib/gloo/objs/cli/bar.rb', line 58

def add_children_on_create?
  return true
end

#add_default_childrenObject

Add children to this object. This is used by containers to add children needed for default configurations.



65
66
67
68
69
# File 'lib/gloo/objs/cli/bar.rb', line 65

def add_default_children
  fac = @engine.factory
  fac.create_string NAME, '', self
  fac.create_int TOTAL, 100, self
end

#msg_advanceObject

Advance the progress bar.



100
101
102
103
104
105
106
107
108
# File 'lib/gloo/objs/cli/bar.rb', line 100

def msg_advance
  x = 1
  if @params&.token_count&.positive?
    expr = Gloo::Expr::Expression.new( @engine, @params.tokens )
    x = expr.evaluate.to_i
  end

  @bar.advance x
end

#msg_runObject

Run for the given number of seconds advancing the bar to the end.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/gloo/objs/cli/bar.rb', line 114

def msg_run
  msg_start

  x = 1
  if @params&.token_count&.positive?
    expr = Gloo::Expr::Expression.new( @engine, @params.tokens )
    x = expr.evaluate.to_i
  end

  100.times do
    sleep ( 0.0 + x ) / 100.0
    @bar.advance 1
  end

  msg_stop
end

#msg_startObject

Start the progress bar.



85
86
87
88
# File 'lib/gloo/objs/cli/bar.rb', line 85

def msg_start
  msg = "#{name_value} [:bar] :percent"
  @bar = TTY::ProgressBar.new( msg, total: total_value )
end

#msg_stopObject

Finish the progress bar.



93
94
95
# File 'lib/gloo/objs/cli/bar.rb', line 93

def msg_stop
  @bar.finish
end

#name_valueObject

Get the bar’s name from the child object.



34
35
36
37
38
39
# File 'lib/gloo/objs/cli/bar.rb', line 34

def name_value
  o = find_child NAME
  return '' unless o

  return o.value
end

#total_valueObject

Get the bar’s total from the child object.



44
45
46
47
48
49
# File 'lib/gloo/objs/cli/bar.rb', line 44

def total_value
  o = find_child TOTAL
  return 100 unless o

  return o.value
end