tco 0.0.1 → 0.1.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.
- checksums.yaml +4 -4
- data/.gitignore +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +241 -0
- data/Rakefile +7 -0
- data/bin/tco +141 -0
- data/examples/rainbow.rb +19 -0
- data/examples/tux.png +0 -0
- data/examples/tux.rb +10 -0
- data/examples/union_jack.rb +28 -0
- data/lib/tco.rb +173 -0
- data/lib/tco/colouring.rb +227 -0
- data/lib/tco/config.rb +137 -0
- data/lib/tco/palette.rb +590 -0
- data/lib/tco/parser.rb +199 -0
- data/lib/tco/style.rb +51 -0
- data/lib/tco/version.rb +26 -0
- data/spec/parser_spec.rb +166 -0
- data/spec/string_ext_spec.rb +106 -0
- data/spec/tco_spec.rb +16 -0
- data/spec/utils.rb +13 -0
- data/tco.conf-example +46 -0
- data/tco.gemspec +28 -0
- metadata +45 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4472e4cc2bf0a46eddb2cd6d31f30c2dce37b386
|
4
|
+
data.tar.gz: c619231ff03672b43f59104873929c6608b73fb7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d10fc13418a767089535cedf5e8923914d33a1e44006183011585c40034e51cdbfae5e9f5a675a1e183ac7625300ccbc039789aa6878bf3f815c369a37070ae
|
7
|
+
data.tar.gz: aecdca9ecae68ce531254be1a5d8c0198434732c09300a0e9f35fe36a3b158048d11dde24d5f9e598a6b82d6745b01ea45cc05d077a20d5dae491ca9beff9da0
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2013, 2014 Radek Pazdera
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,241 @@
|
|
1
|
+
# tco - terminal colours made simple
|
2
|
+
|
3
|
+
[](http://badge.fury.io/rb/tco)
|
4
|
+
|
5
|
+
The purpose of the **tco** gem is to make colouring in the terminal as
|
6
|
+
convenient and as simple as possible. It provides a library for your Ruby gems
|
7
|
+
and also a standalone command line tool that you can use anywhere else.
|
8
|
+
|
9
|
+
## Introduction
|
10
|
+
|
11
|
+
If you've ever worked with the **extended colour palette** in the terminal, you
|
12
|
+
probably know that it's not really easy to find the colour you want. The
|
13
|
+
palette consists of 256 colours that are evenly sampled through the RGB space.
|
14
|
+
As opposed to the **ANSI palette**, the colours tend to be the same across
|
15
|
+
different terminals. They are assigned linearly to a set of escape sequences
|
16
|
+
that are used to apply the colours. And that is the problem; searching
|
17
|
+
through the palette is very unintuitive.
|
18
|
+
|
19
|
+
On the other hand, selecting a RGB colour using your favourite
|
20
|
+
[colour picker](http://www.colourpicker.com/) is a nice, buttery piece of cake
|
21
|
+
with a bit of cream on top. That is everything you need to do; pass the value
|
22
|
+
to `tco` and it will sort out all the boring bits for you. Using the
|
23
|
+
RGB value, the library will find the **perceptually closest** option that is
|
24
|
+
displayable in your terminal and decorate the string with the appropriate
|
25
|
+
escape sequences, letting you focus on more interesting things in life instead.
|
26
|
+
|
27
|
+
On top of that, it comes with a simple built-in templating engine that you can
|
28
|
+
use to decorate only the parts that you want without having to split your
|
29
|
+
string into 15 parts. Check out the following examples.
|
30
|
+
|
31
|
+
### A few examples
|
32
|
+
|
33
|
+
The `tco` terminal utility allows you to do the simple things you might need
|
34
|
+
in your bash scripts:
|
35
|
+
|
36
|
+
[](http://linuxwell.com/assets/images/posts/tco-terminal.png)
|
37
|
+
|
38
|
+
However, using `tco` directly from Ruby gives you much more flexibility. The
|
39
|
+
following short piece of code will draw a rainbow inside your terminal:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require "tco"
|
43
|
+
|
44
|
+
rainbow = ["#622e90", "#2d3091", "#00aaea", "#02a552", "#fdea22", "#eb443b", "#f37f5a"]
|
45
|
+
10.times do
|
46
|
+
rainbow.each { |colour| print " ".bg colour }
|
47
|
+
puts
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
[](http://linuxwell.com/assets/images/posts/tco-rainbow-2.png)
|
52
|
+
|
53
|
+
And if you add a bit of `rmagick` to the equation, you can even render whole
|
54
|
+
images in your terminal with no more than 10 lines of code.
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
require "tco"
|
58
|
+
require "rmagick"
|
59
|
+
|
60
|
+
Magick::Image.read("tux.png")[0].each_pixel do |pixel, col, row|
|
61
|
+
c = [pixel.red, pixel.green, pixel.blue].map { |v| 255*(v/65535.0) }
|
62
|
+
print " ".bg c
|
63
|
+
puts if col >= 53
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
[](http://linuxwell.com/assets/images/posts/tco-tux.png)
|
68
|
+
|
69
|
+
These were just the basic things you can do with `tco`. There are many more
|
70
|
+
options and features available, many of them are described in the **Usage**
|
71
|
+
section just bellow.
|
72
|
+
|
73
|
+
## Usage
|
74
|
+
|
75
|
+
You can use either the `tco` binary for your command line applications or the
|
76
|
+
library directly from your Ruby scripts. Both use cases are explained bellow.
|
77
|
+
|
78
|
+
### The Ruby library
|
79
|
+
|
80
|
+
When it comes to colouring, there are two options available. You can either use
|
81
|
+
the default library interface, or use the `String` extension that also comes
|
82
|
+
with the library. Both of them offer exactly the same functionality (`fg`, `bg`,
|
83
|
+
`bright`, `underline`). See the example of both, below:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
require "tco"
|
87
|
+
|
88
|
+
# The standard interface
|
89
|
+
puts Tco::fg("#ff0000", Tco::bg("#888888", Tco::bright("London")))
|
90
|
+
puts Tco::underline "Underground"
|
91
|
+
|
92
|
+
# The String object extension
|
93
|
+
puts "London".fg("#ff0000").bg("#888888").bright
|
94
|
+
puts "Underground".underline
|
95
|
+
|
96
|
+
# Using predefined style
|
97
|
+
puts "London".style "alert"
|
98
|
+
puts Tco::style "alert", "Underground"
|
99
|
+
```
|
100
|
+
|
101
|
+
In the last bit of the example above, I'm referring to a preconfigured style
|
102
|
+
definition. You can set them either system-wide in `/etc/tco.conf` or just for
|
103
|
+
a single user in the `~/.tco.conf` file. They both are just simple YAML files.
|
104
|
+
The precise format of these configuration files is explained at the end of this
|
105
|
+
file.
|
106
|
+
|
107
|
+
#### Changing the configuration on the fly
|
108
|
+
|
109
|
+
A notable feature in regards of configuration is the fact, that you can easily
|
110
|
+
change it on-the-fly. This can be useful for adding aliases for colours and
|
111
|
+
defining your own styles just for your application, so you don't have to repeat
|
112
|
+
the same settings all over your script. All these settings will be applied on
|
113
|
+
top of the user and system config files.
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
require "tco"
|
117
|
+
|
118
|
+
tco_conf = Tco::config
|
119
|
+
|
120
|
+
tco_conf["names"]["white"] = "#000"
|
121
|
+
tco_conf["styles"]["pass"] = {
|
122
|
+
"fg" => "#000",
|
123
|
+
"bg" => "#00ff00",
|
124
|
+
"bright" => false,
|
125
|
+
"underline" => false,
|
126
|
+
}
|
127
|
+
|
128
|
+
Tco::reconfigure tco_conf
|
129
|
+
```
|
130
|
+
|
131
|
+
Apart from that, the library then contains a few more advanced things which
|
132
|
+
aren't that useful for everyone. Please, have a look at `lib/tco/tco.rb` if
|
133
|
+
you're interested.
|
134
|
+
|
135
|
+
### The command-line tool
|
136
|
+
|
137
|
+
Using the `tco` command is just as simple as using the Ruby library. It expects
|
138
|
+
input either as a positional argument or alternatively at the standard input.
|
139
|
+
|
140
|
+
Apart from the core functionality, the CLI tool adds support for simple
|
141
|
+
templates that let you markup certain parts of your string. All templates
|
142
|
+
are enclosed in double curly brackets `{{fg:bg:ub text}}`. Check out the
|
143
|
+
following examples:
|
144
|
+
|
145
|
+
```bash
|
146
|
+
tco -f "#c0ffee" -b "white" "Some input text"
|
147
|
+
tco -b "grey" -B "Some input text"
|
148
|
+
|
149
|
+
tco "{{alert ERROR:}} The {{::b download}} has failed."
|
150
|
+
echo "{{#000:#ffffff black on white}}" | tco
|
151
|
+
```
|
152
|
+
|
153
|
+
For the full list of options, please refer to the help of the `tco` command.
|
154
|
+
|
155
|
+
### Specifying colours
|
156
|
+
|
157
|
+
With both the Ruby library and the command line tool, there is a number of
|
158
|
+
ways how to specify the colour you would like:
|
159
|
+
|
160
|
+
* **RGB** - you can do either `#c0ffee` or `0xc0ffee`
|
161
|
+
* **name** - e.g., `black` or `white`, you can assign names to colours in your
|
162
|
+
configuration file
|
163
|
+
* **index** - you can also refer to colours through an index to the palette,
|
164
|
+
e.g., `@0` or `@176`.
|
165
|
+
|
166
|
+
|
167
|
+
## Configuration
|
168
|
+
|
169
|
+
There are two places, where you can store your configuration:
|
170
|
+
|
171
|
+
* `/etc/tco.conf` - the system-wide configuration file
|
172
|
+
* `~/.tco.conf` - user configuration file (takes precedence)
|
173
|
+
|
174
|
+
Both of them are simple YAML text files. Pick the first one if you would like
|
175
|
+
to apply your settings system-wide, go with the second option to set things up
|
176
|
+
for yourself only (recommended).
|
177
|
+
|
178
|
+
And now the important bit, in order to make **tco** work best in your
|
179
|
+
environment, it is recommended to configure your **ANSI palette**. These 16
|
180
|
+
colours are configurable in most terminals (you probably have yours customised
|
181
|
+
too). **tco** needs to know your setup, so it can make decisions which colour
|
182
|
+
to use. If you don't configure them, tco will not use the ANSI colours at all
|
183
|
+
in order to avoid mistakes. You can do so like this:
|
184
|
+
|
185
|
+
```yaml
|
186
|
+
# Don't forget changing the values to match your terminal configuration
|
187
|
+
colour_values:
|
188
|
+
"@0": "#3b3b3b"
|
189
|
+
"@1": "#cf6a4c"
|
190
|
+
"@2": "#99ad6a"
|
191
|
+
"@3": "#d8ad4c"
|
192
|
+
"@4": "#597bc5"
|
193
|
+
"@5": "#a037b0"
|
194
|
+
"@6": "#71b9f8"
|
195
|
+
"@7": "#adadad"
|
196
|
+
|
197
|
+
"@8": "#555555"
|
198
|
+
"@9": "#ff5555"
|
199
|
+
"@10": "#55ff55"
|
200
|
+
"@11": "#ffff55"
|
201
|
+
"@12": "#5555ff"
|
202
|
+
"@13": "#ff55ff"
|
203
|
+
"@14": "#55ffff"
|
204
|
+
"@15": "#ffffff"
|
205
|
+
```
|
206
|
+
|
207
|
+
Apart from that you can also create **aliases** for colours and define
|
208
|
+
short-cut styles for configurations you tend to use often. A typical
|
209
|
+
configuration file would look like this:
|
210
|
+
|
211
|
+
```yaml
|
212
|
+
# This is unnecessary (extended palette is enabled by default).
|
213
|
+
# However, you can set this to ansi if you use an older terminal.
|
214
|
+
palette: "extended"
|
215
|
+
|
216
|
+
colour_values:
|
217
|
+
"@0": "#3b3b3b"
|
218
|
+
"@1": "#cf6a4c"
|
219
|
+
"@2": "#99ad6a"
|
220
|
+
"@3": "#d8ad4c"
|
221
|
+
# and so on ...
|
222
|
+
|
223
|
+
names:
|
224
|
+
black: "#000"
|
225
|
+
red: "@1"
|
226
|
+
|
227
|
+
styles:
|
228
|
+
error:
|
229
|
+
fg: "black"
|
230
|
+
bg: "red"
|
231
|
+
bright: "true"
|
232
|
+
underline: "false"
|
233
|
+
```
|
234
|
+
|
235
|
+
## Contributing
|
236
|
+
|
237
|
+
1. Fork it ( http://github.com/pazdera/tco/fork )
|
238
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
239
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
240
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
241
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/tco
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# tco - terminal colouring application and library
|
3
|
+
# Copyright (c) 2013, 2014 Radek Pazdera
|
4
|
+
|
5
|
+
# MIT License
|
6
|
+
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
|
14
|
+
# The above copyright notice and this permission notice shall be included in
|
15
|
+
# all copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23
|
+
# THE SOFTWARE.
|
24
|
+
|
25
|
+
require 'optparse'
|
26
|
+
require 'tco'
|
27
|
+
|
28
|
+
options = {:newline => true}
|
29
|
+
parser = OptionParser.new do |opts|
|
30
|
+
opts.banner = "Usage: tco [options] [TEXT]"
|
31
|
+
|
32
|
+
opts.on("-f COLOUR", "--foreground COLOUR", "Foreground colour") do |fg|
|
33
|
+
options[:fg] = fg
|
34
|
+
end
|
35
|
+
|
36
|
+
opts.on("-b COLOUR", "--background COLOUR", "Background colour") do |bg|
|
37
|
+
options[:bg] = bg
|
38
|
+
end
|
39
|
+
|
40
|
+
opts.on("-B", "--bright", "Use bright/bold font") do |bright|
|
41
|
+
options[:bright] = bright
|
42
|
+
end
|
43
|
+
|
44
|
+
opts.on("-u", "--underline", "Underlined text") do |underline|
|
45
|
+
options[:underline] = underline
|
46
|
+
end
|
47
|
+
|
48
|
+
opts.on("-s STYLE", "--style STYLE",
|
49
|
+
"Decorate with a preset style") do |style|
|
50
|
+
options[:style] = style
|
51
|
+
end
|
52
|
+
|
53
|
+
opts.on("-n", "--no-newline",
|
54
|
+
"Don't append \\n to the end of the string") do |newline|
|
55
|
+
options[:newline] = false
|
56
|
+
end
|
57
|
+
|
58
|
+
opts.on("-d", "--display-palette",
|
59
|
+
"Show the current colour pallete") do |display|
|
60
|
+
options[:display] = true
|
61
|
+
end
|
62
|
+
|
63
|
+
opts.on("-p PALETTE", "--force-palette PALETTE",
|
64
|
+
"Force either ansi or extended palette") do |palette|
|
65
|
+
options[:palette] = palette
|
66
|
+
end
|
67
|
+
|
68
|
+
opts.on("-o", "--disable-parsing",
|
69
|
+
"Disable the {{fg:bg text}} and",
|
70
|
+
"{{style text}} formatting syntax") do |dp|
|
71
|
+
options[:disable_parsing] = true
|
72
|
+
end
|
73
|
+
|
74
|
+
opts.on("-r", "--raw-output", "Print the output escaped") do |raw|
|
75
|
+
options[:raw] = true
|
76
|
+
end
|
77
|
+
|
78
|
+
# TODO: This should output HTML (not implemented yet)
|
79
|
+
#opts.on("-w", "--web", "Output HTML") do |html|
|
80
|
+
# options[:html] = html
|
81
|
+
#end
|
82
|
+
end
|
83
|
+
|
84
|
+
begin
|
85
|
+
parser.parse!
|
86
|
+
|
87
|
+
config = Tco::config
|
88
|
+
|
89
|
+
# Force palette setting
|
90
|
+
config.options["palette"] = options[:palette] if options.has_key? :palette
|
91
|
+
config.options["output"] = :raw if options.has_key? :raw
|
92
|
+
|
93
|
+
Tco::reconfigure config
|
94
|
+
|
95
|
+
if options.has_key? :display
|
96
|
+
Tco::display_palette
|
97
|
+
Kernel.exit! 0
|
98
|
+
end
|
99
|
+
|
100
|
+
if options.has_key? :style
|
101
|
+
style = Tco::get_style options[:style]
|
102
|
+
else
|
103
|
+
style = Tco::Style.new
|
104
|
+
end
|
105
|
+
|
106
|
+
if options.has_key? :fg
|
107
|
+
style.fg = options[:fg]
|
108
|
+
end
|
109
|
+
|
110
|
+
if options.has_key? :bg
|
111
|
+
style.bg = options[:bg]
|
112
|
+
end
|
113
|
+
|
114
|
+
if options.has_key? :bright
|
115
|
+
style.bright = options[:bright]
|
116
|
+
end
|
117
|
+
|
118
|
+
if options.has_key? :underline
|
119
|
+
style.underline = options[:underline]
|
120
|
+
end
|
121
|
+
|
122
|
+
if ARGV.length > 0
|
123
|
+
input = ARGV[0].gsub /\\[nt]/, "\\n" => "\n", "\\t" => "\t"
|
124
|
+
else
|
125
|
+
input = ""
|
126
|
+
while input_line = gets
|
127
|
+
input << input_line.gsub(/\\[nt]/, "\\n" => "\n", "\\t" => "\t")
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
if options.has_key? :disable_parsing
|
132
|
+
print Tco::decorate input, style
|
133
|
+
else
|
134
|
+
print Tco::parse input, style
|
135
|
+
end
|
136
|
+
|
137
|
+
print "\n" if options[:newline] && !(input =~ /\n$/)
|
138
|
+
rescue Exception => e
|
139
|
+
puts e
|
140
|
+
exit
|
141
|
+
end
|
data/examples/rainbow.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "tco"
|
4
|
+
|
5
|
+
conf = Tco.config
|
6
|
+
conf.names["purple"] = "#622e90"
|
7
|
+
conf.names["dark-blue"] = "#2d3091"
|
8
|
+
conf.names["blue"] = "#00aaea"
|
9
|
+
conf.names["green"] = "#02a552"
|
10
|
+
conf.names["yellow"] = "#fdea22"
|
11
|
+
conf.names["orange"] = "#f37f5a"
|
12
|
+
conf.names["red"] = "#eb443b"
|
13
|
+
Tco.reconfigure conf
|
14
|
+
|
15
|
+
rainbow = ["purple", "dark-blue", "blue", "green", "yellow", "orange", "red"]
|
16
|
+
10.times do
|
17
|
+
rainbow.each { |colour| print " ".bg colour }
|
18
|
+
puts
|
19
|
+
end
|