colorato 0.9.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bb6270007938fb0916b87353d58dd27bd2e7955d746480e857b1ed2519211993
4
- data.tar.gz: 3ffb11adc797a497a7b63fafb7e9a92c46fe65b36ae802e53b3651b96f3f292a
3
+ metadata.gz: 6a04ec628455d986e1a98d0046ee1b63b1f046c1bc6d7018ef5cf2524998e18e
4
+ data.tar.gz: 947d85416c2d2f2a2d28645f97858788886a58b5c4088ac4b4bb55917ca781aa
5
5
  SHA512:
6
- metadata.gz: 1a81a30427e2ba3952cddd887f8c113219ae7ef3f96672a9f3464eda375abcfd09c6a74d5a39e28d8a05da24f32b7ce89041ed141c5828c8126f393361aa3f4d
7
- data.tar.gz: 8ac4184b1ecfd075a9ff9e877ac0db21b1c244787a978f57ef6c5226e276fcc36fbcd924e904c296b6eff185b1eac6f62fcdfe0d89d657009bbf079c32203d3c
6
+ metadata.gz: 261f7abe0f7bebe479efae920db1ccc976c547ea2e312e1406b7efa637d0273e1eb77483b6945e63b6c3204c61389c40ef7172db1f4ca6e1bc0d64be8889add1
7
+ data.tar.gz: 968df424e0014f91f5cbe5fce37b51576790ecfd04453e6a88d168e2b096b8de9cbd617b765eef0cf76c0c96c5adf8c765794cad3364daa7c9db4f9b20f85764
data/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
  # colorato
3
3
 
4
4
 
5
+ ## 1.0.0 released 2023-01-03
6
+
7
+ * Introduce `make colo.rb`
8
+ * Allow for Colorato .colors, .nocolours, .nocolors and .no_colors
9
+ * Limit to Colorato.colours and Colorato.no_colours
10
+
11
+
5
12
  ## 0.9.0 released 2023-01-02
6
13
 
7
14
  * initial release
