module DockDriver::Template::Dzen2

Adds DZen2 related methods to the DockDriver::Template DSL.

Public Instance Methods

button( cmd, icon_name = 'cogwheel' ) click to toggle source

Create a clickable icon. The following creates a clickable globe icon that starts firefox:

<%= i3_button( 'firefox', 'globe' ) %>
# File lib/dock_driver/template/dzen2.rb, line 132
def button( cmd, icon_name = 'cogwheel' )
    return "^ca(1,%s)%s^ca()" % [cmd, icon( icon_name )]
end
color_deg_c( value, colors = nil ) click to toggle source

Colorize a temperature reading in Celcius. Default thresholds are a conservative guess at maximum junction temperatures for most CPUs or GPUs. Takes a string or/containing a float or integer. Example:

<%= color_deg_c my_temp_probe %>
# File lib/dock_driver/template/dzen2.rb, line 58
def color_deg_c( value, colors = nil )
    colors ||= {
        0 =>  'blue',
        20 => 'cyan',
        60 => 'green',
        70 => 'yellow',
        90 => 'orange',
        nil => 'red'
    }
    return colorize( value, colors )
end
color_deg_f( value, colors = nil ) click to toggle source

Colorize a temperature reading in degrees Fahrenheit. Default thresholds are a conservative guess at maximum junction temperatures for most CPUs or GPUs. Takes a string or/containing a float or integer. Example:

<%= color_deg_f my_temp_probe %>
# File lib/dock_driver/template/dzen2.rb, line 78
def color_deg_f( value, colors = nil )
    colors ||= {
        32  =>  'blue',
        68  => 'cyan',
        140 => 'green',
        158 => 'yellow',
        194 => 'orange',
        nil => 'red'
    }
    return colorize( value, colors )
end
color_load( value, cores = 1.0, colors = nil ) click to toggle source

Colorize a system load number. Example:

<%= color_load( loadavg_one ) %>
# File lib/dock_driver/template/dzen2.rb, line 95
def color_load( value, cores = 1.0, colors = nil )
    colors ||= {
        0.1 => 'gray50',
        0.5 => 'gray80',
        0.7 => 'white',
        0.9 => 'green',
        1.2 => 'yellow',
        nil => 'red'
    }

    v = value.to_s.to_f / cores.to_f
    t = colors.keys.compact.sort.select do |i|
        next false unless i.is_a? Numeric
        i > v
    end
    color = colors[t.first]
  
    return value if color.nil?
    return "^fg(%s)%s^fg()" % [color,value]
rescue Exception
    return value
end
colorize( value, colors = {} ) click to toggle source

Colorize a string.

Takes a string or/containing a float or integer and hash of thresholds. The hash must be keyed by Numeric values and each value must be a color name. The hash must also have a color name associated with nil, which will be used for values greater than any threshold. For example:

{ 50 => 'green', 70 => 'yellow', nil => 'red' }

Returns the original value if it cannot be assigned a color. Example:

<%= colsorize item, { 1 => 'green', 5 => 'yellow', nil => 'red' } %>
# File lib/dock_driver/template/dzen2.rb, line 35
def colorize( value, colors = {} )
    # I'm lazy. Force conversion.
    v = value.to_s.to_f

    t = colors.keys.compact.sort.select do |i|
        next false unless i.is_a? Numeric
        i > v
    end
    color = colors[t.first]
    
    return value if color.nil?
    return "^fg(%s)%s^fg()" % [color,value]
rescue Exception
    return value
end
i3_button( cmd, icon_name = 'cogwheel' ) click to toggle source

Create a clickable icon. The following creates a clickable magic-wand icon that restarts i3:

<%= i3_button( 'restart', 'magic' ) %>
# File lib/dock_driver/template/dzen2.rb, line 123
def i3_button( cmd, icon_name = 'cogwheel' )
    return "^ca(1,i3-msg '%s')%s^ca()" % [cmd, icon( icon_name )]
end
icon( name ) click to toggle source

Return a string pointing to an XPM in this gem’s datadir.

There’s a library of 420 glyphicons haflings converted to xpm there for use. For now there are gray icons. To list the available names:

dock_driver --list-icons

Example:

<%= icon( 'cogwheel' ) %>
# File lib/dock_driver/template/dzen2.rb, line 16
def icon( name )
    return "^i(%s/icons/gray/%s.xpm)" % [DockDriver::DATADIR, name]
end