class R3Status::StatusLine

This class encapsulates a status output. Blocks are added by the user and are displayed each iteration.

Example

s = StatusLine.new
s = StatusLine.new update_interval: 3

s << Blocks::Volume.new

s.run

Attributes

blocks[R]

A list blocks to display, right to left, in the bar.

postfix[RW]

An object (e.g. String)that will be inserted at the ebd of a block’s text. Default: nil.

prefix[RW]

An object (e.g. String) that will be inserted at the start of a block’s text. Default: _“ ”_.

update_interval[RW]

The time (in seconds) between iterations. Default: 0.4.

Public Class Methods

new(prefix: " ", postfix: nil, update_interval: 0.4) click to toggle source

Creates a new StatusLine object.

# File lib/r3status.rb, line 39
def initialize(prefix: " ", postfix: nil, update_interval: 0.4)
  @blocks = []
  @prefix = prefix
  @postfix = postfix
  @update_interval = update_interval
end

Public Instance Methods

<<(block) click to toggle source

Appends a new block to this StatusLine object.

# File lib/r3status.rb, line 47
def << block
  @blocks << block
end
run() click to toggle source

Starts the operation of the status line and the output of the data to the standard output.

# File lib/r3status.rb, line 52
def run
  @reader_thread = Thread.new do
    loop do
      s = gets.chomp
      parse s
    end
  end
  @reader_thread.run
  
  at_exit do
    @reader_thread.kill
    @blocks.each { |b| b.terminate }
  end
  
  puts "{\"version\":1, \"click_events\":true}["
  loop_with_interval(0.2) do
    @blocks.each {|b| b.update }
    puts "[#{transmission}],"
  end
  
end

Private Instance Methods

parse(str) click to toggle source

Parses a single input from i3bar.

# File lib/r3status.rb, line 82
def parse(str)
  return if str.length < 2
  obj = nil
  str = str.strip.sub(/\A\,/ , "")
  
  begin
    obj = JSON.parse(str)
  rescue Exception => e
    return
  end
  
  block = @blocks.map do |b| 
    if b.respond_to? :blocks
      b.blocks
    else
      b
    end
  end.flatten.find { |b| b.name == obj["name"] }
  
  block.clicked(obj["button"], obj["x"], obj["y"]) unless block.nil?
end
transmission() click to toggle source

Generates a single transmission.

# File lib/r3status.rb, line 76
def transmission
  @blocks.map { |i| i.to_s(postfix: @postfix, prefix: @prefix) }
         .reject(&:nil?).inject { |i, j| i << ",#{j}"}
end