Module: LsColors::ColorManipulation
- Included in:
- Colors
- Defined in:
- lib/lscolors.rb
Overview
Methods and data for manipulating terminal colors.
Constant Summary collapse
- COLOR_HEAD =
The string that begins a terminal-compatible color definition.
"\x1b["- COLOR_FG =
The prefix for the foreground color (256 colors).
'38;5;'- COLOR_BG =
The prefix for the background color (256 colors).
'48;5;'- COLOR_TAIL =
The string that ends a terminal-compatible color definition.
'm'- COLOR_RESET =
This string resets color and style to default.
"\x1b[0m"- STYLES =
The style codes recognized by the terminal.
{ bold: '01', under: '04', blink: '05', reverse: '07', conceal: '08' }
- CODE_DESCRIPTIONS =
Explanation of system-related file codes, for previewing and learning purposes.
{ di: 'directory', fi: 'file', ln: 'symlink', pi: 'fifo', so: 'socket', bd: 'block', or: 'orphan', mi: 'missing', ex: 'executable' }
- DEFAULTS =
The fallback color sheet.
{ system: [ ['di', 33, :bold], ['ln', 45], ['mh', 15, 5], ['pi', 11, 1], ['so', 13], ['do', 5], ['bd', 11, 232], ['cd', 3, 232], ['or', 45, 9], ['mi', 15, 232, :blink], ['su', 15, 196], ['sg', 16, 11], ['ca', 226, 196], ['tw', 16, 10], ['ow', 21, 10], ['st', 15, 21], ['ex', 35, :bold] ], extensions: [ ['sh pl py swf bat exe', 35], ['rb rbw rdoc gemspec gem', 197], ['png jpg jpeg gif bmp tiff xcf', 111], ['mp4 mkv avi ogm mov mpeg flv wmv', 171], ['mp3 mp2 ogg flac wav wma ape mid midi', 168], ['gb gba nds 3ds bin iso dat grp pak', 202], ['rar zip tar gz tgz 7z bz2', 215], ['txt odt doc docx pdf djvu', 247], ['bak old log', 241] ] }
Instance Method Summary collapse
-
#color_card(color) ⇒ String
Returns a preview of a single color.
-
#color_setting(array) ⇒ String
Take an array as per the DEFAULTS format and create a color definition string.
-
#colorizer(array) ⇒ String
Take an array as per the DEFAULTS format and create a terminal style escape string.
-
#preview_extensions ⇒ String
Return a colorized preview of the extension color settings.
-
#preview_system ⇒ String
Return a colorized preview of the system file color settings.
-
#test_array(array) ⇒ String
Test an array of color numbers.
-
#test_cube ⇒ String
Test the 6x6x6 color cube.
-
#test_grayscale ⇒ String
Test the 24 grayscale colors.
-
#test_system ⇒ String
Test the 16 system colors.
Instance Method Details
#color_card(color) ⇒ String
Returns a preview of a single color.
118 119 120 |
# File 'lib/lscolors.rb', line 118 def color_card(color) "#{colorizer ['foo', 0, color]} #{format '%03d', color}#{COLOR_RESET}" end |
#color_setting(array) ⇒ String
Take an array as per the DEFAULTS format and create a color definition string.
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/lscolors.rb', line 137 def color_setting(array) string = '' STYLES.each_pair do |style, code| next unless array.include?(style) || array.include?(style.to_s) string << "#{code};" end string << "#{COLOR_FG}#{array[1]}" string << ";#{COLOR_BG}#{array[2]}" if array[2].is_a? Integer string end |
#colorizer(array) ⇒ String
Take an array as per the DEFAULTS format and create a terminal style escape string.
127 128 129 130 |
# File 'lib/lscolors.rb', line 127 def colorizer(array) fail "No foreground color in #{array}." unless array[1].is_a? Integer "#{COLOR_HEAD}#{color_setting(array)}#{COLOR_TAIL}" end |
#preview_extensions ⇒ String
Return a colorized preview of the extension color settings.
165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/lscolors.rb', line 165 def preview_extensions string = '' @colors[:extensions].each do |c| c.first.split.sort.each do |ext| string << "#{colorizer(c)}*.#{ext}#{COLOR_RESET} " end string << "\n" end string end |
#preview_system ⇒ String
Return a colorized preview of the system file color settings.
153 154 155 156 157 158 159 160 |
# File 'lib/lscolors.rb', line 153 def preview_system array = @colors[:system].map do |c| code = CODE_DESCRIPTIONS.fetch(c.first.to_sym, c.first) "#{colorizer(c)}#{code}#{COLOR_RESET}" end array.join ' ' end |
#test_array(array) ⇒ String
Test an array of color numbers.
108 109 110 111 112 |
# File 'lib/lscolors.rb', line 108 def test_array(array) output = array.map { |color| color_card(color) } << "\n" output.join end |
#test_cube ⇒ String
Test the 6x6x6 color cube.
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/lscolors.rb', line 86 def test_cube output = (0..17).map do |row| column = 16 + (row * 6) + (36 * (row / 6)) cube_1 = (column..column + 5).to_a cube_2 = (column + 36..column + 41).to_a test_array(cube_1 + cube_2) end output.join end |
#test_grayscale ⇒ String
Test the 24 grayscale colors.
100 101 102 |
# File 'lib/lscolors.rb', line 100 def test_grayscale [(232..243), (244..255)].map { |range| test_array range.to_a }.join end |
#test_system ⇒ String
Test the 16 system colors.
79 80 81 |
# File 'lib/lscolors.rb', line 79 def test_system [(0..7), (8..15)].map { |range| test_array range.to_a }.join end |