glark 1.9.0 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. data/bin/glark +2 -2
  2. data/lib/glark.rb +1 -1
  3. data/lib/glark/app/app.rb +48 -0
  4. data/lib/glark/app/help.rb +103 -0
  5. data/lib/glark/app/info/options.rb +84 -0
  6. data/lib/glark/app/options.rb +201 -0
  7. data/lib/glark/app/rcfile.rb +49 -0
  8. data/lib/glark/app/runner.rb +140 -0
  9. data/lib/glark/app/spec.rb +19 -0
  10. data/lib/glark/input/filter/criteria_opts.rb +41 -0
  11. data/lib/glark/input/filter/dir_criteria_opts.rb +37 -0
  12. data/lib/glark/input/filter/file_criteria_opts.rb +33 -0
  13. data/lib/glark/input/filter/filter.rb +59 -0
  14. data/lib/glark/input/filter/options.rb +171 -0
  15. data/lib/glark/input/options.rb +120 -0
  16. data/lib/glark/input/range.rb +104 -0
  17. data/lib/glark/input/spec.rb +39 -0
  18. data/lib/glark/io/file/archive_file.rb +64 -0
  19. data/lib/glark/io/file/binary_file.rb +19 -0
  20. data/lib/glark/io/file/file.rb +57 -0
  21. data/lib/glark/io/file/gz_file.rb +21 -0
  22. data/lib/glark/io/file/tar_file.rb +35 -0
  23. data/lib/glark/io/file/tar_gz_file.rb +52 -0
  24. data/lib/glark/io/file/zip_file.rb +39 -0
  25. data/lib/glark/match/and.rb +83 -0
  26. data/lib/glark/match/and_distance.rb +58 -0
  27. data/lib/glark/match/compound.rb +34 -0
  28. data/lib/glark/match/expression.rb +63 -0
  29. data/lib/glark/match/factory.rb +173 -0
  30. data/lib/glark/match/ior.rb +20 -0
  31. data/lib/glark/match/options.rb +74 -0
  32. data/lib/glark/match/or.rb +41 -0
  33. data/lib/glark/match/re.rb +81 -0
  34. data/lib/glark/match/re_factory.rb +44 -0
  35. data/lib/glark/match/spec.rb +59 -0
  36. data/lib/glark/match/xor.rb +20 -0
  37. data/lib/glark/output/binary_file_summary.rb +17 -0
  38. data/lib/glark/output/common.rb +43 -0
  39. data/lib/glark/output/context.rb +57 -0
  40. data/lib/glark/output/count.rb +26 -0
  41. data/lib/glark/output/file_header.rb +20 -0
  42. data/lib/glark/output/file_name_only.rb +35 -0
  43. data/lib/glark/output/formatted.rb +22 -0
  44. data/lib/glark/output/glark_count.rb +23 -0
  45. data/lib/glark/output/glark_format.rb +62 -0
  46. data/lib/glark/output/glark_lines.rb +36 -0
  47. data/lib/glark/output/grep_count.rb +18 -0
  48. data/lib/glark/output/grep_lines.rb +42 -0
  49. data/lib/glark/output/line_status.rb +46 -0
  50. data/lib/glark/output/lines.rb +100 -0
  51. data/lib/glark/output/match_list.rb +15 -0
  52. data/lib/glark/output/options.rb +103 -0
  53. data/lib/glark/output/results.rb +23 -0
  54. data/lib/glark/output/spec.rb +105 -0
  55. data/lib/glark/output/unfiltered_lines.rb +28 -0
  56. data/lib/glark/util/colors/options.rb +143 -0
  57. data/lib/glark/util/colors/spec.rb +21 -0
  58. data/lib/glark/util/highlight.rb +108 -0
  59. data/lib/glark/util/io/depth.rb +29 -0
  60. data/lib/glark/util/io/fileset.rb +162 -0
  61. data/lib/glark/util/io/filter/criteria.rb +49 -0
  62. data/lib/glark/util/io/filter/filter.rb +10 -0
  63. data/lib/glark/util/io/lines.rb +117 -0
  64. data/lib/glark/util/option.rb +34 -0
  65. data/lib/glark/util/options.rb +12 -0
  66. data/lib/glark/util/optutil.rb +69 -0
  67. data/lib/glark/util/timestamper.rb +18 -0
  68. data/man/glark.1 +1134 -0
  69. data/test/glark/app/and_test.rb +82 -0
  70. data/test/glark/app/compound_test.rb +33 -0
  71. data/test/glark/app/context_test.rb +43 -0
  72. data/test/glark/app/count_test.rb +89 -0
  73. data/test/glark/app/dump_test.rb +16 -0
  74. data/test/glark/app/expression_file_test.rb +22 -0
  75. data/test/glark/app/extended_regexp_test.rb +17 -0
  76. data/test/glark/app/extract_matches_test.rb +27 -0
  77. data/test/glark/app/files_with_match_test.rb +32 -0
  78. data/test/glark/app/files_without_match_test.rb +26 -0
  79. data/test/glark/app/filter_test.rb +118 -0
  80. data/test/glark/app/highlight_test.rb +78 -0
  81. data/test/glark/app/ignore_case_test.rb +22 -0
  82. data/test/glark/app/invert_test.rb +49 -0
  83. data/test/glark/app/ior_test.rb +21 -0
  84. data/test/glark/app/label_test.rb +28 -0
  85. data/test/glark/app/line_number_color_test.rb +42 -0
  86. data/test/glark/app/line_numbers_test.rb +42 -0
  87. data/test/glark/app/match_limit_test.rb +49 -0
  88. data/test/glark/app/options_test.rb +722 -0
  89. data/test/glark/app/range_test.rb +101 -0
  90. data/test/glark/app/rcfile_test.rb +113 -0
  91. data/test/glark/app/record_separator_test.rb +32 -0
  92. data/test/glark/app/regexp_test.rb +48 -0
  93. data/test/glark/app/tc.rb +92 -0
  94. data/test/glark/app/text_color_test.rb +31 -0
  95. data/test/glark/app/whole_lines_test.rb +17 -0
  96. data/test/glark/app/whole_words_test.rb +42 -0
  97. data/test/glark/app/xor_test.rb +19 -0
  98. data/test/glark/input/binary_file_test.rb +0 -0
  99. data/test/glark/input/directory_test.rb +202 -0
  100. data/test/glark/input/dirname_test.rb +69 -0
  101. data/test/glark/input/exclude_matching_test.rb +20 -0
  102. data/test/glark/input/ext_test.rb +65 -0
  103. data/test/glark/input/filter/criteria_test.rb +91 -0
  104. data/test/glark/input/filter/filter_spec_test.rb +27 -0
  105. data/test/glark/input/filter/filter_test.rb +21 -0
  106. data/test/glark/input/name_test.rb +75 -0
  107. data/test/glark/input/path_test.rb +72 -0
  108. data/test/glark/input/range_test.rb +82 -0
  109. data/test/glark/input/size_limit_test.rb +51 -0
  110. data/test/glark/input/split_as_path_test.rb +28 -0
  111. data/test/glark/match_test.rb +192 -0
  112. data/test/glark/resources.rb +21 -0
  113. data/test/glark/tc.rb +37 -0
  114. data/test/resources/add.rb +10 -0
  115. data/test/resources/echo.rb +2 -0
  116. data/test/resources/greet.rb +13 -0
  117. metadata +198 -28
  118. data/README +0 -0
  119. data/bin/jlark +0 -63
  120. data/lib/glark/expression.rb +0 -440
  121. data/lib/glark/exprfactory.rb +0 -248
  122. data/lib/glark/glark.rb +0 -297
  123. data/lib/glark/help.rb +0 -85
  124. data/lib/glark/input.rb +0 -183
  125. data/lib/glark/options.rb +0 -757
  126. data/lib/glark/output.rb +0 -266
  127. data/test/lib/glark/glark_test.rb +0 -317
  128. data/test/lib/glark/options_test.rb +0 -891
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ require 'glark/output/common'
6
+
7
+ class Count < Common
8
+ def display_matches?
9
+ false
10
+ end
11
+
12
+ def write_count ct
13
+ if @show_file_name
14
+ print_file_name
15
+ end
16
+ print_count ct
17
+ end
18
+
19
+ def process_end lnum
20
+ if @invert_match
21
+ write_count @file.get_lines.size - @count
22
+ elsif matched?
23
+ write_count @count
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ require 'glark/util/highlight'
6
+
7
+ class FileHeader
8
+ include Loggable, Highlight
9
+
10
+ def initialize name, highlighter
11
+ @name = name
12
+ @highlighter = highlighter
13
+ end
14
+
15
+ def print out
16
+ name = @name.to_s
17
+ fname = @highlighter ? adorn(@highlighter, name) : name
18
+ out.puts fname
19
+ end
20
+ end
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ require 'glark/output/common'
6
+
7
+ class FileNameOnly < Common
8
+ def initialize fname, spec
9
+ super
10
+ @write_null = spec.write_null
11
+ end
12
+
13
+ def at_match_limit?
14
+ @count > 0
15
+ end
16
+
17
+ def display_matches?
18
+ false
19
+ end
20
+
21
+ def print_file_name
22
+ if @write_null
23
+ @out.print @file.fname
24
+ @out.print "\0"
25
+ else
26
+ @out.puts @file.fname
27
+ end
28
+ end
29
+
30
+ def process_end lnum
31
+ if matched? != @invert_match
32
+ print_file_name
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ # Output that has (possibly) been reformatted, i.e., highlighting of regular
6
+ # expressions.
7
+
8
+ require 'glark/io/file/file'
9
+ require 'glark/output/results'
10
+
11
+ class Formatted < Results
12
+ attr_reader :formatted
13
+
14
+ def initialize
15
+ super
16
+ @formatted = []
17
+ end
18
+
19
+ def get_line_to_print lnum
20
+ @formatted[lnum] || @file.get_line(lnum)
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ require 'glark/output/count'
6
+
7
+ module Glark
8
+ class Count < ::Count
9
+ def initialize fname, spec
10
+ super
11
+ @fname_highlighter = spec.highlight && spec.file_highlight
12
+ end
13
+
14
+ def print_file_name
15
+ file_header = FileHeader.new displayed_name, @fname_highlighter
16
+ file_header.print @out
17
+ end
18
+
19
+ def print_count ct
20
+ @out.puts " " + ct.to_s
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ require 'glark/output/file_header'
6
+ require 'glark/util/highlight'
7
+
8
+ module Glark; end
9
+
10
+ module Glark
11
+ module Format
12
+ include Loggable, Highlight
13
+
14
+ def initialize file, spec
15
+ @file_header = nil # not nil after file header written
16
+ @fname_highlighter = spec.highlight && spec.file_highlight
17
+ @lnum_highlighter = spec.line_number_highlight
18
+
19
+ super
20
+ end
21
+
22
+ def show_file_header
23
+ if @show_file_name && @file_header.nil?
24
+ @file_header = FileHeader.new displayed_name, @fname_highlighter
25
+ @file_header.print @out
26
+ end
27
+ end
28
+
29
+ def print_line_number lnum
30
+ if @lnum_highlighter
31
+ lnumstr = (lnum + 1).to_s
32
+ pad = " " * ([4 - lnumstr.length, 0].max)
33
+ @out.print pad + " " + adorn(@lnum_highlighter, lnumstr) + " "
34
+ else
35
+ super
36
+ end
37
+ end
38
+
39
+ def println ln, ch
40
+ if @show_line_numbers
41
+ print_line_number ln
42
+ end
43
+
44
+ if ch && @print_context
45
+ @out.printf "%s ", ch
46
+ end
47
+
48
+ line = get_line_to_print ln
49
+ @out.puts line
50
+ end
51
+
52
+ # prints the line, and adjusts for the fact that in our world, lines are
53
+ # 0-indexed, whereas they are displayed as if 1-indexed.
54
+ def print_line lnum, ch = nil
55
+ lnums = @file.get_region lnum
56
+ return unless lnums
57
+ lnums.each do |ln|
58
+ println ln, ch
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ require 'glark/output/lines'
6
+ require 'glark/output/glark_format'
7
+
8
+ # -------------------------------------------------------
9
+ # Glark output format
10
+ # -------------------------------------------------------
11
+
12
+ module Glark
13
+ class Lines < ::Lines
14
+ include Format
15
+
16
+ def initialize file, spec
17
+ super
18
+ @print_context = @after != 0 || @before != 0
19
+ end
20
+
21
+ def write_matching from, to
22
+ show_file_header
23
+ super
24
+ end
25
+
26
+ def write_nonmatching from, to
27
+ show_file_header
28
+ super
29
+ end
30
+
31
+ def add_match startline, endline
32
+ super
33
+ set_status startline, endline
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ require 'glark/output/count'
6
+
7
+ module Grep
8
+ class Count < ::Count
9
+ def print_file_name
10
+ fname = displayed_name
11
+ @out.print fname, ":"
12
+ end
13
+
14
+ def print_count ct
15
+ @out.puts ct
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ require 'glark/output/lines'
6
+
7
+ # -------------------------------------------------------
8
+ # Grep output format
9
+ # -------------------------------------------------------
10
+
11
+ module Grep
12
+ # This matches legacy grep, mostly. It is for running within emacs, thus, it
13
+ # does not support context or highlighting.
14
+ class Lines < ::Lines
15
+ # prints the line, and adjusts for the fact that in our world, lines are
16
+ # 0-indexed, whereas they are displayed as if 1-indexed.
17
+ def print_line lnum, ch = nil
18
+ ln = get_line_to_print lnum
19
+ return unless ln
20
+
21
+ print_file_name
22
+ if @show_line_numbers
23
+ @out.printf "%d: ", lnum + 1
24
+ end
25
+
26
+ @out.print ln
27
+ end
28
+
29
+ def print_file_name
30
+ if @show_file_name
31
+ @out.print displayed_name, ":"
32
+ end
33
+ end
34
+
35
+ def add_match startline, endline
36
+ super
37
+ # even with multi-line matches (--and expressions), we'll display
38
+ # only the first matching line, not the range between the matches.
39
+ set_status startline, startline
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ module Glark
5
+ class LineStatus
6
+ WRITTEN = Object.new
7
+ PRE_MATCH = '-'
8
+ MATCH = ':'
9
+ POST_MATCH = '+'
10
+
11
+ def initialize
12
+ @stati = Array.new
13
+ end
14
+
15
+ def is_written? lnum
16
+ @stati[lnum] == WRITTEN
17
+ end
18
+
19
+ def set_as_written lnum
20
+ @stati[lnum] = WRITTEN
21
+ end
22
+
23
+ def char lnum
24
+ @stati[lnum]
25
+ end
26
+
27
+ def set_status from, to, status
28
+ from.upto(to) do |lnum|
29
+ update_status lnum, status
30
+ end
31
+ end
32
+
33
+ def update_status lnum, status
34
+ if @stati[lnum].nil? || (status == MATCH && @stati[lnum] != WRITTEN)
35
+ @stati[lnum] = status
36
+ end
37
+ end
38
+
39
+ def set_match pre_match_start, match_start, match_end, post_match_end
40
+ start = [0, pre_match_start].max
41
+ set_status start, match_start - 1, PRE_MATCH
42
+ set_status match_start, match_end, MATCH
43
+ set_status match_end + 1, post_match_end, POST_MATCH
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,100 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ # Output for displayed lines.
6
+
7
+ require 'glark/io/file/file'
8
+ require 'glark/output/line_status'
9
+ require 'glark/output/common'
10
+
11
+ class Lines < Common
12
+ def initialize file, spec
13
+ super
14
+
15
+ @print_context = false
16
+
17
+ context = spec.context
18
+ @after = context && context.after
19
+ @before = context && context.before
20
+
21
+ @stati = Glark::LineStatus.new
22
+ end
23
+
24
+ def display_matches?
25
+ true
26
+ end
27
+
28
+ def at_match_limit?
29
+ @match_limit && @count >= @match_limit
30
+ end
31
+
32
+ def displayed_name
33
+ @label || @file.fname
34
+ end
35
+
36
+ # Prints the line, which is assumed to be 0-indexed, and is thus adjusted by
37
+ # one.
38
+ def print_line_number lnum
39
+ @out.printf "%5d ", lnum + 1
40
+ end
41
+
42
+ # prints the line, and adjusts for the fact that in our world, lines are
43
+ # 0-indexed, whereas they are displayed as if 1-indexed.
44
+ def print_line lnum, ch = nil
45
+ raise "error: print_line must be implemented by an output subclass"
46
+ end
47
+
48
+ def write_matching from, to
49
+ (from .. to).each do |ln|
50
+ next unless @stati.char(ln) && !@stati.is_written?(ln)
51
+
52
+ # this used to be conditional on show_break, but no more
53
+ if from > 0 && !@stati.char(ln - 1) && @print_context
54
+ @out.puts " ---"
55
+ end
56
+
57
+ print_line ln, @stati.char(ln)
58
+ @stati.set_as_written ln
59
+ end
60
+ end
61
+
62
+ def write_nonmatching from, to
63
+ (from .. to).each do |ln|
64
+ next if @stati.is_written?(ln) || @stati.char(ln) == ':'
65
+ print_line ln
66
+ @stati.set_as_written ln
67
+ end
68
+ end
69
+
70
+ def write_matches from, to
71
+ if @invert_match
72
+ write_nonmatching from, to
73
+ else
74
+ write_matching from, to
75
+ end
76
+ end
77
+
78
+ def process_end lnum
79
+ if @invert_match
80
+ write_nonmatching 0, lnum
81
+ elsif matched?
82
+ write_matching 0, lnum
83
+ end
84
+ end
85
+
86
+ def process_match startline, endline, fromline, toline
87
+ add_match startline, endline
88
+
89
+ if display_matches?
90
+ write_matches fromline, toline
91
+ end
92
+ return at_match_limit?
93
+ end
94
+
95
+ def set_status startline, endline
96
+ fromline = [0, startline - @before].max
97
+ toline = endline + @after
98
+ @stati.set_match fromline, startline, endline, toline
99
+ end
100
+ end