data/README.md CHANGED
@@ -4,6 +4,136 @@
4
4
  ANSI colours tool. Taken out of [flor](https://github.com/floraison/flor).
5
5
 
6
6
 
7
+ ## Usage
8
+
9
+ ```ruby
10
+ require 'colorato'
11
+
12
+ C = Colorato.colours
13
+ NC = Colorato.nocolours
14
+
15
+ C.blue # => "\e[34m"
16
+ C.blue('car') # => "\e[34mcar\e[0;0m"
17
+
18
+ NC.blue('car') # => "car"
19
+ ```
20
+
21
+ ### .decolour(string)
22
+
23
+ ```ruby
24
+ Colorato.decoulour("car") # => 'car'
25
+ Colorato.decoulour("\e[34mcar\e[0;0m") # => 'car'
26
+ ```
27
+
28
+ ### .nocolour_length(string)
29
+
30
+ ```ruby
31
+ Colorato.nocolour_length("car") # => 3
32
+ Colorato.nocolour_length("\e[34mcar\e[0;0m") # => 3
33
+ ```
34
+
35
+ ### .truncate_string(string, maxlength, ellipsis='...')
36
+
37
+ ```ruby
38
+ Colorato.truncate_string("hello world!", 7)
39
+ # => "hello w..."
40
+ Colorato.truncate_string("hello #{@colours.blue('world')}!", 7)
41
+ # => "hello \e[34mw\e[0;0m..."
42
+
43
+ Colorato.truncate_string("hello world!", 7, 'XXX')
44
+ # => "hello wXXX"
45
+ Colorato.truncate_string("hello #{@colours.blue('world')}!", 7, 'XXX')
46
+ # => "hello \e[34mw\e[0;0mXXX"
47
+
48
+ Colorato.truncate_string("hello world!", 7, :ellipsis)
49
+ # => "hello Ol…"
50
+ Colorato.truncate_string("hello #{@colours.blue('world')}!", 7, :ellipsis)
51
+ # => "hello \e[34mw\e[0;0m…"
52
+ ```
53
+
54
+ ### colours
55
+
56
+ ```ruby
57
+ def reset(s=nil)
58
+ def bright(s=nil)
59
+ def dim(s=nil)
60
+ def underlined(s=nil)
61
+ def blink(s=nil)
62
+ def reverse(s=nil)
63
+ def hidden(s=nil)
64
+ def strike(s=nil)
65
+ def default(s=nil)
66
+ def black(s=nil)
67
+ def red(s=nil)
68
+ def green(s=nil)
69
+ def yellow(s=nil)
70
+ def blue(s=nil)
71
+ def magenta(s=nil)
72
+ def cyan(s=nil)
73
+ def light_gray(s=nil)
74
+ def dark_gray(s=nil)
75
+ def light_red(s=nil)
76
+ def light_green(s=nil)
77
+ def light_yellow(s=nil)
78
+ def light_blue(s=nil)
79
+ def light_magenta(s=nil)
80
+ def light_cyan(s=nil)
81
+ def white(s=nil)
82
+ def bg_default(s=nil)
83
+ def bg_black(s=nil)
84
+ def bg_red(s=nil)
85
+ def bg_green(s=nil)
86
+ def bg_yellow(s=nil)
87
+ def bg_blue(s=nil)
88
+ def bg_magenta(s=nil)
89
+ def bg_cyan(s=nil)
90
+ def bg_light_gray(s=nil)
91
+ def bg_dark_gray(s=nil)
92
+ def bg_light_red(s=nil)
93
+ def bg_light_green(s=nil)
94
+ def bg_light_yellow(s=nil)
95
+ def bg_light_blue(s=nil)
96
+ def bg_light_magenta(s=nil)
97
+ def bg_light_cyan(s=nil)
98
+ def bg_white(s=nil)
99
+ alias brown yellow
100
+ alias purple magenta
101
+ alias dark_grey dark_gray
102
+ alias light_grey light_gray
103
+ alias rd red
104
+ alias bl blue
105
+ alias bu blue
106
+ alias ba black
107
+ alias bk black
108
+ alias gn green
109
+ alias gr green
110
+ alias dg dark_gray
111
+ alias gy light_gray
112
+ alias lg light_gray
113
+ alias yl yellow
114
+ alias y yellow
115
+ alias ma magenta
116
+ alias wt white
117
+ alias rs reset
118
+ alias br bright
119
+ alias bri bright
120
+ alias un underlined
121
+ alias rv reverse
122
+ alias bn blink
123
+ alias blg bg_light_gray
124
+ alias und underlined
125
+ alias rev reverse
126
+ ```
127
+
128
+ ### colo.rb
129
+
130
+ ```
131
+ make colo.rb
132
+ ```
133
+
134
+ Generates a static `colo.rb` ready to be shrunk and pasted in a project.
135
+
136
+
7
137
  ## LICENSE
8
138
 
9
139
  MIT, see [LICENSE.txt](LICENSE.txt)
data/lib/colorato/core.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+
3
4
  module Colorato
4
5
 
5
6
  COLOURS = Hash[*%w[
@@ -26,58 +27,49 @@ module Colorato
26
27
 
27
28
  ]].freeze
28
29
 
29
- class Colours
30
-
31
- Colorato::COLOURS.each do |k, v|
32
- if v.match(/\A\d/) # Ruby 2.3 doesn't have String#match?
33
- class_eval(%{
34
- def #{k}(s=nil)
35
- s ? "[#{v}m" + s + "" : "[#{v}m"
36
- end })
37
- else
38
- class_eval(
39
- "alias #{k} #{v}")
40
- end
41
- end
42
- end
43
-
44
- class NoColours
45
-
46
- Colorato::COLOURS.each do |k, v|
47
- if v.match(/\A\d/) # Ruby 2.3 doesn't have String#match?
48
- class_eval("def #{k}(s=''); s; end")
49
- else
50
- class_eval("alias #{k} #{v}")
51
- end
30
+ class Colours; end
31
+ class NoColours; end
32
+ #
33
+ Colorato::COLOURS.each do |k, v|
34
+
35
+ if v.match(/\A\d/) # Ruby 2.3 doesn't have String#match?
36
+
37
+ ::Colorato::Colours.class_eval(%{
38
+ def #{k}(s=nil)
39
+ s ?
40
+ "\e[#{v}m\#{s}\e[0;0m" :
41
+ "\e[#{v}m"
42
+ end })
43
+ ::Colorato::NoColours.class_eval(%{
44
+ def #{k}(s=nil)
45
+ s ?
46
+ s :
47
+ ''
48
+ end })
49
+
50
+ else
51
+
52
+ c = %{ alias #{k} #{v} }
53
+ ::Colorato::Colours.class_eval(c)
54
+ ::Colorato::NoColours.class_eval(c)
52
55
  end
53
56
  end
54
57
 
55
- @colours = Colours.new
56
- @no_colours = NoColours.new
58
+ class << self
57
59
 
58
- def self.no_colours
60
+ def colours
59
61
 
60
- @no_colours
61
- end
62
-
63
- def self.colours(opts={})
64
-
65
- c = nil;
66
- [ :color, :colour, :colors, :colours ].each do |k|
67
- if opts.has_key?(k); c = opts[k]; break; end
68
- end
69
-
70
- return @colours if c == true
71
- return @no_colours if c == false
62
+ @colours ||= Colours.new
63
+ end
72
64
 
73
- o = opts[:out] || $stdout
65
+ def no_colours
74
66
 
75
- return @colours if (
76
- (o.respond_to?(:log_colours?) ? o.log_colours? : o.tty?) ||
77
- ($0[-6..-1] == '/rspec' &&
78
- (ARGV.include?('--tty') || ARGV.include?('--color'))))
67
+ @no_colours ||= NoColours.new
68
+ end
79
69
 
80
- @no_colours
70
+ alias colors colours
71
+ alias nocolors no_colours
72
+ alias no_colors no_colours
81
73
  end
82
74
  end
83
75
 
@@ -14,12 +14,17 @@ module Colorato; class << self
14
14
 
15
15
  def truncate_string(s, maxlen, post='...')
16
16
 
17
+ post = '…' if post == :ellipsis
18
+
17
19
  ncl = no_colour_length(s)
18
20
  r = StringIO.new
19
21
  l = 0
20
22
 
23
+ col = false
24
+
21
25
  s.scan(/(\x1b\[\d+(?:;\d+)?m|[^\x1b]+)/) do |ss, _|
22
26
  if ss[0, 1] == ""
27
+ col = true
23
28
  r << ss
24
29
  else
25
30
  ss = ss[0, maxlen - l]
@@ -31,6 +36,8 @@ module Colorato; class << self
31
36
 
32
37
  return r.string if l < maxlen
33
38
 
39
+ r << "\e[0;0m" if col
40
+
34
41
  if post.is_a?(String)
35
42
  r << post
36
43
  elsif post.is_a?(Proc)
@@ -49,28 +56,3 @@ module Colorato; class << self
49
56
  end; end
50
57
 
51
58
 
52
- #if $0 == __FILE__
53
- #
54
- # puts "# frozen_string_literal: true"
55
- # puts
56
- # puts "module Colorato; class << self"
57
- # Colorato::COLOURS.each do |k, v|
58
- # if v.match?(/\A\d/)
59
- # puts " def #{k}(s=nil); s ? \"[#{v}m\#{s}\" : \"[#{v}m\"; end"
60
- # else
61
- # puts " alias #{k} #{v}"
62
- # end
63
- # end
64
- # puts "end; end"
65
- # puts
66
- # puts "module NoColorato; class << self"
67
- # Colorato::COLOURS.each do |k, v|
68
- # if v.match?(/\A\d/)
69
- # puts " def #{k}(s=nil); s ? s : ''; end"
70
- # else
71
- # puts " alias #{k} #{v}"
72
- # end
73
- # end
74
- # puts "end; end"
75
- #end
76
-
data/lib/colorato.rb CHANGED
@@ -3,9 +3,9 @@
3
3
 
4
4
  module Colorato
5
5
 
6
- VERSION = '0.9.0'
6
+ VERSION = '1.0.0'
7
7
  end
8
8
 
9
9
  require 'colorato/core'
10
- require 'colorato/module'
10
+ require 'colorato/more'
11
11
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: colorato
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Mettraux
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-02 00:00:00.000000000 Z
11
+ date: 2023-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -38,7 +38,7 @@ files:
38
38
  - colorato.gemspec
39
39
  - lib/colorato.rb
40
40
  - lib/colorato/core.rb
41
- - lib/colorato/module.rb
41
+ - lib/colorato/more.rb
42
42
  homepage: https://github.com/floraison/colorato
43
43
  licenses:
44
44
  - MIT