Module: LsColors::Colors

Extended by:
ColorManipulation
Defined in:
lib/lscolors.rb

Overview

Load, modify, and output configuration.

Constant Summary collapse

RC_FILE =

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

Dir.home << '/.lscolors_ruby.rc'

Constants included from ColorManipulation

LsColors::ColorManipulation::CODE_DESCRIPTIONS, LsColors::ColorManipulation::COLOR_BG, LsColors::ColorManipulation::COLOR_FG, LsColors::ColorManipulation::COLOR_HEAD, LsColors::ColorManipulation::COLOR_RESET, LsColors::ColorManipulation::COLOR_TAIL, LsColors::ColorManipulation::DEFAULTS, LsColors::ColorManipulation::STYLES

Class Method Summary collapse

Methods included from ColorManipulation

color_card, color_setting, colorizer, preview_extensions, preview_system, test_array, test_cube, test_grayscale, test_system

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 LsColors::ColorManipulation::DEFAULTS will be loaded.

Parameters:

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

Returns:

  • (Hash)

    the parsed settings.



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/lscolors.rb', line 194

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)


211
212
213
# File 'lib/lscolors.rb', line 211

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)


222
223
224
225
# File 'lib/lscolors.rb', line 222

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

.testString

Test the terminal color capability.

Returns:

  • (String)


230
231
232
233
234
235
236
237
# File 'lib/lscolors.rb', line 230

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)


242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/lscolors.rb', line 242

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