lscolors 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +13 -6
- data/lib/lscolors.rb +148 -141
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce0d0a9417d7943c4914bb1e0ecf597255a46c74
|
4
|
+
data.tar.gz: 78a443cb981cb8a7a8a60f4bdb0c0b9890cee6f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ffb89026c34dc585094d59b97ccf9a8e2901f0fb1388897b2cf57ee7c6d1f29b8f088e17cec83e635ef2e00dd480cb1ea7fdc6ca78003f9cc0b5120caf85092
|
7
|
+
data.tar.gz: 4ae82b94672758012e2806c3747c0a65c34ac54ee6f5a92a5d775931b90edf451db78fc0a941bd9093ea390496d9d56462d6809ef852a0c24f46bba03effd6f9
|
data/README.rdoc
CHANGED
@@ -3,11 +3,15 @@
|
|
3
3
|
{<img src="http://inch-ci.org/github/Winterbraid/lscolors_ruby.svg?branch=master" alt="Inline docs" />}[http://inch-ci.org/github/Winterbraid/lscolors_ruby]
|
4
4
|
{<img src="https://codeclimate.com/github/Winterbraid/lscolors_ruby/badges/gpa.svg" />}[https://codeclimate.com/github/Winterbraid/lscolors_ruby]
|
5
5
|
|
6
|
+
Source code:: https://github.com/Winterbraid/lscolors_ruby
|
7
|
+
Gem:: https://rubygems.org/gems/lscolors
|
8
|
+
Documentation:: http://www.rubydoc.info/gems/lscolors/frames
|
9
|
+
|
6
10
|
Allows loading color definitions from JSON, previewing, and printing a
|
7
11
|
+LS_COLORS+ compatible config string.
|
8
12
|
|
9
13
|
To use the configuration, do the following in your terminal:
|
10
|
-
export LS_COLORS=`ruby -rlscolors -e 'print LsColors'`
|
14
|
+
export LS_COLORS=`ruby -rlscolors -e 'print LsColors::Colors'`
|
11
15
|
You will probably want to add the above line to your shell's +rc+ file.
|
12
16
|
|
13
17
|
By default, LsColors will try to load a file called +.lscolors_ruby.rc+
|
@@ -15,24 +19,27 @@ from the user's home directory. That file can also be created by running
|
|
15
19
|
+LsColors.save+ without any parameters.
|
16
20
|
|
17
21
|
==Example use
|
22
|
+
require lscolors
|
23
|
+
|
24
|
+
include LsColors
|
18
25
|
|
19
26
|
# Output the LS_COLORS configuration string
|
20
|
-
|
27
|
+
Colors.to_s
|
21
28
|
|
22
29
|
# Preview the current settings
|
23
|
-
puts
|
30
|
+
puts Colors.preview
|
24
31
|
|
25
32
|
# Save the settings to a file
|
26
|
-
|
33
|
+
Colors.save 'test.json'
|
27
34
|
|
28
35
|
# Display all available colors and their numbers
|
29
|
-
puts
|
36
|
+
puts Colors.test
|
30
37
|
|
31
38
|
==Troubleshooting
|
32
39
|
|
33
40
|
LsColors assumes a 256-color compatible terminal. Use the following code
|
34
41
|
to test your terminal's capability (this can also help you get color numbers):
|
35
|
-
puts LsColors.test
|
42
|
+
puts LsColors::Colors.test
|
36
43
|
|
37
44
|
If your terminal does not seem to support 256 colors, try adding the following
|
38
45
|
line to your shell's +rc+ file:
|
data/lib/lscolors.rb
CHANGED
@@ -3,7 +3,7 @@ require 'json'
|
|
3
3
|
# {include:file:README.rdoc}
|
4
4
|
module LsColors
|
5
5
|
# Current software version.
|
6
|
-
VERSION = '1.0.
|
6
|
+
VERSION = '1.0.2'
|
7
7
|
|
8
8
|
# Date of the current version.
|
9
9
|
DATE = '2014-10-30'
|
@@ -11,142 +11,67 @@ module LsColors
|
|
11
11
|
# A short description.
|
12
12
|
ABOUT = 'Manipulate LS_COLORS settings.'
|
13
13
|
|
14
|
-
#
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
}
|
76
|
-
|
77
|
-
class << self
|
78
|
-
# Loads or initializes settings. The parameter can be either a Hash or a
|
79
|
-
# String (in the latter case it will be interpreted as the file name of
|
80
|
-
# a JSON file). If missing, the method will look for a JSON file specified
|
81
|
-
# by {RC_FILE}, and if absent, the {DEFAULTS} will be loaded.
|
82
|
-
#
|
83
|
-
# @param [String, Hash] object
|
84
|
-
# @return [Hash] the parsed settings.
|
85
|
-
def load(object = nil)
|
86
|
-
return @colors = object if object.is_a? Hash
|
87
|
-
|
88
|
-
if object.is_a? String
|
89
|
-
return @colors = JSON.parse(File.read(object), symbolize_names: true)
|
90
|
-
elsif File.exist? RC_FILE
|
91
|
-
return @colors = JSON.parse(File.read(RC_FILE), symbolize_names: true)
|
92
|
-
end
|
93
|
-
|
94
|
-
@colors = DEFAULTS
|
95
|
-
end
|
96
|
-
|
97
|
-
# Will preview the current settings as a string of colorized system files
|
98
|
-
# and extensions. Use +print+ or +puts+ to see the effect (a 256-color
|
99
|
-
# terminal is required).
|
100
|
-
#
|
101
|
-
# @return [String]
|
102
|
-
def preview
|
103
|
-
"#{preview_system}\n#{preview_extensions}"
|
104
|
-
end
|
105
|
-
|
106
|
-
# Save the current settings to a file. Without parameters, {RC_FILE} will be
|
107
|
-
# created and/or saved. {RC_FILE} will be loaded automatically from now on,
|
108
|
-
# so it can be modified directly in order to configure the color settings.
|
109
|
-
#
|
110
|
-
# @param [String] file the file name to save to.
|
111
|
-
# @return [0]
|
112
|
-
def save(file = RC_FILE)
|
113
|
-
File.write(file, JSON.pretty_generate(@colors))
|
114
|
-
0
|
115
|
-
end
|
116
|
-
|
117
|
-
# Test the terminal color capability.
|
118
|
-
#
|
119
|
-
# @return [String]
|
120
|
-
def test
|
121
|
-
colors = `tput colors`.to_i
|
122
|
-
fail "256 colors not supported! (#{colors})" if colors < 256
|
123
|
-
|
124
|
-
"System colors (0-15):\n#{test_system}\n" \
|
125
|
-
"Color cube (16-231):\n#{test_cube}\n" \
|
126
|
-
"Grayscale colors (232-255):\n#{test_grayscale}\n"
|
127
|
-
end
|
128
|
-
|
129
|
-
# Will output the +LS_COLORS+ compatible configuration string.
|
130
|
-
#
|
131
|
-
# @return [String]
|
132
|
-
def to_s
|
133
|
-
output = 'rs=0:'
|
134
|
-
|
135
|
-
@colors[:system].each do |c|
|
136
|
-
output << "#{c.first}=#{color_setting(c)}:"
|
137
|
-
end
|
138
|
-
|
139
|
-
@colors[:extensions].each do |c|
|
140
|
-
c.first.split.sort.each do |ext|
|
141
|
-
output << "*.#{ext}=#{color_setting(c)}:"
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
output
|
146
|
-
end
|
147
|
-
alias_method :inspect, :to_s
|
148
|
-
|
149
|
-
private
|
14
|
+
# Methods and data for manipulating terminal colors.
|
15
|
+
module ColorManipulation
|
16
|
+
# The string that begins a terminal-compatible color definition.
|
17
|
+
COLOR_HEAD = "\x1b["
|
18
|
+
|
19
|
+
# The prefix for the foreground color (256 colors).
|
20
|
+
COLOR_FG = '38;5;'
|
21
|
+
|
22
|
+
# The prefix for the background color (256 colors).
|
23
|
+
COLOR_BG = '48;5;'
|
24
|
+
|
25
|
+
# The string that ends a terminal-compatible color definition.
|
26
|
+
COLOR_TAIL = 'm'
|
27
|
+
|
28
|
+
# This string resets color and style to default.
|
29
|
+
COLOR_RESET = "\x1b[0m"
|
30
|
+
|
31
|
+
# The style codes recognized by the terminal.
|
32
|
+
STYLES = {
|
33
|
+
bold: '01',
|
34
|
+
under: '04',
|
35
|
+
blink: '05',
|
36
|
+
reverse: '07',
|
37
|
+
conceal: '08'
|
38
|
+
}
|
39
|
+
|
40
|
+
# Explanation of system-related file codes, for previewing and learning
|
41
|
+
# purposes.
|
42
|
+
CODE_DESCRIPTIONS = {
|
43
|
+
di: 'directory',
|
44
|
+
fi: 'file',
|
45
|
+
ln: 'symlink',
|
46
|
+
pi: 'fifo',
|
47
|
+
so: 'socket',
|
48
|
+
bd: 'block',
|
49
|
+
or: 'orphan',
|
50
|
+
mi: 'missing',
|
51
|
+
ex: 'executable'
|
52
|
+
}
|
53
|
+
|
54
|
+
# The fallback color sheet.
|
55
|
+
DEFAULTS = {
|
56
|
+
system: [
|
57
|
+
['di', 33, :bold], ['ln', 45], ['mh', 15, 5],
|
58
|
+
['pi', 11, 1], ['so', 13], ['do', 5], ['bd', 11, 232], ['cd', 3, 232],
|
59
|
+
['or', 45, 9], ['mi', 15, 232, :blink], ['su', 15, 196],
|
60
|
+
['sg', 16, 11], ['ca', 226, 196], ['tw', 16, 10], ['ow', 21, 10],
|
61
|
+
['st', 15, 21], ['ex', 35, :bold]
|
62
|
+
],
|
63
|
+
extensions: [
|
64
|
+
['sh pl py swf bat exe', 35],
|
65
|
+
['rb rbw rdoc gemspec gem', 197],
|
66
|
+
['png jpg jpeg gif bmp tiff xcf', 111],
|
67
|
+
['mp4 mkv avi ogm mov mpeg flv wmv', 171],
|
68
|
+
['mp3 mp2 ogg flac wav wma ape mid midi', 168],
|
69
|
+
['gb gba nds 3ds bin iso dat grp pak', 202],
|
70
|
+
['rar zip tar gz tgz 7z bz2', 215],
|
71
|
+
['txt odt doc docx pdf djvu', 247],
|
72
|
+
['bak old log', 241]
|
73
|
+
]
|
74
|
+
}
|
150
75
|
|
151
76
|
# Test the 16 system colors.
|
152
77
|
#
|
@@ -161,9 +86,9 @@ module LsColors
|
|
161
86
|
def test_cube
|
162
87
|
output = (0..17).map do |row|
|
163
88
|
column = 16 + (row * 6) + (36 * (row / 6))
|
164
|
-
|
165
|
-
|
166
|
-
)
|
89
|
+
cube_1 = (column..column + 5).to_a
|
90
|
+
cube_2 = (column + 36..column + 41).to_a
|
91
|
+
test_array(cube_1 + cube_2)
|
167
92
|
end
|
168
93
|
|
169
94
|
output.join
|
@@ -250,6 +175,88 @@ module LsColors
|
|
250
175
|
string
|
251
176
|
end
|
252
177
|
end
|
178
|
+
|
179
|
+
# Load, modify, and output configuration.
|
180
|
+
module Colors
|
181
|
+
extend ColorManipulation
|
182
|
+
|
183
|
+
# The default configuration file. This will be loaded at startup if present.
|
184
|
+
RC_FILE = Dir.home << '/.lscolors_ruby.rc'
|
185
|
+
|
186
|
+
class << self
|
187
|
+
# Loads or initializes settings. The parameter can be either a Hash or a
|
188
|
+
# String (in the latter case it will be interpreted as the file name of
|
189
|
+
# a JSON file). If missing, the method will look for a JSON file specified
|
190
|
+
# by {RC_FILE}, and if absent, the {DEFAULTS} will be loaded.
|
191
|
+
#
|
192
|
+
# @param [String, Hash] object
|
193
|
+
# @return [Hash] the parsed settings.
|
194
|
+
def load(object = nil)
|
195
|
+
return @colors = object if object.is_a? Hash
|
196
|
+
|
197
|
+
if object.is_a? String
|
198
|
+
return @colors = JSON.parse(File.read(object), symbolize_names: true)
|
199
|
+
elsif File.exist? RC_FILE
|
200
|
+
return @colors = JSON.parse(File.read(RC_FILE), symbolize_names: true)
|
201
|
+
end
|
202
|
+
|
203
|
+
@colors = DEFAULTS
|
204
|
+
end
|
205
|
+
|
206
|
+
# Will preview the current settings as a string of colorized system files
|
207
|
+
# and extensions. Use +print+ or +puts+ to see the effect (a 256-color
|
208
|
+
# terminal is required).
|
209
|
+
#
|
210
|
+
# @return [String]
|
211
|
+
def preview
|
212
|
+
"#{preview_system}\n#{preview_extensions}"
|
213
|
+
end
|
214
|
+
|
215
|
+
# Save the current settings to a file. Without parameters, {RC_FILE} will
|
216
|
+
# be created and/or saved. {RC_FILE} will be loaded automatically from
|
217
|
+
# now on, so it can be modified directly in order to configure the color
|
218
|
+
# settings.
|
219
|
+
#
|
220
|
+
# @param [String] file the file name to save to.
|
221
|
+
# @return [0]
|
222
|
+
def save(file = RC_FILE)
|
223
|
+
File.write(file, JSON.pretty_generate(@colors))
|
224
|
+
0
|
225
|
+
end
|
226
|
+
|
227
|
+
# Test the terminal color capability.
|
228
|
+
#
|
229
|
+
# @return [String]
|
230
|
+
def test
|
231
|
+
colors = `tput colors`.to_i
|
232
|
+
fail "256 colors not supported! (#{colors})" if colors < 256
|
233
|
+
|
234
|
+
"System colors (0-15):\n#{test_system}\n" \
|
235
|
+
"Color cube (16-231):\n#{test_cube}\n" \
|
236
|
+
"Grayscale colors (232-255):\n#{test_grayscale}\n"
|
237
|
+
end
|
238
|
+
|
239
|
+
# Will output the +LS_COLORS+ compatible configuration string.
|
240
|
+
#
|
241
|
+
# @return [String]
|
242
|
+
def to_s
|
243
|
+
output = 'rs=0:'
|
244
|
+
|
245
|
+
@colors[:system].each do |c|
|
246
|
+
output << "#{c.first}=#{color_setting(c)}:"
|
247
|
+
end
|
248
|
+
|
249
|
+
@colors[:extensions].each do |c|
|
250
|
+
c.first.split.sort.each do |ext|
|
251
|
+
output << "*.#{ext}=#{color_setting(c)}:"
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
output
|
256
|
+
end
|
257
|
+
alias_method :inspect, :to_s
|
258
|
+
end
|
259
|
+
end
|
253
260
|
end
|
254
261
|
|
255
|
-
LsColors.load
|
262
|
+
LsColors::Colors.load
|