hirb-colors 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hirb-colors.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jens Balvig
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,8 @@
1
+ # Hirb-colors
2
+
3
+ Fixes alignment for strings containing color codes.
4
+
5
+ ## Attribution
6
+
7
+ All credit goes to [smoil](https://github.com/smoil/), I merely packaged up [his
8
+ fork](https://github.com/smoil/hirb) of hirb into a gem! :)
@@ -0,0 +1,24 @@
1
+ require 'bundler'
2
+ Bundler.setup
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ namespace :test do
6
+ desc 'Run tests of hirb gem with hirb-colors'
7
+ task :hirb do |t|
8
+ gem 'hirb' # Load hirb specified in gemfile
9
+ hirb_gem_path = Gem.loaded_specs["hirb"].full_gem_path
10
+ test_path = File.expand_path(File.join(hirb_gem_path, 'test', '*_test.rb'))
11
+ sh "bundle exec bacon -q hirb-colors.rb #{test_path}"
12
+ end
13
+
14
+ desc 'Run tests of hirb-color gem'
15
+ task :colors do |t|
16
+ sh 'bundle exec bacon -I. -q test/*_test.rb'
17
+ end
18
+
19
+ end
20
+
21
+ desc 'Run all tests'
22
+ task :test => ["test:hirb", "test:colors"]
23
+
24
+ task :default => :test
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "hirb/colors/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "hirb-colors"
7
+ s.version = Hirb::Colors::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Cyrus Farajpour","Jens Balvig"]
10
+ s.email = ["smoils@gmail.com", "jens@balvig.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Color support for hirb}
13
+ s.description = %q{Color support for hirb}
14
+
15
+ s.add_dependency 'hirb', '~> 0.5'
16
+ # Use the same test utility as `hirb`
17
+ s.add_development_dependency 'bacon', '>= 1.1.0'
18
+ s.add_development_dependency 'mocha'
19
+ s.add_development_dependency 'mocha-on-bacon'
20
+ s.add_development_dependency 'bacon-bits'
21
+ s.add_development_dependency 'rake'
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
26
+ s.require_paths = ["lib"]
27
+ end
@@ -0,0 +1 @@
1
+ require "hirb/colors"
@@ -0,0 +1,3 @@
1
+ require 'hirb'
2
+ require 'hirb/colors/version'
3
+ require 'hirb/colors/string_util'
@@ -0,0 +1,100 @@
1
+ module Hirb
2
+ module Colors
3
+ module StringUtil
4
+ COLORIZED_REGEX = /\e\[\d+m/
5
+
6
+ # :stopdoc:
7
+ if RUBY_VERSION < '1.9'
8
+ def size(string)
9
+ string.gsub(COLORIZED_REGEX, '').scan(/./).length
10
+ end
11
+
12
+ def rjust(string, desired_length)
13
+ leftover = desired_length - size(string)
14
+ leftover > 0 ? " " * leftover + string : string
15
+ end
16
+
17
+ def slice(string, start, finish)
18
+ if string =~ COLORIZED_REGEX
19
+ slice_colorized_string(string, start, finish)
20
+ else
21
+ string.scan(/./).slice(start, finish).join('')
22
+ end
23
+ end
24
+ else
25
+ def size(string)
26
+ string.gsub(COLORIZED_REGEX, '').length
27
+ end
28
+
29
+ def ljust(string, desired_length)
30
+ leftover = desired_length - size(string)
31
+ leftover > 0 ? string + " " * leftover : string
32
+ end
33
+
34
+ def rjust(string, desired_length)
35
+ leftover = desired_length - size(string)
36
+ leftover > 0 ? " " * leftover + string : string
37
+ end
38
+
39
+ def slice(*args)
40
+ string = args.shift
41
+
42
+ # if string contains colorization code
43
+ if string =~ COLORIZED_REGEX
44
+ slice_start, slice_end = args
45
+
46
+ slice_colorized_string(string, slice_start, slice_end)
47
+ else
48
+ string.slice(*args)
49
+ end
50
+ end
51
+ end
52
+
53
+ def slice_colorized_string(string, slice_start, slice_end)
54
+ # store the codes and their position in the original string
55
+ markers = []
56
+ string.scan(COLORIZED_REGEX) do |code|
57
+ marker = { :code => code,
58
+ :position => Regexp.last_match.offset(0).first
59
+ }
60
+ markers << marker
61
+ end
62
+
63
+ markers_before_slice = []
64
+ markers_in_slice = []
65
+ markers_after_slice = []
66
+ # interate over elements in code_markers
67
+ # will be mutating array so cannot use .each
68
+ markers.size.times do
69
+ marker = markers.shift
70
+ # shift remaining markers position by that of the popped code
71
+ markers.map! { |c| c[:position] -= marker[:code].size; c }
72
+ if marker[:position] <= slice_start
73
+ markers_before_slice << marker
74
+ elsif marker[:position] > slice_start && marker[:position] < slice_end
75
+ markers_in_slice << marker
76
+ else
77
+ markers_after_slice << marker
78
+ end
79
+ end
80
+
81
+ # slice the string without the codes
82
+ slice = string.gsub(COLORIZED_REGEX, '').slice(slice_start, slice_end)
83
+
84
+ # insert codes back into the slice
85
+ markers_in_slice.each do |marker|
86
+ slice.insert(marker[:position], marker[:code])
87
+ markers_in_slice.map! { |c| c[:position] += marker[:code].size; c }
88
+ end
89
+
90
+ markers_before_slice.each { |marker| slice.insert(0, marker[:code])}
91
+ markers_after_slice.each { |marker| slice << marker[:code] }
92
+
93
+ slice
94
+ end
95
+ #:startdoc:
96
+ end
97
+ end
98
+ end
99
+
100
+ Hirb::String.extend(Hirb::Colors::StringUtil)
@@ -0,0 +1,5 @@
1
+ module Hirb
2
+ module Colors
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,53 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ describe "Hirb::String" do
4
+
5
+ describe "size" do
6
+ it "should return the size of the string" do
7
+ Hirb::String.size("cats").should == 4
8
+ end
9
+
10
+ describe "with a colorized string" do
11
+ it "should return the size of the string without the color codes" do
12
+ Hirb::String.size("\e[31mcats\e[0m").should == 4
13
+ end
14
+ end
15
+ end
16
+
17
+ describe "ljust" do
18
+ it "should return a properly padded string" do
19
+ Hirb::String.ljust("cats", 6).should == "cats "
20
+ end
21
+
22
+ describe "with a colorized string" do
23
+ it "should return a properly padded string" do
24
+ Hirb::String.ljust("\e[31mcats\e[0m", 6).should == "\e[31mcats\e[0m "
25
+ end
26
+ end
27
+ end
28
+
29
+ describe "rjust" do
30
+ it "should return a properly padded string" do
31
+ Hirb::String.rjust("cats", 6).should == " cats"
32
+ end
33
+
34
+ describe "with a colorized string" do
35
+ it "should return a properly padded string" do
36
+ Hirb::String.rjust("\e[31mcats\e[0m", 6).should == " \e[31mcats\e[0m"
37
+ end
38
+ end
39
+ end
40
+
41
+ describe "slice" do
42
+ it "should return a properly sliced string" do
43
+ Hirb::String.slice("kittycats", 0, 5).should == "kitty"
44
+ end
45
+
46
+ describe "with a colorized string" do
47
+ it "should return a properly sliced string" do
48
+ Hirb::String.slice("\e[31mk\e[30mi\e[29mttyc\e[28mats\e[0m", 0, 5).should == "\e[31mk\e[30mi\e[29mtty\e[28m\e[0m"
49
+ end
50
+ end
51
+ end
52
+
53
+ end
@@ -0,0 +1 @@
1
+ require 'hirb-colors'
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hirb-colors
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Cyrus Farajpour
9
+ - Jens Balvig
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-11-26 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hirb
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '0.5'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '0.5'
31
+ - !ruby/object:Gem::Dependency
32
+ name: bacon
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: 1.1.0
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 1.1.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: mocha
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: mocha-on-bacon
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ - !ruby/object:Gem::Dependency
80
+ name: bacon-bits
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: rake
97
+ requirement: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Color support for hirb
112
+ email:
113
+ - smoils@gmail.com
114
+ - jens@balvig.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - hirb-colors.gemspec
125
+ - lib/hirb-colors.rb
126
+ - lib/hirb/colors.rb
127
+ - lib/hirb/colors/string_util.rb
128
+ - lib/hirb/colors/version.rb
129
+ - test/string_test.rb
130
+ - test/test_helper.rb
131
+ homepage: ''
132
+ licenses: []
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ segments:
144
+ - 0
145
+ hash: -886186224133783565
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ none: false
148
+ requirements:
149
+ - - ! '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ segments:
153
+ - 0
154
+ hash: -886186224133783565
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 1.8.23
158
+ signing_key:
159
+ specification_version: 3
160
+ summary: Color support for hirb
161
+ test_files:
162
+ - test/string_test.rb
163
+ - test/test_helper.rb