Class: Barr::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/barr/manager.rb

Constant Summary collapse

ERROR_ICON =
"%{F#FF0000}\uf071%{F-}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.



10
11
12
13
# File 'lib/barr/manager.rb', line 10

def initialize
  @count = 0
  @blocks = []
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



8
9
10
# File 'lib/barr/manager.rb', line 8

def blocks
  @blocks
end

#countObject (readonly)

Returns the value of attribute count.



8
9
10
# File 'lib/barr/manager.rb', line 8

def count
  @count
end

Instance Method Details

#add(block) ⇒ Object



15
16
17
# File 'lib/barr/manager.rb', line 15

def add(block)
  @blocks << block
end

#add_block(block) ⇒ Object



73
# File 'lib/barr/manager.rb', line 73

def add_block(block); add(block); end

#destroyObject



72
# File 'lib/barr/manager.rb', line 72

def destroy; destroy!; end

#destroy!Object



19
20
21
# File 'lib/barr/manager.rb', line 19

def destroy!
  @blocks.each(&:destroy!)
end

#drawObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/barr/manager.rb', line 23

def draw
  outputs = { l: [], c: [], r: [] }

  @blocks.each do |block|
    outputs[block.align] << block.draw
  end

  left_blocks = outputs[:l].join ''
  centre_blocks = outputs[:c].join ''
  right_blocks = outputs[:r].join ''

  bar_render = ''
  bar_render << "%{l}#{left_blocks} " if left_blocks.length > 0
  bar_render << "%{c} #{centre_blocks} " if centre_blocks.length > 0
  bar_render << "%{r} #{right_blocks}" if right_blocks.length > 0

  bar_render.gsub! "\n", ''

  system('echo', '-e', bar_render.encode('UTF-8'))
end

#runObject



71
# File 'lib/barr/manager.rb', line 71

def run; run!; end

#run!Object



44
45
46
47
48
49
50
# File 'lib/barr/manager.rb', line 44

def run!
  while true
    self.update!
    self.draw
    sleep 1
  end
end

#updateObject

compatibility methods. alias_method would work here, but for consistency with Block I’ll define them this way



70
# File 'lib/barr/manager.rb', line 70

def update; update!; end

#update!Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/barr/manager.rb', line 52

def update!
  @blocks.each do |block|
    begin
      block.update! if @count == 0 || (@count % block.interval == 0)
    rescue StandardError => e
      STDERR.puts e.message
      block << ERROR_ICON unless block.output.include?(ERROR_ICON)
      next
    end
  end

  @count += 1
end