Module: DockDriver::Template::Dzen2

Included in:
DockDriver::Template
Defined in:
lib/dock_driver/template/dzen2.rb

Overview

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

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Extend the including object with ClassMethods



149
150
151
# File 'lib/dock_driver/template/dzen2.rb', line 149

def self::included( klass ) #:nodoc:
    klass.extend ClassMethods
end

Instance Method Details

#button(cmd, icon_name = 'cogwheel') ⇒ Object

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

<%= i3_button( 'firefox', 'globe' ) %>


132
133
134
# File 'lib/dock_driver/template/dzen2.rb', line 132

def button( cmd, icon_name = 'cogwheel' )
    return "^ca(1,i3-msg exec '%s')%s^ca()" % [cmd, icon( icon_name )]
end

#color_deg_c(value, colors = nil) ⇒ Object

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 %>


58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object

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 %>


78
79
80
81
82
83
84
85
86
87
88
# 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) ⇒ Object

Colorize a system load number. Example:

<%= color_load( loadavg_one ) %>


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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 = {}) ⇒ Object

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' } %>


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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') ⇒ Object

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

<%= i3_button( 'restart', 'magic' ) %>


123
124
125
# 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) ⇒ Object

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' ) %>


16
17
18
# File 'lib/dock_driver/template/dzen2.rb', line 16

def icon( name )
    return "^i(%s/icons/gray/%s.xpm)" % [DockDriver::DATADIR, name]
end