colorls 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4985c3a5528c2e34ab5e8ccac1efbbd139e5d5a1
4
- data.tar.gz: b54dc73e35b4d524b648c1348222dce052175b09
3
+ metadata.gz: 1466b9e0c4a5e7e722f61b3bc23c72393c84b209
4
+ data.tar.gz: 2e7fb89508e0e73a5ebf2c895d9054bb2961ea91
5
5
  SHA512:
6
- metadata.gz: 6ee0668a87a81f540e2e828a3f01e43f05180d16a9447b92d6eb65c14e9f0cd7afe1c23b6d1ac3a918748b2f8a9e5e11f5a51e7e9777ce9e80a572b315e01234
7
- data.tar.gz: 5859c0c78a18285d9cbea49b4197d1173cfb1dbf3cfc452a4455d53d5b04ba311e057dc2776120f2a715c888d59be2b13b750280d07f7bb0d975bdeb236b6874
6
+ metadata.gz: 5e9549e3bb540525c3b7118eae86ac2b77bd4d325e5c0e1764866cc82274ffc80010b997b0996c9173f9b921afdbd4afcf1c364e0e67ccee3ec77161db1b43d4
7
+ data.tar.gz: f8f3c48565b330c01dd075b8a1a9e056913d7d1079c196aa7adf350645c8d0fd9fc80264f3393d7334c212f8f168535cc6f8aba22ef332e1005b5cae7357a534
@@ -7,3 +7,4 @@ require 'filesize'
7
7
 
8
8
  require 'colorls/core'
9
9
  require 'colorls/flags'
10
+ require 'colorls/load_from_yaml'
@@ -1,7 +1,7 @@
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, almost_all: false, tree: false)
4
+ one_per_line: false, long: false, almost_all: false, tree: false, colors: [])
5
5
  @input = input || Dir.pwd
6
6
  @count = {folders: 0, recognized_files: 0, unrecognized_files: 0}
7
7
  @all = all
@@ -13,6 +13,7 @@ module ColorLS
13
13
  @long = long
14
14
  @tree = tree
15
15
  @screen_width = ::TermInfo.screen_size.last
16
+ @colors = colors
16
17
 
17
18
  @contents = init_contents(@input)
18
19
  @max_widths = @contents.map(&:length)
@@ -20,7 +21,7 @@ module ColorLS
20
21
  end
21
22
 
22
23
  def ls
23
- return print "\n Nothing to show here\n".colorize(:yellow) if @contents.empty?
24
+ return print "\n Nothing to show here\n".colorize(@colors[:empty]) if @contents.empty?
24
25
 
25
26
  if @tree
26
27
  print "\n"
@@ -52,8 +53,8 @@ module ColorLS
52
53
  @total_content_length = @contents.length
53
54
 
54
55
  return @contents unless @long
55
- init_user_lengths
56
- init_group_lengths
56
+ init_user_lengths(path)
57
+ init_group_lengths(path)
57
58
  @contents
58
59
  end
59
60
 
@@ -62,25 +63,25 @@ module ColorLS
62
63
  @contents.keep_if { |x| !x.start_with? '.' } unless @all || @almost_all
63
64
  end
64
65
 
65
- def init_user_lengths
66
+ def init_user_lengths(path)
66
67
  @userlength = @contents.map do |c|
67
- next 0 unless File.exist?("#{@input}/#{c}")
68
+ next 0 unless File.exist?("#{path}/#{c}")
68
69
  begin
69
- user = Etc.getpwuid(File.stat("#{@input}/#{c}").uid).name
70
+ user = Etc.getpwuid(File.stat("#{path}/#{c}").uid).name
70
71
  rescue ArgumentError
71
- user = File.stat("#{@input}/#{c}").uid
72
+ user = File.stat("#{path}/#{c}").uid
72
73
  end
73
74
  user.to_s.length
74
75
  end.max
75
76
  end
76
77
 
