Class: DockDriver::Dock
- Inherits:
-
Object
- Object
- DockDriver::Dock
- 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
-
.command ⇒ Object
The dock command to run and pipe the template result to.
-
.config ⇒ Object
The loaded config.
-
.icon_color ⇒ Object
Icon Color Set.
-
.items ⇒ Object
The configuration for each user-specified DockItem.
-
.template ⇒ Object
The dock’s output raw ERB template string.
Instance Attribute Summary collapse
-
#items ⇒ Object
A hash of the dock items in use.
Class Method Summary collapse
-
.configure(section) ⇒ Object
Configure the class.
Instance Method Summary collapse
-
#initialize ⇒ Dock
constructor
A bog-standard constructor.
-
#kill ⇒ Object
Shut down the dock gracefully.
-
#pipe ⇒ Object
Lazily execute the dock command.
-
#restart ⇒ Object
Restart the dock.
-
#run ⇒ Object
Start or restart the dock.
-
#update(obj = self) ⇒ Object
Listen to this dock’s items, printing to the dock command when appropriate.
Constructor Details
#initialize ⇒ Dock
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
.command ⇒ Object
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 |
.config ⇒ Object
The loaded config.
24 25 26 |
# File 'lib/dock_driver/dock.rb', line 24 def config @config end |
.icon_color ⇒ Object
Icon Color Set
32 33 34 |
# File 'lib/dock_driver/dock.rb', line 32 def icon_color @icon_color end |
.items ⇒ Object
The configuration for each user-specified DockItem.
28 29 30 |
# File 'lib/dock_driver/dock.rb', line 28 def items @items end |
.template ⇒ Object
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
#items ⇒ Object
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
#kill ⇒ Object
Shut down the dock gracefully.
111 112 113 114 115 116 117 |
# File 'lib/dock_driver/dock.rb', line 111 def kill 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 end |
#pipe ⇒ Object
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 |
#restart ⇒ Object
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 |
#run ⇒ Object
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. $stderr.puts e.backtrace exit end end end |