Commits

Mike Hix committed 00be3ab

Rewrite begins.

  • Participants
  • Parent commits 8750bea

Comments (0)

Files changed (9)

+#!/usr/bin/env ruby
+
+require 'rubygems'
+require 'dock_driver'
+
+DockDriver::ShellCommand.run ARGV

lib/dock_driver.rb

+require 'rubygems'
+require 'loggability'
+
+# A namespace for the project.
+module DockDriver
+
+    require 'dock_driver/mixins'
+    require 'dock_driver/constants'
+    require 'dock_driver/i3'
+    require 'dock_driver/dock'
+    require 'dock_driver/dock_item'
+    require 'dock_driver/workspace_pager'
+    require 'dock_driver/shell_command'
+
+    include Constants
+    extend Loggability
+
+    log_as :dock_driver
+
+    class UserInput < DockItem
+
+        extend Loggability
+        log_to :dock_driver
+
+        def run
+            return if self.running
+            @running = true
+
+            self.log.debug "%s is running." % self.name
+            loop do
+                self.var = gets
+                self.fire
+            end
+        end
+
+    end
+
+end

lib/dock_driver/constants.rb

+module DockDriver::Constants
+
+    VERSION = '0.2.0'
+    REVISION = %$Revision$
+
+end

lib/dock_driver/dock.rb

+class DockDriver::Dock
+
+        @@render_lock = Mutex.new
+
+        extend Loggability
+        log_to :dock_driver
+
+        include Observer
+
+        attr_accessor :items
+        attr_reader :running
+
+        def initialize
+            @items = []
+        end
+
+        def add_item( p )
+            self.items << p
+            p.add_observer self
+        end
+
+        def observe
+            @@render_lock.synchronize do
+                # TODO - output to a process
+                puts self.to_s
+            end
+        end
+
+        # TODO can the run/running code be factored out to a module?
+        def run
+            return if self.running
+            @running = true
+
+            self.items.inject( [] ) do |threads,poll|
+                threads << Thread.new { poll.run }.run
+                threads
+            end.map( &:join )
+        end
+
+        def to_s
+            # TODO - templating engine
+            # TODO - cache the template to avoid rendering unnecessarily
+            return items.map( &:to_s ).join( ' ' )
+        end
+
+end

lib/dock_driver/dock_item.rb

+class DockDriver::DockItem
+
+    extend Loggability
+    log_to :dock_driver
+
+    include Subject
+
+    attr_accessor :name
+    attr_accessor :var
+    attr_accessor :listeners
+    attr_reader :running
+
+    def initialize( name )
+        @name = name
+        @var = 0
+    end
+
+    # TODO can the run/running code be factored out to a module?
+    def run
+        return if self.running
+        @running = true
+
+        self.log.debug "%s is running." % self.name
+        loop do
+            sleep 1.0 + rand
+            self.var += 1
+            self.notify_observers
+        end
+    end
+
+    def to_s
+        # TODO - templating engine
+        # TODO - cache the template to avoid rendering unnecessarily
+        return "%s: %s" % [self.name, self.var]
+    end
+
+end

lib/dock_driver/i3.rb

Empty file added.

lib/dock_driver/mixins.rb

+module DockDriver::Observer
+    def observe
+        raise '%s.observe must be defined.' % self.class.name
+    end
+end
+
+module DockDriver::Subject
+
+    extend Loggability
+    log_to :dock_driver
+
+    def observers
+        return @observers ||= []
+    end
+
+    def add_observer( obj )
+        observers ||= []
+        if obj.respond_to? :observe
+            self.observers << obj
+        else
+            raise "Object must respond to :observe: %p" % [obj]
+        end
+    end
+
+    def notify_observers
+        return unless self.observers
+        self.observers.each do |observer|
+            observer.observe
+        end
+    end
+
+end

lib/dock_driver/shell_command.rb

+class DockDriver::ShellCommand
+
+    extend Loggability
+    log_to :dock_driver
+
+    def self::run( args = {} )
+        self.log.debug 'Running.'
+        dock = Dock.new
+
+        self.log.debug 'Adding items.'
+        name = 'a'
+        3.times do
+            dock.add_item DockItem.new name.dup
+            name.next!
+        end
+
+        dock.add_item UserInput.new "User Input"
+
+        self.log.debug 'Running the dock.'
+        dock.run
+    end
+
+end

lib/dock_driver/workspace_pager.rb

+class DockDriver::WorkspacePager
+end