77
- def init_group_lengths
78
+ def init_group_lengths(path)
78
79
  @grouplength = @contents.map do |c|
79
- next 0 unless File.exist?("#{@input}/#{c}")
80
+ next 0 unless File.exist?("#{path}/#{c}")
80
81
  begin
81
- group = Etc.getgrgid(File.stat("#{@input}/#{c}").gid).name
82
+ group = Etc.getgrgid(File.stat("#{path}/#{c}").gid).name
82
83
  rescue ArgumentError
83
- group = File.stat("#{@input}/#{c}").gid
84
+ group = File.stat("#{path}/#{c}").gid
84
85
  end
85
86
  group.to_s.length
86
87
  end.max
@@ -113,10 +114,10 @@ module ColorLS
113
114
  end
114
115
 
115
116
  def init_icons
116
- @files = load_from_yaml('files.yaml')
117
- @file_aliases = load_from_yaml('file_aliases.yaml', true)
118
- @folders = load_from_yaml('folders.yaml')
119
- @folder_aliases = load_from_yaml('folder_aliases.yaml', true)
117
+ @files = ColorLS.load_from_yaml('files.yaml')
118
+ @file_aliases = ColorLS.load_from_yaml('file_aliases.yaml', true)
119
+ @folders = ColorLS.load_from_yaml('folders.yaml')
120
+ @folder_aliases = ColorLS.load_from_yaml('folder_aliases.yaml', true)
120
121
 
121
122
  @file_keys = @files.keys
122
123
  @file_aliase_keys = @file_aliases.keys
@@ -148,32 +149,32 @@ module ColorLS
148
149
  end
149
150
 
150
151
  def in_line(chunk_size)
151
- return false if @max_widths.sum + 7 * chunk_size > @screen_width
152
+ return false if @max_widths.sum + 8 * chunk_size > @screen_width
152
153
  true
153
154
  end
154
155
 
155
156
  def display_report
156
157
  print "\n Found #{@total_content_length} contents in directory "
157
- .colorize(:white)
158
+ .colorize(@colors[:report])
158
159
 
159
- print File.expand_path(@input).to_s.colorize(:blue)
160
+ print File.expand_path(@input).to_s.colorize(@colors[:dir])
160
161
 
161
162
  puts "\n\n\tFolders\t\t\t: #{@count[:folders]}"\
162
163
  "\n\tRecognized files\t: #{@count[:recognized_files]}"\
163
164
  "\n\tUnrecognized files\t: #{@count[:unrecognized_files]}"
164
- .colorize(:white)
165
+ .colorize(@colors[:report])
165
166
  end
166
167
 
167
168
  def mode_info(stat)
168
169
  mode = ''
169
170
  stat.mode.to_s(2).rjust(16, '0')[-9..-1].each_char.with_index do |c, i|
170
171
  if c == '0'
171
- mode += '-'.colorize(:gray)
172
+ mode += '-'.colorize(@colors[:no_access])
172
173
  else
173
174
  case (i % 3)
174
- when 0 then mode += 'r'.colorize(:yellow)
175
- when 1 then mode += 'w'.colorize(:magenta)
176
- when 2 then mode += 'x'.colorize(:cyan)
175
+ when 0 then mode += 'r'.colorize(@colors[:read])
176
+ when 1 then mode += 'w'.colorize(@colors[:write])
177
+ when 2 then mode += 'x'.colorize(@colors[:exec])
177
178
  end
178
179
  end
179
180
  end
@@ -187,7 +188,7 @@ module ColorLS
187
188
  user = stat.uid
188
189
  end
189
190
  user = user.to_s.ljust(@userlength, ' ')
190
- user.colorize(:green) if user == Etc.getlogin
191
+ user.colorize(@colors[:user]) if user == Etc.getlogin
191
192
  end
192
193
 
193
194
  def group_info(stat)
@@ -196,71 +197,61 @@ module ColorLS
196
197
  rescue ArgumentError
