class Barabara::Modules::Lemonbar

Attributes

panel_clicks[R]
panel_data[RW]
panel_out[RW]
panel_pid[R]

Public Class Methods

new() click to toggle source
# File lib/barabara/modules/lemonbar.rb, line 9
def initialize
  options = GlobalConfig.config.module_config('lemonbar')
  @colors = GlobalConfig.config.colors
  @monitors = GlobalConfig.config.monitors
  @format = options[:format].chomp
  @snippets = fill_snippets(options[:snippets])
  @cmd = ['lemonbar', *bar_options(options)].join(' ')
  @panel_data = bootstrap_panel

  run_panel
end

Public Instance Methods

bar_options(options) click to toggle source
# File lib/barabara/modules/lemonbar.rb, line 33
def bar_options(options)
  cmd_opts = [
    "-B '#{@colors[:in_framebr]}'",
    "-F '#{@colors[:ac_text]}'",
    "-g 'x#{options[:height]}+0+0'",
    "-n '#{options[:name]}'", '-a 30'
  ]
  cmd_opts.concat font_opts(options[:fonts])
  cmd_opts.concat options[:extra_opts] if options.key?(:extra_opts)
  cmd_opts
end
bootstrap_panel() click to toggle source
# File lib/barabara/modules/lemonbar.rb, line 45
def bootstrap_panel
  @snippets.merge(
    window_title: 'Welcome home.',
    tagline: {},
    battery: 'U',
    weather: '',
    time: '',
    volume: Volume.new.update
  )
end
fill_panel() click to toggle source
# File lib/barabara/modules/lemonbar.rb, line 56
def fill_panel
  string = ''
  # STDERR.puts 'Panel data:' + @panel_data.inspect
  @monitors.each do |monitor|
    string << format(@format,
                     @panel_data.merge(
                       tags: @panel_data.dig(:tagline, monitor) || '',
                       monitor: monitor
                     ))
  end
  string.delete "\n"
end
fill_snippets(snippets) click to toggle source
# File lib/barabara/modules/lemonbar.rb, line 21
def fill_snippets(snippets)
  Hash[snippets.map { |k, v| [k, v % @colors] }]
end
render() click to toggle source
# File lib/barabara/modules/lemonbar.rb, line 69
def render
  @panel_out.puts fill_panel + "\n"
end
run_panel() click to toggle source
# File lib/barabara/modules/lemonbar.rb, line 25
def run_panel
  @panel_clicks, slave = PTY.open
  read, @panel_out = IO.pipe
  @panel_pid = spawn(@cmd, in: read, out: slave)
  slave.close
  read.close
end
update_panel(data) click to toggle source
# File lib/barabara/modules/lemonbar.rb, line 73
def update_panel(data)
  @panel_data.merge!(data)
  render
end

Private Instance Methods

font_opts(fonts = {}) click to toggle source
# File lib/barabara/modules/lemonbar.rb, line 80
def font_opts(fonts = {})
  fonts.flat_map do |type, font_def|
    if font_def.is_a? String
      ["-f '#{font_def}'"]
    elsif font_def.key? :offset
      ["-o '#{font_def[:offset]}'", "-f '#{font_def[:name]}'"]
    else
      ["-f '#{font_def[:name]}'"]
    end
  end
end