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

Feature/dot file icons #615

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pkg/
*.gem
.bundle/
.yardoc/
.vscode/
Gemfile.lock
/_yardoc/
*.DS_store
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ A Ruby script that colorizes the `ls` output with color and icons. Here are the
- `--gs` (or) `--git-status`
- `--sd` (or) `--sort-dirs` or `--group-directories-first`
- `--sf` (or) `--sort-files`
- `--df` (or) `--dots-first`
- `-t`
- [Combination of flags](#combination-of-flags)
- [Installation](#installation)
Expand Down
2 changes: 2 additions & 0 deletions colorls.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ POST_INSTALL_MESSAGE = %(
Sort by files : -sf flag has been renamed to --sf
Git status : -gs flag has been renamed to --gs

Sort Dots First: --df, --dots-first - sort dot-files and dot-folders first

Clubbed flags : `colorls -ald` works
Help menu : `colorls -h` provides all possible flag options

Expand Down
66 changes: 50 additions & 16 deletions lib/colorls/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,22 +196,38 @@ def filter_contents
def sort_contents
case @sort
when :extension
@contents.sort_by! do |f|
name = f.name
ext = File.extname(name)
name = name.chomp(ext) unless ext.empty?
[ext, name].map { |s| CLocale.strxfrm(s) }
end
sort_by_extension
when :time
@contents.sort_by! { |a| -a.mtime.to_f }
when :size
@contents.sort_by! { |a| -a.size }
when :df
sort_by_dot_first
else
@contents.sort_by! { |a| CLocale.strxfrm(a.name) }
end
@contents.reverse! if @reverse
end

def sort_by_extension
@contents.sort_by! do |f|
name = f.name
ext = File.extname(name)
name = name.chomp(ext) unless ext.empty?
[ext, name].map { |s| CLocale.strxfrm(s) }
end
end

def sort_by_dot_first
@contents.sort_by! do |a|
name = a.name
# Check if the name starts with a dot
dot_prefix = name.start_with?('.') ? 0 : 1
# Return an array where dot-prefixed names are sorted first
[dot_prefix, CLocale.strxfrm(name)]
end
end

def group_contents
return unless @group

Expand Down Expand Up @@ -415,22 +431,40 @@ 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
dir_options(content)
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?
file_options(content)
end
end

def dir_options(content)
key = content.name.downcase.to_sym
key = @folder_aliases[key] unless @folders.key? key
key = :folder if key.nil?
color = @colors[:dir]
group = :folders
[key, color, group]
end

def file_options(content)
key = determine_key_for_file(content)
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]
end

def determine_key_for_file(content)
extension = File.extname(content.name).delete_prefix('.').downcase
if extension.empty?
filename = content.name.match(/\A\.?(.+)/)[1]
filename.downcase.to_sym
else
extension.to_sym
end || :default
end

def tree_contents(path)
@contents = Dir.entries(path, encoding: ColorLS.file_encoding)

Expand Down
15 changes: 8 additions & 7 deletions lib/colorls/flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,24 @@ def add_sort_options(options)
options.separator ''
options.separator 'sorting options:'
options.separator ''
configure_sort_options(options)
end

def configure_sort_options(options)
options.on('--sd', '--sort-dirs', '--group-directories-first', 'sort directories first') { @opts[:group] = :dirs }
options.on('--sf', '--sort-files', 'sort files first') { @opts[:group] = :files }
options.on('--df', '--dots-first', 'sort dot-files and dot-folders first') { @opts[:sort] = :df }
options.on('-t', 'sort by modification time, newest first') { @opts[:sort] = :time }
options.on('-U', 'do not sort; list entries in directory order') { @opts[:sort] = false }
options.on('-S', 'sort by file size, largest first') { @opts[:sort] = :size }
options.on('-X', 'sort by file extension') { @opts[:sort] = :extension }
options.on(
'--sort=WORD',
%w[none time size extension],
'sort by WORD instead of name: none, size (-S), time (-t), extension (-X)'
%w[none time size extension df],
'sort by WORD instead of name: none, size (-S), time (-t), extension (-X), df (--df)'
) do |word|
@opts[:sort] = case word
when 'none' then false
else word.to_sym
end
@opts[:sort] = (word == 'none' ? false : word.to_sym)
end

options.on('-r', '--reverse', 'reverse order while sorting') { @opts[:reverse] = true }
end

Expand Down
48 changes: 48 additions & 0 deletions lib/yaml/file_aliases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ mp3: audio
ogg: audio
opus: audio
wav: audio
h: c
editorconfig: conf
chh: cpp
cxx: cpp
hpp: cpp
scss: css
docx: doc
gdoc: doc
Expand All @@ -20,9 +24,14 @@ otf: font
ttf: font
woff: font
woff2: font
gitattributes: git
gitconfig: git
gitignore: git
gitignore_global: git
gitkeep: git
gitmodules: git
gitreflog: git
wget-hsts: history
lhs: hs
avif: image
bmp: image
Expand All @@ -41,6 +50,10 @@ webp: image
jar: java
properties: json
tsx: jsx
lesshst: less
depend: makefile
make: makefile
mk: makefile
license: md
markdown: md
mkd: md
Expand All @@ -50,13 +63,20 @@ gslides: ppt
odp: ppt
pptx: ppt
ipynb: py
pxd: py
pyc: py
pyd: py
pyi: py
pyo: py
pyx: py
rdata: r
rds: r
rhistory: r
gemfile: rb
gemspec: rb
guardfile: rb
lock: rb
mkshrc: rb
procfile: rb
rakefile: rb
rspec: rb
Expand All @@ -66,13 +86,31 @@ ru: rb
erb: rubydoc
slim: rubydoc
bash: shell
bash_aliases: shell
bash_completion: shell
bash_env: shell
bash_functions: shell
bash_history: shell
bash_logout: shell
bash_profile: shell
bashrc: shell
environment: shell
fish: shell
inputrc: shell
oh-my-zsh: shell
osh-update: shell
pre-oh-my-zsh: shell
profile: shell
sh: shell
zlogin: shell
zlogout: shell
zprofile: shell
zsh: shell
zsh-syntax-highlighting: shell
zsh-theme: shell
zsh-update: shell
zsh_history: shell
zshenv: shell
zshrc: shell
stylus: styl
cls: tex
Expand All @@ -83,6 +121,14 @@ mov: video
mp4: video
ogv: video
webm: video
exrc: vim
gvimrc: vim
vim-update: vim
vimbackup: vim
viminfo: vim
vimrc: vim
vimswap: vim
vimtags: vim
bat: windows
exe: windows
ini: windows
Expand All @@ -93,6 +139,8 @@ xlsx: xls
xul: xml
yaml: yml
7z: zip
bz: zip
bz2: zip
gz: zip
rar: zip
tar: zip
Expand Down
4 changes: 3 additions & 1 deletion lib/yaml/files.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ gform: ""
git: ""
go: ""
gruntfile.js: ""
history: ""
hs: ""
html: ""
image: ""
Expand All @@ -36,6 +37,7 @@ jsx: ""
less: ""
log: ""
lua: ""
makefile: ""
md: ""
mustache: ""
npmignore: ""
Expand All @@ -62,7 +64,7 @@ ts: ""
twig: ""
txt: ""
video: ""
vim: ""
vim: ""
vue: "﵂"
windows: ""
xls: ""
Expand Down
35 changes: 35 additions & 0 deletions lib/yaml/folders.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,46 @@
.atom: ""
.cache: ""
.config: ""
.dotnet: "󰪮"
.git: ""
.github: ""
.local: ""
.mono: "󰪮"
.pyenv: ""
.rbenv: ""
.repo: ""
.ruby-lsp: ""
.rustup: ""
.rvm: ""
.ssh: "󰣀"
.tmux: ""
.Trash: ""
.venv: ""
.vim: ""
.vscode: ""
.vscode-insiders: ""
.vscode-r: ""
.vscode-remote-containers: ""
.vscode-server: ""
.vscode-server-insiders: ""
__pycache__: ""
config: ""
coverage: ""
dist-packages: ""
eggs: ""
exe: ""
folder: ""
go: ""
hidden: ""
lib: ""
man: "󰗚"
node_modules: ""
pkg: ""
site-packages: ""
spec: "󰙨"
src: "󰳐"
test: "󰙨"
tests: "󰙨"
venv: ""
wheels: ""

Loading
Loading