class Barabara::Modules::Volume

Constants

CMD_SET
CMD_WATCH
ICONS

Class constants (Icons) here.

Public Class Methods

new() click to toggle source
# File lib/barabara/modules/volume.rb, line 17
def initialize
  options = GlobalConfig.config.module_config('volume')
  @colors = GlobalConfig.config.colors
  @icons = options['icons'] || ICONS

  @icon = 'mute'
  @color = :ac_text
  @mute = true
  @level = 0
  fetch
end

Public Instance Methods

down() click to toggle source
# File lib/barabara/modules/volume.rb, line 73
def down
  spawn(CMD_SET + ' 2%- unmute')
  fetch
end
fetch() click to toggle source
# File lib/barabara/modules/volume.rb, line 50
def fetch
  sleep 0.01
  raw_data = `amixer get Master`
  keys = raw_data
           .match(/Front Left:.* \[(?<level>\d+)%\] \[(?<state>[onf]+)\]/)
           .named_captures

  @level = keys['level'].to_i
  @mute = keys['state'] == 'off'
  @color, @icon = parse
  self
end
format_string() click to toggle source
# File lib/barabara/modules/volume.rb, line 78
def format_string
  if @mute
    '%%{F%<color>s}%<icon>s%%{F-}'
  else
    '%%{F%<color>s}%<icon>s %<level>s%%%%%%{F-}'
  end
end
mute() click to toggle source
# File lib/barabara/modules/volume.rb, line 63
def mute
  spawn(CMD_SET + ' toggle')
  fetch
end
parse() click to toggle source
# File lib/barabara/modules/volume.rb, line 29
def parse
  return [:in_text, 'mute'] if @mute

  case @level
  when 60..100 then [:ac_text, 'max']
  when 30..60 then  [:mi_text, 'med']
  when 0..30 then   [:in_text, 'low']
  end
end
parse_line(line) click to toggle source
# File lib/barabara/modules/volume.rb, line 46
def parse_line(line)
  publish(:event, 'volume', update) if line.match?(/^Event 'change' on sink/)
end
render() click to toggle source
# File lib/barabara/modules/volume.rb, line 90
def render
  format(format_string, to_h)
end
to_h() click to toggle source
# File lib/barabara/modules/volume.rb, line 86
def to_h
  { icon: @icons[@icon], color: @colors[@color], level: @level }
end
up() click to toggle source
# File lib/barabara/modules/volume.rb, line 68
def up
  spawn(CMD_SET + ' 2%+ unmute')
  fetch
end
update() click to toggle source
# File lib/barabara/modules/volume.rb, line 94
def update
  fetch
  render
end
watch() click to toggle source
# File lib/barabara/modules/volume.rb, line 39
def watch
  PTY.spawn(CMD_WATCH) do |read, _write, pid|
    read.each { |line| parse_line(line.chomp) }
    Process.wait pid
  end
end