inform 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +47 -0
- data/Rakefile +1 -0
- data/inform.gemspec +21 -0
- data/lib/inform/version.rb +3 -0
- data/lib/inform.rb +77 -0
- data/spec/inform_spec.rb +113 -0
- data/spec/spec_helper.rb +2 -0
- metadata +108 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Inform
|
2
|
+
|
3
|
+
**Inform** prints colourised, interactive logging output to the console.
|
4
|
+
|
5
|
+
For example:
|
6
|
+
|
7
|
+
Inform.level = :info
|
8
|
+
frozbot = Inform.info("Initializing the frozbot") do
|
9
|
+
Frozbot.new
|
10
|
+
end
|
11
|
+
|
12
|
+
Inform.info("Frozbot is %{name}", :name => frozbot.name)
|
13
|
+
|
14
|
+
Will print:
|
15
|
+
|
16
|
+
>>> Initializing the frozbot : Done
|
17
|
+
*** Frozbot is myfrozbot
|
18
|
+
|
19
|
+
### Caveat
|
20
|
+
|
21
|
+
This code was taken directly from the our **dew** project and is not ready for prime time. Interfaces,
|
22
|
+
requirements, etc will change.
|
23
|
+
|
24
|
+
## License
|
25
|
+
|
26
|
+
(The MIT License)
|
27
|
+
|
28
|
+
Copyright (c) 2011 PlayUp
|
29
|
+
|
30
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
31
|
+
a copy of this software and associated documentation files (the
|
32
|
+
'Software'), to deal in the Software without restriction, including
|
33
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
34
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
35
|
+
permit persons to whom the Software is furnished to do so, subject to
|
36
|
+
the following conditions:
|
37
|
+
|
38
|
+
The above copyright notice and this permission notice shall be
|
39
|
+
included in all copies or substantial portions of the Software.
|
40
|
+
|
41
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
42
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
43
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
44
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
45
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
46
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
47
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/inform.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "inform/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "inform"
|
7
|
+
s.version = Inform::VERSION
|
8
|
+
s.authors = ["Michael Pearson", "Chris Ottrey"]
|
9
|
+
s.email = ["mipearson@gmail.com", ""]
|
10
|
+
s.homepage = "https://github.com/playup/inform"
|
11
|
+
s.summary = %q{Interactive, colourised logging}
|
12
|
+
s.description = s.summary
|
13
|
+
|
14
|
+
s.add_runtime_dependency("highline", "~> 1.6.2")
|
15
|
+
s.add_development_dependency("rspec", "~> 2.6.0")
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
data/lib/inform.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require "inform/version"
|
2
|
+
require 'highline/import'
|
3
|
+
|
4
|
+
class Inform
|
5
|
+
class << self
|
6
|
+
DEFAULT_LOG_LEVEL = :info
|
7
|
+
LOG_LEVELS = [:debug, :info, :warning, :error]
|
8
|
+
|
9
|
+
# Highline colours at http://highline.rubyforge.org/doc/classes/HighLine.html
|
10
|
+
|
11
|
+
def level
|
12
|
+
@level.nil? ? DEFAULT_LOG_LEVEL : @level
|
13
|
+
end
|
14
|
+
|
15
|
+
def level= _level
|
16
|
+
raise "Unrecognized log level #{_level} (should be one of #{LOG_LEVELS.join(', ')})" unless LOG_LEVELS.include?(_level)
|
17
|
+
@level = _level
|
18
|
+
end
|
19
|
+
|
20
|
+
def debug(message, args=nil)
|
21
|
+
# log(:debug, message)
|
22
|
+
log(:debug, " <%= color(#{color_args(message, args, :cyan)}, :cyan) %>")
|
23
|
+
end
|
24
|
+
|
25
|
+
def info(message, args=nil)
|
26
|
+
if block_given?
|
27
|
+
log(:info, ">>> <%= color(#{color_args(message, args, :blue)}, :blue) %> :", :no_newline => true)
|
28
|
+
ret = yield
|
29
|
+
log(:info, "<%= color('Done.', :blue) %>", :continue_line => true, :prefix => '>>> ')
|
30
|
+
ret
|
31
|
+
else
|
32
|
+
log(:info, "*** <%= color(#{color_args(message, args, :green)}, :green) %>")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def warning(message, args=nil)
|
37
|
+
log(:warning, "<%= color('WARNING', :yellow, :bold) %>: <%= color(#{color_args(message, args, :yellow)}, :yellow) %>")
|
38
|
+
end
|
39
|
+
|
40
|
+
def error(message, args=nil)
|
41
|
+
log :error, "<%= color('ERROR', :red, :bold) %>: <%= color(#{color_args(message, args, :red)}, :red) %>"
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def log message_level, message, opts={}
|
47
|
+
if LOG_LEVELS.index(level) <= LOG_LEVELS.index(message_level)
|
48
|
+
if @need_newline && !opts[:continue_line]
|
49
|
+
message = "\n" + message
|
50
|
+
end
|
51
|
+
if opts[:continue_line] && !@need_newline
|
52
|
+
message = opts[:prefix] + message
|
53
|
+
end
|
54
|
+
if opts[:no_newline]
|
55
|
+
message += ' ' # say magic
|
56
|
+
@need_newline = true
|
57
|
+
else
|
58
|
+
@need_newline = false
|
59
|
+
end
|
60
|
+
say message
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def color_args(message, args, c)
|
65
|
+
message = message % args.inject({}) { |h,(k,v)| h[k] = "#{$terminal.color(v, c, :bold)}#{$terminal.class.const_get(c.to_s.upcase)}" ; h } if args
|
66
|
+
message.inspect
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
class String
|
72
|
+
# Work around Ruby 1.8.x not supporting % args
|
73
|
+
alias_method :percentage, :%
|
74
|
+
def %(arg)
|
75
|
+
arg.is_a?(Hash) ? arg.inject(self) { |res,(k,v)| res.gsub(/%\{#{k}\}/, v) } : self.percentage(arg)
|
76
|
+
end
|
77
|
+
end
|
data/spec/inform_spec.rb
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
|
2
|
+
|
3
|
+
def should_print method
|
4
|
+
describe method do
|
5
|
+
it "should print a message" do
|
6
|
+
Inform.should_receive(:say).with(/a special message/)
|
7
|
+
Inform.send(method, "a special message")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def should_not_print method
|
13
|
+
describe method do
|
14
|
+
it "should not print a message" do
|
15
|
+
Inform.should_not_receive(:say)
|
16
|
+
Inform.should_not_receive(:print)
|
17
|
+
Inform.send(method, "a special message")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# TODO: refactor, deal with all combinations of message / log level
|
23
|
+
describe Inform do
|
24
|
+
before :each do
|
25
|
+
@oldlevel = Inform.level
|
26
|
+
end
|
27
|
+
after :each do
|
28
|
+
Inform.level = @oldlevel
|
29
|
+
end
|
30
|
+
|
31
|
+
describe :level= do
|
32
|
+
it "should set the log level" do
|
33
|
+
Inform.level = :info
|
34
|
+
Inform.level.should == :info
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should not allow us to set a log level that isn't recognized" do
|
38
|
+
lambda {Inform.level = :warble}.should raise_error /unrecognized/i
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "with log level debug" do
|
43
|
+
before :each do
|
44
|
+
Inform.level = :debug
|
45
|
+
Inform.stub(:say => nil) # SSSSSH.
|
46
|
+
end
|
47
|
+
|
48
|
+
should_print :debug
|
49
|
+
should_print :info
|
50
|
+
|
51
|
+
describe ":info with a block" do
|
52
|
+
it "should print out the task being executed" do
|
53
|
+
Inform.should_receive(:say).with(/message/)
|
54
|
+
Inform.info("message") { true }
|
55
|
+
end
|
56
|
+
it "should print Done once the task is complete" do
|
57
|
+
Inform.should_receive(:say).with(/Done/)
|
58
|
+
Inform.info("") { true }
|
59
|
+
end
|
60
|
+
it "should evaluate the passed block and return the result" do
|
61
|
+
Inform.info("") { 'hello' }.should == 'hello'
|
62
|
+
end
|
63
|
+
it "should allow us to print messages from within a block" do
|
64
|
+
Inform.should_receive(:say).with(/open/)
|
65
|
+
Inform.should_receive(:say).with(/inner/)
|
66
|
+
Inform.info("open") { Inform.debug("inner") }
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
should_print :warning
|
71
|
+
should_print :error
|
72
|
+
end
|
73
|
+
|
74
|
+
context "with log level set to :info" do
|
75
|
+
before { Inform.level = :info }
|
76
|
+
should_not_print :debug
|
77
|
+
should_print :info
|
78
|
+
should_print :warning
|
79
|
+
should_print :error
|
80
|
+
end
|
81
|
+
|
82
|
+
context "with log level set to :warning" do
|
83
|
+
before { Inform.level = :warning }
|
84
|
+
should_not_print :debug
|
85
|
+
should_not_print :info
|
86
|
+
should_print :warning
|
87
|
+
should_print :error
|
88
|
+
end
|
89
|
+
|
90
|
+
context "with log level set to :error " do
|
91
|
+
before { Inform.level = :error }
|
92
|
+
should_not_print :debug
|
93
|
+
should_not_print :info
|
94
|
+
should_not_print :warning
|
95
|
+
should_print :error
|
96
|
+
|
97
|
+
describe :info do
|
98
|
+
it "should still execute the code block" do
|
99
|
+
Inform.info("") { 'hello' }.should == 'hello'
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe String do
|
106
|
+
describe :% do
|
107
|
+
it {
|
108
|
+
(
|
109
|
+
"%{greeting} there %{name}. How are you today %{name}?" % { :name => 'Ash', :greeting => 'Hi' }
|
110
|
+
).should == "Hi there Ash. How are you today Ash?"
|
111
|
+
}
|
112
|
+
end
|
113
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: inform
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Michael Pearson
|
14
|
+
- Chris Ottrey
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-06-14 00:00:00 Z
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: highline
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 11
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 6
|
33
|
+
- 2
|
34
|
+
version: 1.6.2
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rspec
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 23
|
46
|
+
segments:
|
47
|
+
- 2
|
48
|
+
- 6
|
49
|
+
- 0
|
50
|
+
version: 2.6.0
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
53
|
+
description: Interactive, colourised logging
|
54
|
+
email:
|
55
|
+
- mipearson@gmail.com
|
56
|
+
- ""
|
57
|
+
executables: []
|
58
|
+
|
59
|
+
extensions: []
|
60
|
+
|
61
|
+
extra_rdoc_files: []
|
62
|
+
|
63
|
+
files:
|
64
|
+
- .gitignore
|
65
|
+
- Gemfile
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- inform.gemspec
|
69
|
+
- lib/inform.rb
|
70
|
+
- lib/inform/version.rb
|
71
|
+
- spec/inform_spec.rb
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
homepage: https://github.com/playup/inform
|
74
|
+
licenses: []
|
75
|
+
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
hash: 3
|
96
|
+
segments:
|
97
|
+
- 0
|
98
|
+
version: "0"
|
99
|
+
requirements: []
|
100
|
+
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 1.7.2
|
103
|
+
signing_key:
|
104
|
+
specification_version: 3
|
105
|
+
summary: Interactive, colourised logging
|
106
|
+
test_files:
|
107
|
+
- spec/inform_spec.rb
|
108
|
+
- spec/spec_helper.rb
|