197
198
  group = stat.gid
198
199
  end
199
- group.to_s.ljust(@grouplength, ' ')
200
+ group.to_s.ljust(@grouplength, ' ').colorize(@colors[:normal])
200
201
  end
201
202
 
202
203
  def size_info(stat)
203
204
  size = Filesize.from("#{stat.size} B").pretty.split(' ')
204
- "#{size[0][0..-4].rjust(3,' ')} #{size[1].ljust(3,' ')}"
205
+ "#{size[0][0..-4].rjust(3,' ')} #{size[1].ljust(3,' ')}".colorize(@colors[:normal])
205
206
  end
206
207
 
207
208
  def mtime_info(stat)
208
- mtime = stat.mtime.asctime
209
- mtime = mtime.colorize(:yellow) if Time.now - stat.mtime < 24 * 60 * 60
210
- mtime = mtime.colorize(:green) if Time.now - stat.mtime < 60 * 60
209
+ mtime = stat.mtime.asctime.colorize(@colors[:no_modifier])
210
+ mtime = mtime.colorize(@colors[:day_old]) if Time.now - stat.mtime < 24 * 60 * 60
211
+ mtime = mtime.colorize(@colors[:hour_old]) if Time.now - stat.mtime < 60 * 60
211
212
  mtime
212
213
  end
213
214
 
214
- def long_info(content)
215
+ def long_info(path, content)
215
216
  return '' unless @long
216
- unless File.exist?("#{@input}/#{content}")
217
- return '[No Info]'.colorize(:red) + ' ' * (39 + @userlength + @grouplength)
217
+ unless File.exist?("#{path}/#{content}")
218
+ return '[No Info]'.colorize(@colors[:error]) + ' ' * (39 + @userlength + @grouplength)
218
219
  end
219
- stat = File.stat("#{@input}/#{content}")
220
+ stat = File.stat("#{path}/#{content}")
220
221
  [mode_info(stat), user_info(stat), group_info(stat), size_info(stat), mtime_info(stat)].join(' ')
221
222
  end
222
223
 
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)
224
+ def symlink_info(path, content)
225
+ return '' unless @long && File.lstat("#{path}/#{content}").symlink?
226
+ if File.exist?("#{path}/#{content}")
227
+ " ⇒ #{File.readlink("#{path}/#{content}")}/".colorize(@colors[:link])
227
228
  else
228
- ' ⇒ [Dead link]'.colorize(:red)
229
+ ' ⇒ [Dead link]'.colorize(@colors[:dead_link])
229
230
  end
230
231
  end
231
232
 
232
- def slash?(content)
233
- Dir.exist?("#{@input}/#{content}") ? '/'.colorize(:blue) : ' '
233
+ def slash?(path, content)
234
+ Dir.exist?("#{path}/#{content}") ? '/'.colorize(@colors[:dir]) : ' '
234
235
  end
235
236
 
236
- def fetch_string(content, key, color, increment)
237
+ def fetch_string(path, content, key, color, increment)
237
238
  @count[increment] += 1
238
239
  value = increment == :folders ? @folders[key] : @files[key]
239
240
  logo = value.gsub(/\\u[\da-f]{4}/i) { |m| [m[-4..-1].to_i(16)].pack('U') }
240
241
 
241
242
  [
242
- long_info(content),
243
+ long_info(path, content),
243
244
  logo.colorize(color),
244
- "#{content.colorize(color)}#{slash?(content)}#{symlink_info(content)}"
245
+ "#{content.colorize(color)}#{slash?(path, content)}#{symlink_info(path, content)}"
245
246
  ].join(' ')
246
247
  end
247
248
 
248
- def load_from_yaml(filename, aliase=false)
249
- filepath = File.join(File.dirname(__FILE__),"../yaml/#{filename}")
250
- yaml = YAML.safe_load(File.read(filepath)).symbolize_keys
251
- return yaml unless aliase
252
- yaml
253
- .to_a
254
- .map! { |k, v| [k, v.to_sym] }
255
- .to_h
256
- end
257
-
258
249
  def ls_line(chunk)
