diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index 6538c065..8aeb484c 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -292,13 +292,15 @@ def symlink_info(content) end def slash?(content) - content.directory? ? '/'.colorize(@colors[:dir]) : ' ' + content.directory? ? '/'.colorize(dir_color(content)) : ' ' end def fetch_string(path, content, key, color, increment) @count[increment] += 1 value = increment == :folders ? @folders[key] : @files[key] + logo = value.gsub(/\\u[\da-f]{4}/i) { |m| [m[-4..-1].to_i(16)].pack('U') } + name = content.name name = make_link(path, name) if @hyperlink @@ -320,6 +322,18 @@ def ls_line(chunk) print "\n" end + def dir_color(content) + return @colors[:dir] unless @colors[:dir_suffixes] + + name = content.name + + suffix_arr = @colors[:dir_suffixes].find do |suffix| + name.end_with? suffix[0].to_s + end + + suffix_arr ? suffix_arr[1] : @colors[:dir] + end + def file_color(file, key) color_key = case when file.chardev? then :chardev @@ -331,24 +345,32 @@ def file_color(file, key) @colors[color_key] end - def options(content) - if content.directory? - key = content.name.to_sym - key = @folder_aliases[key] unless @folders.key? key - key = :folder if key.nil? - color = @colors[:dir] - group = :folders - else - key = content.name.split('.').last.downcase.to_sym - key = @file_aliases[key] unless @files.key? key - key = :file if key.nil? - color = file_color(content, key) - group = @files.key?(key) ? :recognized_files : :unrecognized_files - end + def dir_options(content) + key = content.name.to_sym + key = @folder_aliases[key] unless @folders.key? key + key = :folder if key.nil? + group = :folders + color = dir_color(content) + + [key, color, group] + end + + def file_options(content) + key = content.name.split('.').last.downcase.to_sym + key = @file_aliases[key] unless @files.key? key + key = :file if key.nil? + color = file_color(content, key) + group = @files.key?(key) ? :recognized_files : :unrecognized_files [key, color, group] end + def options(content) + return dir_options(content) if content.directory? + + file_options(content) + end + def tree_traverse(path, prespace, depth, indent) contents = init_contents(path) contents.each do |content| diff --git a/lib/colorls/yaml.rb b/lib/colorls/yaml.rb index 81b2457b..d61b9a0b 100644 --- a/lib/colorls/yaml.rb +++ b/lib/colorls/yaml.rb @@ -7,6 +7,19 @@ def initialize(filename) @user_config_filepath = File.join(Dir.home, ".config/colorls/#{filename}") end + def deep_transform_key_vals_in_object(object, &block) + case object + when Hash + object.each_with_object({}) do |(key, value), result| + result[yield(key)] = deep_transform_key_vals_in_object(value, &block) + end + when Array + object.map { |e| deep_transform_key_vals_in_object(e, &block) } + else + yield object + end + end + def load(aliase: false) yaml = read_file(@filepath) if File.exist?(@user_config_filepath) @@ -16,7 +29,7 @@ def load(aliase: false) return yaml unless aliase - yaml.to_a.map! { |k, v| [k, v.to_sym] }.to_h + deep_transform_key_vals_in_object(yaml.to_a, &:to_sym).to_h end def read_file(filepath) diff --git a/lib/yaml/dark_colors.yaml b/lib/yaml/dark_colors.yaml index f3ac5234..2076dd52 100644 --- a/lib/yaml/dark_colors.yaml +++ b/lib/yaml/dark_colors.yaml @@ -2,6 +2,8 @@ unrecognized_file: gold recognized_file: lime dir: dodgerblue +dir_suffixes: + _venv: royalblue # Link dead_link: red