ruby-dzen 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +21 -0
- data/README.md +128 -0
- data/Rakefile +16 -0
- data/lib/dzen.rb +38 -0
- data/lib/dzen/base.rb +170 -0
- data/lib/dzen/helpers.rb +46 -0
- data/lib/dzen/macros.rb +70 -0
- data/test/helper.rb +38 -0
- data/test/test_base.rb +74 -0
- data/test/test_helper.rb +33 -0
- metadata +73 -0
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright (c) Tom Preston-Werner, Rick Olson
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the 'Software'), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
# ruby-dzen
|
2
|
+
|
3
|
+
A small wrapper for [dzen2][dzen]'s in-text formatting
|
4
|
+
|
5
|
+
You can define what dzen2 displays by using pure ruby code.
|
6
|
+
|
7
|
+
The DSL is heavily inspired by [Sinatra][].
|
8
|
+
|
9
|
+
## Simple DSL
|
10
|
+
There are just 4 methods you need to know: `app`, `configure`, `before_run` and `order`
|
11
|
+
|
12
|
+
The simplest way to use ruby-dzen is to define a simple app:
|
13
|
+
|
14
|
+
app :clock do
|
15
|
+
Time.now.strftime("%d.%m.%Y %H:%M")
|
16
|
+
end
|
17
|
+
|
18
|
+
Each of these `app` blocks must return a string to be displayed.
|
19
|
+
|
20
|
+
You can easily configure the output:
|
21
|
+
|
22
|
+
configure do |c|
|
23
|
+
c.interval = 3
|
24
|
+
end
|
25
|
+
|
26
|
+
There are several options configurable. See [dzen/base.rb][base.rb] for the defaults. ruby-dzen comes with two pre-defined option sets:
|
27
|
+
|
28
|
+
* One for real dzen2-formatted text including colors and delimiters. This is used by default.
|
29
|
+
* One for simple text console output using escape sequences to display colors. Just define TERMINAL in your code or via commandline. (I will change this to a more ruby-ish way, soon)
|
30
|
+
|
31
|
+
You can define a handler to be run beforehand, for example to display a short loading message:
|
32
|
+
|
33
|
+
before_run do
|
34
|
+
"--- Loading ---"
|
35
|
+
end
|
36
|
+
|
37
|
+
The output order will either be in the order as the apps are defined in your app file or you can sort them using the `order` method:
|
38
|
+
|
39
|
+
order :clock, :loadavg
|
40
|
+
|
41
|
+
If you define an app but you don't define it in your order list, it won't be displayed at all.
|
42
|
+
|
43
|
+
See the [example file][example] for a already working script.
|
44
|
+
|
45
|
+
## Run it!
|
46
|
+
|
47
|
+
As I'm to lazy for now to create a proper gem, just clone my repo and write your own small app collection.
|
48
|
+
|
49
|
+
Then run it with
|
50
|
+
|
51
|
+
ruby -Ipath/to/ruby-dzen/lib yourscript.rb | dzen2
|
52
|
+
|
53
|
+
Make sure you have the svn-Version of dzen2, as it has some extra things which are not in the released packages:
|
54
|
+
|
55
|
+
svn checkout http://dzen.googlecode.com/svn/trunk/ dzen
|
56
|
+
|
57
|
+
You can then set dzen2's output by its commandline options. For example change the used font with:
|
58
|
+
|
59
|
+
... | dzen2 -fn "-*-terminus-medium-r-normal--14-120-75-75-C-70-iso8859-1"
|
60
|
+
|
61
|
+
See [dzen2's documentation][dzen] for all possible options.
|
62
|
+
|
63
|
+
If you want to use the text console output (for debugging or whatever) run it as:
|
64
|
+
|
65
|
+
TERMINAL=1 ruby -Ipath/to/ruby-dzen/lib yourscript.rb
|
66
|
+
|
67
|
+
This will change soon to a more ruby-ish way, I hope.
|
68
|
+
|
69
|
+
## Helpers
|
70
|
+
|
71
|
+
As it is all pure ruby code, just define a method in your code and call it within your app module.
|
72
|
+
|
73
|
+
I already wrote 2 small helper functions: `_color` and `color_critical`.
|
74
|
+
|
75
|
+
`_color` colorizes a given string using the callbacks defined by DZEN::Base (or its subclass).
|
76
|
+
|
77
|
+
`color_critical` colorizes a number value based on wether it's below a critical value or not.
|
78
|
+
|
79
|
+
See the docu for their arguments in [dzen/helpers.rb][helpers.rb].
|
80
|
+
|
81
|
+
To use them in your code just do the following in your script:
|
82
|
+
|
83
|
+
include DZEN::Helpers
|
84
|
+
|
85
|
+
and use them in your apps.
|
86
|
+
|
87
|
+
## New outputs
|
88
|
+
|
89
|
+
To define a new output class, just subclass DZEN::Base and make sure your class defines the `Config` constant overwriting the existent config keys.
|
90
|
+
|
91
|
+
This is exactly the way DZEN::Default and DZEN::Terminal are defined. See [dzen/base.rb][base.rb].
|
92
|
+
|
93
|
+
## ToDo
|
94
|
+
|
95
|
+
* gemify it!
|
96
|
+
* caching of apps (nearly finished, just need a proper API)
|
97
|
+
* more ruby-ish way to switch output class
|
98
|
+
|
99
|
+
Feel free to implement what you need and let me know about your changes.
|
100
|
+
|
101
|
+
## License
|
102
|
+
|
103
|
+
The code is released under the MIT license. See [LICENSE][].
|
104
|
+
|
105
|
+
## Contribute
|
106
|
+
|
107
|
+
If you'd like to hack on ruby-dzen, start by forking my repo on GitHub:
|
108
|
+
|
109
|
+
http://github.com/badboy/ruby-dzen
|
110
|
+
|
111
|
+
ruby-dzen has no external dependencies other than dzen2 itself.
|
112
|
+
|
113
|
+
1. Clone down your fork
|
114
|
+
1. Create a thoughtfully named topic branch to contain your change
|
115
|
+
1. Hack away
|
116
|
+
1. Add tests and make sure everything still passes by running `rake`
|
117
|
+
1. If you are adding new functionality, document it in the README
|
118
|
+
1. Do not change the version number, I will do that on my end
|
119
|
+
1. If necessary, rebase your commits into logical chunks, without errors
|
120
|
+
1. Push the branch up to GitHub
|
121
|
+
1. Send me (badboy) a pull request for your branch
|
122
|
+
|
123
|
+
[dzen]: http://dzen.geekmode.org/dwiki/doku.php
|
124
|
+
[base.rb]: http://github.com/badboy/ruby-dzen/blob/master/lib/dzen/base.rb
|
125
|
+
[helpers.rb]: http://github.com/badboy/ruby-dzen/blob/master/lib/dzen/helpers.rb
|
126
|
+
[sinatra]: http://www.sinatrarb.com/
|
127
|
+
[LICENSE]: http://github.com/badboy/ruby-dzen/blob/master/LICENSE
|
128
|
+
[example]: http://github.com/badboy/ruby-dzen/blob/master/example/sample.rb
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
begin
|
3
|
+
require 'mg'
|
4
|
+
rescue LoadError
|
5
|
+
abort "Please `gem install mg`"
|
6
|
+
end
|
7
|
+
|
8
|
+
MG.new("ruby-dzen.gemspec")
|
9
|
+
|
10
|
+
Rake::TestTask.new(:test) do |test|
|
11
|
+
test.libs << 'lib' << 'test'
|
12
|
+
test.pattern = 'test/**/test_*.rb'
|
13
|
+
test.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
task :default => :test
|
data/lib/dzen.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'dzen/helpers'
|
4
|
+
require 'dzen/macros'
|
5
|
+
require 'dzen/base'
|
6
|
+
|
7
|
+
module DZEN
|
8
|
+
VERSION = '0.0.1'
|
9
|
+
|
10
|
+
@@app_file = lambda do
|
11
|
+
ignore = [
|
12
|
+
/lib\/dzen.*\.rb/, # Library
|
13
|
+
/\(.*\)/, # Generated code
|
14
|
+
/custom_require\.rb/ # RubyGems require
|
15
|
+
]
|
16
|
+
|
17
|
+
path = caller.map { |line| line.split(/:\d/, 2).first }.find do |file|
|
18
|
+
next if ignore.any? { |pattern| file =~ pattern }
|
19
|
+
file
|
20
|
+
end
|
21
|
+
|
22
|
+
path || $0
|
23
|
+
end.call
|
24
|
+
|
25
|
+
#
|
26
|
+
# File name of the application file. Inspired by Sinatra
|
27
|
+
#
|
28
|
+
def self.app_file
|
29
|
+
@@app_file
|
30
|
+
end
|
31
|
+
|
32
|
+
#
|
33
|
+
# Runs application if application file is the script being executed
|
34
|
+
#
|
35
|
+
def self.run?
|
36
|
+
self.app_file == $0
|
37
|
+
end
|
38
|
+
end
|
data/lib/dzen/base.rb
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'ostruct'
|
4
|
+
|
5
|
+
module DZEN
|
6
|
+
# Subclass this and define own options
|
7
|
+
# to implement a different output
|
8
|
+
class Base
|
9
|
+
Config = {
|
10
|
+
:delimiter => '',
|
11
|
+
:ending => '',
|
12
|
+
:interval => 3,
|
13
|
+
:output => $stdout,
|
14
|
+
:output_method => :puts,
|
15
|
+
:color => {
|
16
|
+
:start => nil,
|
17
|
+
:end => nil
|
18
|
+
}
|
19
|
+
}
|
20
|
+
|
21
|
+
# Gets the actual used config
|
22
|
+
attr_reader :config
|
23
|
+
|
24
|
+
# Public: Initialize a new dzen2 output instance
|
25
|
+
#
|
26
|
+
# config - A Hash containing the config keys
|
27
|
+
#
|
28
|
+
# Returns a newly initialized DZEN::Base instance
|
29
|
+
def initialize(config={})
|
30
|
+
@config = OpenStruct.new(self.class::Config.merge(config))
|
31
|
+
|
32
|
+
@before_run_handler = []
|
33
|
+
@apps = []
|
34
|
+
@order = []
|
35
|
+
end
|
36
|
+
|
37
|
+
# Public: Configure the instance.
|
38
|
+
#
|
39
|
+
# The block will be yielded the current configuration.
|
40
|
+
#
|
41
|
+
# Returns the config
|
42
|
+
def configure
|
43
|
+
yield @config
|
44
|
+
@config
|
45
|
+
end
|
46
|
+
|
47
|
+
# Public: Add before_run handler.
|
48
|
+
#
|
49
|
+
# It's possible to define more than one before_run handler.
|
50
|
+
#
|
51
|
+
# blk - The block to be run when starting
|
52
|
+
def add_before_run(&blk)
|
53
|
+
@before_run_handler << blk
|
54
|
+
end
|
55
|
+
|
56
|
+
# Public: Set the order of apps.
|
57
|
+
#
|
58
|
+
# apps - Array of app names in sorted order.
|
59
|
+
#
|
60
|
+
# Returns the passed order array.
|
61
|
+
def order=(apps)
|
62
|
+
@order = apps
|
63
|
+
end
|
64
|
+
|
65
|
+
# Public: Add new handler for an app.
|
66
|
+
#
|
67
|
+
# name - Name of the app.
|
68
|
+
# option - Some options [not used yet].
|
69
|
+
# blk - The actual handler block.
|
70
|
+
def add_handler(name, options, &blk)
|
71
|
+
@apps << [name, options||{}, blk]
|
72
|
+
end
|
73
|
+
|
74
|
+
# Sort the apps as defined by @order
|
75
|
+
# Any not-listed app is not added to the actual output array
|
76
|
+
def sort_apps!
|
77
|
+
return if @order.empty?
|
78
|
+
@apps = @order.map do |app|
|
79
|
+
@apps.find { |e| e.first == app }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# Public: Run the instance in an endless loop
|
84
|
+
#
|
85
|
+
# These endless loop may be stopped by sending it a SIGINT
|
86
|
+
# It runs the before_run handler first, then executes the
|
87
|
+
# defined app modules each interval
|
88
|
+
def run!
|
89
|
+
trap(:INT) { @output.puts; exit 0; }
|
90
|
+
|
91
|
+
sort_apps!
|
92
|
+
|
93
|
+
@before_run_handler.each do |handler|
|
94
|
+
@config.output.puts handler.call
|
95
|
+
end
|
96
|
+
|
97
|
+
loop do
|
98
|
+
normal_text = @apps.map { |(name, options, callback)|
|
99
|
+
if options[:cache]
|
100
|
+
# TODO: implement the cache
|
101
|
+
callback.call
|
102
|
+
else
|
103
|
+
callback.call
|
104
|
+
end
|
105
|
+
}.join(@config.delimiter)
|
106
|
+
|
107
|
+
@config.output.send(@config.output_method, @config.start) if @config.start
|
108
|
+
@config.output.print(normal_text)
|
109
|
+
@config.output.send(@config.output_method, @config.ending) if @config.ending
|
110
|
+
@config.output.flush
|
111
|
+
sleep @config.interval
|
112
|
+
end
|
113
|
+
rescue Errno::EPIPE
|
114
|
+
exit 0
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# Default Dzen2 output
|
119
|
+
# The in-text formating commands are used as defined at
|
120
|
+
# http://dzen.geekmode.org/dwiki/doku.php?id=dzen:command-and-option-list
|
121
|
+
class Default < Base
|
122
|
+
Config = {
|
123
|
+
:delimiter => "^fg(#333333)^p(5;-2)^ro(2)^p()^fg()^p(5)",
|
124
|
+
:ending => "^p(6)\n",
|
125
|
+
:interval => 3,
|
126
|
+
:output => $stdout,
|
127
|
+
:output_method => :puts,
|
128
|
+
:color => {
|
129
|
+
:start => proc{|c| "^fg(#{c})"},
|
130
|
+
:end => proc{|c| "^fg()" }
|
131
|
+
}
|
132
|
+
}
|
133
|
+
end
|
134
|
+
|
135
|
+
# Simple Terminal-aware output.
|
136
|
+
# May be used within a simple "watch" command
|
137
|
+
# Maybe integration in screen is possible
|
138
|
+
class Terminal < Base
|
139
|
+
Colors = {
|
140
|
+
:black => 30,
|
141
|
+
:red => 31,
|
142
|
+
:green => 32,
|
143
|
+
:yellow => 33,
|
144
|
+
:blue => 34,
|
145
|
+
:magenta => 35,
|
146
|
+
:cyan => 36,
|
147
|
+
:white => 37
|
148
|
+
}
|
149
|
+
|
150
|
+
Config = {
|
151
|
+
:delimiter => " | ",
|
152
|
+
:start => "\r",
|
153
|
+
:interval => 3,
|
154
|
+
:output => $stdout,
|
155
|
+
:output_method => :print,
|
156
|
+
:color => {
|
157
|
+
:start => proc{|c|
|
158
|
+
Colors[c] && "\e[#{Colors[c]}m"
|
159
|
+
},
|
160
|
+
:end => proc{|c| Colors[c] && "\e[0m" }
|
161
|
+
}
|
162
|
+
}
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
# Expose DSL
|
167
|
+
include DZEN::Macros
|
168
|
+
|
169
|
+
# Run DZEN instance if any
|
170
|
+
at_exit { @@dzen.run! if $!.nil? && run? }
|
data/lib/dzen/helpers.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module DZEN
|
4
|
+
# Some small helpers used in app module outputs.
|
5
|
+
#
|
6
|
+
# You have to
|
7
|
+
# include DZEN::Helpers
|
8
|
+
# in your app file to actually use them.
|
9
|
+
module Helpers
|
10
|
+
|
11
|
+
# Public: Colorize a given string using the callbacks
|
12
|
+
# defined by DZEN::Base (or its subclass).
|
13
|
+
#
|
14
|
+
# c - The color string, like "red" or "#ff0000"
|
15
|
+
# Make sure the implementation of config.color callbacks
|
16
|
+
# can interpret this colors.
|
17
|
+
# text - The text to be colored.
|
18
|
+
#
|
19
|
+
# Returns the colored string.
|
20
|
+
def _color(c, text)
|
21
|
+
config = dzen.config.color
|
22
|
+
%|#{config[:start].call(c)}#{text}#{config[:end].call(c)}|
|
23
|
+
end
|
24
|
+
|
25
|
+
# Public: Colorize a number based on wether it's
|
26
|
+
# below a critical value or not.
|
27
|
+
#
|
28
|
+
# n - The number to colorize.
|
29
|
+
# critical - The critical value.
|
30
|
+
# options - A Hash of colors for the different colors
|
31
|
+
# :normal - for the value equal or below `critical`
|
32
|
+
# :critical - for the value above `critical`
|
33
|
+
#
|
34
|
+
# Returns the colored string.
|
35
|
+
def color_critical(n, critical, options = {})
|
36
|
+
options = { :normal => "#ff8700", :critical => "red" }.merge(options)
|
37
|
+
if n.to_i == 0
|
38
|
+
n.to_s
|
39
|
+
elsif n.to_i < critical
|
40
|
+
_color(options[:normal], n.to_i)
|
41
|
+
else
|
42
|
+
_color(options[:critical], n.to_i)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/dzen/macros.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module DZEN
|
4
|
+
module Macros
|
5
|
+
def self.included(mod)
|
6
|
+
@@dzen = nil
|
7
|
+
end
|
8
|
+
|
9
|
+
# Public: Configure the instance.
|
10
|
+
#
|
11
|
+
# The block will be yielded the current configuration.
|
12
|
+
#
|
13
|
+
# Example:
|
14
|
+
# configure do |c|
|
15
|
+
# c.interval = 3
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# Returns the config
|
19
|
+
def configure(&blk)
|
20
|
+
dzen.configure(&blk)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Public: Set the order of apps.
|
24
|
+
#
|
25
|
+
# apps - App names in sorted order.
|
26
|
+
#
|
27
|
+
# Returns the passed order array.
|
28
|
+
def order(*apps)
|
29
|
+
dzen.order = apps
|
30
|
+
end
|
31
|
+
|
32
|
+
# Public: Add before_run handler.
|
33
|
+
#
|
34
|
+
# blk - The block to be run when starting
|
35
|
+
def before_run(&blk)
|
36
|
+
dzen.add_before_run(&blk)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Public: Add new handler for an app.
|
40
|
+
#
|
41
|
+
# name - Name of the app.
|
42
|
+
# option - Some options [not used yet].
|
43
|
+
# blk - The actual handler block.
|
44
|
+
def app(name, options=nil, &blk)
|
45
|
+
dzen.add_handler(name, options, &blk)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Public: Gets the current DZEN instance.
|
49
|
+
#
|
50
|
+
# Define TERMINAL or ENV['TERMINAL'] to use DZEN::Terminal
|
51
|
+
# instead of DZEN::Default.
|
52
|
+
#
|
53
|
+
# Returns a newly created or currently existing DZEN instance.
|
54
|
+
def dzen
|
55
|
+
return @@dzen unless @@dzen.nil?
|
56
|
+
|
57
|
+
@@dzen = DZEN::Terminal.new if defined?(::TERMINAL) || !!ENV['TERMINAL']
|
58
|
+
@@dzen = DZEN::Default.new unless @@dzen
|
59
|
+
|
60
|
+
@@dzen
|
61
|
+
end
|
62
|
+
|
63
|
+
# Public: Gets wether to run the DZEN output at exit or not.
|
64
|
+
#
|
65
|
+
# Returns a boolean.
|
66
|
+
def run?
|
67
|
+
!!@@dzen
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
require 'mocha'
|
5
|
+
require 'fileutils'
|
6
|
+
|
7
|
+
dir = File.dirname(File.expand_path(__FILE__))
|
8
|
+
$LOAD_PATH.unshift(File.join(dir, '..', 'lib'))
|
9
|
+
$LOAD_PATH.unshift(dir)
|
10
|
+
|
11
|
+
require 'dzen'
|
12
|
+
|
13
|
+
# Make sure we're in the test dir, the tests expect that to be the current
|
14
|
+
# directory.
|
15
|
+
TEST_DIR = File.join(File.dirname(__FILE__), *%w[.])
|
16
|
+
|
17
|
+
def testpath(path)
|
18
|
+
File.join(TEST_DIR, path)
|
19
|
+
end
|
20
|
+
|
21
|
+
# test/spec/mini 3
|
22
|
+
# http://gist.github.com/25455
|
23
|
+
# chris@ozmm.org
|
24
|
+
# file:lib/test/spec/mini.rb
|
25
|
+
def context(*args, &block)
|
26
|
+
return super unless (name = args.first) && block
|
27
|
+
require 'test/unit'
|
28
|
+
klass = Class.new(defined?(ActiveSupport::TestCase) ? ActiveSupport::TestCase : Test::Unit::TestCase) do
|
29
|
+
def self.test(name, &block)
|
30
|
+
define_method("test_#{name.gsub(/\W/,'_')}", &block) if block
|
31
|
+
end
|
32
|
+
def self.xtest(*args) end
|
33
|
+
def self.setup(&block) define_method(:setup, &block) end
|
34
|
+
def self.teardown(&block) define_method(:teardown, &block) end
|
35
|
+
end
|
36
|
+
(class << klass; self end).send(:define_method, :name) { name.gsub(/\W/,'_') }
|
37
|
+
klass.class_eval &block
|
38
|
+
end
|
data/test/test_base.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
context "Base" do
|
4
|
+
setup do
|
5
|
+
@base = DZEN::Base.new
|
6
|
+
end
|
7
|
+
|
8
|
+
test "configure" do
|
9
|
+
@base.configure { |c| c.interval = 42 }
|
10
|
+
assert_equal 42, @base.config.interval
|
11
|
+
end
|
12
|
+
|
13
|
+
test "add before_run handler" do
|
14
|
+
@base.add_before_run { 42 }
|
15
|
+
assert @base.instance_eval{@before_run_handler}
|
16
|
+
assert_equal 1, @base.instance_eval{@before_run_handler}.length
|
17
|
+
assert_equal 42, @base.instance_eval{@before_run_handler}.first.call
|
18
|
+
end
|
19
|
+
|
20
|
+
test "set order" do
|
21
|
+
@base.order = [:cpu, :load, :clock]
|
22
|
+
assert_equal [:cpu, :load, :clock], @base.instance_eval{@order}
|
23
|
+
end
|
24
|
+
|
25
|
+
test "add app handler" do
|
26
|
+
@base.add_handler(:cpu, {}) { }
|
27
|
+
assert_equal 1, @base.instance_eval{@apps}.size
|
28
|
+
@base.add_handler(:load, {}) { }
|
29
|
+
assert_equal 2, @base.instance_eval{@apps}.size
|
30
|
+
end
|
31
|
+
|
32
|
+
test "sort apps" do
|
33
|
+
@base.add_handler(:load, {}) { }
|
34
|
+
@base.add_handler(:cpu, {}) { }
|
35
|
+
assert_equal [:load, :cpu], @base.instance_eval{@apps}.map{|a|a.first}
|
36
|
+
@base.order = [:cpu, :load]
|
37
|
+
@base.sort_apps!
|
38
|
+
assert_equal [:cpu, :load], @base.instance_eval{@apps}.map{|a|a.first}
|
39
|
+
end
|
40
|
+
|
41
|
+
class OutputHandler
|
42
|
+
attr_accessor :output
|
43
|
+
def initialize
|
44
|
+
@output = ''
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_s
|
48
|
+
@output.to_s
|
49
|
+
end
|
50
|
+
|
51
|
+
def puts(msg)
|
52
|
+
@output << msg << "\n"
|
53
|
+
end
|
54
|
+
|
55
|
+
def print(msg)
|
56
|
+
@output << msg
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
test "run it" do
|
61
|
+
output = OutputHandler.new
|
62
|
+
@base.configure do |c|
|
63
|
+
c.interval = 0.1
|
64
|
+
c.delimiter = '|'
|
65
|
+
c.output = output
|
66
|
+
end
|
67
|
+
@base.add_handler(:one, {}) { "1" }
|
68
|
+
@base.add_handler(:two, {}) { "2" }
|
69
|
+
t = Thread.new { @base.run! }
|
70
|
+
sleep @base.config.interval*2
|
71
|
+
t.kill
|
72
|
+
assert_equal "1|2\n", output.to_s
|
73
|
+
end
|
74
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
context "Base" do
|
5
|
+
class TestClass
|
6
|
+
include DZEN::Helpers
|
7
|
+
|
8
|
+
def dzen
|
9
|
+
OpenStruct.new({
|
10
|
+
:config => OpenStruct.new({:color => {
|
11
|
+
:start => proc{|c|"<c(#{c})>"},
|
12
|
+
:end => proc{|c|"</c(#{c})>"}
|
13
|
+
}
|
14
|
+
})
|
15
|
+
})
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
setup do
|
20
|
+
@test = TestClass.new
|
21
|
+
end
|
22
|
+
|
23
|
+
test "_color" do
|
24
|
+
assert_equal %|<c(red)>foo</c(red)>|, @test._color('red', 'foo')
|
25
|
+
end
|
26
|
+
|
27
|
+
test "color_critical" do
|
28
|
+
colors = { :normal => 'white', :critical => 'red' }
|
29
|
+
assert_equal %|0|, @test.color_critical(0, 0, colors)
|
30
|
+
assert_equal %|<c(white)>5</c(white)>|, @test.color_critical(5, 6, colors)
|
31
|
+
assert_equal %|<c(red)>7</c(red)>|, @test.color_critical(7, 6, colors)
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-dzen
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- badboy
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-09-18 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: " ruby-dzen is a small wrapper for dzen2's in-text formatting\n With its simple DSL you can define what dzen2 display by using pure ruby code.\n\n Just define your app modules in a file, execute it and pipe the output to dzen.\n\n The simplest example is the following:\n app :hello do\n \"hello\"\n end\n"
|
22
|
+
email: badboy@archlinux.us
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- README.md
|
31
|
+
- Rakefile
|
32
|
+
- LICENSE
|
33
|
+
- lib/dzen.rb
|
34
|
+
- lib/dzen/macros.rb
|
35
|
+
- lib/dzen/base.rb
|
36
|
+
- lib/dzen/helpers.rb
|
37
|
+
- test/test_helper.rb
|
38
|
+
- test/helper.rb
|
39
|
+
- test/test_base.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/badboy/ruby-dzen
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.7
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: A small wrapper DSL for dzen2's in-text formatting
|
72
|
+
test_files: []
|
73
|
+
|