smart_colored 1.0.0

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.
@@ -0,0 +1 @@
1
+ /*.gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Ivan Kuchin
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,34 @@
1
+ # smart_colored
2
+
3
+ Color and formatting in terminal
4
+
5
+ ## Synopsis
6
+
7
+ require 'smart_colored'
8
+
9
+ puts 'important'.colored.red
10
+ puts 'important'.colored.bold
11
+ puts 'important'.colored.red.bold
12
+ puts 'important'.colored.red_bold
13
+ puts 'important'.colored.red_bold_on_yellow
14
+
15
+ or
16
+
17
+ require 'smart_colored/extend'
18
+
19
+ puts 'important'.red
20
+ puts 'important'.bold
21
+ puts 'important'.red.bold
22
+ puts 'important'.red_bold
23
+ puts 'important'.red_bold_on_yellow
24
+
25
+ smart means you can apply format to string which already got formatting
26
+
27
+ require 'smart_colored/extend'
28
+
29
+ puts "sometimes there could be #{'very important'.bold.underline} text in simply important one".red
30
+ puts "white, #{'underlined red'.underline.red}, #{'inversed with background green'.inverse_green} and #{'bold blue'.bold_blue} on black background".white.on_black
31
+
32
+ ## Copyright
33
+
34
+ Copyright (c) 2010 Ivan Kuchin. See LICENSE.txt for details.
@@ -0,0 +1,25 @@
1
+ require 'rake'
2
+ require 'jeweler'
3
+ require 'rake/gem_ghost_task'
4
+ require 'rspec/core/rake_task'
5
+
6
+ name = 'smart_colored'
7
+
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = name
10
+ gem.summary = %Q{Color and formatting in terminal}
11
+ gem.homepage = "http://github.com/toy/#{name}"
12
+ gem.license = 'MIT'
13
+ gem.authors = ['Ivan Kuchin']
14
+ gem.add_development_dependency 'jeweler', '~> 1.5.1'
15
+ gem.add_development_dependency 'rake-gem-ghost'
16
+ gem.add_development_dependency 'rspec'
17
+ end
18
+ Jeweler::RubygemsDotOrgTasks.new
19
+ Rake::GemGhostTask.new
20
+
21
+ RSpec::Core::RakeTask.new(:spec) do |spec|
22
+ spec.rspec_opts = ['--colour --format progress']
23
+ spec.pattern = 'spec/**/*_spec.rb'
24
+ end
25
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,117 @@
1
+ require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/
2
+
3
+ module SmartColored
4
+ COLOR_CODES = {
5
+ :black => 30,
6
+ :red => 31,
7
+ :green => 32,
8
+ :yellow => 33,
9
+ :blue => 34,
10
+ :magenta => 35,
11
+ :cyan => 36,
12
+ :white => 37
13
+ }
14
+
15
+ FORMAT_CODES = {
16
+ :bold => 1,
17
+ :underline => 4,
18
+ :inverse => 7
19
+ }
20
+
21
+ CLEAR_SEQUENCE = "\e[0m"
22
+
23
+ def apply_format(format = {})
24
+ apply_attributes = {}
25
+ if color = COLOR_CODES[format[:color]]
26
+ apply_attributes[:color] = color
27
+ end
28
+ if background = COLOR_CODES[format[:background]]
29
+ apply_attributes[:background] = background + 10
30
+ end
31
+ FORMAT_CODES.each do |key, value|
32
+ if format[key]
33
+ apply_attributes[value] = true
34
+ end
35
+ end
36
+
37
+ previous_attributes = {}
38
+ clear_sequence = false
39
+ str = "#{CLEAR_SEQUENCE}#{self}"
40
+ str.gsub!(/(?:(?:\e\[\d+(?:;\d+)*m)+)/) do |m|
41
+ unless $'.empty?
42
+ codes = m.scan(/\d+/).map(&:to_i).uniq
43
+ sequence_attributes = {}
44
+ codes.each do |code|
45
+ case code
46
+ when 0
47
+ when 30..37
48
+ sequence_attributes[:color] = code
49
+ when 40..47
50
+ sequence_attributes[:background] = code
51
+ else
52
+ sequence_attributes[code] = true
53
+ end
54
+ end
55
+
56
+ current_attributes = apply_attributes.merge(codes.include?(0) ? sequence_attributes : previous_attributes.merge(sequence_attributes))
57
+ sequence_attributes = if (previous_attributes.keys - current_attributes.keys).empty?
58
+ current_attributes.to_a - previous_attributes.to_a
59
+ else
60
+ current_attributes.merge(0 => true)
61
+ end
62
+ codes = sequence_attributes.map do |key, value|
63
+ key == :color || key == :background ? value : key
64
+ end
65
+ previous_attributes = current_attributes
66
+
67
+ unless codes.empty?
68
+ clear_sequence = true
69
+ "\e[#{codes.sort.join(';')}m"
70
+ end
71
+ end
72
+ end
73
+ str << CLEAR_SEQUENCE if clear_sequence
74
+ self.class.new(str)
75
+ end
76
+
77
+ NAME_TO_ATTRIBUTES = {}
78
+ def self.map_name_to_attributes(name, attributes)
79
+ name = name.to_sym
80
+ NAME_TO_ATTRIBUTES[name] = attributes
81
+ define_method name do
82
+ apply_format(attributes)
83
+ end
84
+ end
85
+
86
+ COLOR_CODES.each do |color, code|
87
+ map_name_to_attributes color, {:color => color}
88
+ map_name_to_attributes "on_#{color}", {:background => color}
89
+ end
90
+ FORMAT_CODES.each do |extra, code|
91
+ map_name_to_attributes extra, {extra => true}
92
+ end
93
+
94
+ part_regexp = "(?:on_)?(?:#{COLOR_CODES.keys.join('|')})|#{FORMAT_CODES.keys.join('|')}"
95
+ COMBINED_REGEXP = /^#{part_regexp}{2,}$/
96
+ COMBINED_REGEXP_PART = /#{part_regexp}/
97
+
98
+ def method_missing(method, *arguments, &block)
99
+ if (method_s = "#{method}_") =~ COMBINED_REGEXP
100
+ attributes = NAME_TO_ATTRIBUTES.values_at(*method_s.scan(COMBINED_REGEXP_PART).map(&:to_sym)).inject(&:merge)
101
+ SmartColored.map_name_to_attributes method, attributes
102
+ apply_format(attributes)
103
+ else
104
+ super
105
+ end
106
+ end
107
+
108
+ class String < ::String
109
+ include SmartColored
110
+ end
111
+ end
112
+
113
+ class String
114
+ def colored
115
+ SmartColored::String.new(self)
116
+ end
117
+ end
@@ -0,0 +1,3 @@
1
+ require 'smart_colored'
2
+
3
+ String.send(:include, SmartColored)
@@ -0,0 +1,126 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe SmartColored do
4
+ describe "simple" do
5
+ it "should apply none" do
6
+ 'clear'.apply_format.should == 'clear'
7
+ end
8
+
9
+ it "should apply color" do
10
+ 'red'.red.should == "\e[31m" 'red' "\e[0m"
11
+ end
12
+
13
+ it "should apply background" do
14
+ 'red'.on_red.should == "\e[41m" 'red' "\e[0m"
15
+ end
16
+
17
+ it "should apply bold" do
18
+ 'bold'.bold.should == "\e[1m" 'bold' "\e[0m"
19
+ end
20
+
21
+ it "should apply underline" do
22
+ 'underlined'.underline.should == "\e[4m" 'underlined' "\e[0m"
23
+ end
24
+
25
+ it "should apply inverse" do
26
+ 'inversed'.inverse.should == "\e[7m" 'inversed' "\e[0m"
27
+ end
28
+ end
29
+
30
+ describe "combining" do
31
+ it "should apply red and blue" do
32
+ 'red'.red.blue.should == "\e[31m" 'red' "\e[0m"
33
+ end
34
+
35
+ it "should apply red and bold" do
36
+ 'red'.red.bold.should == "\e[1;31m" 'red' "\e[0m"
37
+ end
38
+
39
+ it "should apply bold and red" do
40
+ 'red'.bold.red.should == "\e[1;31m" 'red' "\e[0m"
41
+ end
42
+
43
+ it "should apply red on blue" do
44
+ 'red on blue'.red.on_blue.should == "\e[31;44m" 'red on blue' "\e[0m"
45
+ end
46
+
47
+ it "should apply bold and underline" do
48
+ 'bold underlined'.bold.underline.should == "\e[1;4m" 'bold underlined' "\e[0m"
49
+ end
50
+
51
+ it "should apply bold, underlined and cyan" do
52
+ 'bold underlined cyan'.bold.underline.cyan.should == "\e[1;4;36m" 'bold underlined cyan' "\e[0m"
53
+ end
54
+ end
55
+
56
+ describe "combined methods" do
57
+ it "should apply red and blue" do
58
+ 'red'.red_blue.should == "\e[34m" 'red' "\e[0m"
59
+ end
60
+
61
+ it "should apply red and bold" do
62
+ 'red'.bold_red.should == "\e[1;31m" 'red' "\e[0m"
63
+ end
64
+
65
+ it "should apply red on blue" do
66
+ 'red on blue'.red_on_blue.should == "\e[31;44m" 'red on blue' "\e[0m"
67
+ end
68
+
69
+ it "should apply bold and underline" do
70
+ 'bold underlined'.bold_underline.should == "\e[1;4m" 'bold underlined' "\e[0m"
71
+ end
72
+
73
+ it "should apply bold, underlined and cyan" do
74
+ 'bold underlined cyan'.bold_underline_cyan.should == "\e[1;4;36m" 'bold underlined cyan' "\e[0m"
75
+ end
76
+
77
+ it "should apply bold, underlined, cyan on yellow" do
78
+ %w[bold underlined cyan on_yellow].permutation.each do |parts|
79
+ 'bold underlined cyan'.send(parts.join('_')).should == "\e[1;4;36;43m" 'bold underlined cyan' "\e[0m"
80
+ end
81
+ end
82
+ end
83
+
84
+ describe "complex" do
85
+ it "should apply blue on string with red" do
86
+ 'red'.red.should == "\e[31m" 'red' "\e[0m"
87
+ "blue #{'red'.red} blue".blue.should == "\e[34m" 'blue ' "\e[31m" 'red' "\e[34m" ' blue' "\e[0m"
88
+ end
89
+
90
+ it "should apply red on string with red" do
91
+ 'red'.red.should == "\e[31m" 'red' "\e[0m"
92
+ "red #{'red'.red} red".red.should == "\e[31m" 'red red red' "\e[0m"
93
+ end
94
+
95
+ it "should apply bold on string with red applied on string with bold" do
96
+ 'bold'.bold.should == "\e[1m" 'bold' "\e[0m"
97
+ "red #{'bold'.bold} red".red.should == "\e[31m" 'red ' "\e[1m" 'bold' "\e[0;31m" ' red' "\e[0m"
98
+ "bold #{"red #{'bold'.bold} red".red} bold".bold.should == "\e[1m" 'bold ' "\e[31m" 'red bold red' "\e[0;1m" ' bold' "\e[0m"
99
+ end
100
+
101
+ it "should apply bald and red on string with red applied on string with bold and red" do
102
+ 'bold red'.bold.red.should == "\e[1;31m" 'bold red' "\e[0m"
103
+ "red #{'bold red'.bold.red} red".red.should == "\e[31m" 'red ' "\e[1m" 'bold red' "\e[0;31m" ' red' "\e[0m"
104
+ "bold #{"red #{'bold red'.bold.red} red".red} bold".bold.red.should == "\e[1;31m" 'bold red bold red red bold' "\e[0m"
105
+ end
106
+
107
+ it "should apply green on string with blue applied on string with red (moved left)" do
108
+ 'red'.red.should == "\e[31m" 'red' "\e[0m"
109
+ "#{'red'.red} blue".blue.should == "\e[31m" 'red' "\e[34m" ' blue' "\e[0m"
110
+ "#{"#{'red'.red} blue".blue} green".green.should == "\e[31m" 'red' "\e[34m" ' blue' "\e[32m" ' green' "\e[0m"
111
+ end
112
+
113
+ it "should apply green on string with blue applied on string with red (moved right)" do
114
+ 'red'.red.should == "\e[31m" 'red' "\e[0m"
115
+ "blue #{'red'.red}".blue.should == "\e[34m" 'blue ' "\e[31m" 'red' "\e[0m"
116
+ "green #{"blue #{'red'.red}".blue}".green.should == "\e[32m" 'green ' "\e[34m" 'blue ' "\e[31m" 'red' "\e[0m"
117
+ end
118
+
119
+ it "should apply blue.inversed on string with on_yellow applied on string with red.underline and string with bold.red" do
120
+ 'red underlined'.red.underline.should == "\e[4;31m" 'red underlined' "\e[0m"
121
+ "bold red".bold.red.should == "\e[1;31m" 'bold red' "\e[0m"
122
+ "on yellow #{'red underlined'.red.underline} on yellow".on_yellow.should == "\e[43m" 'on yellow ' "\e[4;31m" 'red underlined' "\e[0;43m" ' on yellow' "\e[0m"
123
+ "#{"on yellow #{'red underlined'.red.underline} on yellow".on_yellow} #{"bold red".bold.red} blue inversed".blue.inverse.should == "\e[7;34;43m" 'on yellow ' "\e[4;31m" 'red underlined' "\e[0;7;34;43m" ' on yellow' "\e[0;7;34m" ' ' "\e[1;31m" 'bold red' "\e[0;7;34m" ' blue inversed' "\e[0m"
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,3 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'rspec'
3
+ require 'smart_colored/extend'
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smart_colored
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Ivan Kuchin
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-12 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: jeweler
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 1
30
+ segments:
31
+ - 1
32
+ - 5
33
+ - 1
34
+ version: 1.5.1
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rake-gem-ghost
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: rspec
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :development
64
+ version_requirements: *id003
65
+ description:
66
+ email:
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files:
72
+ - LICENSE.txt
73
+ - README.markdown
74
+ files:
75
+ - .tmignore
76
+ - LICENSE.txt
77
+ - README.markdown
78
+ - Rakefile
79
+ - VERSION
80
+ - lib/smart_colored.rb
81
+ - lib/smart_colored/extend.rb
82
+ - spec/smart_colored_spec.rb
83
+ - spec/spec_helper.rb
84
+ has_rdoc: true
85
+ homepage: http://github.com/toy/smart_colored
86
+ licenses:
87
+ - MIT
88
+ post_install_message:
89
+ rdoc_options: []
90
+
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ hash: 3
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ requirements: []
112
+
113
+ rubyforge_project:
114
+ rubygems_version: 1.5.0
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Color and formatting in terminal
118
+ test_files:
119
+ - spec/smart_colored_spec.rb
120
+ - spec/spec_helper.rb