glark 1.9.0 → 1.10.0

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.
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,15 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ module Glark
6
+ # not yet supported; will be matches stored instead of written to stdout.
7
+ class MatchList
8
+ attr_reader :matches
9
+
10
+ def initialize file, show_file_names
11
+ super
12
+ @matches = Array.new
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,103 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ require 'glark/output/spec'
6
+ require 'glark/util/options'
7
+
8
+ module Glark
9
+ class OutputOptions < OutputSpec
10
+ include OptionUtil
11
+
12
+ def initialize colors, optdata
13
+ super colors
14
+
15
+ add_as_options optdata
16
+ end
17
+
18
+ def config_fields
19
+ fields = {
20
+ "after-context" => @context.after,
21
+ "before-context" => @context.before,
22
+ "filter" => @filter,
23
+ "output" => @style,
24
+ }
25
+ end
26
+
27
+ def dump_fields
28
+ fields = {
29
+ "after" => @context.after,
30
+ "before" => @context.before,
31
+ "count" => @count,
32
+ "file_names_only" => @file_names_only,
33
+ "filter" => @filter,
34
+ "invert_match" => @invert_match,
35
+ "label" => @label,
36
+ "match_limit" => @match_limit,
37
+ "output" => @style,
38
+ "show_file_names" => @show_file_names,
39
+ "show_line_numbers" => @show_line_numbers,
40
+ "write_null" => @write_null
41
+ }
42
+ end
43
+
44
+ def update_fields fields
45
+ fields.each do |name, values|
46
+ case name
47
+ when "grep"
48
+ self.style = "grep" if to_boolean values.last
49
+ when "output"
50
+ self.style = values.last
51
+ end
52
+ end
53
+ @context.update_fields fields
54
+ end
55
+
56
+ def add_as_options optdata
57
+ @context.add_as_option optdata
58
+
59
+ add_opt_true optdata, :invert_match, %w{ -v --invert-match }
60
+
61
+ add_opt_true optdata, :filter, %w{ --filter }
62
+ add_opt_false optdata, :filter, %w{ --no-filter --nofilter }
63
+
64
+ add_opt_true optdata, :show_line_numbers, %w{ -n --line-number }
65
+ add_opt_false optdata, :show_line_numbers, %w{ -N --no-line-number }
66
+
67
+ add_opt_blk(optdata, %w{ -l --files-with-matches }) { set_file_names_only false }
68
+ add_opt_blk(optdata, %w{ -L --files-without-match }) { set_file_names_only true }
69
+
70
+ add_opt_true optdata, :write_null, %w{ -Z --null }
71
+
72
+ add_opt_str optdata, :label, %w{ --label }
73
+
74
+ add_opt_int optdata, :match_limit, %w{ -m --match-limit }
75
+
76
+ add_opt_blk(optdata, %w{ -U --no-highlight }) { @colors.text_color_style = nil }
77
+ add_opt_blk(optdata, %w{ -g --grep }) { self.style = "grep" }
78
+
79
+ optdata << lnum_color_option = {
80
+ :tags => %w{ --line-number-color },
81
+ :arg => [ :string ],
82
+ :set => Proc.new { |val| @colors.line_number_color = @colors.create_color "line-number-color", val },
83
+ }
84
+
85
+ add_opt_true optdata, :count, %w{ -c --count }
86
+
87
+ add_opt_true optdata, :show_file_names, %w{ -H --with-filename }
88
+ add_opt_false optdata, :show_file_names, %w{ -h --no-filename }
89
+
90
+ optdata << highlight_option = {
91
+ :tags => %w{ -u --highlight },
92
+ :arg => [ :optional, :regexp, %r{ ^ (?:(multi|single)|none) $ }x ],
93
+ :set => Proc.new { |md| val = md ? md[1] : "multi"; @colors.text_color_style = val }
94
+ }
95
+
96
+ optdata << file_color_option = {
97
+ :tags => %w{ --file-color },
98
+ :arg => [ :string ],
99
+ :set => Proc.new { |val| @colors.file_name_color = @colors.create_color "file-color", val }
100
+ }
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ # Results of searching files.
6
+
7
+ class Results
8
+ include Loggable
9
+
10
+ attr_reader :count
11
+
12
+ def initialize
13
+ @count = 0
14
+ end
15
+
16
+ def matched?
17
+ @count > 0
18
+ end
19
+
20
+ def add_match
21
+ @count += 1
22
+ end
23
+ end
@@ -0,0 +1,105 @@
1
+ #!/usr/bin/ruby -w
2
+ #!ruby -w
3
+ # vim: set filetype=ruby : set sw=2
4
+
5
+ require 'glark/output/binary_file_summary'
6
+ require 'glark/output/context'
7
+ require 'glark/output/file_name_only'
8
+ require 'glark/output/glark_count'
9
+ require 'glark/output/glark_lines'
10
+ require 'glark/output/grep_count'
11
+ require 'glark/output/grep_lines'
12
+ require 'glark/output/unfiltered_lines'
13
+
14
+ module Glark
15
+ class OutputSpec
16
+ attr_accessor :context # lines before and after
17
+ attr_accessor :count # only count the matches
18
+ attr_accessor :file_names_only # display only the file names
19
+ attr_accessor :filter # display only matches
20
+ attr_accessor :invert_match # display non-matching lines
21
+ attr_accessor :label
22
+ attr_accessor :match_limit # the maximum number of matches to display per file
23
+ attr_accessor :out
24
+ attr_accessor :show_file_names # display file names
25
+ attr_accessor :show_line_numbers # display numbers of matching lines
26
+ attr_accessor :write_null # in @file_names_only mode, write '\0' instead of '\n'
27
+
28
+ attr_reader :colors
29
+ attr_reader :style # grep, glark
30
+
31
+ def initialize colors
32
+ @colors = colors
33
+ @context = Glark::Context.new
34
+ @count = false
35
+ @file_names_only = false
36
+ @filter = true
37
+ @highlight = nil
38
+ @invert_match = false
39
+ @label = nil
40
+ @match_limit = nil
41
+ @out = $stdout
42
+ @show_file_names = nil # nil == > 1; true == >= 1; false means never
43
+ @show_line_numbers = true
44
+ @style = nil
45
+ @write_null = false
46
+
47
+ @output_cls = nil
48
+
49
+ self.style = "glark"
50
+ end
51
+
52
+ def line_number_highlight
53
+ @colors.line_number_color
54
+ end
55
+
56
+ def file_highlight
57
+ @colors.file_name_color
58
+ end
59
+
60
+ def highlight
61
+ @colors.text_color_style
62
+ end
63
+
64
+ def style= style
65
+ @style = style
66
+ if @style == "glark"
67
+ @colors.text_color_style = "multi"
68
+ elsif @style == "grep"
69
+ @colors.text_color_style = false
70
+ @show_line_numbers = false
71
+ @context.clear
72
+ else
73
+ raise "error: unrecognized style '" + style + "'"
74
+ end
75
+ @output_cls = nil
76
+ end
77
+
78
+ def create_output_type file
79
+ output_type_cls.new file, self
80
+ end
81
+
82
+ def output_type_cls
83
+ @output_cls ||= if @count
84
+ if @style == "grep"
85
+ Grep::Count
86
+ else
87
+ Glark::Count
88
+ end
89
+ elsif @file_names_only
90
+ FileNameOnly
91
+ elsif !@filter
92
+ UnfilteredLines
93
+ elsif @style == "grep"
94
+ Grep::Lines
95
+ else
96
+ Glark::Lines
97
+ end
98
+ end
99
+
100
+ def set_file_names_only invert_match
101
+ @file_names_only = true
102
+ @invert_match = invert_match
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,28 @@
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
+ module Glark
9
+ class UnfilteredLines < Lines
10
+ include Glark::Format
11
+
12
+ def display_matches?
13
+ false
14
+ end
15
+
16
+ def process_end lnum
17
+ show_file_header
18
+ write_all
19
+ end
20
+
21
+ def write_all
22
+ show_file_header
23
+ (0 ... @file.get_lines.length).each do |ln|
24
+ print_line ln
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,143 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'glark/util/colors/spec'
5
+
6
+ module Glark
7
+ class ColorOptions < ColorSpec
8
+ DEFAULT_COLORS = [
9
+ # "FF88CC",
10
+ "black on yellow",
11
+ "black on green",
12
+ "black on magenta",
13
+ "yellow on black",
14
+ "magenta on black",
15
+ "green on black",
16
+ "cyan on black",
17
+ "blue on yellow",
18
+ "blue on magenta",
19
+ "blue on green",
20
+ "blue on cyan",
21
+ "yellow on blue",
22
+ "magenta on blue",
23
+ "green on blue",
24
+ "cyan on blue",
25
+ ]
26
+
27
+ attr_reader :text_color_style # single, multi, or nil (no text highlights)
28
+
29
+ def initialize
30
+ super
31
+ @highlighter = nil
32
+ @text_color_style = "multi"
33
+ end
34
+
35
+ # creates a color for the given option, based on its value
36
+ def create_color opt, value
37
+ if @highlighter
38
+ if value
39
+ make_color value
40
+ else
41
+ raise "error: '" + opt + "' requires a color"
42
+ end
43
+ else
44
+ log { "no highlighter defined" }
45
+ end
46
+ end
47
+
48
+ def make_color color
49
+ @highlighter.make_color color
50
+ end
51
+
52
+ def make_rgb_color red, green, blue, fgbg
53
+ @highlighter.make_rgb_color red, green, blue, fgbg
54
+ end
55
+
56
+ def make_colors limit = -1
57
+ DEFAULT_COLORS[0 .. limit].collect do |color|
58
+ make_color color
59
+ end
60
+ end
61
+
62
+ def multi_colors
63
+ make_colors
64
+ # [ make_rgb_color(4, 3, 2, :bg) + make_rgb_color(0, 2, 1, :fg) ]
65
+ end
66
+
67
+ def single_color
68
+ make_colors 0
69
+ end
70
+
71
+ def text_color_style= tcstyle
72
+ @text_color_style = tcstyle
73
+ if @text_color_style
74
+ @highlighter = @text_color_style && HlWrapper.new
75
+ @text_colors = case @text_color_style
76
+ when highlight_multi?(@text_color_style), true
77
+ multi_colors
78
+ when "single"
79
+ single_color
80
+ else
81
+ raise "highlight format '" + @text_color_style.to_s + "' not recognized"
82
+ end
83
+
84
+ @file_name_color = make_color "bold"
85
+ @line_number_color = nil
86
+ else
87
+ @highlighter = nil
88
+ @text_colors = Array.new
89
+ @file_name_color = nil
90
+ @line_number_color = nil
91
+ end
92
+ end
93
+
94
+ def set_text_color index, color
95
+ @text_colors[index] = color
96
+ end
97
+
98
+ def highlight_multi? str
99
+ %w{ multi on true yes }.detect { |x| str == x }
100
+ end
101
+
102
+ def config_fields
103
+ fields = {
104
+ "file-color" => @file_name_color,
105
+ "highlight" => @text_color_style,
106
+ "line-number-color" => @line_number_color,
107
+ }
108
+ end
109
+
110
+ def dump_fields
111
+ fields = {
112
+ "file_name_color" => colorize(@file_name_color, "filename"),
113
+ "highlight" => @text_color_style,
114
+ "line_number_color" => colorize(@line_number_color, "12345"),
115
+ }
116
+ end
117
+
118
+ def colorize field, str
119
+ if field
120
+ field + str + Text::Color::RESET
121
+ else
122
+ str
123
+ end
124
+ end
125
+
126
+ def update_fields fields
127
+ fields.each do |name, values|
128
+ case name
129
+ when "file-color"
130
+ @file_name_color = create_color name, values.last
131
+ when "highlight"
132
+ self.text_color_style = values.last
133
+ when "line-number-color"
134
+ @line_number_color = create_color name, values.last
135
+ when "text-color"
136
+ @text_colors = [ create_color(name, values.last) ]
137
+ when %r{^text\-color\-(\d+)$}
138
+ set_text_color $1.to_i, create_color(name, values.last)
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'rubygems'
5
+ require 'riel/log/loggable'
6
+
7
+ module Glark
8
+ class ColorSpec
9
+ include Loggable
10
+
11
+ attr_accessor :text_colors
12
+ attr_accessor :file_name_color
13
+ attr_accessor :line_number_color
14
+
15
+ def initialize
16
+ @text_colors = Array.new
17
+ @file_name_color = nil
18
+ @line_number_color = nil
19
+ end
20
+ end
21
+ end