crayon 0.0.1 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document CHANGED
@@ -1,5 +1,4 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
1
+ README.md
5
2
  LICENSE
3
+ lib/**/*.rb
4
+ spec/**/*.rb
data/.gitignore CHANGED
@@ -1,10 +0,0 @@
1
- # VIM
2
- *.swp
3
-
4
- ## PROJECT::GENERAL
5
- coverage
6
- diff
7
- rdoc
8
- pkg
9
-
10
- ## PROJECT::SPECIFIC
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+ gem "yard"
3
+ gem "rspec"
@@ -0,0 +1,69 @@
1
+ # crayon
2
+
3
+ ### current version : 0.0.3
4
+
5
+ [http://github.com/mikowitz/crayon][github]
6
+
7
+ ## Description
8
+
9
+ A simple, flexible gem that provides an open-ended API to print colored and styled output to the terminal.
10
+
11
+ ## Installation
12
+
13
+ Install the gemcutter gem
14
+
15
+ ~$ gem install gemcutter
16
+
17
+ Add gemcutter.org to your gem remote sources
18
+
19
+ ~$ gem tumble
20
+
21
+ Download and install this gem
22
+
23
+ ~$ gem install crayon
24
+
25
+ ## Usage examples
26
+
27
+ To include this gem in a project
28
+
29
+ require 'crayon'
30
+
31
+ The following are all methods that `Crayon` understands, and should give you an idea of what is possible.
32
+
33
+ Crayon.blue("this will be printed as blue text")
34
+ Crayon.on_red("this will be printed on a red background")
35
+ Crayon.bold("this will be bold")
36
+ Crayon.underline_blue_on_yellow("this will be underlined blue text on a yellow background")
37
+
38
+ `Crayon` also provides intermediary methods `puts` and `print` which determine whether `Crayon` adds a newline to the end of the text being displayed.
39
+ By default, a newline is added.
40
+
41
+ Crayon.puts.blue("this is on a line by itself.")
42
+ Crayon.print.blue("the next line will be printed right next to this")
43
+ Crayon.puts.green("this will on the second line of output, but will create a newline.")
44
+
45
+ `puts` and `print` set an internal, persistent variable, so a specified newline presence or absence will last until `puts` or `print` is invoked again.
46
+
47
+ Crayon.print.blue("on the first line")
48
+ Crayon.green("also on the first line")
49
+ Crayon.red("also on the first line")
50
+ Crayon.puts.cyan("Also on the first line, but with a trailing newline")
51
+ Crayon.blue("On the second line by itself.")
52
+
53
+ ## Flexibility
54
+
55
+ The order of elements in the method name is unimportant. For example
56
+
57
+ Color.bold_underline_blue_on_yellow("sample text")
58
+
59
+ will look the same as
60
+
61
+ Color.on_yellow_bold_blue_underline("sample text")
62
+
63
+ Check out `test/proof.rb` and `~$ rake proof` if you don't believe me.
64
+
65
+ ## Copyright
66
+
67
+ Copyright (c) 2010 Michael Berkowitz. See LICENSE for details.
68
+
69
+ [github]: http://github.com/mikowitz/crayon "Crayon repository"
data/Rakefile CHANGED
@@ -1,4 +1,3 @@
1
- require 'rubygems'
2
1
  require 'rake'
3
2
 
4
3
  begin
@@ -10,8 +9,6 @@ begin
10
9
  gem.email = "michael.berkowitz@gmail.com"
11
10
  gem.homepage = "http://github.com/mikowitz/crayon"
12
11
  gem.authors = ["Michael Berkowitz"]
13
- gem.add_development_dependency "tinytest", ">= 0"
14
- gem.add_development_dependency "yard", ">= 0"
15
12
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
13
  end
17
14
  Jeweler::GemcutterTasks.new
@@ -19,58 +16,31 @@ rescue LoadError
19
16
  puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
17
  end
21
18
 
22
- require 'rake/testtask'
23
- Rake::TestTask.new(:test) do |test|
24
- test.libs << 'lib' << 'test'
25
- test.pattern = 'test/**/test_*.rb'
26
- test.verbose = true
19
+ require 'spec/rake/spectask'
20
+ Spec::Rake::SpecTask.new(:spec) do |spec|
21
+ spec.libs << "lib" << "spec"
22
+ spec.spec_files = FileList["spec/**/*_spec.rb"]
27
23
  end
28
24
 
29
- begin
30
- require 'rcov/rcovtask'
31
- Rcov::RcovTask.new do |test|
32
- test.libs << 'test'
33
- test.pattern = 'test/**/test_*.rb'
34
- test.verbose = true
35
- end
36
- rescue LoadError
37
- task :rcov do
38
- abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
- end
25
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
26
+ spec.libs << "lib" << "spec"
27
+ spec.pattern = "spec/**/*_spec.rb"
28
+ spec.rcov = true
40
29
  end
41
30
 
42
- task :test => :check_dependencies
43
-
44
- begin
45
- require 'reek/adapters/rake_task'
46
- Reek::RakeTask.new do |t|
47
- t.fail_on_error = true
48
- t.verbose = false
49
- t.source_files = 'lib/**/*.rb'
50
- end
51
- rescue LoadError
52
- task :reek do
53
- abort "Reek is not available. In order to run reek, you must: sudo gem install reek"
54
- end
55
- end
31
+ task :spec => :check_dependencies
32
+ task :default => :spec
56
33
 
57
- begin
58
- require 'roodi'
59
- require 'roodi_task'
60
- RoodiTask.new do |t|
61
- t.verbose = false
62
- end
63
- rescue LoadError
64
- task :roodi do
65
- abort "Roodi is not available. In order to run roodi, you must: sudo gem install roodi"
66
- end
34
+ desc "'Proof is the bottom line for everyone' -- Paul Simon"
35
+ task :proof do
36
+ system "ruby test/proof.rb"
67
37
  end
68
38
 
69
- task :default => :test
70
-
71
39
  begin
72
40
  require 'yard'
73
- YARD::Rake::YardocTask.new
41
+ YARD::Rake::YardocTask.new do |t|
42
+ t.options = ['--no-private', '-mmarkdown']
43
+ end
74
44
  rescue LoadError
75
45
  task :yardoc do
76
46
  abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.5
@@ -1,75 +1,108 @@
1
1
  module Crayon
2
2
  extend self
3
3
 
4
- @@newline = true
5
-
6
- COLORS = {
7
- "black" => 0,
8
- "red" => 1,
9
- "green" => 2,
10
- "yellow" => 3,
11
- "blue" => 4,
12
- "magenta" => 5,
13
- "cyan" => 6,
14
- "white" => 7
15
- }
16
-
17
- FORMATS = {
18
- "bold" => 1,
19
- "underline" => 4
20
- }
4
+ class << self
5
+ # @private
6
+ attr_accessor :foreground, :background, :formatting, :method_name, :color, :newline
7
+ end
21
8
 
9
+ # @private
10
+ COLORS = { "black" => 0, "red" => 1, "green" => 2, "yellow" => 3, "blue" => 4, "magenta" => 5, "cyan" => 6, "white" => 7 }
11
+ # @private
12
+ FORMATS = { "bold" => 1, "underline" => 4 }
13
+ # @private
22
14
  TERMINATION_STRING = "\e[0m"
23
15
 
24
- def newline?; @@newline; end
16
+ # @private
17
+ def newline?; @newline.nil? ? true : @newline; end
18
+ # @private
25
19
  def io; $stderr; end
26
20
 
27
- def self.print; @@newline = false; return Crayon; end
21
+ def self.print; @newline = false; return Crayon; end
28
22
 
29
- def self.puts; @@newline = true; return Crayon; end
23
+ def self.puts; @newline = true; return Crayon; end
30
24
 
31
25
  def method_missing(method_name, string)
32
- io.print prepare_string(string, *parse_method_name(method_name))
33
- io.flush
26
+ @method_name = method_name
27
+ parse_method_name
28
+ io.print prepare_string(string)
29
+ nullify_variables
30
+ end
31
+
32
+ ##
33
+ # Converts a method name into color and formatting parameters
34
+ # @example
35
+ # Crayon.parse_method_name(:bold_red_on_green) #=> "['red', 'green', ['bold']]"
36
+ # @private
37
+ def parse_method_name
38
+ @method_name = @method_name.to_s.split("_")
39
+ @background = parse_background
40
+ @foreground = parse_foreground
41
+ @formatting = parse_formatting
42
+ end
43
+
44
+ # @private
45
+ def parse_background
46
+ _idx = @method_name.index("on")
47
+ if _idx
48
+ @method_name.delete("on")
49
+ return @method_name.delete_at(_idx)
50
+ end
34
51
  end
35
52
 
36
- def prepare_string(string, foreground=nil, background=nil, formatting=[])
37
- [
38
- prepare_foreground_color(foreground),
39
- prepare_background_color(background),
40
- prepare_formatting(*formatting),
53
+ # @private
54
+ def parse_foreground
55
+ @method_name.find {|color| COLORS.keys.include?(color) }
56
+ end
57
+
58
+ # @private
59
+ def parse_formatting
60
+ @method_name.select {|format| FORMATS.keys.include?(format) }
61
+ end
62
+
63
+ ##
64
+ # Builds output string with color escape characters.
65
+ # @example
66
+ # Crayon.prepare_string('hello', 'red', 'blue', ['underline']) #=> "\e[31m\e[44m\e[4mhello\e[0m"
67
+ # @private
68
+ def prepare_string(string) #, foreground=nil, background=nil, formatting=[])
69
+ [ prepare_foreground_color,
70
+ prepare_background_color,
71
+ prepare_formatting,
41
72
  string,
42
- TERMINATION_STRING,
73
+ (TERMINATION_STRING if @foreground || @background || !@formatting.empty?),
43
74
  (newline? ? "\n" : "")
44
75
  ].join("")
45
76
  end
46
77
 
47
- def parse_method_name(method_name)
48
- _name = method_name.to_s.split("_")
49
- if _idx = _name.index("on")
50
- _name.delete("on")
51
- _background_color = _name.delete_at(_idx)
52
- end
53
- _foreground_color = _name.find {|color| COLORS.keys.include?(color) }
54
- _formatting = _name.select{|format| FORMATS.keys.include?(format) }
55
- [_foreground_color, _background_color, _formatting]
78
+ # @private
79
+ def prepare_foreground_color
80
+ @color = @foreground
81
+ handle_color(3)
56
82
  end
57
83
 
58
- def prepare_foreground_color(color=nil)
59
- handle_color(3, color)
84
+ # @private
85
+ def prepare_background_color
86
+ @color = @background
87
+ handle_color(4)
60
88
  end
61
89
 
62
- def prepare_background_color(color=nil)
63
- handle_color(4, color)
90
+ # @private
91
+ def prepare_formatting
92
+ return "" if @formatting.empty?
93
+ @formatting.map{|format| "\e[#{FORMATS[format]}m"}.join("")
64
94
  end
65
95
 
66
- def prepare_formatting(*formats)
67
- return "" if formats.empty?
68
- formats.map{|f| "\e[#{FORMATS[f]}m"}.join("")
96
+ # @private
97
+ def handle_color(lead)
98
+ return "" unless @color
99
+ "\e[#{lead}#{COLORS[@color]}m"
69
100
  end
70
101
 
71
- def handle_color(lead, color=nil)
72
- return "" unless color
73
- "\e[#{lead}#{COLORS[color]}m"
102
+ # @private
103
+ def nullify_variables
104
+ @foreground, @background, @formatting = nil, nil, []
105
+ @method_name, @color = nil, nil
106
+ io.flush
74
107
  end
75
108
  end
@@ -0,0 +1,101 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/helper")
2
+
3
+ def test_parse_method_name(input, *output)
4
+ describe "for input :#{input}" do
5
+ before do
6
+ Crayon.method_name = input
7
+ Crayon.parse_method_name
8
+ @fore, @back, @form = output
9
+ end
10
+ after { [:foreground, :background, :formatting].each {|method| Crayon.send(:"#{method}=", nil) } }
11
+ it "should return #{@fore.inspect} for foreground" do
12
+ Crayon.foreground.should == @fore
13
+ end
14
+ it "should return #{@back.inspect} for background" do
15
+ Crayon.background.should == @back
16
+ end
17
+ it "should return #{@form.inspect} for formatting" do
18
+ Crayon.formatting.should == @form
19
+ end
20
+ end
21
+ end
22
+
23
+ def test_prepare_string(expected_output, *args)
24
+ it "should return correctly for #{args.inspect}" do
25
+ Crayon.foreground = args.fetch(1) { nil }
26
+ Crayon.background = args.fetch(2) { nil }
27
+ Crayon.formatting = args.fetch(3) { [] }
28
+ Crayon.prepare_string(args.first).should == expected_output
29
+ end
30
+ end
31
+
32
+ describe "Crayon" do
33
+ it "should, by default, add line breaks" do
34
+ Crayon.newline?.should be
35
+ end
36
+ describe "after a print command" do
37
+ before { Crayon.print }
38
+ it "should not add line breaks" do
39
+ Crayon.newline?.should_not be
40
+ end
41
+ end
42
+ describe "after a puts command" do
43
+ before { Crayon.puts }
44
+ it "should add line breaks" do
45
+ Crayon.newline?.should be
46
+ end
47
+ end
48
+ describe "method_missing" do
49
+ describe "should call :prepare string" do
50
+ before { Crayon.should_receive(:prepare_string).with("hello") }
51
+ it "when Crayon.red is called" do
52
+ Crayon.red("hello")
53
+ end
54
+ end
55
+ describe "should unset instance variables after being called" do
56
+ before { Crayon.bold_red_on_green("hello") }
57
+ it "should unset foreground" do
58
+ Crayon.foreground.should be_nil
59
+ end
60
+ it "should unset background" do
61
+ Crayon.background.should be_nil
62
+ end
63
+ it "should unset formatting" do
64
+ Crayon.formatting.should be_empty
65
+ end
66
+ it "should unset method_name" do
67
+ Crayon.method_name.should be_nil
68
+ end
69
+ end
70
+ end
71
+ describe "parse_method_name" do
72
+ test_parse_method_name(:red, "red", nil, [])
73
+ test_parse_method_name(:on_red, nil, "red", [])
74
+ test_parse_method_name(:bold, nil, nil, ["bold"])
75
+
76
+ test_parse_method_name(:blue_on_green, "blue", "green", [])
77
+ test_parse_method_name(:bold_cyan_on_magenta, "cyan", "magenta", ["bold"])
78
+
79
+ test_parse_method_name(:red_green, "red", nil, [])
80
+ test_parse_method_name(:red_bold_underline, "red", nil, ["bold", "underline"])
81
+ test_parse_method_name(:underline_on_green_bold, nil, "green", ["underline", "bold"])
82
+ end
83
+
84
+ describe "prepare_string" do
85
+ describe "with no line break" do
86
+ before { Crayon.print }
87
+ test_prepare_string("hello", "hello")
88
+ test_prepare_string("\e[31mhello\e[0m", "hello", "red", nil)
89
+ test_prepare_string("\e[33m\e[47mhello\e[0m", "hello", "yellow", "white")
90
+ test_prepare_string("\e[4m\e[1mhello\e[0m", "hello", nil, nil, ["underline", "bold"])
91
+ end
92
+
93
+ describe "with a line break" do
94
+ before { Crayon.puts }
95
+ test_prepare_string("hello\n", "hello")
96
+ test_prepare_string("\e[31mhello\e[0m\n", "hello", "red", nil)
97
+ test_prepare_string("\e[33m\e[47mhello\e[0m\n", "hello", "yellow", "white")
98
+ test_prepare_string("\e[4m\e[1mhello\e[0m\n", "hello", nil, nil, ["underline", "bold"])
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,8 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
3
+ require 'crayon'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+ end
@@ -0,0 +1,2 @@
1
+ -fs
2
+ --color
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Michael Berkowitz
@@ -14,33 +14,10 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-15 00:00:00 -04:00
17
+ date: 2010-04-21 00:00:00 -04:00
18
18
  default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- name: tinytest
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 0
29
- version: "0"
30
- type: :development
31
- version_requirements: *id001
32
- - !ruby/object:Gem::Dependency
33
- name: yard
34
- prerelease: false
35
- requirement: &id002 !ruby/object:Gem::Requirement
36
- requirements:
37
- - - ">="
38
- - !ruby/object:Gem::Version
39
- segments:
40
- - 0
41
- version: "0"
42
- type: :development
43
- version_requirements: *id002
19
+ dependencies: []
20
+
44
21
  description: A simple, flexible gem that provides an open-ended API to print colored and styled output to the terminal.
45
22
  email: michael.berkowitz@gmail.com
46
23
  executables: []
@@ -49,17 +26,19 @@ extensions: []
49
26
 
50
27
  extra_rdoc_files:
51
28
  - LICENSE
52
- - README.rdoc
29
+ - README.md
53
30
  files:
54
31
  - .document
55
32
  - .gitignore
33
+ - Gemfile
56
34
  - LICENSE
57
- - README.rdoc
35
+ - README.md
58
36
  - Rakefile
59
37
  - VERSION
60
38
  - lib/crayon.rb
61
- - test/helper.rb
62
- - test/test_crayon.rb
39
+ - spec/crayon_spec.rb
40
+ - spec/helper.rb
41
+ - spec/spec.opts
63
42
  has_rdoc: true
64
43
  homepage: http://github.com/mikowitz/crayon
65
44
  licenses: []
@@ -91,5 +70,5 @@ signing_key:
91
70
  specification_version: 3
92
71
  summary: a new version of 'color' with a less common name.
93
72
  test_files:
94
- - test/helper.rb
95
- - test/test_crayon.rb
73
+ - spec/crayon_spec.rb
74
+ - spec/helper.rb
@@ -1,45 +0,0 @@
1
- = crayon
2
-
3
- === current version : 0.0.2
4
-
5
- http://github.com/mikowitz/crayon
6
-
7
- == Description
8
-
9
- A simple, flexible gem that provides an open-ended API to print colored and styled output to the terminal.
10
-
11
- == Installation
12
-
13
- Install the gemcutter gem
14
- ~$ gem install gemcutter
15
- Add gemcutter.org to your gem remote sources
16
- ~$ gem tumble
17
- Download and install this gem
18
- ~$ gem install crayon
19
-
20
- == Usage examples
21
-
22
- To include this gem in a project
23
- require 'crayon'
24
-
25
- The following are all methods that <code>Crayon</code> understands, and should give you an idea of what is possible.
26
-
27
- Crayon.blue("this will be printed as blue text")
28
- Crayon.on_red("this will be printed on a red background")
29
- Crayon.bold("this will be bold")
30
- Crayon.underline_blue_on_yellow("this will be underlined blue text on a yellow background")
31
-
32
- The order of elements in the method name is also unimportant. For example
33
-
34
- Color.bold_underline_blue_on_yellow("sample text")
35
-
36
- will look the same as
37
-
38
- Color.on_yellow_bold_blue_underline("sample text")
39
-
40
- Check out <code>test/proof.rb"</code> and <code>`rake proof`</code> if you don't believe me.
41
-
42
- == Copyright
43
-
44
- Copyright (c) 2010 Michael Berkowitz. See LICENSE for details.
45
-
@@ -1,7 +0,0 @@
1
- require 'rubygems'
2
- require 'tinytest'
3
-
4
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
- $LOAD_PATH.unshift(File.dirname(__FILE__))
6
- require 'crayon'
7
-
@@ -1,27 +0,0 @@
1
- require 'helper'
2
-
3
- include TinyTest
4
-
5
- is { Crayon.newline? } # by default?
6
-
7
- is {
8
- Crayon.print
9
- not Crayon.newline?
10
- }
11
-
12
- is {
13
- Crayon.puts
14
- Crayon.newline?
15
- }
16
-
17
- # Crayon.parse_method_name
18
- does { Crayon.parse_method_name(:red) == ["red", nil, []] }
19
- does { Crayon.parse_method_name(:on_red) == [nil, "red", []] }
20
- does { Crayon.parse_method_name(:bold) == [nil, nil, ["bold"]] }
21
-
22
- does { Crayon.parse_method_name(:blue_on_green) == ["blue", "green", []] }
23
- does { Crayon.parse_method_name(:bold_cyan_on_magenta) == ["cyan", "magenta", ["bold"]] }
24
-
25
- does { Crayon.parse_method_name(:red_green) == ["red", nil, []] }
26
- does { Crayon.parse_method_name(:red_bold_underline) == ["red", nil, ["bold", "underline"]] }
27
- does { Crayon.parse_method_name(:underline_on_green_bold) == [nil, "green", ["underline", "bold"]] }