colorls 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +8 -3
- data/lib/colorls/core.rb +79 -30
- data/lib/colorls/flags.rb +20 -7
- data/lib/colorls/version.rb +1 -1
- data/lib/yaml/files.yaml +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4985c3a5528c2e34ab5e8ccac1efbbd139e5d5a1
|
4
|
+
data.tar.gz: b54dc73e35b4d524b648c1348222dce052175b09
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ee0668a87a81f540e2e828a3f01e43f05180d16a9447b92d6eb65c14e9f0cd7afe1c23b6d1ac3a918748b2f8a9e5e11f5a51e7e9777ce9e80a572b315e01234
|
7
|
+
data.tar.gz: 5859c0c78a18285d9cbea49b4197d1173cfb1dbf3cfc452a4455d53d5b04ba311e057dc2776120f2a715c888d59be2b13b750280d07f7bb0d975bdeb236b6874
|
data/.travis.yml
CHANGED
@@ -12,16 +12,21 @@ script:
|
|
12
12
|
- bundle exec rspec
|
13
13
|
- rake install
|
14
14
|
- colorls
|
15
|
+
- colorls -1
|
15
16
|
- colorls -a
|
17
|
+
- colorls -A
|
16
18
|
- colorls -d
|
17
19
|
- colorls -f
|
20
|
+
- colorls -l
|
21
|
+
- colorls -l spec/fixtures/symlinks
|
22
|
+
- colorls -r
|
18
23
|
- colorls -sd
|
19
24
|
- colorls -sf
|
20
|
-
- colorls -
|
21
|
-
- colorls
|
22
|
-
- colorls -l
|
25
|
+
- colorls -t
|
26
|
+
- colorls spec/fixtures/symlinks
|
23
27
|
- colorls README.md
|
24
28
|
- colorls *
|
29
|
+
- colorls | grep /
|
25
30
|
|
26
31
|
install:
|
27
32
|
- gem install bundler
|
data/lib/colorls/core.rb
CHANGED
@@ -1,58 +1,70 @@
|
|
1
1
|
module ColorLS
|
2
2
|
class Core
|
3
3
|
def initialize(input=nil, all: false, report: false, sort: false, show: false,
|
4
|
-
one_per_line: false, long: false)
|
4
|
+
one_per_line: false, long: false, almost_all: false, tree: false)
|
5
5
|
@input = input || Dir.pwd
|
6
6
|
@count = {folders: 0, recognized_files: 0, unrecognized_files: 0}
|
7
7
|
@all = all
|
8
|
+
@almost_all = almost_all
|
8
9
|
@report = report
|
9
10
|
@sort = sort
|
10
11
|
@show = show
|
11
12
|
@one_per_line = one_per_line
|
12
13
|
@long = long
|
14
|
+
@tree = tree
|
13
15
|
@screen_width = ::TermInfo.screen_size.last
|
14
16
|
|
15
|
-
init_contents
|
17
|
+
@contents = init_contents(@input)
|
16
18
|
@max_widths = @contents.map(&:length)
|
17
19
|
init_icons
|
18
20
|
end
|
19
21
|
|
20
22
|
def ls
|
21
|
-
if @contents.empty?
|
22
|
-
|
23
|
+
return print "\n Nothing to show here\n".colorize(:yellow) if @contents.empty?
|
24
|
+
|
25
|
+
if @tree
|
26
|
+
print "\n"
|
27
|
+
tree_traverse(@input, 0, 2)
|
23
28
|
else
|
24
29
|
@contents = chunkify
|
25
30
|
@max_widths = @contents.transpose.map { |c| c.map(&:length).max }
|
26
31
|
@contents.each { |chunk| ls_line(chunk) }
|
27
|
-
print "\n"
|
28
|
-
display_report if @report
|
29
32
|
end
|
33
|
+
print "\n"
|
34
|
+
display_report if @report
|
30
35
|
|
31
36
|
true
|
32
37
|
end
|
33
38
|
|
34
39
|
private
|
35
40
|
|
36
|
-
def init_contents
|
37
|
-
@contents = if Dir.exist?(
|
38
|
-
Dir.entries(
|
41
|
+
def init_contents(path)
|
42
|
+
@contents = if Dir.exist?(path)
|
43
|
+
Dir.entries(path)
|
39
44
|
else
|
40
|
-
[
|
45
|
+
[path]
|
41
46
|
end
|
42
47
|
|
43
|
-
|
44
|
-
filter_contents if @show
|
45
|
-
sort_contents if @sort
|
48
|
+
filter_hidden_contents
|
49
|
+
filter_contents(path) if @show
|
50
|
+
sort_contents(path) if @sort
|
46
51
|
|
47
52
|
@total_content_length = @contents.length
|
48
53
|
|
49
|
-
return unless @long
|
54
|
+
return @contents unless @long
|
50
55
|
init_user_lengths
|
51
56
|
init_group_lengths
|
57
|
+
@contents
|
58
|
+
end
|
59
|
+
|
60
|
+
def filter_hidden_contents
|
61
|
+
@contents -= %w[. ..] unless @all
|
62
|
+
@contents.keep_if { |x| !x.start_with? '.' } unless @all || @almost_all
|
52
63
|
end
|
53
64
|
|
54
65
|
def init_user_lengths
|
55
66
|
@userlength = @contents.map do |c|
|
67
|
+
next 0 unless File.exist?("#{@input}/#{c}")
|
56
68
|
begin
|
57
69
|
user = Etc.getpwuid(File.stat("#{@input}/#{c}").uid).name
|
58
70
|
rescue ArgumentError
|
@@ -64,6 +76,7 @@ module ColorLS
|
|
64
76
|
|
65
77
|
def init_group_lengths
|
66
78
|
@grouplength = @contents.map do |c|
|
79
|
+
next 0 unless File.exist?("#{@input}/#{c}")
|
67
80
|
begin
|
68
81
|
group = Etc.getgrgid(File.stat("#{@input}/#{c}").gid).name
|
69
82
|
rescue ArgumentError
|
@@ -73,20 +86,20 @@ module ColorLS
|
|
73
86
|
end.max
|
74
87
|
end
|
75
88
|
|
76
|
-
def filter_contents
|
89
|
+
def filter_contents(path)
|
77
90
|
@contents.keep_if do |x|
|
78
|
-
next Dir.exist?("#{
|
79
|
-
!Dir.exist?("#{
|
91
|
+
next Dir.exist?("#{path}/#{x}") if @show == :dirs
|
92
|
+
!Dir.exist?("#{path}/#{x}")
|
80
93
|
end
|
81
94
|
end
|
82
95
|
|
83
|
-
def sort_contents
|
84
|
-
@contents.sort! { |a, b| cmp_by_dirs(a, b) }
|
96
|
+
def sort_contents(path)
|
97
|
+
@contents.sort! { |a, b| cmp_by_dirs(path, a, b) }
|
85
98
|
end
|
86
99
|
|
87
|
-
def cmp_by_dirs(a, b)
|
88
|
-
is_a_dir = Dir.exist?("#{
|
89
|
-
is_b_dir = Dir.exist?("#{
|
100
|
+
def cmp_by_dirs(path, a, b)
|
101
|
+
is_a_dir = Dir.exist?("#{path}/#{a}")
|
102
|
+
is_b_dir = Dir.exist?("#{path}/#{b}")
|
90
103
|
|
91
104
|
return cmp_by_alpha(a, b) unless is_a_dir ^ is_b_dir
|
92
105
|
|
@@ -135,7 +148,7 @@ module ColorLS
|
|
135
148
|
end
|
136
149
|
|
137
150
|
def in_line(chunk_size)
|
138
|
-
return false if @max_widths.sum +
|
151
|
+
return false if @max_widths.sum + 7 * chunk_size > @screen_width
|
139
152
|
true
|
140
153
|
end
|
141
154
|
|
@@ -199,8 +212,25 @@ module ColorLS
|
|
199
212
|
end
|
200
213
|
|
201
214
|
def long_info(content)
|
215
|
+
return '' unless @long
|
216
|
+
unless File.exist?("#{@input}/#{content}")
|
217
|
+
return '[No Info]'.colorize(:red) + ' ' * (39 + @userlength + @grouplength)
|
218
|
+
end
|
202
219
|
stat = File.stat("#{@input}/#{content}")
|
203
|
-
|
220
|
+
[mode_info(stat), user_info(stat), group_info(stat), size_info(stat), mtime_info(stat)].join(' ')
|
221
|
+
end
|
222
|
+
|
223
|
+
def symlink_info(content)
|
224
|
+
return '' unless @long && File.lstat("#{@input}/#{content}").symlink?
|
225
|
+
if File.exist?("#{@input}/#{content}")
|
226
|
+
" ⇒ #{File.readlink("#{@input}/#{content}")}/".colorize(:cyan)
|
227
|
+
else
|
228
|
+
' ⇒ [Dead link]'.colorize(:red)
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
def slash?(content)
|
233
|
+
Dir.exist?("#{@input}/#{content}") ? '/'.colorize(:blue) : ' '
|
204
234
|
end
|
205
235
|
|
206
236
|
def fetch_string(content, key, color, increment)
|
@@ -208,7 +238,11 @@ module ColorLS
|
|
208
238
|
value = increment == :folders ? @folders[key] : @files[key]
|
209
239
|
logo = value.gsub(/\\u[\da-f]{4}/i) { |m| [m[-4..-1].to_i(16)].pack('U') }
|
210
240
|
|
211
|
-
|
241
|
+
[
|
242
|
+
long_info(content),
|
243
|
+
logo.colorize(color),
|
244
|
+
"#{content.colorize(color)}#{slash?(content)}#{symlink_info(content)}"
|
245
|
+
].join(' ')
|
212
246
|
end
|
213
247
|
|
214
248
|
def load_from_yaml(filename, aliase=false)
|
@@ -226,14 +260,13 @@ module ColorLS
|
|
226
260
|
chunk.each_with_index do |content, i|
|
227
261
|
break if content.empty?
|
228
262
|
|
229
|
-
print " #{fetch_string(content, *options(content))}"
|
230
|
-
print
|
231
|
-
print ' ' * (@max_widths[i] - content.length)
|
263
|
+
print " #{fetch_string(content, *options(@input, content))}"
|
264
|
+
print ' ' * (@max_widths[i] - content.length) unless @one_per_line || @long
|
232
265
|
end
|
233
266
|
end
|
234
267
|
|
235
|
-
def options(content)
|
236
|
-
if Dir.exist?("#{
|
268
|
+
def options(path, content)
|
269
|
+
if Dir.exist?("#{path}/#{content}")
|
237
270
|
key = content.to_sym
|
238
271
|
return %i[folder blue folders] unless @all_folders.include?(key)
|
239
272
|
key = @folder_aliases[key] unless @folder_keys.include?(key)
|
@@ -251,5 +284,21 @@ module ColorLS
|
|
251
284
|
key = @file_aliases[key] unless @file_keys.include?(key)
|
252
285
|
[key, :green, :recognized_files]
|
253
286
|
end
|
287
|
+
|
288
|
+
def tree_traverse(path, prespace, indent)
|
289
|
+
contents = init_contents(path)
|
290
|
+
contents.each do |content|
|
291
|
+
icon = content == contents.last || Dir.exist?("#{path}/#{content}") ? ' └──' : ' ├──'
|
292
|
+
print tree_branch_preprint(prespace, indent, icon).colorize(:cyan)
|
293
|
+
print " #{fetch_string(content, *options(path, content))} \n"
|
294
|
+
next unless Dir.exist? "#{path}/#{content}"
|
295
|
+
tree_traverse("#{path}/#{content}", prespace + indent, indent)
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
def tree_branch_preprint(prespace, indent, prespace_icon)
|
300
|
+
return prespace_icon if prespace.zero?
|
301
|
+
' │ ' * (prespace/indent) + prespace_icon + '─' * indent
|
302
|
+
end
|
254
303
|
end
|
255
304
|
end
|
data/lib/colorls/flags.rb
CHANGED
@@ -6,21 +6,24 @@ module ColorLS
|
|
6
6
|
show: fetch_show_opts,
|
7
7
|
sort: fetch_sort_opts,
|
8
8
|
all: flag_given?(%w[-a --all]),
|
9
|
+
almost_all: flag_given?(%w[-A --almost-all]),
|
9
10
|
report: flag_given?(%w[-r --report]),
|
10
|
-
one_per_line: flag_given?(%w[-1])
|
11
|
-
long: flag_given?(%w[-l --long])
|
11
|
+
one_per_line: flag_given?(%w[-1]) || !STDOUT.tty?,
|
12
|
+
long: flag_given?(%w[-l --long]),
|
13
|
+
tree: flag_given?(%w[-t --tree])
|
12
14
|
}
|
13
15
|
|
14
|
-
return if @opts[:show].nil? || @opts[:sort].nil?
|
15
|
-
|
16
16
|
@args.keep_if { |arg| !arg.start_with?('-') }
|
17
17
|
end
|
18
18
|
|
19
19
|
def process
|
20
|
+
incompatible = report_incompatible_flags
|
21
|
+
return STDERR.puts "\n #{incompatible}".colorize(:red) if incompatible
|
22
|
+
|
20
23
|
return Core.new(@opts).ls if @args.empty?
|
21
24
|
|
22
25
|
@args.each do |path|
|
23
|
-
next STDERR.puts "\n
|
26
|
+
next STDERR.puts "\n Specified path '#{path}' doesn't exist.".colorize(:red) unless File.exist?(path)
|
24
27
|
Core.new(path, @opts).ls
|
25
28
|
end
|
26
29
|
end
|
@@ -37,7 +40,7 @@ module ColorLS
|
|
37
40
|
show_files_only = flag_given? %w[-f --files]
|
38
41
|
|
39
42
|
if show_files_only && show_dirs_only
|
40
|
-
STDERR.puts "\n
|
43
|
+
STDERR.puts "\n Restrain from using -d and -f flags together."
|
41
44
|
.colorize(:red)
|
42
45
|
return nil
|
43
46
|
else
|
@@ -52,7 +55,7 @@ module ColorLS
|
|
52
55
|
sort_files_first = flag_given? %w[-sf --sort-files]
|
53
56
|
|
54
57
|
if sort_dirs_first && sort_files_first
|
55
|
-
STDERR.puts "\n
|
58
|
+
STDERR.puts "\n Restrain from using -sd and -sf flags together."
|
56
59
|
.colorize(:red)
|
57
60
|
return nil
|
58
61
|
else
|
@@ -61,5 +64,15 @@ module ColorLS
|
|
61
64
|
false
|
62
65
|
end
|
63
66
|
end
|
67
|
+
|
68
|
+
def report_incompatible_flags
|
69
|
+
return '' if @opts[:show].nil? || @opts[:sort].nil?
|
70
|
+
|
71
|
+
return 'Restrain from using -t (--tree) and -r (--report) flags together.' if @opts[:tree] && @opts[:report]
|
72
|
+
|
73
|
+
return 'Restrain from using -t (--tree) and -a (--all) flags together.' if @opts[:tree] && @opts[:all]
|
74
|
+
|
75
|
+
nil
|
76
|
+
end
|
64
77
|
end
|
65
78
|
end
|
data/lib/colorls/version.rb
CHANGED
data/lib/yaml/files.yaml
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: colorls
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Athitya Kumar
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-07-
|
11
|
+
date: 2017-07-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|