inform 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -21,6 +21,13 @@ Will print:
21
21
  This code was taken directly from the our **dew** project and is not ready for prime time. Interfaces,
22
22
  requirements, etc will change.
23
23
 
24
+ ### TODO
25
+
26
+ * Make debug, warning, error accept blocks
27
+ * Refactor tests
28
+ * Colourisation isn't tested at all - should it be?
29
+ * RDoc
30
+
24
31
  ## License
25
32
 
26
33
  (The MIT License)
data/examples/frozbot ADDED
@@ -0,0 +1,18 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require "inform"
4
+
5
+ class Frozbot
6
+ attr_reader :name
7
+
8
+ def initialize(name)
9
+ @name = name
10
+ end
11
+ end
12
+
13
+ Inform.level = :info
14
+ frozbot = Inform.info("Initializing the frozbot...") do
15
+ Frozbot.new 'puddy'
16
+ end
17
+
18
+ Inform.info("Frozbot is %{name}", :name => frozbot.name)
data/inform.gemspec CHANGED
@@ -11,7 +11,6 @@ Gem::Specification.new do |s|
11
11
  s.summary = %q{Interactive, colourised logging}
12
12
  s.description = s.summary
13
13
 
14
- s.add_runtime_dependency("highline", "~> 1.6.2")
15
14
  s.add_development_dependency("rspec", "~> 2.6.0")
16
15
 
17
16
  s.files = `git ls-files`.split("\n")
@@ -1,3 +1,3 @@
1
1
  class Inform
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/inform.rb CHANGED
@@ -1,12 +1,25 @@
1
1
  require "inform/version"
2
- require 'highline/import'
3
2
 
4
3
  class Inform
5
- class << self
6
- DEFAULT_LOG_LEVEL = :info
7
- LOG_LEVELS = [:debug, :info, :warning, :error]
8
4
 
9
- # Highline colours at http://highline.rubyforge.org/doc/classes/HighLine.html
5
+ CLEAR = "\e[0m"
6
+
7
+ BOLD = "\e[1m"
8
+ UNDERLINE = "\e[4m"
9
+
10
+ BLACK = "\e[30m"
11
+ RED = "\e[31m"
12
+ GREEN = "\e[32m"
13
+ YELLOW = "\e[33m"
14
+ BLUE = "\e[34m"
15
+ MAGENTA = "\e[35m"
16
+ CYAN = "\e[36m"
17
+ WHITE = "\e[37m"
18
+
19
+ DEFAULT_LOG_LEVEL = :info
20
+ LOG_LEVELS = [:debug, :info, :warning, :error]
21
+
22
+ class << self
10
23
 
11
24
  def level
12
25
  @level.nil? ? DEFAULT_LOG_LEVEL : @level
@@ -18,33 +31,33 @@ class Inform
18
31
  end
19
32
 
20
33
  def debug(message, args=nil)
21
- # log(:debug, message)
22
- log(:debug, " <%= color(#{color_args(message, args, :cyan)}, :cyan) %>")
34
+ log(:debug, " " + color_args(message, args, CYAN))
23
35
  end
24
36
 
25
37
  def info(message, args=nil)
26
38
  if block_given?
27
- log(:info, ">>> <%= color(#{color_args(message, args, :blue)}, :blue) %> :", :no_newline => true)
39
+ log(:info, ">>> " + color_args(message, args, BLUE) + " :", :no_newline => true)
28
40
  ret = yield
29
- log(:info, "<%= color('Done.', :blue) %>", :continue_line => true, :prefix => '>>> ')
41
+ log(:info, color('Done.', BLUE), :continue_line => true, :prefix => '>>> ')
30
42
  ret
31
43
  else
32
- log(:info, "*** <%= color(#{color_args(message, args, :green)}, :green) %>")
44
+ log(:info, "*** " + color_args(message, args, GREEN))
33
45
  end
34
46
  end
35
47
 
36
48
  def warning(message, args=nil)
37
- log(:warning, "<%= color('WARNING', :yellow, :bold) %>: <%= color(#{color_args(message, args, :yellow)}, :yellow) %>")
49
+ log(:warning, color('WARNING', YELLOW, BOLD) + ': ' + color_args(message, args, YELLOW))
38
50
  end
39
51
 
40
52
  def error(message, args=nil)
41
- log :error, "<%= color('ERROR', :red, :bold) %>: <%= color(#{color_args(message, args, :red)}, :red) %>"
53
+ log(:error, color('ERROR', RED, BOLD) + ': ' + color_args(message, args, RED))
42
54
  end