259
250
  print "\n"
260
251
  chunk.each_with_index do |content, i|
261
252
  break if content.empty?
262
253
 
263
- print " #{fetch_string(content, *options(@input, content))}"
254
+ print " #{fetch_string(@input, content, *options(@input, content))}"
264
255
  print ' ' * (@max_widths[i] - content.length) unless @one_per_line || @long
265
256
  end
266
257
  end
@@ -268,29 +259,28 @@ module ColorLS
268
259
  def options(path, content)
269
260
  if Dir.exist?("#{path}/#{content}")
270
261
  key = content.to_sym
271
- return %i[folder blue folders] unless @all_folders.include?(key)
262
+ color = @colors[:dir]
263
+ return [:folder, color, :folders] unless @all_folders.include?(key)
272
264
  key = @folder_aliases[key] unless @folder_keys.include?(key)
273
- return [key, :blue, :folders]
265
+ return [key, color, :folders]
274
266
  end
275
267
 
276
- key = content.downcase.to_sym
277
-
278
- return [key, :green, :recognized_files] if @file_keys.include?(key)
268
+ color = @colors[:recognized_file]
269
+ return [content.downcase.to_sym, color, :recognized_files] if @file_keys.include?(key)
279
270
 
280
271
  key = content.split('.').last.downcase.to_sym
281
-
282
- return %i[file yellow unrecognized_files] unless @all_files.include?(key)
272
+ return [:file, @colors[:unrecognized_file], :unrecognized_files] unless @all_files.include?(key)
283
273
 
284
274
  key = @file_aliases[key] unless @file_keys.include?(key)
285
- [key, :green, :recognized_files]
275
+ [key, color, :recognized_files]
286
276
  end
287
277
 
288
278
  def tree_traverse(path, prespace, indent)
289
279
  contents = init_contents(path)
290
280
  contents.each do |content|
291
281
  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"
282
+ print tree_branch_preprint(prespace, indent, icon).colorize(@colors[:tree])
283
+ print " #{fetch_string(path, content, *options(path, content))} \n"
294
284
  next unless Dir.exist? "#{path}/#{content}"
295
285
  tree_traverse("#{path}/#{content}", prespace + indent, indent)
296
286
  end
@@ -2,6 +2,8 @@ module ColorLS
2
2
  class Flags
3
3
  def initialize(*args)
4
4
  @args = args
5
+ set_color_opts
6
+
5
7
  @opts = {
6
8
  show: fetch_show_opts,
7
9
  sort: fetch_sort_opts,
@@ -10,7 +12,8 @@ module ColorLS
10
12
  report: flag_given?(%w[-r --report]),
11
13
  one_per_line: flag_given?(%w[-1]) || !STDOUT.tty?,
12
14
  long: flag_given?(%w[-l --long]),
13
- tree: flag_given?(%w[-t --tree])
15
+ tree: flag_given?(%w[-t --tree]),
16
+ colors: @colors
14
17
  }
15
18
 
16
19
  @args.keep_if { |arg| !arg.start_with?('-') }
@@ -35,13 +38,30 @@ module ColorLS
35
38
  false
36
39
  end
37
40
 
41
+ def set_color_opts
42
+ light_colors = flag_given? %w[--light]
43
+ dark_colors = flag_given? %w[--dark]
44
+
45
+ if light_colors && dark_colors
46
+ @colors = ColorLS.load_from_yaml('dark_colors.yaml', true)
47
+ STDERR.puts "\n Restrain from using --light and --dark flags together."
48
+ .colorize(@colors[:error])
49
+ elsif light_colors
50
+ @colors = ColorLS.load_from_yaml('light_colors.yaml', true)
51
+ else # default colors
52
+ @colors = ColorLS.load_from_yaml('dark_colors.yaml', true)
53
+ end
54
+
55
+ @colors
56
+ end
57
+
38
58
  def fetch_show_opts
