Class: DZEN::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/dzen/base.rb

Overview

Subclass this and define own options to implement a different output

Direct Known Subclasses

Default, Terminal

Constant Summary collapse

Config =
{
  :delimiter => '',
  :ending => '',
  :interval => 3,
  :output => $stdout,
  :output_method => :puts,
  :color => {
    :start => nil,
    :end => nil
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Base

Public: Initialize a new dzen2 output instance

config - A Hash containing the config keys

Returns a newly initialized DZEN::Base instance



29
30
31
32
33
34
35
# File 'lib/dzen/base.rb', line 29

def initialize(config={})
  @config = OpenStruct.new(self.class::Config.merge(config))

  @before_run_handler = []
  @apps = []
  @order = []
end

Instance Attribute Details

#configObject (readonly)

Gets the actual used config



22
23
24
# File 'lib/dzen/base.rb', line 22

def config
  @config
end

Instance Method Details

#add_before_run(&blk) ⇒ Object

Public: Add before_run handler.

It's possible to define more than one before_run handler.

blk - The block to be run when starting



52
53
54
# File 'lib/dzen/base.rb', line 52

def add_before_run(&blk)
  @before_run_handler << blk
end

#add_handler(name, options, &blk) ⇒ Object

Public: Add new handler for an app.

name - Name of the app. option - Some options [not used yet]. blk - The actual handler block.



70
71
72
# File 'lib/dzen/base.rb', line 70

def add_handler(name, options, &blk)
  @apps << [name, options||{}, blk]
end

#configure {|@config| ... } ⇒ Object

Public: Configure the instance.

The block will be yielded the current configuration.

Returns the config

Yields:



42
43
44
45
# File 'lib/dzen/base.rb', line 42

def configure
  yield @config
  @config
end

#order=(apps) ⇒ Object

Public: Set the order of apps.

apps - Array of app names in sorted order.

Returns the passed order array.



61
62
63
# File 'lib/dzen/base.rb', line 61

def order=(apps)
  @order = apps
end

#run!Object

Public: Run the instance in an endless loop

These endless loop may be stopped by sending it a SIGINT It runs the before_run handler first, then executes the defined app modules each interval



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dzen/base.rb', line 88

def run!
    trap(:INT) { @output.puts; exit 0; }

    sort_apps!

    @before_run_handler.each do |handler|
      @config.output.puts handler.call
    end

    loop do
      normal_text = @apps.map { |(name, options, callback)|
        if options[:cache]
          # TODO: implement the cache
          callback.call
        else
          callback.call
        end
      }.join(@config.delimiter)

      @config.output.send(@config.output_method, @config.start) if @config.start
      @config.output.print(normal_text)
      @config.output.send(@config.output_method, @config.ending) if @config.ending
      @config.output.flush
      sleep @config.interval
    end
  rescue Errno::EPIPE
  exit 0
end

#sort_apps!Object

Sort the apps as defined by @order Any not-listed app is not added to the actual output array



76
77
78
79
80
81
# File 'lib/dzen/base.rb', line 76

def sort_apps!
  return if @order.empty?
  @apps = @order.map do |app|
    @apps.find { |e| e.first == app }
  end
end