Class: Infobar

Inherits:
Object show all
Extended by:
Tins::Delegate
Includes:
ComplexConfig::Provider::Shortcuts, FancyInterface, InputOutput, Singleton
Defined in:
lib/infobar.rb,
lib/infobar.rb,
lib/infobar/version.rb

Defined Under Namespace

Modules: FancyInterface, InputOutput Classes: Counter, Display, Duration, FIFO, Frequency, Message, Number, Rate, Spinner, Timer, Trend

Constant Summary collapse

VERSION =

Infobar version

'0.8.1'
VERSION_ARRAY =

:nodoc:

VERSION.split('.').map(&:to_i)
VERSION_MAJOR =

:nodoc:

VERSION_ARRAY[0]
VERSION_MINOR =

:nodoc:

VERSION_ARRAY[1]
VERSION_BUILD =

:nodoc:

VERSION_ARRAY[2]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FancyInterface

#+, #+@, #-, #-@, #<<, #add, #coerce, #~

Methods included from InputOutput

#clear, #newline

Constructor Details

#initializeInfobar

Returns a new instance of Infobar.



24
25
26
27
28
# File 'lib/infobar.rb', line 24

def initialize
  @counter = Infobar::Counter.new
  @display = Infobar::Display.new
  reset display: false
end

Instance Attribute Details

#counterObject (readonly)

Returns the value of attribute counter.



36
37
38
# File 'lib/infobar.rb', line 36

def counter
  @counter
end

#displayObject (readonly)

Returns the value of attribute display.



38
39
40
# File 'lib/infobar.rb', line 38

def display
  @display
end

#labelObject

Returns the value of attribute label.



40
41
42
# File 'lib/infobar.rb', line 40

def label
  @label
end

#messageObject (readonly)

Returns the value of attribute message.



34
35
36
# File 'lib/infobar.rb', line 34

def message
  @message
end

Class Method Details

.busy(**opts, &block) ⇒ Object



93
94
95
# File 'lib/infobar.rb', line 93

def self.busy(**opts, &block)
  instance.busy(**opts, &block)
end

.call(**opts) ⇒ Object



60
61
62
# File 'lib/infobar.rb', line 60

def self.call(**opts)
  instance.call(**opts)
end

.convert_to_message(message) ⇒ Object



146
147
148
# File 'lib/infobar.rb', line 146

def self.convert_to_message(message)
  instance.convert_to_message(message)
end

.displayObject



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

def self.display
  infobar.display
end

Instance Method Details

#busy(frames: :circle1, **opts, &block) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/infobar.rb', line 97

def busy(frames: :circle1, **opts, &block)
  block_given? or raise ArgumentError, 'block is required as an argument'
  duration = opts.delete(:sleep) || 0.1
  reset
  call(**opts | {
    total: Float::INFINITY,
    message: { format: ' %l %te %s ', '%s' => { frames: frames } },
  })
  ib = Thread.new {
    loop do
      +infobar
      sleep duration
    end
  }
  r = nil
  t = Thread.new { r = yield }
  t.join
  ib.kill
  r
end

#call(total:, current: 0, label: cc.infobar?&.label || 'Infobar', message: cc.infobar?&.message?&.to_h, show: cc.infobar?&.show?, style: cc.infobar?&.style?&.to_h&.symbolize_keys_recursive, as_styles: cc.infobar?&.as_styles?&.to_h&.symbolize_keys_recursive, frequency: cc.infobar?&.frequency?, update: false, input: $stdin, output: $stdout) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/infobar.rb', line 64

def call(
  total:,
  current:   0,
  label:     cc.infobar?&.label || 'Infobar',
  message:   cc.infobar?&.message?&.to_h,
  show:      cc.infobar?&.show?,
  style:     cc.infobar?&.style?&.to_h&.symbolize_keys_recursive,
  as_styles: cc.infobar?&.as_styles?&.to_h&.symbolize_keys_recursive,
  frequency: cc.infobar?&.frequency?,
  update:    false,
  input:     $stdin,
  output:    $stdout
)
  self.label = label
  counter.reset(total: total, current: current)
  display.reset clear: false
  @message = convert_to_message(
    message.full? || '%l %c/%t in %te, ETA %e @%E %s'
  )
  show.nil? or self.show = show
  frequency.nil? or display.frequency = frequency
  style.nil? or self.style = style
  self.as_styles = as_styles
  self.input     = input
  self.output    = output
  update and update(message: @message, force: true)
  self
end

#convert_to_message(message) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/infobar.rb', line 150

def convert_to_message(message)
  case message
  when Message
    message
  when Hash
    Message.new(message)
  when String
    Message.new({format: message})
  else
    @message
  end
end

#finish(message: nil) ⇒ Object



139
140
141
142
143
144
# File 'lib/infobar.rb', line 139

def finish(message: nil)
  counter.finish
  @message = convert_to_message(message)
  display.update(message: @message, force: true, counter: counter)
  self
end

#progress(by: 1, as: nil, message: nil, finish: true, force: false) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/infobar.rb', line 131

def progress(by: 1, as: nil, message: nil, finish: true, force: false)
  counter.progress(by: by, as: as)
  @message = convert_to_message(message)
  display.update(message: @message, force: force, counter: counter)
  finish && counter.done? and finish(message: finish)
  self
end

#reset(display: true) ⇒ Object



118
119
120
121
122
123
# File 'lib/infobar.rb', line 118

def reset(display: true)
  @message = convert_to_message('%l %c/%t in %te, ETA %e @%E %s')
  counter.reset(total: 0, current: 0)
  display and self.display.reset
  self
end

#update(message: nil, force: true) ⇒ Object



125
126
127
128
129
# File 'lib/infobar.rb', line 125

def update(message: nil, force: true)
  @message = convert_to_message(message)
  display.update(message: @message, counter: counter, force: force)
  self
end