Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom dir suffix colors #231

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
52 changes: 37 additions & 15 deletions lib/colorls/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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|
Expand Down
15 changes: 14 additions & 1 deletion lib/colorls/yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/yaml/dark_colors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
unrecognized_file: gold
recognized_file: lime
dir: dodgerblue
dir_suffixes:
_venv: royalblue

# Link
dead_link: red
Expand Down