module LsColors::Colors
Load, modify, and output configuration.
Constants
- RC_FILE
The default configuration file. This will be loaded at startup if present.
Public Class Methods
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.
@param [String, Hash] object @return [Hash] the parsed settings.
# 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
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).
@return [String]
# File lib/lscolors.rb, line 211 def preview "#{preview_system}\n#{preview_extensions}" end
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.
@param [String] file the file name to save to. @return [0]
# File lib/lscolors.rb, line 222 def save(file = RC_FILE) File.write(file, JSON.pretty_generate(@colors)) 0 end
Test the terminal color capability.
@return [String]
# 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
Will output the LS_COLORS
compatible configuration string.
@return [String]
# 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