39
59
  show_dirs_only = flag_given? %w[-d --dirs]
40
60
  show_files_only = flag_given? %w[-f --files]
41
61
 
42
62
  if show_files_only && show_dirs_only
43
- STDERR.puts "\n Restrain from using -d and -f flags together."
44
- .colorize(:red)
63
+ STDERR.puts "\n Restrain from using -d and -f flags together."
64
+ .colorize(@colors[:error])
45
65
  return nil
46
66
  else
47
67
  return :files if show_files_only
@@ -55,8 +75,8 @@ module ColorLS
55
75
  sort_files_first = flag_given? %w[-sf --sort-files]
56
76
 
57
77
  if sort_dirs_first && sort_files_first
58
- STDERR.puts "\n Restrain from using -sd and -sf flags together."
59
- .colorize(:red)
78
+ STDERR.puts "\n Restrain from using -sd and -sf flags together."
79
+ .colorize(@colors[:error])
60
80
  return nil
61
81
  else
62
82
  return :files if sort_files_first
@@ -0,0 +1,11 @@
1
+ module ColorLS
2
+ def self.load_from_yaml(filename, aliase=false)
3
+ filepath = File.join(File.dirname(__FILE__),"../yaml/#{filename}")
4
+ yaml = YAML.safe_load(File.read(filepath)).symbolize_keys
5
+ return yaml unless aliase
6
+ yaml
7
+ .to_a
8
+ .map! { |k, v| [k, v.to_sym] }
9
+ .to_h
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module ColorLS
2
- VERSION = '0.1.6'.freeze
2
+ VERSION = '0.1.7'.freeze
3
3
  end
@@ -0,0 +1,26 @@
1
+ # Main Colors
2
+ unrecognized_file: yellow
3
+ recognized_file: green
4
+ dir: blue
5
+
6
+ # Link
7
+ dead_link: red
8
+ link: cyan
9
+
10
+ # Access Modes
11
+ write: magenta
12
+ read: yellow
13
+ exec: cyan
14
+ no_access: grey
15
+
16
+ # Age
17
+ day_old: yellow
18
+ hour_old: green
19
+ no_modifier: white
20
+
21
+ # Random
22
+ report: white
23
+ user: green
24
+ tree: cyan
25
+ empty: yellow
26
+ normal: white
@@ -0,0 +1,26 @@
1
+ # Main Colors
2
+ unrecognized_file: yellow
3
+ recognized_file: green
4
+ dir: blue
5
+
6
+ # Link
7
+ dead_link: red
8
+ link: cyan
9
+
10
+ # Access Modes
11
+ write: red
12
+ read: yellow
13
+ exec: blue
14
+ no_access: black
15
+
16
+ # Age
17
+ day_old: yellow
18
+ hour_old: green
19
+ no_modifier: black
20
+
21
+ # Random
22
+ report: black
23
+ user: green
24
+ tree: cyan
25
+ empty: yellow
26
+ normal: black
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.6
4
+ version: 0.1.7
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-13 00:00:00.000000000 Z
11
+ date: 2017-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -158,11 +158,14 @@ files:
158
158
  - lib/colorls.rb
159
159
  - lib/colorls/core.rb
160
160
  - lib/colorls/flags.rb
161
+ - lib/colorls/load_from_yaml.rb
161
162
  - lib/colorls/version.rb
163
+ - lib/yaml/dark_colors.yaml
162
164
  - lib/yaml/file_aliases.yaml
163
165
  - lib/yaml/files.yaml
164
166
  - lib/yaml/folder_aliases.yaml
165
167
  - lib/yaml/folders.yaml
168
+ - lib/yaml/light_colors.yaml
166
169
  - readme/pending.png
167
170
  - readme/usage1.png
168
171
  - readme/usage2.png