Add the option to handle depth for the tree flag (#253)

* Add the option to handle depth for the tree flag

* Update README to describe the new option for the tree flag

* Add tests
This commit is contained in:
Alex Cano 2019-01-11 07:36:02 +01:00 committed by Claudio Bley
parent 3897c6cba2
commit c60a2ebec8
8 changed files with 44 additions and 16 deletions

View file

@ -55,4 +55,6 @@ script:
- colorls --color=auto
- colorls --color=never
- colorls --color=always
- colorls --tree
- colorls --tree=1

View file

@ -26,10 +26,11 @@ A Ruby script that colorizes the `ls` output with color and icons. Here are the
- `-h` (or) `--help`
- `-l` (or) `--long`
- `-r` (or) `--report`
- `-t` (or) `--tree`
- `--tree` (or) `--tree=[DEPTH]`
- `--gs` (or) `--git-status`
- `--sd` (or) `--sort-dirs` or `--group-directories-first`
- `--sf` (or) `--sort-files`
- `-t`
- [Combination of flags](#combination-of-flags)
- [Installation](#installation)
- [Recommended configurations](#recommended-configurations)
@ -79,7 +80,7 @@ Man pages have been added. Checkout `man colorls`.
![image](https://user-images.githubusercontent.com/17109060/32149082-96a83fec-bd25-11e7-9081-7f77e4c90e90.png)
- With `-t` (or) `--tree` : Shows tree view of the directory
- With `--tree` (or) `--tree=[DEPTH]` : Shows tree view of the directory with the specified depth (default 3)
![image](https://user-images.githubusercontent.com/17109060/32149051-32e596e4-bd25-11e7-93a9-5e50c8d2bb19.png)
@ -95,6 +96,8 @@ Man pages have been added. Checkout `man colorls`.
![image](https://user-images.githubusercontent.com/17109060/32149071-6b379de4-bd25-11e7-8764-a0c577e526a1.png)
- With `-t` : Sort by modification time, newest first (NEED TO ADD IMAGE)
- With color options : `--light` or `--dark` can be passed as a flag, to choose the appropriate color scheme. By default, the dark color scheme is chosen. In order to tweak any color, read [Custom configurations](#custom-configurations).
### Combination of flags

View file

@ -4,7 +4,7 @@ module ColorLS
class Core
def initialize(input, all: false, report: false, sort: false, show: false,
mode: nil, git_status: false, almost_all: false, colors: [], group: nil,
reverse: false, hyperlink: false)
reverse: false, hyperlink: false, tree_depth: nil)
@input = File.absolute_path(input)
@count = {folders: 0, recognized_files: 0, unrecognized_files: 0}
@all = all
@ -17,7 +17,7 @@ module ColorLS
@show = show
@one_per_line = mode == :one_per_line
@long = mode == :long
@tree = mode == :tree
@tree = {mode: mode == :tree, depth: tree_depth}
process_git_status_details(git_status)
@screen_width = IO.console.winsize[1]
@ -33,9 +33,9 @@ module ColorLS
def ls
return print "\n Nothing to show here\n".colorize(@colors[:empty]) if @contents.empty?
if @tree
if @tree[:mode]
print "\n"
tree_traverse(@input, 0, 2)
tree_traverse(@input, 0, 1, 2)
else
@contents = chunkify
@contents.each { |chunk| ls_line(chunk) }
@ -349,7 +349,7 @@ module ColorLS
[key, color, group]
end
def tree_traverse(path, prespace, indent)
def tree_traverse(path, prespace, depth, indent)
contents = init_contents(path)
contents.each do |content|
icon = content == contents.last || content.directory? ? ' └──' : ' ├──'
@ -357,10 +357,14 @@ module ColorLS
print " #{fetch_string(path, content, *options(content))} \n"
next unless content.directory?
tree_traverse("#{path}/#{content}", prespace + indent, indent)
tree_traverse("#{path}/#{content}", prespace + indent, depth + 1, indent) if keep_going(depth)
end
end
def keep_going(depth)
@tree[:depth].nil? || depth < @tree[:depth]
end
def tree_branch_preprint(prespace, indent, prespace_icon)
return prespace_icon if prespace.zero?

View file

@ -20,7 +20,8 @@ module ColorLS
almost_all: false,
report: false,
git_status: false,
colors: []
colors: [],
tree_depth: 3
}
parse_options
@ -116,8 +117,11 @@ module ColorLS
end
options.on('-1', 'list one file per line') { @opts[:mode] = :one_per_line }
options.on('-l', '--long', 'use a long listing format') { @opts[:mode] = :long }
options.on('--tree', 'shows tree view of the directory') { @opts[:mode] = :tree }
options.on('-x', 'list entries by lines instead of by columns') { @opts[:mode] = true }
options.on('--tree=[DEPTH]', Integer, 'shows tree view of the directory') do |depth|
@opts[:tree_depth] = depth
@opts[:mode] = :tree
end
end
def add_general_options(options)

View file

@ -1,7 +1,7 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "COLORLS" "1" "October 2018" "colorls 1.1.1" "colorls Manual"
.TH "COLORLS" "1" "January 2019" "colorls 1.1.1" "colorls Manual"
.
.SH "NAME"
\fBcolorls\fR \- list directory contents with colors and icons
@ -59,14 +59,14 @@ list one file per line
use a long listing format
.
.TP
\fB\-\-tree\fR
shows tree view of the directory
.
.TP
\fB\-x\fR
list entries by lines instead of by columns
.
.TP
\fB\-\-tree\fR
shows tree view of the directory
.
.TP
\fB\-\-sd\fR, \fB\-\-sort\-dirs\fR, \fB\-\-group\-directories\-first\fR
sort directories first
.

View file

@ -184,6 +184,21 @@ RSpec.describe ColorLS::Flags do
let(:args) { ['--tree', FIXTURES] }
it { is_expected.to match(/├──/) } # displays file hierarchy
it { is_expected.to match(/third-level-file.txt/) }
end
context 'with --tree=1 flag' do
let(:args) { ['--tree=1', FIXTURES] }
it { is_expected.to match(/├──/) } # displays file hierarchy
it { is_expected.not_to match(/ReadmeLink.md|Supportlink|doesnotexisttest.txt|third-level-file.txt/) }
end
context 'with --tree=3 flag' do
let(:args) { ['--tree=3', FIXTURES] }
it { is_expected.to match(/├──/) } # displays file hierarchy
it { is_expected.to match(/third-level-file.txt/) }
end
context 'with --hyperlink flag' do

View file

@ -19,8 +19,8 @@ _arguments -s -S \
"-1[list one file per line]" \
"-l[use a long listing format]" \
"--long[use a long listing format]" \
"--tree[shows tree view of the directory]" \
"-x[list entries by lines instead of by columns]" \
"--tree[shows tree view of the directory]" \
"--sd[sort directories first]" \
"--sort-dirs[sort directories first]" \
"--group-directories-first[sort directories first]" \