Class: DockDriver::Dock

Inherits:
Object
  • Object
show all
Extended by:
Configurability, Loggability
Includes:
Singleton
Defined in:
lib/dock_driver/dock.rb

Overview

Manages the dock process and templating.

Constant Summary collapse

CONFIG_DEFAULTS =

The default configuration for this class.

{
  :items => [],
  :command => 'dzen2 -dock -fg white -bg black -ta l',
  :template => '<%= time %>'
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDock

A bog-standard constructor.



58
59
60
61
# File 'lib/dock_driver/dock.rb', line 58

def initialize #:nodoc:
  @items = {}
  @lock = Mutex.new
end

Class Attribute Details

.commandObject

The dock command to run and pipe the template result to.



26
27
28
# File 'lib/dock_driver/dock.rb', line 26

def command
  @command
end

.configObject

The loaded config.



24
25
26
# File 'lib/dock_driver/dock.rb', line 24

def config
  @config
end

.icon_colorObject

Icon Color Set



32
33
34
# File 'lib/dock_driver/dock.rb', line 32

def icon_color
  @icon_color
end

.itemsObject

The configuration for each user-specified DockItem.



28
29
30
# File 'lib/dock_driver/dock.rb', line 28

def items
  @items
end

.templateObject

The dock’s output raw ERB template string.



30
31
32
# File 'lib/dock_driver/dock.rb', line 30

def template
  @template
end

Instance Attribute Details

#itemsObject

A hash of the dock items in use.



55
56
57
# File 'lib/dock_driver/dock.rb', line 55

def items
  @items
end

Class Method Details

.configure(section) ⇒ Object

Configure the class.



43
44
45
46
47
48
# File 'lib/dock_driver/dock.rb', line 43

def self::configure( section )
  self.config   = CONFIG_DEFAULTS.merge( section || {} )
  self.command  = self.config[:command]
  self.items    = self.config[:items] || {}
  self.template = self.config[:template]
end

Instance Method Details

#killObject

Shut down the dock gracefully.



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/dock_driver/dock.rb', line 111

def kill
  begin
    self.log.debug 'Killed.'
  rescue ThreadError
    # We can't write to the log from the trap context.
  end
  self.items.map { |name,item| item.thread.kill }
  self.items.clear
  @pipe.close if @pipe and not @pipe.closed?
  @pipe = nil
end

#pipeObject

Lazily execute the dock command.



64
65
66
# File 'lib/dock_driver/dock.rb', line 64

def pipe
  return @pipe ||= IO.popen( self.class.command, 'w' )
end

#restartObject

Restart the dock.



101
102
103
104
105
106
107
108
# File 'lib/dock_driver/dock.rb', line 101

def restart
  self.log.debug 'Killed.'
  self.items.map { |name,item| item.thread.kill }
  self.items.clear
  @pipe.close if @pipe and not @pipe.closed?
  @pipe = nil
  self.run
end

#runObject

Start or restart the dock.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/dock_driver/dock.rb', line 85

def run
  self.log.debug "Building items from config."

  self.items.map { |name,item| item.thread.kill }
  self.items = self.class.items.inject( {} ) do |hash,opts|
    item = DockItem.new( opts )
    item.add_observer self
    hash[item.name] = item
    hash
  end

  self.log.debug "Starting items."
  self.items.map { |name,item| item.thread.run }
end

#update(obj = self) ⇒ Object

Listen to this dock’s items, printing to the dock command when appropriate.



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/dock_driver/dock.rb', line 70

def update( obj = self )
  @lock.synchronize do
    begin
      self.pipe.puts Template.render(
        self.class.template, self.items )
    rescue Exception => e
      self.log.error "Unable to write to the dock."
      self.log.error e.message
      $stderr.puts e.backtrace
      exit
    end
  end
end