lscolors 1.0.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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.rdoc +45 -0
  4. data/lib/lscolors.rb +255 -0
  5. metadata +63 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 672e30fd44487f9322be34872a3c36943fcd32fd
4
+ data.tar.gz: b57d54a378ad63b864482ad0a88c5380e2deb660
5
+ SHA512:
6
+ metadata.gz: ed3897bd901e5ed8d73f6bcea77fd447d1870c49181dc07691035d05f09b4fc0212b6c6a3e6bea896d5f76a13af8d1ae967bf820e152bc81ab8703c336ce2da7
7
+ data.tar.gz: 9d047750e84c7c01b09387d43bbcd4d31906d76d81408ad96dab31e9b9dac0a5f6cc9a74f915fe7c0d5b1cec9c0707811eb6ac0b9b3d378a1fcd3eed94b0da69
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Winterbraid
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.
@@ -0,0 +1,45 @@
1
+ =LsColors
2
+
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
+ {<img src="https://codeclimate.com/github/Winterbraid/lscolors_ruby/badges/gpa.svg" />}[https://codeclimate.com/github/Winterbraid/lscolors_ruby]
5
+
6
+ Allows loading color definitions from JSON, previewing, and printing a
7
+ +LS_COLORS+ compatible config string.
8
+
9
+ To use the configuration, do the following in your terminal:
10
+ export LS_COLORS=`ruby -rlscolors -e 'print LsColors'`
11
+ You will probably want to add the above line to your shell's +rc+ file.
12
+
13
+ By default, LsColors will try to load a file called +.lscolors_ruby.rc+
14
+ from the user's home directory. That file can also be created by running
15
+ +LsColors.save+ without any parameters.
16
+
17
+ ==Example use
18
+
19
+ # Output the LS_COLORS configuration string
20
+ LsColors.to_s
21
+
22
+ # Preview the current settings
23
+ puts LsColors.preview
24
+
25
+ # Save the settings to a file
26
+ LsColors.save 'test.json'
27
+
28
+ # Display all available colors and their numbers
29
+ puts LsColors.test
30
+
31
+ ==Troubleshooting
32
+
33
+ LsColors assumes a 256-color compatible terminal. Use the following code
34
+ to test your terminal's capability (this can also help you get color numbers):
35
+ puts LsColors.test
36
+
37
+ If your terminal does not seem to support 256 colors, try adding the following
38
+ line to your shell's +rc+ file:
39
+ export TERM=xterm-256color
40
+
41
+ If you are looking for a good graphic terminal emulator, try
42
+ {Terminator}[http://gnometerminator.blogspot.com/p/introduction.html].
43
+ If the colors do not seem to display correctly in +screen+, refer to
44
+ {this guide}[http://robotsrule.us/vim/] or try
45
+ {Tmux}[http://tmux.sourceforge.net/], which can do all +screen+ can and more.
@@ -0,0 +1,255 @@
1
+ require 'json'
2
+
3
+ # {include:file:README.rdoc}
4
+ module LsColors
5
+ # Current software version.
6
+ VERSION = '1.0.0'
7
+
8
+ # Date of the current version.
9
+ DATE = '2014-10-30'
10
+
11
+ # A short description.
12
+ ABOUT = 'Manipulate LS_COLORS settings.'
13
+
14
+ # The default configuration file. This will be loaded at startup if present.
15
+ RC_FILE = Dir.home << '/.lscolors_ruby.rc'
16
+
17
+ # The string that begins a terminal-compatible color definition.
18
+ COLOR_HEAD = "\x1b["
19
+
20
+ # The prefix for the foreground color (256 colors).
21
+ COLOR_FG = '38;5;'
22
+
23
+ # The prefix for the background color (256 colors).
24
+ COLOR_BG = '48;5;'
25
+
26
+ # The string that ends a terminal-compatible color definition.
27
+ COLOR_TAIL = 'm'
28
+
29
+ # This string resets color and style to default.
30
+ COLOR_RESET = "\x1b[0m"
31
+
32
+ # The style codes recognized by the terminal.
33
+ STYLES = {
34
+ bold: '01',
35
+ under: '04',
36
+ blink: '05',
37
+ reverse: '07',
38
+ conceal: '08'
39
+ }
40
+
41
+ # Explanation of system-related file codes, for previewing and learning
42
+ # purposes.
43
+ CODE_DESCRIPTIONS = {
44
+ di: 'directory',
45
+ fi: 'file',
46
+ ln: 'symlink',
47
+ pi: 'fifo',
48
+ so: 'socket',
49
+ bd: 'block',
50
+ or: 'orphan',
51
+ mi: 'missing',
52
+ ex: 'executable'
53
+ }
54
+
55
+ # The fallback color sheet.
56
+ DEFAULTS = {
57
+ system: [
58
+ ['di', 33, :bold], ['ln', 45], ['mh', 15, 5],
59
+ ['pi', 11, 1], ['so', 13], ['do', 5], ['bd', 11, 232], ['cd', 3, 232],
60
+ ['or', 45, 9], ['mi', 15, 232, :blink], ['su', 15, 196],
61
+ ['sg', 16, 11], ['ca', 226, 196], ['tw', 16, 10], ['ow', 21, 10],
62
+ ['st', 15, 21], ['ex', 35, :bold]
63
+ ],
64
+ extensions: [
65
+ ['sh pl py swf bat exe', 35],
66
+ ['rb rbw rdoc gemspec gem', 197],
67
+ ['png jpg jpeg gif bmp tiff xcf', 111],
68
+ ['mp4 mkv avi ogm mov mpeg flv wmv', 171],
69
+ ['mp3 mp2 ogg flac wav wma ape mid midi', 168],
70
+ ['gb gba nds 3ds bin iso dat grp pak', 202],
71
+ ['rar zip tar gz tgz 7z bz2', 215],
72
+ ['txt odt doc docx pdf djvu', 247],
73
+ ['bak old log', 241]
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
150
+
151
+ # Test the 16 system colors.
152
+ #
153
+ # @return [String]
154
+ def test_system
155
+ [(0..7), (8..15)].map { |range| test_array range.to_a }.join
156
+ end
157
+
158
+ # Test the 6x6x6 color cube.
159
+ #
160
+ # @return [String]
161
+ def test_cube
162
+ output = (0..17).map do |row|
163
+ column = 16 + (row * 6) + (36 * (row / 6))
164
+ test_array(
165
+ ((column..column + 5).to_a + (column + 36..column + 41).to_a)
166
+ )
167
+ end
168
+
169
+ output.join
170
+ end
171
+
172
+ # Test the 24 grayscale colors.
173
+ #
174
+ # @return [String]
175
+ def test_grayscale
176
+ [(232..243), (244..255)].map { |range| test_array range.to_a }.join
177
+ end
178
+
179
+ # Test an array of color numbers.
180
+ #
181
+ # @param [Array] array the color numbers to test.
182
+ # @return [String]
183
+ def test_array(array)
184
+ output = array.map { |color| color_card(color) } << "\n"
185
+
186
+ output.join
187
+ end
188
+
189
+ # Returns a preview of a single color.
190
+ #
191
+ # @param [Integer] color
192
+ # @return [String]
193
+ def color_card(color)
194
+ "#{colorizer ['foo', 0, color]} #{format '%03d', color}#{COLOR_RESET}"
195
+ end
196
+
197
+ # Take an array as per the {DEFAULTS} format and create a terminal style
198
+ # escape string.
199
+ #
200
+ # @param [Array] array
201
+ # @return [String]
202
+ def colorizer(array)
203
+ fail "No foreground color in #{array}." unless array[1].is_a? Integer
204
+ "#{COLOR_HEAD}#{color_setting(array)}#{COLOR_TAIL}"
205
+ end
206
+
207
+ # Take an array as per the {DEFAULTS} format and create a color definition
208
+ # string.
209
+ #
210
+ # @param [Array] array
211
+ # @return [String]
212
+ def color_setting(array)
213
+ string = ''
214
+
215
+ STYLES.each_pair do |style, code|
216
+ next unless array.include?(style) || array.include?(style.to_s)
217
+ string << "#{code};"
218
+ end
219
+ string << "#{COLOR_FG}#{array[1]}"
220
+ string << ";#{COLOR_BG}#{array[2]}" if array[2].is_a? Integer
221
+
222
+ string
223
+ end
224
+
225
+ # Return a colorized preview of the system file color settings.
226
+ #
227
+ # @return [String]
228
+ def preview_system
229
+ array = @colors[:system].map do |c|
230
+ code = CODE_DESCRIPTIONS.fetch(c.first.to_sym, c.first)
231
+ "#{colorizer(c)}#{code}#{COLOR_RESET}"
232
+ end
233
+
234
+ array.join ' '
235
+ end
236
+
237
+ # Return a colorized preview of the extension color settings.
238
+ #
239
+ # @return [String]
240
+ def preview_extensions
241
+ string = ''
242
+
243
+ @colors[:extensions].each do |c|
244
+ c.first.split.sort.each do |ext|
245
+ string << "#{colorizer(c)}*.#{ext}#{COLOR_RESET} "
246
+ end
247
+ string << "\n"
248
+ end
249
+
250
+ string
251
+ end
252
+ end
253
+ end
254
+
255
+ LsColors.load
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lscolors
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Winterbraid
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: |2
28
+ Parses hashes or JSON files and generates LS_COLORS compatible config
29
+ strings.
30
+ email: Winterbraid@users.noreply.github.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - LICENSE.txt
36
+ - README.rdoc
37
+ - lib/lscolors.rb
38
+ homepage: https://github.com/Winterbraid/lscolors_ruby
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.2.2
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Manipulate LS_COLORS settings.
62
+ test_files: []
63
+ has_rdoc: