Skip to content

Commit 64a42c1

Browse files
authored
Merge pull request #417 from t-mangoe/add-option-o-g
add -o and -g options.
2 parents a3a895e + 175f2dc commit 64a42c1

File tree

5 files changed

+232
-32
lines changed

5 files changed

+232
-32
lines changed

lib/colorls/core.rb

+13-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def self.file_encoding
1111
class Core
1212
def initialize(input, all: false, report: false, sort: false, show: false,
1313
mode: nil, git_status: false, almost_all: false, colors: [], group: nil,
14-
reverse: false, hyperlink: false, tree_depth: nil)
14+
reverse: false, hyperlink: false, tree_depth: nil, show_group: true, show_user: true)
1515
@input = (+input).force_encoding(ColorLS.file_encoding)
1616
@count = {folders: 0, recognized_files: 0, unrecognized_files: 0}
1717
@all = all
@@ -23,7 +23,7 @@ def initialize(input, all: false, report: false, sort: false, show: false,
2323
@group = group
2424
@show = show
2525
@one_per_line = mode == :one_per_line
26-
@long = mode == :long
26+
init_long_format(mode,show_group,show_user)
2727
@tree = {mode: mode == :tree, depth: tree_depth}
2828
@horizontal = mode == :horizontal
2929
process_git_status_details(git_status)
@@ -75,6 +75,12 @@ def init_colors(colors)
7575
end
7676
end
7777

78+
def init_long_format(mode,show_group,show_user)
79+
@long = mode == :long
80+
@show_group = show_group
81+
@show_user = show_user
82+
end
83+
7884
# how much characters an item occupies besides its name
7985
CHARS_PER_ITEM = 12
8086

@@ -274,8 +280,11 @@ def long_info(content)
274280

275281
links = content.nlink.to_s.rjust(@linklength)
276282

277-
[mode_info(content.stats), links, user_info(content), group_info(content.group),
278-
size_info(content.size), mtime_info(content.mtime)].join(' ')
283+
line_array = [mode_info(content.stats), links]
284+
line_array.push user_info(content) if @show_user
285+
line_array.push group_info(content.group) if @show_group
286+
line_array.concat [size_info(content.size), mtime_info(content.mtime)]
287+
line_array.join(' ')
279288
end
280289

281290
def symlink_info(content)

lib/colorls/flags.rb

+44-21
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,7 @@ def initialize(*args)
1010
@args = args
1111
@light_colors = false
1212

13-
@opts = {
14-
show: false,
15-
sort: true,
16-
reverse: false,
17-
group: nil,
18-
mode: STDOUT.tty? ? :vertical : :one_per_line, # rubocop:disable Style/GlobalStdStream
19-
all: false,
20-
almost_all: false,
21-
report: false,
22-
git_status: false,
23-
colors: [],
24-
tree_depth: 3
25-
}
13+
@opts = default_opts
2614

2715
parse_options
2816

@@ -81,6 +69,24 @@ def init_locale
8169
warn "WARN: #{e}, check your locale settings"
8270
end
8371

72+
def default_opts
73+
{
74+
show: false,
75+
sort: true,
76+
reverse: false,
77+
group: nil,
78+
mode: STDOUT.tty? ? :vertical : :one_per_line, # rubocop:disable Style/GlobalStdStream
79+
all: false,
80+
almost_all: false,
81+
report: false,
82+
git_status: false,
83+
colors: [],
84+
tree_depth: 3,
85+
show_group: true,
86+
show_user: true
87+
}
88+
end
89+
8490
def add_sort_options(options)
8591
options.separator ''
8692
options.separator 'sorting options:'
@@ -126,8 +132,7 @@ def add_format_options(options)
126132
when 'single-column' then @opts[:mode] = :one_per_line
127133
end
128134
end
129-
options.on('-1', 'list one file per line') { @opts[:mode] = :one_per_line }
130-
options.on('-l', '--long', 'use a long listing format') { @opts[:mode] = :long }
135+
options.on('-1', 'list one file per line') { @opts[:mode] = :one_per_line }
131136
options.on('--tree=[DEPTH]', Integer, 'shows tree view of the directory') do |depth|
132137
@opts[:tree_depth] = depth
133138
@opts[:mode] = :tree
@@ -136,6 +141,19 @@ def add_format_options(options)
136141
options.on('-C', 'list entries by columns instead of by lines') { @opts[:mode] = :vertical }
137142
end
138143

144+
def add_long_style_options(options)
145+
options.on('-l', '--long', 'use a long listing format') { @opts[:mode] = :long }
146+
options.on('-o', 'use a long listing format without group information') do
147+
@opts[:mode] = :long
148+
@opts[:show_group] = false
149+
end
150+
options.on('-g', 'use a long listing format without owner information') do
151+
@opts[:mode] = :long
152+
@opts[:show_user] = false
153+
end
154+
options.on('-G', '--no-group', 'show no group information in a long listing') { @opts[:show_group] = false }
155+
end
156+
139157
def add_general_options(options)
140158
options.separator ''
141159
options.separator 'general options:'
@@ -196,17 +214,22 @@ def show_examples
196214
EXAMPLES
197215
end
198216

217+
def assign_each_options(opts)
218+
add_common_options(opts)
219+
add_format_options(opts)
220+
add_long_style_options(opts)
221+
add_sort_options(opts)
222+
add_compatiblity_options(opts)
223+
add_general_options(opts)
224+
add_help_option(opts)
225+
end
226+
199227
def parse_options
200228
@parser = OptionParser.new do |opts|
201229
opts.banner = 'Usage: colorls [OPTION]... [FILE]...'
202230
opts.separator ''
203231

204-
add_common_options(opts)
205-
add_format_options(opts)
206-
add_sort_options(opts)
207-
add_compatiblity_options(opts)
208-
add_general_options(opts)
209-
add_help_option(opts)
232+
assign_each_options(opts)
210233

211234
opts.on_tail('--version', 'show version') do
212235
puts ColorLS::VERSION

man/colorls.1

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.\" generated with Ronn/v0.7.3
22
.\" http://github.com/rtomayko/ronn/tree/0.7.3
33
.
4-
.TH "COLORLS" "1" "October 2020" "colorls 1.4.2" "colorls Manual"
4+
.TH "COLORLS" "1" "November 2020" "colorls 1.4.2" "colorls Manual"
55
.
66
.SH "NAME"
77
\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
5555
list one file per line
5656
.
5757
.TP
58-
\fB\-l\fR, \fB\-\-long\fR
59-
use a long listing format
60-
.
61-
.TP
6258
\fB\-\-tree\fR
6359
shows tree view of the directory
6460
.
@@ -71,6 +67,22 @@ list entries by lines instead of by columns
7167
list entries by columns instead of by lines
7268
.
7369
.TP
70+
\fB\-l\fR, \fB\-\-long\fR
71+
use a long listing format
72+
.
73+
.TP
74+
\fB\-o\fR
75+
use a long listing format without group information
76+
.
77+
.TP
78+
\fB\-g\fR
79+
use a long listing format without owner information
80+
.
81+
.TP
82+
\fB\-G\fR, \fB\-\-no\-group\fR
83+
show no group information in a long listing
84+
.
85+
.TP
7486
\fB\-\-sd\fR, \fB\-\-sort\-dirs\fR, \fB\-\-group\-directories\-first\fR
7587
sort directories first
7688
.

spec/color_ls/flags_spec.rb

+152
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,156 @@
325325
expect(subject).to eq 2
326326
end
327327
end
328+
329+
context 'with -o flag' do
330+
let(:args) { ['-o', "#{FIXTURES}/a.txt"] }
331+
332+
before do
333+
fileInfo = instance_double(
334+
'FileInfo',
335+
:group => "sys",
336+
:mtime => Time.now,
337+
:directory? => false,
338+
:owner => "user",
339+
:name => "a.txt",
340+
:show => "a.txt",
341+
:nlink => 1,
342+
:size => 128,
343+
:blockdev? => false,
344+
:chardev? => false,
345+
:socket? => false,
346+
:symlink? => false,
347+
:stats => OpenStruct.new(
348+
mode: 0o444, # read for user, owner, other
349+
setuid?: true,
350+
setgid?: true,
351+
sticky?: true
352+
),
353+
:executable? => false
354+
)
355+
356+
allow(ColorLS::FileInfo).to receive(:new).with("#{FIXTURES}/a.txt", link_info: true) { fileInfo }
357+
end
358+
359+
it 'lists without group info' do
360+
expect { subject }.not_to output(/sys/).to_stdout
361+
end
362+
it 'lists with user info' do
363+
expect { subject }.to output(/user/).to_stdout
364+
end
365+
end
366+
367+
context 'with -g flag' do
368+
let(:args) { ['-g', "#{FIXTURES}/a.txt"] }
369+
370+
before do
371+
fileInfo = instance_double(
372+
'FileInfo',
373+
:group => "sys",
374+
:mtime => Time.now,
375+
:directory? => false,
376+
:owner => "user",
377+
:name => "a.txt",
378+
:show => "a.txt",
379+
:nlink => 1,
380+
:size => 128,
381+
:blockdev? => false,
382+
:chardev? => false,
383+
:socket? => false,
384+
:symlink? => false,
385+
:stats => OpenStruct.new(
386+
mode: 0o444, # read for user, owner, other
387+
setuid?: true,
388+
setgid?: true,
389+
sticky?: true
390+
),
391+
:executable? => false
392+
)
393+
394+
allow(ColorLS::FileInfo).to receive(:new).with("#{FIXTURES}/a.txt", link_info: true) { fileInfo }
395+
end
396+
397+
it 'lists with group info' do
398+
expect { subject }.to output(/sys/).to_stdout
399+
end
400+
it 'lists without user info' do
401+
expect { subject }.not_to output(/user/).to_stdout
402+
end
403+
end
404+
405+
context 'with -o and -g flag' do
406+
let(:args) { ['-og', "#{FIXTURES}/a.txt"] }
407+
408+
before do
409+
fileInfo = instance_double(
410+
'FileInfo',
411+
:group => "sys",
412+
:mtime => Time.now,
413+
:directory? => false,
414+
:owner => "user",
415+
:name => "a.txt",
416+
:show => "a.txt",
417+
:nlink => 1,
418+
:size => 128,
419+
:blockdev? => false,
420+
:chardev? => false,
421+
:socket? => false,
422+
:symlink? => false,
423+
:stats => OpenStruct.new(
424+
mode: 0o444, # read for user, owner, other
425+
setuid?: true,
426+
setgid?: true,
427+
sticky?: true
428+
),
429+
:executable? => false
430+
)
431+
432+
allow(ColorLS::FileInfo).to receive(:new).with("#{FIXTURES}/a.txt", link_info: true) { fileInfo }
433+
end
434+
435+
it 'lists without group info' do
436+
expect { subject }.not_to output(/sys/).to_stdout
437+
end
438+
it 'lists without user info' do
439+
expect { subject }.not_to output(/user/).to_stdout
440+
end
441+
end
442+
443+
context 'with -G flag in a listing format' do
444+
let(:args) { ['-l', '-G', "#{FIXTURES}/a.txt"] }
445+
446+
before do
447+
fileInfo = instance_double(
448+
'FileInfo',
449+
:group => "sys",
450+
:mtime => Time.now,
451+
:directory? => false,
452+
:owner => "user",
453+
:name => "a.txt",
454+
:show => "a.txt",
455+
:nlink => 1,
456+
:size => 128,
457+
:blockdev? => false,
458+
:chardev? => false,
459+
:socket? => false,
460+
:symlink? => false,
461+
:stats => OpenStruct.new(
462+
mode: 0o444, # read for user, owner, other
463+
setuid?: true,
464+
setgid?: true,
465+
sticky?: true
466+
),
467+
:executable? => false
468+
)
469+
470+
allow(ColorLS::FileInfo).to receive(:new).with("#{FIXTURES}/a.txt", link_info: true) { fileInfo }
471+
end
472+
473+
it 'lists without group info' do
474+
expect { subject }.not_to output(/sys/).to_stdout
475+
end
476+
it 'lists with user info' do
477+
expect { subject }.to output(/user/).to_stdout
478+
end
479+
end
328480
end

zsh/_colorls

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ _arguments -s -S \
1717
"--report[show brief report]" \
1818
"--format[use format: across (-x), horizontal (-x), long (-l), single-column (-1), vertical (-C)]" \
1919
"-1[list one file per line]" \
20-
"-l[use a long listing format]" \
21-
"--long[use a long listing format]" \
2220
"--tree[shows tree view of the directory]" \
2321
"-x[list entries by lines instead of by columns]" \
2422
"-C[list entries by columns instead of by lines]" \
23+
"-l[use a long listing format]" \
24+
"--long[use a long listing format]" \
25+
"-o[use a long listing format without group information]" \
26+
"-g[use a long listing format without owner information]" \
27+
"-G[show no group information in a long listing]" \
28+
"--no-group[show no group information in a long listing]" \
2529
"--sd[sort directories first]" \
2630
"--sort-dirs[sort directories first]" \
2731
"--group-directories-first[sort directories first]" \

0 commit comments

Comments
 (0)