43
55
 
44
56
  private
45
57
 
46
58
  def log message_level, message, opts={}
47
59
  if LOG_LEVELS.index(level) <= LOG_LEVELS.index(message_level)
60
+ end_with_newline = true
48
61
  if @need_newline && !opts[:continue_line]
49
62
  message = "\n" + message
50
63
  end
@@ -52,26 +65,30 @@ class Inform
52
65
  message = opts[:prefix] + message
53
66
  end
54
67
  if opts[:no_newline]
55
- message += ' ' # say magic
68
+ end_with_newline = false
56
69
  @need_newline = true
57
70
  else
58
71
  @need_newline = false
59
72
  end
60
- say message
73
+ message += "\n" if end_with_newline
74
+ print message
61
75
  end
62
76
  end
63
77
 
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
78
+ def color_args(message, args, *colours)
79
+ colours = colours.join("")
80
+ message = colours + message
81
+ if args
82
+ args.each do |k, v|
83
+ v = "#{BOLD}#{v}#{CLEAR}#{colours}"
84
+ message.gsub!(/%\{#{k}\}/, v)
85
+ end
86
+ end
87
+ message + CLEAR
88
+ end
89
+
90
+ def color(message, *colours)
91
+ colours.join("") + message + CLEAR
67
92
  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
93
  end
77
94
  end
data/spec/inform_spec.rb CHANGED
@@ -3,7 +3,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
3
3
  def should_print method
4
4
  describe method do
5
5
  it "should print a message" do
6
- Inform.should_receive(:say).with(/a special message/)
6
+ Inform.should_receive(:print).with(/a special message/)
7
7
  Inform.send(method, "a special message")
8
8
  end
9
9
  end
@@ -42,7 +42,7 @@ describe Inform do
42
42
  context "with log level debug" do
43
43
  before :each do
44
44
  Inform.level = :debug
45
- Inform.stub(:say => nil) # SSSSSH.
45
+ Inform.stub(:print => nil) # SSSSSH.
46
46
  end
47
47
 
48
48
  should_print :debug
@@ -50,19 +50,23 @@ describe Inform do
50
50
 
51
51
  describe ":info with a block" do
52
52
  it "should print out the task being executed" do
53
- Inform.should_receive(:say).with(/message/)
53
+ Inform.should_receive(:print).with(/message/)
54
54
  Inform.info("message") { true }
55
55
  end
56
56
  it "should print Done once the task is complete" do
57
- Inform.should_receive(:say).with(/Done/)
57
+ Inform.should_receive(:print).with(/Done/)
58
58
  Inform.info("") { true }
59
59
  end
60
60
  it "should evaluate the passed block and return the result" do
61
61
  Inform.info("") { 'hello' }.should == 'hello'
62
62
  end
63
+ it "should interpolate keyword arguments" do
64
+ Inform.should_receive(:print).with(/hello.+hey.+you.+goodbye/)
65
+ Inform.info("hello %{a} %{b} goodbye", :a => 'hey', :b => 'you')
66
+ end
63
67
  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/)
68
+ Inform.should_receive(:print).with(/open/)
69
+ Inform.should_receive(:print).with(/inner/)
66
70
  Inform.info("open") { Inform.debug("inner") }
67
71
  end
68
72
  end
@@ -100,14 +104,4 @@ describe Inform do
100
104
  end
101
105
  end
102
106
  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
107
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inform
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Pearson
@@ -18,26 +18,10 @@ cert_chain: []
18
18
 
19
19
  date: 2011-06-14 00:00:00 Z
20
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
21
  - !ruby/object:Gem::Dependency
38
22
  name: rspec
39
23
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ requirement: &id001 !ruby/object:Gem::Requirement
41
25
  none: false
42
26
  requirements:
43
27
  - - ~>
@@ -49,7 +33,7 @@ dependencies:
49
33
  - 0
50
34
  version: 2.6.0
51
35
  type: :development
52
- version_requirements: *id002
36
+ version_requirements: *id001
53
37
  description: Interactive, colourised logging
54
38
  email:
55
39
  - mipearson@gmail.com
@@ -65,6 +49,7 @@ files:
65
49
  - Gemfile
66
50
  - README.md
67
51
  - Rakefile
52
+ - examples/frozbot
68
53
  - inform.gemspec
69
54
  - lib/inform.rb
70
55
  - lib/inform/version.rb