directory_paradise 1.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1874 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # === DirectoryParadise::Report
6
+ #
7
+ # The purpose of this class is to show the content of a directory, either
8
+ # in a plain variant without any colours, or in a fancified variant, with
9
+ # colours.
10
+ #
11
+ # The initial reason for creating this class was that I needed a general
12
+ # adaptor for when I would be using ruby on windows, or on another
13
+ # operating system, whenever I would try to use larger projects such
14
+ # as the Roebe::Shell component, which is part of the gem called
15
+ # "roebe".
16
+ #
17
+ # In the past, this was the sole use case that class ShowDirectoryContent
18
+ # had to fulfil, but lateron I thought that I could factor this code out
19
+ # into a standalone class, and make it available for other projects as
20
+ # well - as true standalone project. Thus, the directory_content gem
21
+ # was born. \o/
22
+ #
23
+ # This class here has, at least, the following main features that it
24
+ # has to support:
25
+ #
26
+ # - The ability to show the modification time.
27
+ # - The ability to truncate long file names.
28
+ # - The ability to list hidden files.
29
+ # - The ability to sort by size, by passing the argument "size" to it.
30
+ # - The ability to set and define your own colours.
31
+ # - The ability to show a simplified, shorter result.
32
+ # - The ability to make the colour output optional.
33
+ # - The ability to filter on the content, e. g. via '*'.
34
+ #
35
+ # A good general overview for available options for the GNU "ls" command
36
+ # can be found here:
37
+ #
38
+ # https://ss64.com/bash/ls.html
39
+ #
40
+ # =========================================================================== #
41
+ # require 'directory_paradise/report/report.rb'
42
+ # DirectoryParadise::Report.new(ARGV)
43
+ # =========================================================================== #
44
+ require 'directory_paradise/base/base.rb'
45
+
46
+ module DirectoryParadise
47
+
48
+ class Report < Base # === DirectoryParadise::Report
49
+
50
+ require 'colours'
51
+ require 'directory_paradise/project/project.rb'
52
+ require 'directory_paradise/content/content.rb'
53
+ require 'directory_paradise/report/menu.rb'
54
+
55
+ # ========================================================================= #
56
+ # === COLOURIZE_KB_AND_MB
57
+ #
58
+ # If true then we colourize KB and MB. This also includes B.
59
+ # ========================================================================= #
60
+ COLOURIZE_KB_AND_MB = true
61
+
62
+ # ========================================================================= #
63
+ # === FILE_SIZE_THRESHOLD
64
+ #
65
+ # When an inode entry has more than this amount of chars, we will
66
+ # truncate.
67
+ # ========================================================================= #
68
+ FILE_SIZE_THRESHOLD = 50
69
+
70
+ # ========================================================================= #
71
+ # === FILE_COLOURS_FOR_BYTES_VALUES
72
+ # ========================================================================= #
73
+ FILE_COLOURS_FOR_BYTES_VALUES = ::DirectoryParadise.project_base_directory?+
74
+ 'yaml/colours_for_bytes_values.yml'
75
+
76
+ # ========================================================================= #
77
+ # === HASH_COLOURS_FOR_BYTES_VALUE
78
+ # ========================================================================= #
79
+ if File.exist? FILE_COLOURS_FOR_BYTES_VALUES
80
+ HASH_COLOURS_FOR_BYTES_VALUE =
81
+ YAML.load_file(
82
+ FILE_COLOURS_FOR_BYTES_VALUES
83
+ )
84
+ end
85
+
86
+ # ========================================================================= #
87
+ # === initialize
88
+ #
89
+ # The first argument is the target directory that should be displayed.
90
+ # ========================================================================= #
91
+ def initialize(
92
+ work_on_this_directory = return_pwd,
93
+ run_already = true
94
+ )
95
+ register_sigint
96
+ reset
97
+ # ======================================================================= #
98
+ # === Handle the run_already variable next
99
+ # ======================================================================= #
100
+ case run_already
101
+ # ======================================================================= #
102
+ # === :do_not_run_yet
103
+ # ======================================================================= #
104
+ when :do_not_run_yet,
105
+ :dont_run_yet,
106
+ :dont_run_as_of_yet,
107
+ :dontrunyet
108
+ run_already = false
109
+ # ======================================================================= #
110
+ # === :display
111
+ # ======================================================================= #
112
+ when :display,
113
+ :do_show,
114
+ :do_show_it
115
+ do_show_the_content
116
+ run_already = true
117
+ # ======================================================================= #
118
+ # === :run
119
+ # ======================================================================= #
120
+ when :run
121
+ run_already = true
122
+ end
123
+ # ======================================================================= #
124
+ # Assign to an instance variable next. This must come after
125
+ # reset().
126
+ # ======================================================================= #
127
+ @internal_hash[:run_already] = run_already
128
+ set_commandline_arguments(
129
+ work_on_this_directory
130
+ )
131
+ set_work_on_this_directory( # This method call can alter @run_already.
132
+ work_on_this_directory
133
+ )
134
+ run if @internal_hash[:run_already]
135
+ end
136
+
137
+ # ========================================================================= #
138
+ # === reset (reset tag)
139
+ # ========================================================================= #
140
+ def reset
141
+ infer_the_namespace
142
+ # ======================================================================= #
143
+ # === @internal_hash
144
+ # ======================================================================= #
145
+ @internal_hash = {}
146
+ # ======================================================================= #
147
+ # === :work_on_this_directory
148
+ # ======================================================================= #
149
+ @internal_hash[:work_on_this_directory] = return_pwd
150
+ # ======================================================================= #
151
+ # === :collapse
152
+ #
153
+ # Whether to collapse or not.
154
+ # ======================================================================= #
155
+ @internal_hash[:collapse] = false
156
+ # ======================================================================= #
157
+ # === :total_filesize
158
+ # ======================================================================= #
159
+ @internal_hash[:total_filesize] = 0 # Keep the total file size.
160
+ # ======================================================================= #
161
+ # === :report_filesize
162
+ #
163
+ # Tell the user the total filesize of the listing.
164
+ # ======================================================================= #
165
+ @internal_hash[:report_filesize] = true
166
+ # ======================================================================= #
167
+ # === :default_colour
168
+ # ======================================================================= #
169
+ @internal_hash[:default_colour] = :steelblue
170
+ # ======================================================================= #
171
+ # === :colourize_kb_and_mb
172
+ # ======================================================================= #
173
+ @internal_hash[:colourize_kb_and_mb] = COLOURIZE_KB_AND_MB
174
+ # ======================================================================= #
175
+ # === :threshold_for_file_size
176
+ #
177
+ # Only honoured if @truncate_long_file_names is true.
178
+ # ======================================================================= #
179
+ @internal_hash[:threshold_for_file_size] = FILE_SIZE_THRESHOLD
180
+ # ======================================================================= #
181
+ # === :show_group_information
182
+ # ======================================================================= #
183
+ @internal_hash[:show_group_information] = true
184
+ # ======================================================================= #
185
+ # === :show_modification_time
186
+ #
187
+ # if true then we show the modification time of the inode in question.
188
+ # ======================================================================= #
189
+ @internal_hash[:show_modification_time] = true
190
+ # ======================================================================= #
191
+ # === :use_colours
192
+ # ======================================================================= #
193
+ @internal_hash[:use_colours] = true
194
+ # ======================================================================= #
195
+ # === :show_leading_slash
196
+ #
197
+ # Show the leading slash in / directory.
198
+ # ======================================================================= #
199
+ @internal_hash[:show_leading_slash] = false
200
+ # ======================================================================= #
201
+ # === :sort_by
202
+ #
203
+ # This value must be nil initially, so that we will not do any
204
+ # sorting at all whatsoever.
205
+ # ======================================================================= #
206
+ @internal_hash[:sort_by] = nil
207
+ # ======================================================================= #
208
+ # === :show_only_symlinks
209
+ # ======================================================================= #
210
+ @internal_hash[:show_only_symlinks] = false
211
+ # ======================================================================= #
212
+ # === truncate_long_file_names
213
+ #
214
+ # If true, we truncate too-long file names.
215
+ # ======================================================================= #
216
+ @internal_hash[:truncate_long_file_names] = false
217
+ # ======================================================================= #
218
+ # === :colour_for_symlinks
219
+ # ======================================================================= #
220
+ @internal_hash[:colour_for_symlinks] = Colours.colour_for_symlinks
221
+ # ======================================================================= #
222
+ # === :show_size
223
+ #
224
+ # Whether to show the size of the entry at hand.
225
+ # ======================================================================= #
226
+ @internal_hash[:show_size] = true
227
+ # ======================================================================= #
228
+ # === :show_header
229
+ # ======================================================================= #
230
+ @internal_hash[:show_header] = true
231
+ # ======================================================================= #
232
+ # === :show_index
233
+ #
234
+ # Whether to show a leading index or not. I like this, so it is on
235
+ # by default.
236
+ # ======================================================================= #
237
+ @internal_hash[:show_index] = true
238
+ # ======================================================================= #
239
+ # === :colourize_path
240
+ #
241
+ # If the next variable is set to true then the path, such as
242
+ # /foo/bar.md will be colourized when .report() is called.
243
+ # ======================================================================= #
244
+ @internal_hash[:colourize_path] = true
245
+ # ======================================================================= #
246
+ # === :show_permission_bits
247
+ #
248
+ # If the following variable is set to true then the entry at hand
249
+ # will show inclusive "drwxr-xr-x".
250
+ # ======================================================================= #
251
+ @internal_hash[:show_permission_bits] = true
252
+ # ======================================================================= #
253
+ # === :show_dots_only_entries
254
+ #
255
+ # This setting filters away '.' and '..' from the result.
256
+ # ======================================================================= #
257
+ @internal_hash[:show_dots_only_entries] = false
258
+ # ======================================================================= #
259
+ # === :show_statistics
260
+ # ======================================================================= #
261
+ @internal_hash[:show_statistics] = true
262
+ # ======================================================================= #
263
+ # === :directory_content
264
+ #
265
+ # This variable will keep a reference to class
266
+ # DirectoryParadise::Content.
267
+ # ======================================================================= #
268
+ @internal_hash[:directory_content] = DirectoryParadise::Content.new
269
+ # ======================================================================= #
270
+ # === :hash_colours_for_bytes_value
271
+ #
272
+ # We need to capture this here because we may try to want to sanitize
273
+ # the given values, such as when the user types an incorrect colour,
274
+ # like "mediumpurples" rather than "mediumpurple".
275
+ # ======================================================================= #
276
+ @internal_hash[:hash_colours_for_bytes_value] = HASH_COLOURS_FOR_BYTES_VALUE
277
+ # ======================================================================= #
278
+ # === :show_simplified
279
+ #
280
+ # The following variable tells this class to keep a simplified directory
281
+ # layout.
282
+ #
283
+ # If it is true, then we only display a short variant of the file
284
+ # content.
285
+ # ======================================================================= #
286
+ @internal_hash[:show_simplified] = true
287
+ # ======================================================================= #
288
+ # === :result
289
+ # ======================================================================= #
290
+ @internal_hash[:result] = ''.dup
291
+ do_not_show_hidden_files # Do not show hidden files by default.
292
+ do_show_header
293
+ do_show_index
294
+ do_show_modification_time
295
+ do_show_the_content
296
+ do_not_stop_on_missing_symlink
297
+ end
298
+
299
+ # ========================================================================= #
300
+ # === try_to_report_the_total_filesize
301
+ #
302
+ # Use this method to report the total filesize from a given directory.
303
+ # ========================================================================= #
304
+ def try_to_report_the_total_filesize
305
+ total_file_size = total_file_size?
306
+ if total_file_size > 0
307
+ ee "\nTotal file size in the directory "
308
+ if use_colours?
309
+ e colourize_directory(which_dir?)
310
+ else
311
+ e which_dir?
312
+ end
313
+ append("\nTotal file size in the directory #{which_dir?}")
314
+ n_KB = (total_file_size / 1024.0)
315
+ n_MB = (n_KB / 1024.0)
316
+ append(
317
+ " #{total_file_size} bytes (#{n_KB.round(2)} KB) "\
318
+ "(#{n_MB.round(2)} MB)"
319
+ )
320
+ e steelblue(
321
+ " #{total_file_size} bytes (#{n_KB.round(2)} KB) "\
322
+ "(#{n_MB.round(2)} MB)"
323
+ )
324
+ end
325
+ end
326
+
327
+ # ========================================================================= #
328
+ # === return_file_permission_of
329
+ # ========================================================================= #
330
+ def return_file_permission_of(i)
331
+ if condensed? and i.size == 10 # Then we assume we have the long format.
332
+ Roebe::PermissionAsciiFormat[i, :convert_into_decimal_format]
333
+ else # else we must convert it back.
334
+ i # Simply return as-is here.
335
+ end
336
+ end
337
+
338
+ # ========================================================================= #
339
+ # === set_sort_by
340
+ # ========================================================================= #
341
+ def set_sort_by(i)
342
+ @internal_hash[:sort_by] = i
343
+ end; alias sort_by= set_sort_by # === sort_by=
344
+ alias sort_by set_sort_by # === sort_by
345
+
346
+ # ========================================================================= #
347
+ # === sort_how (sort tag, sort how tag)
348
+ #
349
+ # Here we determine how to sort.
350
+ #
351
+ # The sort possibilities are:
352
+ #
353
+ # - by datesort_how
354
+ # - by size
355
+ # - in a reversed manner
356
+ #
357
+ # ========================================================================= #
358
+ def sort_how(
359
+ how = :size
360
+ )
361
+ how = how.to_s.downcase.to_sym # We want a downcased Symbol here.
362
+ case how # case tag
363
+ # ======================================================================= #
364
+ # === :reversed
365
+ # ======================================================================= #
366
+ when :reversed,
367
+ :reverse,
368
+ :by_reverse,
369
+ :by_reversed
370
+ set_sort_by(:reversed)
371
+ # ======================================================================= #
372
+ # === :date
373
+ # ======================================================================= #
374
+ when :date,
375
+ :by_date
376
+ set_sort_by(:date)
377
+ # ======================================================================= #
378
+ # === :size
379
+ # ======================================================================= #
380
+ when :size,
381
+ :sized,
382
+ :by_size # Sort by size.
383
+ set_sort_by(:size)
384
+ # ======================================================================= #
385
+ # === :modification_time
386
+ # ======================================================================= #
387
+ when :modification_time,
388
+ :by_modification_time
389
+ set_sort_by(:modification_time)
390
+ end
391
+ end
392
+
393
+ # ========================================================================= #
394
+ # === show_help_options (help tag)
395
+ #
396
+ # To invoke this method, try:
397
+ #
398
+ # sdc --help
399
+ #
400
+ # ========================================================================= #
401
+ def show_help_options # Help tag.
402
+ e "#{rev}The currently available help options for "\
403
+ "#{simp('DirectoryContent::Report')} are:"
404
+ e
405
+ show_this_help_line :collapse, :short
406
+ show_this_help_line :toggle
407
+ show_this_help_line :nocolours
408
+ show_this_help_line :only_dirs
409
+ show_this_help_line :show_hidden_files
410
+ show_this_help_line :sized
411
+ show_this_help_line :date
412
+ show_this_help_line :lead
413
+ show_this_help_line :disable_show_modification_time
414
+ show_this_help_line :condensed
415
+ e
416
+ e 'Additionally, the following options are a bit more '\
417
+ 'extensively documented here:'
418
+ e
419
+ Colours.eparse ' --show-only-symlinks # '\
420
+ 'This will show only symlinks in the target directory'
421
+ e
422
+ exit
423
+ end
424
+
425
+ # ========================================================================= #
426
+ # === show_dots_only_entries?
427
+ # ========================================================================= #
428
+ def show_dots_only_entries?
429
+ @internal_hash[:show_dots_only_entries]
430
+ end
431
+
432
+ # ========================================================================= #
433
+ # === show_only_symlinks?
434
+ # ========================================================================= #
435
+ def show_only_symlinks?
436
+ @internal_hash[:show_only_symlinks]
437
+ end
438
+
439
+ # ========================================================================= #
440
+ # === ignore_backups?
441
+ # ========================================================================= #
442
+ def ignore_backups?
443
+ @internal_hash[:ignore_backups]
444
+ end
445
+
446
+ # ========================================================================= #
447
+ # === collapse?
448
+ #
449
+ # The revers of whether we will show the full path or not.
450
+ # ========================================================================= #
451
+ def collapse?
452
+ !show_full_path?
453
+ end
454
+
455
+ # ========================================================================= #
456
+ # === content?
457
+ # ========================================================================= #
458
+ def content?
459
+ @internal_hash[:directory_content].content?
460
+ end; alias content content? # === content?
461
+
462
+ # ========================================================================= #
463
+ # === show_inode?
464
+ # ========================================================================= #
465
+ def show_inode?
466
+ @internal_hash[:show_inode]
467
+ end
468
+
469
+ # ========================================================================= #
470
+ # === show_full_path?
471
+ # ========================================================================= #
472
+ def show_full_path?
473
+ @internal_hash[:show_full_path]
474
+ end; alias show_full_path show_full_path? # === show_full_path
475
+
476
+ # ========================================================================= #
477
+ # === show_group_information?
478
+ # ========================================================================= #
479
+ def show_group_information?
480
+ @internal_hash[:show_group_information]
481
+ end
482
+
483
+ # ========================================================================= #
484
+ # === show_size?
485
+ # ========================================================================= #
486
+ def show_size?
487
+ @internal_hash[:show_size]
488
+ end
489
+
490
+ # ========================================================================= #
491
+ # === colourize_path?
492
+ # ========================================================================= #
493
+ def colourize_path?
494
+ @internal_hash[:colourize_path]
495
+ end
496
+
497
+ # ========================================================================= #
498
+ # === show_permissionsbits?
499
+ # ========================================================================= #
500
+ def show_permission_bits?
501
+ @internal_hash[:show_permission_bits]
502
+ end; alias show_permissions? show_permission_bits? # === show_permissions?
503
+
504
+ # ========================================================================= #
505
+ # === directory_content?
506
+ # ========================================================================= #
507
+ def directory_content?
508
+ @internal_hash[:directory_content]
509
+ end
510
+
511
+ # ========================================================================= #
512
+ # === obtain_from_directory_content
513
+ # ========================================================================= #
514
+ def obtain_from_directory_content(i)
515
+ @internal_hash[:directory_content].obtain_from(i)
516
+ end
517
+
518
+ # ========================================================================= #
519
+ # === display_only_directories?
520
+ # ========================================================================= #
521
+ def display_only_directories?
522
+ @internal_hash[:display_only_directories]
523
+ end
524
+
525
+ # ========================================================================= #
526
+ # === show_hidden_files?
527
+ # ========================================================================= #
528
+ def show_hidden_files?
529
+ @internal_hash[:show_hidden_files]
530
+ end
531
+
532
+ # ========================================================================= #
533
+ # === show_index?
534
+ # ========================================================================= #
535
+ def show_index?
536
+ @internal_hash[:show_index]
537
+ end; alias use_index? show_index? # === use_index?
538
+
539
+ # ========================================================================= #
540
+ # === entries?
541
+ # ========================================================================= #
542
+ def entries?
543
+ @internal_hash[:directory_content].entries
544
+ end; alias _ entries? # === _
545
+
546
+ # ========================================================================= #
547
+ # === sort_by?
548
+ # ========================================================================= #
549
+ def sort_by?
550
+ @internal_hash[:sort_by]
551
+ end
552
+
553
+ # ========================================================================= #
554
+ # === symlinks?
555
+ # ========================================================================= #
556
+ def symlinks?
557
+ @internal_hash[:directory_content].symlinks?
558
+ end
559
+
560
+ # ========================================================================= #
561
+ # === n_symlinks?
562
+ # ========================================================================= #
563
+ def n_symlinks?
564
+ symlinks?.size
565
+ end
566
+
567
+ # ========================================================================= #
568
+ # === directories?
569
+ # ========================================================================= #
570
+ def directories? # Feedback all available directories here.
571
+ @internal_hash[:directory_content].directories?
572
+ end
573
+
574
+ # ========================================================================= #
575
+ # === files?
576
+ #
577
+ # Query for all available files, by delegating towards class
578
+ # DirectoryContent.
579
+ # ========================================================================= #
580
+ def files?
581
+ @internal_hash[:directory_content].files?
582
+ end
583
+
584
+ # ========================================================================= #
585
+ # === n_files?
586
+ # ========================================================================= #
587
+ def n_files?
588
+ files?.size
589
+ end
590
+
591
+ # ========================================================================= #
592
+ # === show_statistics?
593
+ # ========================================================================= #
594
+ def show_statistics?
595
+ @internal_hash[:show_statistics]
596
+ end
597
+
598
+ # ========================================================================= #
599
+ # === return_directory_content
600
+ # ========================================================================= #
601
+ def return_directory_content(
602
+ i = @internal_hash[:work_on_this_directory]
603
+ )
604
+ obtain_from_directory_content(i)
605
+ consider_sorting
606
+ _ = @internal_hash[:directory_content].entries?
607
+ return _
608
+ end; alias gather_content return_directory_content # === gather_content
609
+ alias gather return_directory_content # === gather
610
+ alias run_main_loop return_directory_content # === run_main_loop
611
+ alias obtain_directory_listing return_directory_content # === obtain_directory_listing
612
+ alias obtain_entries return_directory_content # === obtain_entries
613
+
614
+ # ========================================================================= #
615
+ # === display_content?
616
+ # ========================================================================= #
617
+ def display_content?
618
+ @internal_hash[:display_content]
619
+ end
620
+
621
+ # ========================================================================= #
622
+ # === do_not_show_simplified
623
+ # ========================================================================= #
624
+ def do_not_show_simplified
625
+ @internal_hash[:show_simplified] = false
626
+ end
627
+
628
+ # ========================================================================= #
629
+ # === do_show_simplified
630
+ # ========================================================================= #
631
+ def do_show_simplified
632
+ @internal_hash[:show_simplified] = true
633
+ end; alias show_simplified do_show_simplified # === show_simplified
634
+
635
+ # ========================================================================= #
636
+ # === n_directories?
637
+ #
638
+ # This will exclude directories with a trailing '.'
639
+ # ========================================================================= #
640
+ def n_directories?
641
+ directories = directories?.reject {|entry| entry.end_with? '/.' }
642
+ return directories.size
643
+ end
644
+
645
+ # ========================================================================= #
646
+ # === hash_colours_for_bytes_value?
647
+ # ========================================================================= #
648
+ def hash_colours_for_bytes_value?
649
+ @internal_hash[:hash_colours_for_bytes_value]
650
+ end
651
+
652
+ # ========================================================================= #
653
+ # === report_filesize?
654
+ # ========================================================================= #
655
+ def report_filesize?
656
+ @internal_hash[:report_filesize]
657
+ end; alias report_the_total_filesize? report_filesize? # === report_the_total_filesize?
658
+
659
+ # ========================================================================= #
660
+ # === total_filesize?
661
+ # ========================================================================= #
662
+ def total_filesize?
663
+ @internal_hash[:total_filesize]
664
+ end; alias total_file_size? total_filesize? # === total_file_size?
665
+
666
+ # ========================================================================= #
667
+ # === work_on_which_directory?
668
+ # ========================================================================= #
669
+ def work_on_which_directory?
670
+ @internal_hash[:work_on_this_directory]
671
+ end; alias from_which_directory? work_on_which_directory? # === from_which_directory?
672
+ alias base_directory? work_on_which_directory? # === base_directory?
673
+ alias input? work_on_which_directory? # === input?
674
+ alias which_directory? work_on_which_directory? # === which_directory?
675
+ alias which_dir? work_on_which_directory? # === which_dir?
676
+ alias base_dir? work_on_which_directory? # === base_dir?
677
+ alias dir? work_on_which_directory? # === dir?
678
+
679
+ # ========================================================================= #
680
+ # === dont_show_header
681
+ # ========================================================================= #
682
+ def dont_show_header
683
+ @internal_hash[:show_header] = false
684
+ end; alias do_not_show_the_header dont_show_header # === do_not_show_the_header
685
+
686
+ # ========================================================================= #
687
+ # === do_show_hidden_files
688
+ # ========================================================================= #
689
+ def do_show_hidden_files
690
+ @internal_hash[:show_hidden_files] = true
691
+ end
692
+
693
+ # ========================================================================= #
694
+ # == dont_show_index
695
+ # ========================================================================= #
696
+ def dont_show_index
697
+ @internal_hash[:show_index] = false
698
+ end; alias do_not_show_the_index dont_show_index # === do_not_show_the_index
699
+ alias do_not_show_index dont_show_index # === do_not_show_index
700
+
701
+ # ========================================================================= #
702
+ # === toggle_colourize
703
+ # ========================================================================= #
704
+ def toggle_colourize
705
+ if @internal_hash[:colourize_kb_and_mb] == false
706
+ @internal_hash[:colourize_kb_and_mb] = true
707
+ else
708
+ @internal_hash[:colourize_kb_and_mb] = false
709
+ end
710
+ end; alias toggle toggle_colourize # === toggle
711
+
712
+ # ========================================================================= #
713
+ # === do_not_ignore_backups
714
+ # ========================================================================= #
715
+ def do_not_ignore_backups
716
+ @internal_hash[:ignore_backups] = false
717
+ end
718
+
719
+ # ========================================================================= #
720
+ # === do_colourize_kb_and_mb
721
+ # ========================================================================= #
722
+ def do_colourize_kb_and_mb
723
+ @internal_hash[:colourize_kb_and_mb] = true
724
+ end
725
+
726
+ # ========================================================================= #
727
+ # === collapse=
728
+ # ========================================================================= #
729
+ def collapse=(i)
730
+ @internal_hash[:collapse] = i
731
+ end
732
+
733
+ # ========================================================================= #
734
+ # === stop_on_missing_symlink=
735
+ # ========================================================================= #
736
+ def stop_on_missing_symlink=(i)
737
+ @internal_hash[:stop_on_missing_symlink] = i
738
+ end
739
+
740
+ # ========================================================================= #
741
+ # === do_show_full_path
742
+ # ========================================================================= #
743
+ def do_show_full_path
744
+ @internal_hash[:show_full_path] = true
745
+ end
746
+
747
+ # ========================================================================= #
748
+ # === dont_show_full_path
749
+ #
750
+ # I assume that this option is interconnected with another option,
751
+ # namely the ability to show, or rather not show, a leading slash.
752
+ #
753
+ # Thus, as of May 2015, we will also disable showing the leading
754
+ # slash whenever we invoke this method here.
755
+ # ========================================================================= #
756
+ def dont_show_full_path
757
+ set_show_full_path(false)
758
+ do_not_show_leading_slash
759
+ end; alias do_not_show_full_path dont_show_full_path # === do_not_show_full_path
760
+ alias enable_collapse dont_show_full_path # === enable_collapse
761
+ alias do_not_show_the_full_path dont_show_full_path # === do_not_show_the_full_path
762
+ alias do_show_only_filename dont_show_full_path # === do_show_only_filename
763
+
764
+ # ========================================================================= #
765
+ # === check_whether_we_use_colours
766
+ #
767
+ # We will enable the colours if necessary.
768
+ # ========================================================================= #
769
+ def check_whether_we_use_colours
770
+ if use_colours?
771
+ enable_colours
772
+ check_whether_the_colours_defined_in_the_yaml_file_are_valid
773
+ define_colours
774
+ end
775
+ end
776
+
777
+ # ========================================================================= #
778
+ # === do_not_stop_on_missing_symlink
779
+ # ========================================================================= #
780
+ def do_not_stop_on_missing_symlink
781
+ @internal_hash[:stop_on_missing_symlink] = false # if true we will stop on missing symlink.
782
+ end
783
+
784
+ # ========================================================================= #
785
+ # === do_show_condensed_permissions
786
+ # ========================================================================= #
787
+ def do_show_condensed_permissions
788
+ @internal_hash[:show_condensed_permissions] = true
789
+ end
790
+
791
+ # ========================================================================= #
792
+ # === ignore_backups
793
+ # ========================================================================= #
794
+ def ignore_backups
795
+ @internal_hash[:ignore_backups] = true
796
+ end
797
+
798
+ # ========================================================================= #
799
+ # === do_not_report_filesize
800
+ # ========================================================================= #
801
+ def do_not_report_filesize
802
+ @internal_hash[:report_filesize] = false
803
+ end; alias dont_report_total_filesize do_not_report_filesize # === dont_report_total_filesize
804
+
805
+ # ========================================================================= #
806
+ # === do_show_index
807
+ # ========================================================================= #
808
+ def do_show_index
809
+ @internal_hash[:show_index] = true # if true then we will show the index.
810
+ end
811
+
812
+ # ========================================================================= #
813
+ # === do_report_filesize
814
+ # ========================================================================= #
815
+ def do_report_filesize
816
+ @internal_hash[:report_filesize] = true
817
+ end
818
+
819
+ # ========================================================================= #
820
+ # === do_not_show_size
821
+ # ========================================================================= #
822
+ def do_not_show_size
823
+ @internal_hash[:show_size] = false
824
+ end
825
+
826
+ # ========================================================================= #
827
+ # === do_show_almost_nothing
828
+ # ========================================================================= #
829
+ def do_show_almost_nothing
830
+ do_not_show_size
831
+ do_not_show_index
832
+ do_not_show_permissions
833
+ do_not_show_modtime
834
+ end
835
+
836
+ # ========================================================================= #
837
+ # === do_truncate_long_file_names
838
+ # ========================================================================= #
839
+ def do_truncate_long_file_names
840
+ @internal_hash[:truncate_long_file_names] = true
841
+ end; alias truncate do_truncate_long_file_names # === truncate
842
+
843
+ # ========================================================================= #
844
+ # === disable_show_modification_time
845
+ # ========================================================================= #
846
+ def disable_show_modification_time
847
+ @internal_hash[:show_modification_time] = false
848
+ end; alias dont_show_modification_time disable_show_modification_time
849
+ alias do_not_show_modification_time disable_show_modification_time
850
+ alias do_not_show_modtime disable_show_modification_time
851
+
852
+ # ========================================================================= #
853
+ # === do_shorten_display
854
+ # ========================================================================= #
855
+ def do_shorten_display
856
+ enable_collapse
857
+ disable_show_modification_time # Added Aug 2012.
858
+ end
859
+
860
+ # ========================================================================= #
861
+ # === do_not_debug
862
+ # ========================================================================= #
863
+ def do_not_debug
864
+ @internal_hash[:debug] = false # Whether we debug this class or not.
865
+ end
866
+
867
+ # ========================================================================= #
868
+ # === do_not_truncate_long_file_names
869
+ # ========================================================================= #
870
+ def do_not_truncate_long_file_names
871
+ @internal_hash[:truncate_long_file_names] = false
872
+ end
873
+
874
+ # ========================================================================= #
875
+ # === do_show_leading_slash
876
+ # ========================================================================= #
877
+ def do_show_leading_slash
878
+ @internal_hash[:show_leading_slash] = true
879
+ end
880
+
881
+ # ========================================================================= #
882
+ # === do_show_modification_time
883
+ # ========================================================================= #
884
+ def do_show_modification_time
885
+ @internal_hash[:show_modification_time] = true
886
+ end
887
+
888
+ # ========================================================================= #
889
+ # === do_not_show_inode
890
+ # ========================================================================= #
891
+ def do_not_show_inode
892
+ @internal_hash[:show_inode] = false
893
+ end
894
+
895
+ # ========================================================================= #
896
+ # === do_show_inode
897
+ # ========================================================================= #
898
+ def do_show_inode # Show the inode number if true.
899
+ @internal_hash[:show_inode] = true
900
+ end
901
+
902
+ # ========================================================================= #
903
+ # === do_hide_files
904
+ # ========================================================================= #
905
+ def do_hide_files
906
+ @internal_hash[:show_hidden_files] = false
907
+ end; alias do_not_show_hidden_files do_hide_files # === do_not_show_hidden_files
908
+
909
+ # ========================================================================= #
910
+ # === do_show_uncondensed_permissions
911
+ # ========================================================================= #
912
+ def do_show_uncondensed_permissions
913
+ @internal_hash[:show_condensed_permissions] = false
914
+ end; alias dont_show_condensed_permissions do_show_uncondensed_permissions
915
+
916
+ # ========================================================================= #
917
+ # === show_this_help_line
918
+ # ========================================================================= #
919
+ def show_this_help_line(
920
+ i = :toggle, optional_additional_argument = nil
921
+ )
922
+ if optional_additional_argument
923
+ e " - #{sfancy(i)} / #{sfancy(optional_additional_argument)}"
924
+ else
925
+ e " - #{sfancy(i)}"
926
+ end
927
+ end
928
+
929
+ # ========================================================================= #
930
+ # === set_show_full_path
931
+ # ========================================================================= #
932
+ def set_show_full_path(i = true)
933
+ @internal_hash[:show_full_path] = i
934
+ end
935
+
936
+ # ========================================================================= #
937
+ # === run_already?
938
+ # ========================================================================= #
939
+ def run_already?
940
+ @internal_hash[:run_already]
941
+ end
942
+
943
+ # ========================================================================= #
944
+ # === set_work_on_this_directory
945
+ #
946
+ # This method will determine on which target directory (the base
947
+ # directory) this class will work on.
948
+ #
949
+ # It must be ensured that this variable will have a trailing '/'
950
+ # character, to properly indicate that we are dealing with a
951
+ # directory here.
952
+ # ========================================================================= #
953
+ def set_work_on_this_directory(
954
+ i = return_pwd
955
+ )
956
+ # ======================================================================= #
957
+ # === Handle Hashes first
958
+ # ======================================================================= #
959
+ if i.is_a? Hash
960
+ if i.has_key? :from
961
+ i = i.delete(:from)
962
+ end
963
+ end
964
+ if i.is_a? Array
965
+ _ = return_non_hyphen_entries_from(i)
966
+ if _.empty?
967
+ i = return_pwd
968
+ else
969
+ i = _
970
+ end
971
+ i = i.join(' ').strip if i.is_a? Array
972
+ end
973
+ case i
974
+ # ======================================================================= #
975
+ # === :dont_run_yet
976
+ # ======================================================================= #
977
+ when :dont_run_yet
978
+ i = nil
979
+ @internal_hash[:run_already] = false
980
+ # ======================================================================= #
981
+ # === :show_content
982
+ # ======================================================================= #
983
+ when :show_content
984
+ do_show_the_content
985
+ i = return_pwd
986
+ end
987
+ begin
988
+ # ===================================================================== #
989
+ # Default values come next here in this clause.
990
+ # ===================================================================== #
991
+ i = return_pwd if i.nil?
992
+ i = i.dup if i.frozen?
993
+ # ===================================================================== #
994
+ # We need to default to the current directory if there exists a
995
+ # directory with that name.
996
+ # ===================================================================== #
997
+ i = return_pwd unless Dir.exist?(i)
998
+ if i.is_a? String and i.empty?
999
+ i = return_pwd
1000
+ end
1001
+ rescue Errno::ENOENT # Rescue non-existing directories.
1002
+ e 'An error occurred. The directory may have been removed or the local'
1003
+ e "filesystem is unavailable. Thus, we will display \"/\" instead.#{N}"
1004
+ i = '/'
1005
+ end
1006
+ i = i.dup if i.frozen?
1007
+ i << '/' unless i.end_with? '/'
1008
+ i.squeeze!('/')
1009
+ @internal_hash[:work_on_this_directory] = i
1010
+ end; alias set_from_which_directory set_work_on_this_directory # === set_from_which_directory
1011
+ alias set_dir set_work_on_this_directory # === set_dir
1012
+ alias set_main_directory set_work_on_this_directory # === set_main_directory
1013
+ alias set_base_directory set_work_on_this_directory # === set_base_directory
1014
+
1015
+ # ========================================================================= #
1016
+ # === yaml_file?
1017
+ # ========================================================================= #
1018
+ def yaml_file?
1019
+ FILE_COLOURS_FOR_BYTES_VALUES
1020
+ end
1021
+
1022
+ # ========================================================================= #
1023
+ # === check_whether_the_colours_defined_in_the_yaml_file_are_valid
1024
+ # ========================================================================= #
1025
+ def check_whether_the_colours_defined_in_the_yaml_file_are_valid
1026
+ _ = @internal_hash[:hash_colours_for_bytes_value]
1027
+ _.each_pair {|key, value| # The entries will be 'KB' and :lightgreen, for instance.
1028
+ if Object.const_defined?(:Colours) and
1029
+ ::Colours.respond_to?(value)
1030
+ else
1031
+ e 'Attention please: the Colours namespace does not have a'
1032
+ e 'colour named `'+value.to_s+'` defined.'
1033
+ e
1034
+ e 'One key (the one named '+key.to_s+') currently has this'
1035
+ e 'value defined, though.'
1036
+ e
1037
+ e 'Please change this by modifying the following file:'
1038
+ e
1039
+ e sfile(" #{yaml_file?}")
1040
+ e
1041
+ e 'To get a listing of all defined colour-names, do this:'
1042
+ e
1043
+ e " require 'colours'"
1044
+ e ' pp Colours.html_colours?'
1045
+ e
1046
+ exit
1047
+ end
1048
+ }
1049
+ end
1050
+
1051
+ # ========================================================================= #
1052
+ # === apply_filter
1053
+ #
1054
+ # Use this method here to apply a filter on the main entries.
1055
+ # ========================================================================= #
1056
+ def apply_filter(i)
1057
+ i = i.dup if i.frozen?
1058
+ i.delete!('*') if i.include? '*'
1059
+ i.delete!('/') if i.include? '/' # This may be incorrect.
1060
+ obtain_entries unless entries?
1061
+ entries?.select! {|entry|
1062
+ entry.include?(i)
1063
+ }
1064
+ entries?
1065
+ end
1066
+
1067
+ # ========================================================================= #
1068
+ # === consider_colourizing_filename
1069
+ # ========================================================================= #
1070
+ def consider_colourizing_filename(filename)
1071
+ result = ''.dup
1072
+ if File.exist? filename
1073
+ filetype = File.ftype(filename)
1074
+ unless show_leading_slash?
1075
+ filename[0,1] = '' if filename.start_with? '/'
1076
+ end
1077
+ # ===================================================================== #
1078
+ # Next, honour the variable @show_full_path if it was set to true.
1079
+ # ===================================================================== #
1080
+ unless show_full_path?
1081
+ filename = File.basename(filename)
1082
+ end
1083
+ case filetype
1084
+ # ===================================================================== #
1085
+ # === link
1086
+ # ===================================================================== #
1087
+ when 'link' # Handle Symlinks here.
1088
+ result = colourize_symlink(filename)
1089
+ # ===================================================================== #
1090
+ # === directory
1091
+ # ===================================================================== #
1092
+ when 'directory'
1093
+ result = colourize_directory(filename) # This is actually sdir().
1094
+ # ===================================================================== #
1095
+ # === file
1096
+ # ===================================================================== #
1097
+ when 'file'
1098
+ result = sfile(filename)
1099
+ end
1100
+ end
1101
+ return result
1102
+ end
1103
+
1104
+ # ========================================================================= #
1105
+ # === do_ignore_backups
1106
+ # ========================================================================= #
1107
+ def do_ignore_backups
1108
+ # ======================================================================= #
1109
+ # Do not list implied entries ending with '~' in the following option.
1110
+ # ======================================================================= #
1111
+ if ignore_backups?
1112
+ directory_content?.ignore_backups
1113
+ end
1114
+ end
1115
+
1116
+ # ========================================================================= #
1117
+ # === consider_sorting_by
1118
+ #
1119
+ # This is the specific sorting-action. The method will delegate onto
1120
+ # class DirectoryContent::ShowDirectoryContent.
1121
+ # ========================================================================= #
1122
+ def consider_sorting_by(
1123
+ i = sort_by?
1124
+ )
1125
+ directory_content?.consider_sorting_by(i)
1126
+ end
1127
+
1128
+ # ========================================================================= #
1129
+ # === shorten_file_elegantly
1130
+ #
1131
+ # We need to take into account that our file could be a directory too.
1132
+ # ========================================================================= #
1133
+ def shorten_file_elegantly(
1134
+ i, threshold = FILE_SIZE_THRESHOLD
1135
+ )
1136
+ i = i.to_s.dup
1137
+ middle_pos = (i.size / 2) - 10
1138
+ oversize = i.size - threshold # The oversize value
1139
+ if oversize > 0
1140
+ start_pos = middle_pos - (oversize / 2)
1141
+ end_pos = start_pos + oversize
1142
+ i[start_pos .. end_pos] = '[… Truncated …]' # This is the padding to use.
1143
+ end
1144
+ return i
1145
+ end
1146
+
1147
+ # ========================================================================= #
1148
+ # === do_not_display_content
1149
+ # ========================================================================= #
1150
+ def do_not_display_content
1151
+ @internal_hash[:display_content] = false # if true, we display() the result.
1152
+ end
1153
+
1154
+ # ========================================================================= #
1155
+ # === do_show_the_content
1156
+ # ========================================================================= #
1157
+ def do_show_the_content
1158
+ @internal_hash[:display_content] = true
1159
+ end; alias do_display_content do_show_the_content # === do_display_content
1160
+
1161
+ # ========================================================================= #
1162
+ # === colourize_symlink
1163
+ #
1164
+ # Use this method to colourize a symlink.
1165
+ # ========================================================================= #
1166
+ def colourize_symlink(i)
1167
+ result = return_name_of_this_symlink(i)
1168
+ # ======================================================================= #
1169
+ # Check whether we will use colours or whether we will not.
1170
+ # ======================================================================= #
1171
+ if File.symlink?(i)
1172
+ if use_colours?
1173
+ if stop_on_missing_symlink? and !File.readlink?(i)
1174
+ opn; e "The symlink for #{i} does not exist."
1175
+ opn; e 'Exiting now, as set per a configuration option.'
1176
+ exit
1177
+ end
1178
+ result = " → #{skyblue(File.readlink(i))}" # Need to readlink on the original variant.
1179
+ else
1180
+ result = " → #{File.readlink(i)}"
1181
+ end
1182
+ end
1183
+ return result
1184
+ end
1185
+
1186
+ # ========================================================================= #
1187
+ # === return_name_of_this_symlink
1188
+ #
1189
+ # File.basename() kills off leading '/', hence we need this method.
1190
+ # ========================================================================= #
1191
+ def return_name_of_this_symlink(i)
1192
+ i = File.basename(i)
1193
+ if return_pwd == '/' and show_leading_slash?
1194
+ i.prepend '/'
1195
+ end
1196
+ return i
1197
+ end
1198
+
1199
+ # ========================================================================= #
1200
+ # === do_show_only_symlinks
1201
+ # ========================================================================= #
1202
+ def do_show_only_symlinks
1203
+ @internal_hash[:show_only_symlinks] = true
1204
+ end
1205
+
1206
+ # ========================================================================= #
1207
+ # === toggle_permission
1208
+ # ========================================================================= #
1209
+ def toggle_permission
1210
+ _ = @internal_hash[:show_condensed_permissions]
1211
+ @internal_hash[:show_condensed_permissions] = !_ # Toggle it here.
1212
+ end
1213
+
1214
+ # ========================================================================= #
1215
+ # === do_not_display_only_directories
1216
+ # ========================================================================= #
1217
+ def do_not_display_only_directories
1218
+ @internal_hash[:display_only_directories] = false # Whether to display only directories or not.
1219
+ end
1220
+
1221
+ # ========================================================================= #
1222
+ # === do_not_show_permissions
1223
+ # ========================================================================= #
1224
+ def do_not_show_permissions
1225
+ @internal_hash[:show_permission_bits] = false
1226
+ end
1227
+
1228
+ # ========================================================================= #
1229
+ # === do_show_header
1230
+ # ========================================================================= #
1231
+ def do_show_header
1232
+ @internal_hash[:show_header] = true
1233
+ end
1234
+
1235
+ # ========================================================================= #
1236
+ # === do_sort_reversed
1237
+ # ========================================================================= #
1238
+ def do_sort_reversed
1239
+ sort_how :reverse
1240
+ end
1241
+
1242
+ # ========================================================================= #
1243
+ # === do_not_show_group_information
1244
+ # ========================================================================= #
1245
+ def do_not_show_group_information
1246
+ @internal_hash[:show_group_information] = false
1247
+ end; alias no_groups do_not_show_group_information # === no_groups
1248
+
1249
+ # ========================================================================= #
1250
+ # === do_not_show_the_owner
1251
+ # ========================================================================= #
1252
+ def do_not_show_the_owner
1253
+ @internal_hash[:show_owner] = false
1254
+ end
1255
+
1256
+ # ========================================================================= #
1257
+ # === do_show_group_information
1258
+ # ========================================================================= #
1259
+ def do_show_group_information
1260
+ @internal_hash[:show_group_information] = true
1261
+ end
1262
+
1263
+ # ========================================================================= #
1264
+ # === do_show_owner
1265
+ # ========================================================================= #
1266
+ def do_show_owner
1267
+ @internal_hash[:show_owner] = true
1268
+ end
1269
+
1270
+ # ========================================================================= #
1271
+ # === do_not_show_leading_slash
1272
+ # ========================================================================= #
1273
+ def do_not_show_leading_slash
1274
+ @internal_hash[:show_leading_slash] = false
1275
+ end
1276
+
1277
+ # ========================================================================= #
1278
+ # === do_show_permissions
1279
+ # ========================================================================= #
1280
+ def do_show_permissions
1281
+ @internal_hash[:show_permission_bits] = true
1282
+ end
1283
+
1284
+ # ========================================================================= #
1285
+ # === only_directories
1286
+ #
1287
+ # Only get directories, with this method.
1288
+ # ========================================================================= #
1289
+ def only_directories
1290
+ @internal_hash[:display_only_directories] = true
1291
+ end
1292
+
1293
+ # ========================================================================= #
1294
+ # === run_then_display
1295
+ # ========================================================================= #
1296
+ def run_then_display
1297
+ run
1298
+ display
1299
+ end; alias gather_then_display_content run_then_display # === gather_then_display_content
1300
+
1301
+ # ========================================================================= #
1302
+ # === show_header?
1303
+ #
1304
+ # Whether we will show the header or whether we will not. That header
1305
+ # is not typical for the normal GNU "ls" command, hence why we need
1306
+ # to have such an option.
1307
+ # ========================================================================= #
1308
+ def show_header?
1309
+ @internal_hash[:show_header]
1310
+ end
1311
+
1312
+ # ========================================================================= #
1313
+ # === colourize_kb_and_mb?
1314
+ # ========================================================================= #
1315
+ def colourize_kb_and_mb?
1316
+ @internal_hash[:colourize_kb_and_mb]
1317
+ end
1318
+
1319
+ # ========================================================================= #
1320
+ # === stop_on_missing_symlink?
1321
+ # ========================================================================= #
1322
+ def stop_on_missing_symlink?
1323
+ @internal_hash[:stop_on_missing_symlink]
1324
+ end
1325
+
1326
+ # ========================================================================= #
1327
+ # === show_condensed_permissions?
1328
+ # ========================================================================= #
1329
+ def show_condensed_permissions?
1330
+ @internal_hash[:show_condensed_permissions]
1331
+ end; alias condensed? show_condensed_permissions? # === condensed?
1332
+
1333
+ # ========================================================================= #
1334
+ # === owner?
1335
+ # ========================================================================= #
1336
+ def owner?(i)
1337
+ @internal_hash[:directory_content].owner?(i)
1338
+ end
1339
+
1340
+ # ========================================================================= #
1341
+ # === truncate_long_file_names?
1342
+ # ========================================================================= #
1343
+ def truncate_long_file_names?
1344
+ @internal_hash[:truncate_long_file_names]
1345
+ end
1346
+
1347
+ # ========================================================================= #
1348
+ # === show_modification_time?
1349
+ # ========================================================================= #
1350
+ def show_modification_time?
1351
+ @internal_hash[:show_modification_time]
1352
+ end
1353
+
1354
+ # ========================================================================= #
1355
+ # === show_leading_slash?
1356
+ # ========================================================================= #
1357
+ def show_leading_slash?
1358
+ @internal_hash[:show_leading_slash]
1359
+ end
1360
+
1361
+ # ========================================================================= #
1362
+ # === colour_for_files?
1363
+ # ========================================================================= #
1364
+ def colour_for_files?
1365
+ @internal_hash[:colour_for_files]
1366
+ end
1367
+
1368
+ # ========================================================================= #
1369
+ # === colour_for_symlinks?
1370
+ # ========================================================================= #
1371
+ def colour_for_symlinks?
1372
+ @internal_hash[:colour_for_symlinks]
1373
+ end
1374
+
1375
+ # ========================================================================= #
1376
+ # === colour_for_directories?
1377
+ # ========================================================================= #
1378
+ def colour_for_directories?
1379
+ @internal_hash[:colour_for_directories]
1380
+ end
1381
+
1382
+ # ========================================================================= #
1383
+ # === show_simplified?
1384
+ # ========================================================================= #
1385
+ def show_simplified?
1386
+ @internal_hash[:show_simplified]
1387
+ end
1388
+
1389
+ # ========================================================================= #
1390
+ # === do_sort_by_size
1391
+ # ========================================================================= #
1392
+ def do_sort_by_size
1393
+ sort_how :by_size
1394
+ end
1395
+
1396
+ # ========================================================================= #
1397
+ # === consider_sorting
1398
+ # ========================================================================= #
1399
+ def consider_sorting
1400
+ if sort_by?
1401
+ @internal_hash[:directory_content].do_sort(sort_by?)
1402
+ end
1403
+ end
1404
+
1405
+ # ========================================================================= #
1406
+ # === show_content_for
1407
+ # ========================================================================= #
1408
+ def show_content_for(
1409
+ this_path = '/'
1410
+ )
1411
+ @directory_content.obtain_entries(this_path)
1412
+ report
1413
+ end
1414
+
1415
+ # ========================================================================= #
1416
+ # === report (report tag, display tag, d tag)
1417
+ #
1418
+ # This method will always report the main dataset, as determined by
1419
+ # class DirectoryParadise::Content.
1420
+ # ========================================================================= #
1421
+ def report(
1422
+ _ = return_directory_content # This will return a Hash.
1423
+ )
1424
+ result?.clear # Clear the old results.
1425
+ @internal_hash[:total_filesize] = 0 # Reset it always here.
1426
+ show_condensed_permissions = show_condensed_permissions?
1427
+ show_the_index = show_index?
1428
+ show_hidden_files = show_hidden_files?
1429
+ show_permission_bits = show_permission_bits?
1430
+ colourize_path = colourize_path?
1431
+ display_only_directories = display_only_directories?
1432
+ show_only_symlinks = show_only_symlinks?
1433
+ show_modification_time = show_modification_time?
1434
+ if display_only_directories
1435
+ _.select! {|path, inner_hash| inner_hash[:type] == 'directory' }
1436
+ end
1437
+ if show_only_symlinks
1438
+ _.select! {|path, inner_hash| inner_hash[:type] == 'link' }
1439
+ end
1440
+ # ======================================================================= #
1441
+ # === Build up the header (header tag)
1442
+ #
1443
+ # Build up the header first, then display it, but only if the user
1444
+ # allows this to happen:
1445
+ # ======================================================================= #
1446
+ if show_header?
1447
+ if show_statistics?
1448
+ n_files = n_files?
1449
+ n_directories = n_directories?
1450
+ n_symlinks = n_symlinks?
1451
+ file_string = ''
1452
+ file_string = 's' if (n_files >= 0) # plural or not.
1453
+ # =================================================================== #
1454
+ # Pluralize the directory-string.
1455
+ # =================================================================== #
1456
+ directory_string = 'y'
1457
+ symlink_string = 's'
1458
+ symlink_string = '' if n_symlinks == 1
1459
+ # =================================================================== #
1460
+ # Last but not least notify the user of the findings.
1461
+ # =================================================================== #
1462
+ cmd = "#{rev}The directory #{sdir(work_on_which_directory?)}"
1463
+ append(
1464
+ "The directory #{work_on_which_directory?}"
1465
+ )
1466
+ e cmd
1467
+ cmd = "#{rev}contains "+
1468
+ sfile(
1469
+ n_files.to_s+' file'+file_string
1470
+ )+', '+
1471
+ sdir(
1472
+ n_directories.to_s+' director'+directory_string
1473
+ )+' and '+
1474
+ ssymlink(
1475
+ n_symlinks.to_s+' symlink'+symlink_string
1476
+ )+'.'
1477
+ append("contains "+
1478
+ n_files.to_s+' file'+file_string+
1479
+ ', '+
1480
+ n_directories.to_s+' director'+directory_string+
1481
+ ' and '+
1482
+ n_symlinks.to_s+' symlink'+symlink_string+
1483
+ '.'
1484
+ )
1485
+ e cmd
1486
+ end
1487
+ header = ''.dup
1488
+ header << "#{rev}" if use_colours?
1489
+ append(:liner)
1490
+ # ===================================================================== #
1491
+ # Add the index next onto the header.
1492
+ # ===================================================================== #
1493
+ if show_the_index
1494
+ padded = 'Index'.ljust(6)
1495
+ header << sfancy(padded)
1496
+ raw_append(padded)
1497
+ end
1498
+ # ===================================================================== #
1499
+ # Show permission bits
1500
+ # ===================================================================== #
1501
+ if show_permission_bits
1502
+ permissions = 'Permissions'.ljust(16)
1503
+ header << springgreen(permissions)
1504
+ raw_append(permissions)
1505
+ end
1506
+ # ===================================================================== #
1507
+ # Next, determine whether we will display the modification-time
1508
+ # or whether we will not:
1509
+ # ===================================================================== #
1510
+ if show_modification_time
1511
+ modtime = 'ModTime'.ljust(8)
1512
+ raw_append(modtime)
1513
+ header << modtime
1514
+ end
1515
+ header << 'FileSize'.ljust(9)
1516
+ raw_append('FileSize'.ljust(9))
1517
+ header << 'Name'.ljust(5)
1518
+ append('Name'.ljust(5))
1519
+ eliner
1520
+ append(:liner)
1521
+ e header
1522
+ end
1523
+ # ======================================================================= #
1524
+ # This may look like so:
1525
+ #
1526
+ # "/Depot/j/yodel.rb"=>
1527
+ # {:size=>129,ei leader is lined up against a w
1528
+ # :type=>"file",
1529
+ # :is_executable=>false,
1530
+ # :path=>"/Depot/j/yodel.rb",
1531
+ # :ascii_representation=>"",
1532
+ # :last_modified=>2021-05-31 20:55:53.826658499 +0000,
1533
+ # :gid=>0,
1534
+ # :inode_number=>93858692,
1535
+ # :uid=>0,
1536
+ # :access_mode=>"644"}}}>
1537
+ #
1538
+ # ======================================================================= #
1539
+ index = 0
1540
+ _.each_pair {|path, inner_hash| # each tag
1541
+ if path.end_with?('.') and !show_dots_only_entries? and
1542
+ ( (path.end_with? '.') or (path.end_with? '..') )
1543
+ next
1544
+ end
1545
+ if File.basename(path).start_with?('.') and !show_hidden_files
1546
+ # =================================================================== #
1547
+ # Ignore hidden files if the user specified so.
1548
+ # =================================================================== #
1549
+ next
1550
+ end
1551
+ index += 1
1552
+ # ===================================================================== #
1553
+ # Obtain the file size next, then add it onto the :total_filesize
1554
+ # entry in the Hash.
1555
+ # ===================================================================== #
1556
+ file_size = inner_hash[:size]
1557
+ name_of_owner = inner_hash[:name_of_owner]
1558
+ name_of_owner = '%-10s' % owner?(name_of_owner) # Beautify this.
1559
+ name_of_owner.strip!
1560
+ @internal_hash[:total_filesize] += file_size.to_i # .to_i to avoid pesky nil values.
1561
+ # ===================================================================== #
1562
+ # Make the filesize human readable.
1563
+ # ===================================================================== #
1564
+ file_size = to_human_readable(file_size)
1565
+ last_modified = inner_hash[:last_modified] # Example: 2021-01-11 05:53:35.844402868 +0000,
1566
+ # ===================================================================== #
1567
+ # All the output will be stored onto the variable called "line".
1568
+ # ===================================================================== #
1569
+ line = ''.dup
1570
+ # ===================================================================== #
1571
+ # === Index (index tag)
1572
+ #
1573
+ # Show the index next, if the user has enabled this.
1574
+ # ===================================================================== #
1575
+ if show_the_index
1576
+ use_this_value_for_ljusting = 6
1577
+ if _.size.to_s.size > 3
1578
+ use_this_value_for_ljusting += (_.size.to_s.size - 3)
1579
+ end
1580
+ index_to_display = ("(#{index})") # Add () here as well.
1581
+ index_to_display = index_to_display.ljust(use_this_value_for_ljusting)
1582
+ raw_append(index_to_display)
1583
+ if use_colours?
1584
+ index_to_display.gsub!(/\((.+)\)/, '('+sfancy("\\1")+')')
1585
+ end
1586
+ line << index_to_display
1587
+ end
1588
+ # ===================================================================== #
1589
+ # Add the permissions next - either in good old classical number
1590
+ # format, such as 755, or as its ascii_representation.
1591
+ # ===================================================================== #
1592
+ if show_permission_bits
1593
+ # =================================================================== #
1594
+ # We update the inode-entry next to the hash.
1595
+ # =================================================================== #
1596
+ if show_inode?
1597
+ inode = ' Inode: '
1598
+ raw_append(inode)
1599
+ inode = simp(inode) if use_colours?
1600
+ line << inode+File.stat(path).ino.to_s # Add the Inode here.
1601
+ append(File.stat(path).ino.to_s)
1602
+ end
1603
+ # =================================================================== #
1604
+ # Next show the permission bits, if requested to do so.
1605
+ # =================================================================== #
1606
+ raw_append("#{return_chmod_value_of_this_file(path).ljust(5)}")
1607
+ line << springgreen("#{return_chmod_value_of_this_file(path).ljust(5)}")
1608
+ unless show_condensed_permissions
1609
+ raw_append(
1610
+ return_file_permission_of(
1611
+ inner_hash[:ascii_representation]
1612
+ ).ljust(11)
1613
+ )
1614
+ line << return_file_permission_of(
1615
+ inner_hash[:ascii_representation]
1616
+ ).ljust(11)
1617
+ end
1618
+ end
1619
+ # ===================================================================== #
1620
+ # === Show the modification time
1621
+ #
1622
+ # Add the last-modified part next.
1623
+ #
1624
+ # This could be last_modified.strftime(' %d.%m.%Y %H:%M') instead.
1625
+ # ===================================================================== #
1626
+ if show_modification_time?
1627
+ raw_append("#{return_assumed_modification_time(last_modified)} ")
1628
+ line << "#{return_assumed_modification_time(last_modified)} "
1629
+ end
1630
+ original_file_size_as_string = file_size.rjust(8)
1631
+ file_size = original_file_size_as_string.dup
1632
+ # ===================================================================== #
1633
+ # Insert proper units next.
1634
+ # ===================================================================== #
1635
+ if colourize_kb_and_mb? && use_colours? # use .gsub directly
1636
+ hash = hash_colours_for_bytes_value?
1637
+ if file_size.include? ' KB'
1638
+ file_size.sub!(/KB/, send(hash['KB'].to_sym, 'KB'))
1639
+ elsif file_size.include? ' MB'
1640
+ file_size.sub!(/MB/, send(hash['MB'].to_sym, 'MB'))
1641
+ elsif file_size.include? 'GB'
1642
+ file_size.sub!(/GB/, send(hash['GB'].to_sym, 'GB'))
1643
+ elsif file_size.include? ' B'
1644
+ file_size.sub!(/ B/, send(hash['B'].to_sym, ' B'))
1645
+ end
1646
+ end
1647
+ # ===================================================================== #
1648
+ # === Show the Owner
1649
+ #
1650
+ # Add the owner. Must be padded, like the group name, in order to
1651
+ # prevent mis-alignments.
1652
+ # ===================================================================== #
1653
+ unless show_simplified?
1654
+ raw_append(" #{name_of_owner.ljust(6)}")
1655
+ line << " #{name_of_owner.ljust(6)}"
1656
+ end
1657
+ # ===================================================================== #
1658
+ # === Show the Group Information
1659
+ #
1660
+ # Add the group, if we allow that.
1661
+ # ===================================================================== #
1662
+ if show_group_information?
1663
+ if inner_hash and inner_hash[:name_of_group]
1664
+ raw_append(" #{inner_hash[:name_of_group].ljust(7)}")
1665
+ line << " #{inner_hash[:name_of_group].ljust(7)}"
1666
+ end
1667
+ end
1668
+ # ===================================================================== #
1669
+ # Add the file size next:
1670
+ # ===================================================================== #
1671
+ raw_append(" #{original_file_size_as_string}")
1672
+ line << " #{file_size}"
1673
+ # ===================================================================== #
1674
+ # === Truncate long file names
1675
+ #
1676
+ # We have to check whether the filename is too long. If it is then
1677
+ # we will modify a call to .shorten_file_elegantly() but only if
1678
+ # the user has requested this option.
1679
+ #
1680
+ # Directories will be handled in a special manner.
1681
+ # ===================================================================== #
1682
+ if (path.size > FILE_SIZE_THRESHOLD) and truncate_long_file_names?
1683
+ # dirname = File.dirname(path)
1684
+ # basename = File.basename(path)[0 .. FILE_SIZE_THRESHOLD]
1685
+ # path = dirname+'/'+basename
1686
+ path = shorten_file_elegantly(path)
1687
+ end
1688
+ unless show_full_path?
1689
+ path = File.basename(path)
1690
+ end
1691
+ if File.directory?(path) and !path.end_with?('/')
1692
+ path << '/'
1693
+ end
1694
+ raw_append(" #{path}")
1695
+ line << " #{steelblue(path)}" # ← Add the filename here.
1696
+ if colourize_path and path.include?('/')
1697
+ splitted = path.split('/')
1698
+ # =================================================================== #
1699
+ # The last part can be a directory, symlink or file, typically.
1700
+ # =================================================================== #
1701
+ last = splitted.last
1702
+ if File.directory? last
1703
+ last << '/' unless last.end_with?('/')
1704
+ end
1705
+ raw_append(splitted[0..-2].join('/')+'/'+last)
1706
+ last = lightblue(last)
1707
+ path = splitted[0..-2].join('/')+'/'+last
1708
+ end
1709
+ if File.symlink?(path)
1710
+ if stop_on_missing_symlink? # add functionality to stop on a missing symlink.
1711
+ if !File.exist?(File.readlink(path))
1712
+ add ' ← FILE DOES NOT EXIST!!! STOPPING NOW!'
1713
+ exit
1714
+ else # ok symlink exists.
1715
+ # e ' IS A SYMLINK '
1716
+ end
1717
+ else
1718
+ line << colourize_symlink(path)
1719
+ end
1720
+ end
1721
+ append_newline
1722
+ # ===================================================================== #
1723
+ # Finally display the line to the user.
1724
+ # ===================================================================== #
1725
+ e line
1726
+ }
1727
+ append(:liner)
1728
+ eliner
1729
+ # ======================================================================= #
1730
+ # Only add the filesize if we will report it.
1731
+ # ======================================================================= #
1732
+ try_to_report_the_total_filesize if report_the_total_filesize?
1733
+ end; alias show report # === show
1734
+ alias display report # === display
1735
+ alias display_result report # === display_result
1736
+ alias display_content report # === display_content
1737
+ alias display_listing report # === display_listing
1738
+ alias do_report report # === do_report
1739
+ alias display_main_string report # === display_main_string
1740
+
1741
+ # ========================================================================= #
1742
+ # === result?
1743
+ #
1744
+ # This will show the string that can be displayed.
1745
+ # ========================================================================= #
1746
+ def result?
1747
+ @internal_hash[:result]
1748
+ end
1749
+
1750
+ # ========================================================================= #
1751
+ # === string?
1752
+ # ========================================================================= #
1753
+ def string?
1754
+ result?.to_s
1755
+ end; alias text? string? # === text?
1756
+
1757
+ # ========================================================================= #
1758
+ # === append_to_the_result
1759
+ #
1760
+ # This currently always appends with a newline as well.
1761
+ # ========================================================================= #
1762
+ def append_to_the_result(i)
1763
+ case i
1764
+ # ======================================================================= #
1765
+ # === :liner
1766
+ # ======================================================================= #
1767
+ when :liner,
1768
+ :cliner
1769
+ i = '―' * 80
1770
+ end
1771
+ @internal_hash[:result] << "#{i}\n"
1772
+ end; alias append append_to_the_result # === append
1773
+
1774
+ # ========================================================================= #
1775
+ # === append_newline
1776
+ # ========================================================================= #
1777
+ def append_newline
1778
+ @internal_hash[:result] << "\n"
1779
+ end
1780
+
1781
+ # ========================================================================= #
1782
+ # === raw_append
1783
+ # ========================================================================= #
1784
+ def raw_append(i)
1785
+ @internal_hash[:result] << i
1786
+ end
1787
+
1788
+ # ========================================================================= #
1789
+ # === rev
1790
+ #
1791
+ # The rev value can be re-defined by the user of this class.
1792
+ # ========================================================================= #
1793
+ def rev
1794
+ @internal_hash[:default_colour]
1795
+ end
1796
+
1797
+ # ========================================================================= #
1798
+ # === define_colours
1799
+ #
1800
+ # define_colours() is called from reset()
1801
+ # ========================================================================= #
1802
+ def define_colours(
1803
+ optional_hash_use_these_colours = nil
1804
+ ) # Colours tag, colors tag.
1805
+ _ = optional_hash_use_these_colours
1806
+ if use_colours?
1807
+ if _ # If the Hash is defined, enter here.
1808
+ pp 111
1809
+ @internal_hash[:default_colour] = Colours.beautiful _['colour_for_normal']
1810
+ @internal_hash[:colour_for_files] = Colours.beautiful _['colour_for_files']
1811
+ @internal_hash[:colour_for_symlinks] = Colours.beautiful _['colour_for_symlinks']
1812
+ @internal_hash[:colour_for_directories] = Colours.beautiful _['colour_for_directories']
1813
+ else # Try to use Konsole after this point.
1814
+ if Object.const_defined? :Colours
1815
+ pp 222
1816
+ @internal_hash[:default_colour] = Colours.restore? # Restore the default again.
1817
+ @internal_hash[:colour_for_files] = Colours.springgreen
1818
+ @internal_hash[:colour_for_symlinks] = Colours.slateblue
1819
+ @internal_hash[:colour_for_directories] = Colours.paleturquoise
1820
+ else # Else, use the default colours. They are defined in the module Colours.
1821
+ pp 333
1822
+ @internal_hash[:default_colour] = Colours::WHITE
1823
+ @internal_hash[:colour_for_files] = Colours::GREEN
1824
+ @internal_hash[:colour_for_symlinks] = Colours::BLUE
1825
+ @internal_hash[:colour_for_directories] = Colours::CYAN # Which colour to use for directories.
1826
+ end
1827
+ end
1828
+ end
1829
+ end
1830
+
1831
+ # ========================================================================= #
1832
+ # === run (run tag)
1833
+ # ========================================================================= #
1834
+ def run
1835
+ check_whether_we_use_colours
1836
+ menu
1837
+ report if display_content?
1838
+ do_display_content # Re-toggle it again after run() is used.
1839
+ end
1840
+
1841
+ end
1842
+
1843
+ # =========================================================================== #
1844
+ # === DirectoryParadise.show
1845
+ # =========================================================================== #
1846
+ def self.show(i = ARGV)
1847
+ DirectoryParadise::Report.new(i)
1848
+ end; self.instance_eval { alias [] show } # === DirectoryParadise[]
1849
+ self.instance_eval { alias run show } # === DirectoryParadise.run
1850
+ self.instance_eval { alias do_show show } # === DirectoryParadise.do_show
1851
+ self.instance_eval { alias report show } # === DirectoryParadise.report
1852
+
1853
+ # =========================================================================== #
1854
+ # === DirectoryParadise.new
1855
+ # =========================================================================== #
1856
+ def self.new(
1857
+ work_on_this_directory = return_pwd,
1858
+ run_already = true
1859
+ )
1860
+ DirectoryParadise.new(work_on_this_directory, run_already)
1861
+ end
1862
+
1863
+ end
1864
+
1865
+ if __FILE__ == $PROGRAM_NAME
1866
+ alias e puts
1867
+ _ = DirectoryParadise.report(ARGV)
1868
+ #pp _
1869
+ # _.print_the_namespace
1870
+ e
1871
+ e 'The raw string is:'
1872
+ e
1873
+ e _.result?
1874
+ end # rcontent