Class: Infobar

Inherits:
Object show all
Extended by:
Tins::Delegate
Includes:
ComplexConfig::Provider::Shortcuts, FancyInterface, InputOutput, Tins::SexySingleton
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.6.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

.displayObject



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

def self.display
  Infobar.instance.display
end

Instance Method Details

#busy(**opts) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/infobar.rb', line 87

def busy(**opts)
  block_given? or raise ArgumentError, 'block is required as an argument'
  duration = opts.delete(:sleep) || 0.1
  call(opts | {
    total: Float::INFINITY,
    message: { format: ' %l %te %s ', '%s' => { frames: :circle1 } },
  })
  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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/infobar.rb', line 60

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)
  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



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/infobar.rb', line 135

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



128
129
130
131
132
133
# File 'lib/infobar.rb', line 128

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



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

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



107
108
109
110
111
112
# File 'lib/infobar.rb', line 107

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



114
115
116
117
118
# File 'lib/infobar.rb', line 114

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