Merge pull request #417 from t-mangoe/add-option-o-g

add -o and -g options.
This commit is contained in:
Claudio Bley 2020-12-07 22:54:17 +01:00 committed by GitHub
commit 64a42c105d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 232 additions and 32 deletions

View file

@ -11,7 +11,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, tree_depth: nil)
reverse: false, hyperlink: false, tree_depth: nil, show_group: true, show_user: true)
@input = (+input).force_encoding(ColorLS.file_encoding)
@count = {folders: 0, recognized_files: 0, unrecognized_files: 0}
@all = all
@ -23,7 +23,7 @@ module ColorLS
@group = group
@show = show
@one_per_line = mode == :one_per_line
@long = mode == :long
init_long_format(mode,show_group,show_user)
@tree = {mode: mode == :tree, depth: tree_depth}
@horizontal = mode == :horizontal
process_git_status_details(git_status)
@ -75,6 +75,12 @@ module ColorLS
end
end
def init_long_format(mode,show_group,show_user)
@long = mode == :long
@show_group = show_group
@show_user = show_user
end
# how much characters an item occupies besides its name
CHARS_PER_ITEM = 12
@ -274,8 +280,11 @@ module ColorLS
links = content.nlink.to_s.rjust(@linklength)
[mode_info(content.stats), links, user_info(content), group_info(content.group),
size_info(content.size), mtime_info(content.mtime)].join(' ')
line_array = [mode_info(content.stats), links]
line_array.push user_info(content) if @show_user
line_array.push group_info(content.group) if @show_group
line_array.concat [size_info(content.size), mtime_info(content.mtime)]
line_array.join(' ')
end
def symlink_info(content)

View file

@ -10,19 +10,7 @@ module ColorLS
@args = args
@light_colors = false
@opts = {
show: false,
sort: true,
reverse: false,
group: nil,
mode: STDOUT.tty? ? :vertical : :one_per_line, # rubocop:disable Style/GlobalStdStream
all: false,
almost_all: false,
report: false,
git_status: false,
colors: [],
tree_depth: 3
}
@opts = default_opts
parse_options
@ -81,6 +69,24 @@ module ColorLS
warn "WARN: #{e}, check your locale settings"
end
def default_opts
{
show: false,
sort: true,
reverse: false,
group: nil,
mode: STDOUT.tty? ? :vertical : :one_per_line, # rubocop:disable Style/GlobalStdStream
all: false,
almost_all: false,
report: false,
git_status: false,
colors: [],
tree_depth: 3,
show_group: true,
show_user: true
}
end
def add_sort_options(options)
options.separator ''
options.separator 'sorting options:'
@ -126,8 +132,7 @@ module ColorLS
when 'single-column' then @opts[:mode] = :one_per_line
end
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('-1', 'list one file per line') { @opts[:mode] = :one_per_line }
options.on('--tree=[DEPTH]', Integer, 'shows tree view of the directory') do |depth|
@opts[:tree_depth] = depth
@opts[:mode] = :tree
@ -136,6 +141,19 @@ module ColorLS
options.on('-C', 'list entries by columns instead of by lines') { @opts[:mode] = :vertical }
end
def add_long_style_options(options)
options.on('-l', '--long', 'use a long listing format') { @opts[:mode] = :long }
options.on('-o', 'use a long listing format without group information') do
@opts[:mode] = :long
@opts[:show_group] = false
end
options.on('-g', 'use a long listing format without owner information') do
@opts[:mode] = :long
@opts[:show_user] = false
end
options.on('-G', '--no-group', 'show no group information in a long listing') { @opts[:show_group] = false }
end
def add_general_options(options)
options.separator ''
options.separator 'general options:'
@ -196,17 +214,22 @@ module ColorLS
EXAMPLES
end
def assign_each_options(opts)
add_common_options(opts)
add_format_options(opts)
add_long_style_options(opts)
add_sort_options(opts)
add_compatiblity_options(opts)
add_general_options(opts)
add_help_option(opts)
end
def parse_options
@parser = OptionParser.new do |opts|
opts.banner = 'Usage: colorls [OPTION]... [FILE]...'
opts.separator ''
add_common_options(opts)
add_format_options(opts)
add_sort_options(opts)
add_compatiblity_options(opts)
add_general_options(opts)
add_help_option(opts)
assign_each_options(opts)
opts.on_tail('--version', 'show version') do
puts ColorLS::VERSION

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 2020" "colorls 1.4.2" "colorls Manual"
.TH "COLORLS" "1" "November 2020" "colorls 1.4.2" "colorls Manual"
.
.SH "NAME"
\fBcolorls\fR \- list directory contents with colors and icons
@ -55,10 +55,6 @@ use format: across (\-x), horizontal (\-x), long (\-l), single\-column (\-1), ve
list one file per line
.
.TP
\fB\-l\fR, \fB\-\-long\fR
use a long listing format
.
.TP
\fB\-\-tree\fR
shows tree view of the directory
.
@ -71,6 +67,22 @@ list entries by lines instead of by columns
list entries by columns instead of by lines
.
.TP
\fB\-l\fR, \fB\-\-long\fR
use a long listing format
.
.TP
\fB\-o\fR
use a long listing format without group information
.
.TP
\fB\-g\fR
use a long listing format without owner information
.
.TP
\fB\-G\fR, \fB\-\-no\-group\fR
show no group information in a long listing
.
.TP
\fB\-\-sd\fR, \fB\-\-sort\-dirs\fR, \fB\-\-group\-directories\-first\fR
sort directories first
.

