Skip to content

Commit

Permalink
Merge pull request #595 from ayushpoddar/color-hidden-files
Browse files Browse the repository at this point in the history
Colorise hidden files and directories
  • Loading branch information
avdv authored Jul 12, 2024
2 parents f729bea + 305e248 commit 597a8c6
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
35 changes: 24 additions & 11 deletions lib/colorls/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ def file_color(file, key)
when file.blockdev? then :blockdev
when file.socket? then :socket
when file.executable? then :executable_file
when file.hidden? then :hidden
when @files.key?(key) then :recognized_file
else :unrecognized_file
end
Expand All @@ -415,20 +416,32 @@ def file_color(file, key)

def options(content)
if content.directory?
key = content.name.downcase.to_sym
key = @folder_aliases[key] unless @folders.key? key
key = :folder if key.nil?
color = @colors[:dir]
group = :folders
options_directory(content).values_at(:key, :color, :group)
else
key = File.extname(content.name).delete_prefix('.').downcase.to_sym
key = @file_aliases[key] unless @files.key? key
color = file_color(content, key)
group = @files.key?(key) ? :recognized_files : :unrecognized_files
key = :file if key.nil?
options_file(content).values_at(:key, :color, :group)
end
end

def options_directory(content)
key = content.name.downcase.to_sym
key = @folder_aliases[key] unless @folders.key?(key)
key = :folder if key.nil?

color = content.hidden? ? @colors[:hidden_dir] : @colors[:dir]

{key: key, color: color, group: :folders}
end

def options_file(content)
key = File.extname(content.name).delete_prefix('.').downcase.to_sym
key = @file_aliases[key] unless @files.key?(key)

color = file_color(content, key)
group = @files.key?(key) ? :recognized_files : :unrecognized_files

key = :file if key.nil?

[key, color, group]
{key: key, color: color, group: group}
end

def tree_contents(path)
Expand Down
4 changes: 4 additions & 0 deletions lib/colorls/fileinfo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def dead?
@dead
end

def hidden?
@name.start_with?('.')
end

def owner
return @@users[@stats.uid] if @@users.key? @stats.uid

Expand Down
8 changes: 5 additions & 3 deletions lib/yaml/dark_colors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ dead_link: red
link: cyan

# special files
socket: green
blockdev: green
chardev: green
socket: green
blockdev: green
chardev: green
hidden: burlywood
hidden_dir: slategray

# Access Modes
write: darkkhaki
Expand Down
8 changes: 5 additions & 3 deletions lib/yaml/light_colors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ dead_link: red
link: cyan

# special files
socket: darkgray
blockdev: darkgray
chardev: darkgray
socket: darkgray
blockdev: darkgray
chardev: darkgray
hidden: seagreen
hidden_dir: royalblue

# Access Modes
write: red
Expand Down
3 changes: 3 additions & 0 deletions spec/color_ls/core_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
chardev?: false,
socket?: false,
symlink?: false,
hidden?: false,
stats: instance_double(File::Stat,
mode: 0o444, # read for user, owner, other
setuid?: false,
Expand All @@ -47,6 +48,7 @@
chardev?: false,
socket?: false,
symlink?: false,
hidden?: false,
stats: instance_double(File::Stat,
mode: 0o444, # read for user, owner, other
setuid?: false,
Expand Down Expand Up @@ -77,6 +79,7 @@
chardev?: false,
socket?: false,
symlink?: false,
hidden?: true,
executable?: false
)

Expand Down
2 changes: 2 additions & 0 deletions spec/color_ls/flags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
chardev?: false,
socket?: false,
symlink?: false,
hidden?: false,
stats: instance_double(File::Stat,
mode: 0o444, # read for user, owner, other
setuid?: true,
Expand Down Expand Up @@ -467,6 +468,7 @@
chardev?: false,
socket?: false,
symlink?: true,
hidden?: false,
link_target: "#{FIXTURES}/z.txt",
dead?: false,
executable?: false
Expand Down

0 comments on commit 597a8c6

Please sign in to comment.