tco 0.1.0 → 0.1.1

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
  SHA1:
3
- metadata.gz: 4472e4cc2bf0a46eddb2cd6d31f30c2dce37b386
4
- data.tar.gz: c619231ff03672b43f59104873929c6608b73fb7
3
+ metadata.gz: 94ecede596bc95edcd19290a9cdb8220eb64a9a6
4
+ data.tar.gz: 5bb02dc3efa44a85fa79c2b9af4550489360b08e
5
5
  SHA512:
6
- metadata.gz: 9d10fc13418a767089535cedf5e8923914d33a1e44006183011585c40034e51cdbfae5e9f5a675a1e183ac7625300ccbc039789aa6878bf3f815c369a37070ae
7
- data.tar.gz: aecdca9ecae68ce531254be1a5d8c0198434732c09300a0e9f35fe36a3b158048d11dde24d5f9e598a6b82d6745b01ea45cc05d077a20d5dae491ca9beff9da0
6
+ metadata.gz: b2b8caeefe0f8aabc4976422ecd4a4558e2d31d545ed8b4d23e9fdbe4fb3854da21394150ad46c3178f4c89af8ed26cb6ec6776e5e3d0ac7875fe0f4ad192bce
7
+ data.tar.gz: bf03af845a23893bd97b7f5897251f6e907640e6ef23ccd514978599ddbd66a9ea720c18daad6dd52189f3f95370bbc9426ef29eed1b4bf515e24e4b2b146fa9
data/bin/tco CHANGED
@@ -25,7 +25,7 @@
25
25
  require 'optparse'
26
26
  require 'tco'
27
27
 
28
- options = {:newline => true}
28
+ options = {:newline => true, :debug => false}
29
29
  parser = OptionParser.new do |opts|
30
30
  opts.banner = "Usage: tco [options] [TEXT]"
31
31
 
@@ -75,6 +75,10 @@ parser = OptionParser.new do |opts|
75
75
  options[:raw] = true
76
76
  end
77
77
 
78
+ opts.on("-D", "--debug", "Run tco in debug mode") do |raw|
79
+ options[:debug] = true
80
+ end
81
+
78
82
  # TODO: This should output HTML (not implemented yet)
79
83
  #opts.on("-w", "--web", "Output HTML") do |html|
80
84
  # options[:html] = html
@@ -83,7 +87,6 @@ end
83
87
 
84
88
  begin
85
89
  parser.parse!
86
-
87
90
  config = Tco::config
88
91
 
89
92
  # Force palette setting
@@ -120,7 +123,7 @@ begin
120
123
  end
121
124
 
122
125
  if ARGV.length > 0
123
- input = ARGV[0].gsub /\\[nt]/, "\\n" => "\n", "\\t" => "\t"
126
+ input = ARGV[0].gsub(/\\[nt]/, "\\n" => "\n", "\\t" => "\t")
124
127
  else
125
128
  input = ""
126
129
  while input_line = gets
@@ -136,6 +139,10 @@ begin
136
139
 
137
140
  print "\n" if options[:newline] && !(input =~ /\n$/)
138
141
  rescue Exception => e
139
- puts e
140
- exit
142
+ if options[:debug]
143
+ raise e
144
+ else
145
+ puts e
146
+ exit
147
+ end
141
148
  end
data/lib/tco/colouring.rb CHANGED
@@ -59,8 +59,8 @@ module Tco
59
59
  def decorate(string, (fg, bg, bright, underline))
60
60
  return string unless STDOUT.isatty || @output_type == :raw
61
61
 
62
- fg = to_colour fg
63
- bg = to_colour bg
62
+ fg = get_colour_instance fg
63
+ bg = get_colour_instance bg
64
64
 
65
65
  output = []
66
66
  lines = string.lines.map(&:chomp)
@@ -116,12 +116,20 @@ module Tco
116
116
 
117
117
  def colour_ansi(string, fg=nil, bg=nil)
118
118
  unless fg == nil
119
- colour_id = @palette.match_colour(fg)
119
+ colour_id = if fg.is_a? Unknown
120
+ fg.id
121
+ else
122
+ @palette.match_colour(fg)
123
+ end
120
124
  string = e(colour_id + 30) + string
121
125
  end
122
126
 
