colorato 0.9.0 → 1.1.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: b28fd8267ee3efc81eeff84c4d0cb31904898b0c97f52277966e720f2e739ffd
4
+ data.tar.gz: 2d096f6c8970cebea52a1e30fba7bd4fab9d5d85741c1968caed17cc0941d060
5
5
  SHA512:
6
- metadata.gz: 1a81a30427e2ba3952cddd887f8c113219ae7ef3f96672a9f3464eda375abcfd09c6a74d5a39e28d8a05da24f32b7ce89041ed141c5828c8126f393361aa3f4d
7
- data.tar.gz: 8ac4184b1ecfd075a9ff9e877ac0db21b1c244787a978f57ef6c5226e276fcc36fbcd924e904c296b6eff185b1eac6f62fcdfe0d89d657009bbf079c32203d3c
6
+ metadata.gz: '08c68f4329d2d36927ccfe3d02805ef8f21bde89f0ddb70a71b935454f8b2fcbe6557098d3ded29b2e38664a9307849f5fd9978fe2227867fafa2792ff1c6d75'
7
+ data.tar.gz: 8cc6529dd0968df22087e816cc6a78b8b6ca4a6a109455b2401e7bf85273072b820b6979af0497aabb8c927b4298dd757a2bc2d4f3f5bb2f3c5f71535bbf210e
data/CHANGELOG.md CHANGED
@@ -2,6 +2,19 @@
2
2
  # colorato
3
3
 
4
4
 
5
+ ## 1.1.0 released 2025-01-16
6
+
7
+ * colorato.blue { 'blo' + 'ck' }
8
+ * Show the colours when `ruby lib/colorato.rb`
9
+
10
+
11
+ ## 1.0.0 released 2023-01-03
12
+
13
+ * Introduce `make colo.rb`
14
+ * Allow for Colorato .colors, .nocolours, .nocolors and .no_colors
15
+ * Limit to Colorato.colours and Colorato.no_colours
16
+
17
+
5
18
  ## 0.9.0 released 2023-01-02
6
19
 
7
20
  * initial release
data/LICENSE.txt CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- Copyright (c) 2015-2023, John Mettraux, jmettraux+flor@gmail.com
2
+ Copyright (c) 2015-2025, John Mettraux, jmettraux+flor@gmail.com
3
3
 
4
4
  Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -4,6 +4,138 @@
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
+ C.blue { 'car' } # => "\e[34mcar\e[0;0m"
18
+
19
+ NC.blue('car') # => "car"
20
+ NC.blue { 'car' } # => "car"
21
+ ```
22
+
23
+ ### .decolour(string)
24
+
25
+ ```ruby
26
+ Colorato.decoulour("car") # => 'car'
27
+ Colorato.decoulour("\e[34mcar\e[0;0m") # => 'car'
28
+ ```
29
+
30
+ ### .nocolour_length(string)
31
+
32
+ ```ruby
33
+ Colorato.nocolour_length("car") # => 3
34
+ Colorato.nocolour_length("\e[34mcar\e[0;0m") # => 3
35
+ ```
36
+
37
+ ### .truncate_string(string, maxlength, ellipsis='...')
38
+
39
+ ```ruby
40
+ Colorato.truncate_string("hello world!", 7)
41
+ # => "hello w..."
42
+ Colorato.truncate_string("hello #{@colours.blue('world')}!", 7)
43
+ # => "hello \e[34mw\e[0;0m..."
44
+
45
+ Colorato.truncate_string("hello world!", 7, 'XXX')
46
+ # => "hello wXXX"
47
+ Colorato.truncate_string("hello #{@colours.blue('world')}!", 7, 'XXX')
48
+ # => "hello \e[34mw\e[0;0mXXX"
49
+
50
+ Colorato.truncate_string("hello world!", 7, :ellipsis)
51
+ # => "hello Ol…"
52
+ Colorato.truncate_string("hello #{@colours.blue('world')}!", 7, :ellipsis)
53
+ # => "hello \e[34mw\e[0;0m…"
54
+ ```
55
+
56
+ ### colours
57
+
58
+ ```ruby
59
+ def reset(s=nil)
60
+ def bright(s=nil)
61
+ def dim(s=nil)
62
+ def underlined(s=nil)
63
+ def blink(s=nil)
64
+ def reverse(s=nil)
65
+ def hidden(s=nil)
66
+ def strike(s=nil)
67
+ def default(s=nil)
68
+ def black(s=nil)
69
+ def red(s=nil)
70
+ def green(s=nil)
71
+ def yellow(s=nil)
72
+ def blue(s=nil)
73
+ def magenta(s=nil)
74
+ def cyan(s=nil)
75
+ def light_gray(s=nil)
76
+ def dark_gray(s=nil)
77
+ def light_red(s=nil)
78
+ def light_green(s=nil)
79
+ def light_yellow(s=nil)
80
+ def light_blue(s=nil)
81
+ def light_magenta(s=nil)
82
+ def light_cyan(s=nil)
83
+ def white(s=nil)
84
+ def bg_default(s=nil)
85
+ def bg_black(s=nil)
86
+ def bg_red(s=nil)
87
+ def bg_green(s=nil)
88
+ def bg_yellow(s=nil)
89
+ def bg_blue(s=nil)
90
+ def bg_magenta(s=nil)
91
+ def bg_cyan(s=nil)
92
+ def bg_light_gray(s=nil)
93
+ def bg_dark_gray(s=nil)
94
+ def bg_light_red(s=nil)
95
+ def bg_light_green(s=nil)
96
+ def bg_light_yellow(s=nil)
97
+ def bg_light_blue(s=nil)
98
+ def bg_light_magenta(s=nil)
99
+ def bg_light_cyan(s=nil)
100
+ def bg_white(s=nil)
101
+ alias brown yellow
102
+ alias purple magenta
103
+ alias dark_grey dark_gray
104
+ alias light_grey light_gray
105
+ alias rd red
106
+ alias bl blue
107
+ alias bu blue
108
+ alias ba black
109
+ alias bk black
110
+ alias gn green
111
+ alias gr green
112
+ alias dg dark_gray
113
+ alias gy light_gray
114
+ alias lg light_gray
115
+ alias yl yellow
116
+ alias y yellow
117
+ alias ma magenta
118
+ alias wt white
119
+ alias rs reset
120
+ alias br bright
121
+ alias bri bright
122
+ alias un underlined
123
+ alias rv reverse
124
+ alias bn blink
125
+ alias blg bg_light_gray
126
+ alias und underlined
127
+ alias rev reverse
128
+ ```
129
+
130
+ ### colo.rb
131
+
132
+ ```
133
+ make colo.rb
134
+ ```
135
+
136
+ Generates a static `colo.rb` ready to be shrunk and pasted in a project.
137
+
138
+
7
139
  ## LICENSE
8
140
 
9
141
  MIT, see [LICENSE.txt](LICENSE.txt)
data/colorato.gemspec CHANGED
@@ -26,6 +26,7 @@ terminal colors extracted from flor
26
26
  'homepage_uri' => s.homepage,
27
27
  'source_code_uri' => s.homepage,
28
28
  #'wiki_uri' => s.homepage + '/wiki',
29
+ 'rubygems_mfa_required' => 'true',
29
30
  }
30
31
 
31
32
  #s.files = `git ls-files`.split("\n")
@@ -40,7 +41,8 @@ terminal colors extracted from flor
40
41
  #s.add_runtime_dependency 'raabro', '~> 1.4'
41
42
  #s.add_runtime_dependency 'et-orbi', '~> 1', '>= 1.2.7'
42
43
 
43
- s.add_development_dependency 'rspec', '~> 3.12'
44
+ #s.add_development_dependency 'rspec', '~> 3.12'
45
+ s.add_development_dependency 'probatio', '~> 1'
44
46
 
45
47
  s.require_path = 'lib'
46
48
  end
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[
@@ -11,6 +12,7 @@ module Colorato
11
12
  black 30 red 31 green 32 yellow 33 blue 34 magenta 35 cyan 36
12
13
  light_gray 37 dark_gray 90 light_red 91 light_green 92 light_yellow 93
13
14
  light_blue 94 light_magenta 95 light_cyan 96 white 97
15
+
14
16
  bg_default 49 bg_black 40 bg_red 41 bg_green 42 bg_yellow 43 bg_blue 44
15
17
  bg_magenta 45 bg_cyan 46 bg_light_gray 47 bg_dark_gray 100
16
18
  bg_light_red 101 bg_light_green 102 bg_light_yellow 103
@@ -26,58 +28,79 @@ module Colorato
26
28
 
27
29
  ]].freeze
28
30
 
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
31
+ class Colours; end
32
+ class NoColours; end
33
+ #
34
+ Colorato::COLOURS.each do |k, v|
35
+
36
+ if v.match(/\A\d/) # Ruby 2.3 doesn't have String#match?
37
+
38
+ ::Colorato::Colours.class_eval(%{
39
+ def #{k}(s=nil, &block)
40
+ (x = (block && block.call) || s) ?
41
+ "\e[#{v}m\#{x}\e[0;0m" :
42
+ "\e[#{v}m"
43
+ end })
44
+ ::Colorato::NoColours.class_eval(%{
45
+ def #{k}(s=nil, &block)
46
+ (x = (block && block.call) || s) ? x : ''
47
+ end })
48
+
49
+ else
50
+
51
+ c = %{ alias #{k} #{v} }
52
+ ::Colorato::Colours.class_eval(c)
53
+ ::Colorato::NoColours.class_eval(c)
41
54
  end
42
55
  end
43
56
 
44
- class NoColours
57
+ class << self
45
58
 
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
59
+ def colours
60
+
61
+ @colours ||= Colours.new
52
62
  end
53
- end
54
63
 
55
- @colours = Colours.new
56
- @no_colours = NoColours.new
64
+ def no_colours
57
65
 
58
- def self.no_colours
66
+ @no_colours ||= NoColours.new
67
+ end
59
68
 
60
- @no_colours
61
- end
69
+ alias colors colours
70
+ alias nocolors no_colours
71
+ alias no_colors no_colours
62
72
 
63
- def self.colours(opts={})
73
+ def show_colours
64
74
 
65
- c = nil;
66
- [ :color, :colour, :colors, :colours ].each do |k|
67
- if opts.has_key?(k); c = opts[k]; break; end
68
- end
75
+ cs = Colorato.colours.public_methods
76
+ .inject([]) { |a, n|
77
+ m = n.to_s.match(/^bg_(.+)$/)
78
+ a << m[1] if m
79
+ a }
80
+
81
+ #%w[ (default) bright dim ].each do |filter|
69
82
 
70
- return @colours if c == true
71
- return @no_colours if c == false
83
+ puts
72
84
 
73
- o = opts[:out] || $stdout
85
+ fs = %w[ (def) bright dim ]
74
86
 
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'))))
87
+ puts "%14s %-9s %-9s %-9s" % ([ '' ] + fs)
88
+ puts
79
89
 
80
- @no_colours
90
+ cs.each do |n|
91
+ a = [ n ]
92
+ fs.each do |f|
93
+ f = Colorato.colours.send(f) rescue ''
94
+ fgc = f + Colorato.colours.send(n)
95
+ bgc = f + Colorato.colours.send("bg_#{n}")
96
+ a << (fgc + 'Text' + Colorato.colours.reset)
97
+ a << (bgc + ' ' + Colorato.colours.reset)
98
+ end
99
+ puts "%14s: %s %s %s %s %s %s" % a
100
+ end
101
+
102
+ puts
103
+ end
81
104
  end
82
105
  end
83
106
 
@@ -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,12 @@
3
3
 
4
4
  module Colorato
5
5
 
6
- VERSION = '0.9.0'
6
+ VERSION = '1.1.0'
7
7
  end
8
8
 
9
9
  require 'colorato/core'
10
- require 'colorato/module'
10
+ require 'colorato/more'
11
+
12
+
13
+ Colorato.show_colours if __FILE__ == $0
11
14
 
metadata CHANGED
@@ -1,29 +1,29 @@
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.1.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: 2025-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rspec
14
+ name: probatio
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '3.12'
19
+ version: '1'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '3.12'
26
+ version: '1'
27
27
  description: terminal colors extracted from flor
28
28
  email:
29
29
  - jmettraux+flor@gmail.com
@@ -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
@@ -48,6 +48,7 @@ metadata:
48
48
  bug_tracker_uri: https://github.com/floraison/colorato/issues
49
49
  homepage_uri: https://github.com/floraison/colorato
50
50
  source_code_uri: https://github.com/floraison/colorato
51
+ rubygems_mfa_required: 'true'
51
52
  post_install_message:
52
53
  rdoc_options: []
53
54
  require_paths:
@@ -63,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
64
  - !ruby/object:Gem::Version
64
65
  version: '0'
65
66
  requirements: []
66
- rubygems_version: 3.1.6
67
+ rubygems_version: 3.5.16
67
68
  signing_key:
68
69
  specification_version: 4
69
70
  summary: terminal colors for flor