Module: LsColors

Defined in:
lib/lscolors.rb

Overview

Constant Summary collapse

VERSION =

Current software version.

'1.0.1'
DATE =

Date of the current version.

'2014-10-30'
ABOUT =

A short description.

'Manipulate LS_COLORS settings.'
RC_FILE =

The default configuration file. This will be loaded at startup if present.

Dir.home << '/.lscolors_ruby.rc'
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]
  ]
}

Class Method Summary collapse

Class Method Details

.load(object = nil) ⇒ Hash

Loads or initializes settings. The parameter can be either a Hash or a String (in the latter case it will be interpreted as the file name of a JSON file). If missing, the method will look for a JSON file specified by RC_FILE, and if absent, the DEFAULTS will be loaded.

Parameters:

  • object (String, Hash) (defaults to: nil)

Returns:

  • (Hash)

    the parsed settings.



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/lscolors.rb', line 85

def load(object = nil)
  return @colors = object if object.is_a? Hash

  if object.is_a? String
    return @colors = JSON.parse(File.read(object), symbolize_names: true)
  elsif File.exist? RC_FILE
    return @colors = JSON.parse(File.read(RC_FILE), symbolize_names: true)
  end

  @colors = DEFAULTS
end

.previewString

Will preview the current settings as a string of colorized system files and extensions. Use print or puts to see the effect (a 256-color terminal is required).

Returns:

  • (String)


102
103
104
# File 'lib/lscolors.rb', line 102

def preview
  "#{preview_system}\n#{preview_extensions}"
end

.save(file = RC_FILE) ⇒ 0

Save the current settings to a file. Without parameters, RC_FILE will be created and/or saved. RC_FILE will be loaded automatically from now on, so it can be modified directly in order to configure the color settings.

Parameters:

  • file (String) (defaults to: RC_FILE)

    the file name to save to.

Returns:

  • (0)


112
113
114
115
# File 'lib/lscolors.rb', line 112

def save(file = RC_FILE)
  File.write(file, JSON.pretty_generate(@colors))
  0
end

.testString

Test the terminal color capability.

Returns:

  • (String)


120
121
122
123
124
125
126
127
# File 'lib/lscolors.rb', line 120

def test
  colors = `tput colors`.to_i
  fail "256 colors not supported! (#{colors})" if colors < 256

  "System colors (0-15):\n#{test_system}\n" \
    "Color cube (16-231):\n#{test_cube}\n" \
    "Grayscale colors (232-255):\n#{test_grayscale}\n"
end

.to_sString Also known as: inspect

Will output the LS_COLORS compatible configuration string.

Returns:

  • (String)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/lscolors.rb', line 132

def to_s
  output = 'rs=0:'

  @colors[:system].each do |c|
    output << "#{c.first}=#{color_setting(c)}:"
  end

  @colors[:extensions].each do |c|
    c.first.split.sort.each do |ext|
      output << "*.#{ext}=#{color_setting(c)}:"
    end
  end

  output
end