123
127
  unless bg == nil
124
- colour_id = @palette.match_colour(bg)
128
+ colour_id = if bg.is_a? Unknown
129
+ bg.id
130
+ else
131
+ @palette.match_colour(bg)
132
+ end
125
133
  string = e(colour_id + 40) + string
126
134
  end
127
135
 
@@ -134,12 +142,20 @@ module Tco
134
142
 
135
143
  def colour_extended(string, fg=nil, bg=nil)
136
144
  unless fg == nil
137
- colour_id = @palette.match_colour(fg)
145
+ colour_id = if fg.is_a? Unknown
146
+ fg.id
147
+ else
148
+ @palette.match_colour(fg)
149
+ end
138
150
  string = e("38;5;#{colour_id}") + string
139
151
  end
140
152
 
141
153
  unless bg == nil
142
- colour_id = @palette.match_colour(bg)
154
+ colour_id = if bg.is_a? Unknown
155
+ bg.id
156
+ else
157
+ @palette.match_colour(bg)
158
+ end
143
159
  string = e("48;5;#{colour_id}") + string
144
160
  end
145
161
 
@@ -192,11 +208,17 @@ module Tco
192
208
 
193
209
  def resolve_colour_def(colour_def)
194
210
  return nil if colour_def == "" || colour_def == "default"
211
+
195
212
  begin
196
- @palette.get_colour_value parse_colour_id colour_def
213
+ id = parse_colour_id colour_def
214
+ if @palette.is_known? id
215
+ Colour.new @palette.get_colour_value id
216
+ else
217
+ Unknown.new id
218
+ end
197
219
  rescue RuntimeError
198
220
  begin
199
- parse_rgb_value colour_def
221
+ Colour.new parse_rgb_value colour_def
200
222
  rescue RuntimeError
201
223
  begin
202
224
  colour_def = resolve_colour_name colour_def
@@ -212,16 +234,19 @@ module Tco
212
234
  end
213
235
  end
214
236
 
215
- def to_colour(value)
216
- rgb = case
217
- when value.is_a?(String) then resolve_colour_def value
218
- when value.is_a?(Array) then value
219
- when value.is_a?(Colour) then value.rgb
220
- when value == nil then return nil
221
- else raise "Colour value type '#{value.class}' not supported."
222
- end
223
-
224
- Colour.new rgb
237
+ def get_colour_instance(value)
238
+ case
239
+ when value.is_a?(String)
240
+ resolve_colour_def value
241
+ when value.is_a?(Array)
242
+ Colour.new value
243
+ when value.is_a?(Colour) || value.is_a?(Unknown)
244
+ value
245
+ when value == nil
246
+ nil
247
+ else
248
+ raise "Colour value type '#{value.class}' not supported."
249
+ end
225
250
  end
226
251
  end
227
252
  end
data/lib/tco/palette.rb CHANGED
@@ -22,6 +22,18 @@
22
22
  # THE SOFTWARE.
23
23
 
24
24
  module Tco
25
+ class Unknown
26
+ attr_reader :id
27
+
28
+ def initialize(id)
29
+ @id = id
30
+ end
31
+
32
+ def to_s
33
+ "@#{@id}"
34
+ end
35
+ end
36
+
25
37
  class Colour
26
38
  attr_reader :rgb, :lab
27
39
 
@@ -243,7 +255,8 @@ module Tco
243
255
  xDL = xDL / (kl * xSL)
244
256
  xDC = xDC / (kc * xSC)
245
257
  xDH = xDH / (kh * xSH)
246
- de = Math.sqrt(xDL**2 + xDC**2 + xDH**2 + xRT * xDC * xDH)
258
+
259
+ Math.sqrt(xDL**2 + xDC**2 + xDH**2 + xRT * xDC * xDH)
247
260
  end
248
261
  end
249
262
 
@@ -260,22 +273,22 @@ module Tco
260
273
  # they were explicitly configured in tco.conf.
261
274
  #
262
275
  # The colour values in comments are the defaults for xterm.