View file

@ -325,4 +325,156 @@ RSpec.describe ColorLS::Flags do
expect(subject).to eq 2
end
end
context 'with -o flag' do
let(:args) { ['-o', "#{FIXTURES}/a.txt"] }
before do
fileInfo = instance_double(
'FileInfo',
:group => "sys",
:mtime => Time.now,
:directory? => false,
:owner => "user",
:name => "a.txt",
:show => "a.txt",
:nlink => 1,
:size => 128,
:blockdev? => false,
:chardev? => false,
:socket? => false,
:symlink? => false,
:stats => OpenStruct.new(
mode: 0o444, # read for user, owner, other
setuid?: true,
setgid?: true,
sticky?: true
),
:executable? => false
)
allow(ColorLS::FileInfo).to receive(:new).with("#{FIXTURES}/a.txt", link_info: true) { fileInfo }
end
it 'lists without group info' do
expect { subject }.not_to output(/sys/).to_stdout
end
it 'lists with user info' do
expect { subject }.to output(/user/).to_stdout
end
end
context 'with -g flag' do
let(:args) { ['-g', "#{FIXTURES}/a.txt"] }
before do
fileInfo = instance_double(
'FileInfo',
:group => "sys",
:mtime => Time.now,
:directory? => false,
:owner => "user",
:name => "a.txt",
:show => "a.txt",
:nlink => 1,
:size => 128,
:blockdev? => false,
:chardev? => false,
:socket? => false,
:symlink? => false,
:stats => OpenStruct.new(
mode: 0o444, # read for user, owner, other
setuid?: true,
setgid?: true,
sticky?: true
),
:executable? => false
)
allow(ColorLS::FileInfo).to receive(:new).with("#{FIXTURES}/a.txt", link_info: true) { fileInfo }
end
it 'lists with group info' do
expect { subject }.to output(/sys/).to_stdout
end
it 'lists without user info' do
expect { subject }.not_to output(/user/).to_stdout
end
end
context 'with -o and -g flag' do
let(:args) { ['-og', "#{FIXTURES}/a.txt"] }
before do
fileInfo = instance_double(
'FileInfo',
:group => "sys",
:mtime => Time.now,
:directory? => false,
:owner => "user",
:name => "a.txt",
:show => "a.txt",
:nlink => 1,
:size => 128,
:blockdev? => false,
:chardev? => false,
:socket? => false,
:symlink? => false,
:stats => OpenStruct.new(
mode: 0o444, # read for user, owner, other
setuid?: true,
setgid?: true,
sticky?: true
),
:executable? => false
)
allow(ColorLS::FileInfo).to receive(:new).with("#{FIXTURES}/a.txt", link_info: true) { fileInfo }
end
it 'lists without group info' do
expect { subject }.not_to output(/sys/).to_stdout
end
it 'lists without user info' do
expect { subject }.not_to output(/user/).to_stdout
end
end
context 'with -G flag in a listing format' do
let(:args) { ['-l', '-G', "#{FIXTURES}/a.txt"] }
before do
fileInfo = instance_double(
'FileInfo',
:group => "sys",
:mtime => Time.now,
:directory? => false,
:owner => "user",
:name => "a.txt",
:show => "a.txt",
:nlink => 1,
:size => 128,
:blockdev? => false,
:chardev? => false,
:socket? => false,
:symlink? => false,
:stats => OpenStruct.new(
mode: 0o444, # read for user, owner, other
setuid?: true,
setgid?: true,
sticky?: true
),
:executable? => false
)
allow(ColorLS::FileInfo).to receive(:new).with("#{FIXTURES}/a.txt", link_info: true) { fileInfo }
end
it 'lists without group info' do
expect { subject }.not_to output(/sys/).to_stdout
end
it 'lists with user info' do
expect { subject }.to output(/user/).to_stdout
end
end
end

View file

@ -17,11 +17,15 @@ _arguments -s -S \
"--report[show brief report]" \
"--format[use format: across (-x), horizontal (-x), long (-l), single-column (-1), vertical (-C)]" \
"-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]" \
"-C[list entries by columns instead of by lines]" \
"-l[use a long listing format]" \
"--long[use a long listing format]" \
"-o[use a long listing format without group information]" \
"-g[use a long listing format without owner information]" \
"-G[show no group information in a long listing]" \
"--no-group[show no group information in a long listing]" \
"--sd[sort directories first]" \
"--sort-dirs[sort directories first]" \
"--group-directories-first[sort directories first]" \