263
- nil, # [0, 0, 0]
264
- nil, # [205, 0, 0]
265
- nil, # [0, 205, 0]
266
- nil, # [205, 205, 0]
267
- nil, # [0, 0, 238]
268
- nil, # [205, 0, 205]
269
- nil, # [0, 205, 205]
270
- nil, # [229, 229, 229]
271
- nil, # [127, 127, 127]
272
- nil, # [255, 0, 0]
273
- nil, # [0, 255, 0]
274
- nil, # [255, 255, 0]
275
- nil, # [92, 92, 255]
276
- nil, # [255, 0, 255]
277
- nil, # [0, 255, 255]
278
- nil, # [255, 255, 255]
276
+ Unknown.new(0), # [0, 0, 0]
277
+ Unknown.new(1), # [205, 0, 0]
278
+ Unknown.new(2), # [0, 205, 0]
279
+ Unknown.new(3), # [205, 205, 0]
280
+ Unknown.new(4), # [0, 0, 238]
281
+ Unknown.new(5), # [205, 0, 205]
282
+ Unknown.new(6), # [0, 205, 205]
283
+ Unknown.new(7), # [229, 229, 229]
284
+ Unknown.new(8), # [127, 127, 127]
285
+ Unknown.new(9), # [255, 0, 0]
286
+ Unknown.new(10), # [0, 255, 0]
287
+ Unknown.new(11), # [255, 255, 0]
288
+ Unknown.new(12), # [92, 92, 255]
289
+ Unknown.new(13), # [255, 0, 255]
290
+ Unknown.new(14), # [0, 255, 255]
291
+ Unknown.new(15), # [255, 255, 255]
279
292
 
280
293
  # The colours bellow are the definitions from xterm extended
281
294
  # colour palette. They should be the same across terminals.
@@ -529,10 +542,15 @@ module Tco
529
542
 
530
543
  def get_colour_value(id)
531
544
  raise "Id '#{id}' out of range." unless id.between?(0, @palette.length-1)
545
+ raise "Value of colour '#{id}' is unknown" if @palette[id].is_a? Unknown
532
546
  @palette[id].rgb if @palette[id]
533
547
  end
534
548
 
535
- # Returns an index of the closest colour in the palette
549
+ def is_known?(id)
550
+ raise "Id '#{id}' out of range." unless id.between?(0, @palette.length-1)
551
+ !@palette[id].is_a? Unknown
552
+ end
553
+
536
554
  def match_colour(colour)
537
555
  unless colour.is_a? Colour
538
556
  msg = "Unsupported argument type '#{colour.class}', must be 'Colour'."
@@ -547,7 +565,7 @@ module Tco
547
565
  if @cache.has_key? colour.to_s
548
566
  @cache[colour.to_s]
549
567
  else
550
- distances = colours.map { |c| c ? c - colour : Float::INFINITY }
568
+ distances = colours.map { |c| c.is_a?(Colour) ? c - colour : Float::INFINITY }
551
569
  colour_index = distances.each_with_index.min[1]
552
570
 
553
571
  # TODO: No cache eviction is currently in place
data/lib/tco/version.rb CHANGED
@@ -22,5 +22,5 @@
22
22
  # THE SOFTWARE.
23
23
 
24
24
  module Tco
25
- VERSION = "0.1.0"
25
+ VERSION = "0.1.1"
26
26
  end
data/lib/tco.rb CHANGED
@@ -88,6 +88,13 @@ module Tco
88
88
  def self.config
89
89
  @config
90
90
  end
91
+
92
+ def self.configure
93
+ c = config
94
+ yield(c)
95
+ reconfigure(c)
96
+ c
97
+ end
91
98
 
92
99
  def self.reconfigure(config)
93
100
  @config = config
@@ -113,11 +120,10 @@ module Tco
113
120
  black = Tco::Colour.new([0,0,0])
114
121
  white = Tco::Colour.new([255,255,255])
115
122
 
116
- font_colour = if (colours[c] - black).abs > (colours[c] - white).abs
117
- black
118
- else
119
- white
120
- end
123
+ font_colour = black
124
+ if colours[c].is_a?(Colour) && (colours[c] - black).abs < (colours[c] - white).abs
125
+ font_colour = white
126
+ end
121
127
 
122
128
  square_styles.push [c, font_colour, colours[c]]
123
129
  c += 1
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tco
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Radek Pazdera
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-30 00:00:00.000000000 Z
11
+ date: 2014-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler