find_line_in_file 1.0.18

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7bd4460bc1c1920abb9e440f39c623c889aeef5ec3552a22660c6e07d1f575ce
4
+ data.tar.gz: 7a68f891f687c905a06cb5b74aa790f8fa6cba533834d75478df78a8759c5c51
5
+ SHA512:
6
+ metadata.gz: bec228d68af758d1f494e60049d67717f497b55bbfb2e264976ccdc4f341849067e4ccf808af7c2bab48863f46d3e0e120519a56df9772cf8deca9bfece6f028
7
+ data.tar.gz: f8398fc4b662b96ffac95b22203f7b422c2bb4b29c22a0f02324346b71c2b808fc247c6690e7d3706489760c5fd3f9b4272c837c0f867a1da021897bc2ac969d
@@ -0,0 +1,57 @@
1
+ # =========================================================================== #
2
+ # Gemspec for Project FindLineInFile.
3
+ # =========================================================================== #
4
+ require 'find_line_in_file/version/version.rb'
5
+
6
+ Gem::Specification.new { |s|
7
+
8
+ s.name = 'find_line_in_file'
9
+ s.version = FindLineInFile::VERSION
10
+ s.date = Time.now.strftime('%Y-%m-%d')
11
+
12
+ s.summary = <<-EOF
13
+
14
+ This small class simply gives you the first line
15
+ found if you need to search a file for the occurance
16
+ of a given line.
17
+
18
+ If you have specific suggestions to make this gem more
19
+ useful for others, please drop me an email at:
20
+
21
+ shevegen@gmail.com
22
+
23
+ Thank you.
24
+
25
+ EOF
26
+
27
+ s.description = <<-EOF
28
+
29
+ This small class simply gives you the first line
30
+ found if you need to search a file for the occurance
31
+ of a given line.
32
+
33
+ This library is called find_line_in_file.
34
+
35
+ To use it, do something like:
36
+
37
+ FindLineInFile[]
38
+
39
+ EOF
40
+
41
+ s.extra_rdoc_files = %w()
42
+
43
+ s.authors = ['Robert A. Heiler']
44
+ s.email = 'shevegen@gmail.com'
45
+ s.files = Dir['**/*']
46
+ s.license = 'GPL-2.0'
47
+ s.homepage = 'http://rubygems.org/gems/find_line_in_file'
48
+
49
+ s.required_ruby_version = '>= '+RUBY_VERSION
50
+ s.required_rubygems_version = '>= '+Gem::VERSION
51
+ s.rubygems_version = '>= '+Gem::VERSION
52
+
53
+ s.add_dependency 'colours'
54
+ s.add_dependency 'cliner'
55
+ s.add_dependency 'opn'
56
+
57
+ }
@@ -0,0 +1 @@
1
+ require 'find_line_in_file/find_line_in_file.rb'
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'find_line_in_file/class_methods.rb'
6
+ # FindLineInFile.find[search_term: 'foo', where: '/this/file.rb']
7
+ # =========================================================================== #
8
+ class FindLineInFile
9
+
10
+ # ========================================================================= #
11
+ # === FindLineInFile.find
12
+ #
13
+ # The first argument to this method should be the search string in
14
+ # question, that is, the line you want to find exactly.
15
+ #
16
+ # The second argument should ideally be a Hash but it can also be a
17
+ # String, hence the check below in the method body. It will tell
18
+ # us the file location.
19
+ #
20
+ # If the line has been found then this method will return an Integer
21
+ # number, aka the fileline - starting at line number 1. (There is,
22
+ # logically, no line number called 0, hence why the code here behaves
23
+ # in that way.)
24
+ #
25
+ # Usage examples:
26
+ #
27
+ # FindLineInFile.find(search_term, :in => 'test.txt')
28
+ # FindLineInFile[search_term: 'foo', where: '/this/file.rb']
29
+ #
30
+ # ========================================================================= #
31
+ def self.find(
32
+ search_term, where = ''
33
+ )
34
+ if search_term.is_a? Hash
35
+ # ===================================================================== #
36
+ # === :where
37
+ # ===================================================================== #
38
+ if search_term.has_key? :where
39
+ if where.is_a?(String) and where.empty?
40
+ where = search_term.delete(:where)
41
+ end
42
+ end
43
+ # ===================================================================== #
44
+ # === :what
45
+ # ===================================================================== #
46
+ if search_term.has_key? :what
47
+ if search_term.has_key? :in
48
+ where = search_term.delete :in
49
+ elsif search_term.has_key? :where
50
+ where = search_term.delete :where
51
+ end
52
+ search_term = search_term.delete :what
53
+ end
54
+ # ===================================================================== #
55
+ # === :search_term
56
+ #
57
+ # This should come last.
58
+ # ===================================================================== #
59
+ if search_term.has_key? :search_term
60
+ search_term = search_term.delete(:search_term)
61
+ end
62
+ end
63
+ if where.is_a? Hash
64
+ if where.has_key? :in
65
+ where = where.delete :in
66
+ elsif where.has_key? :where
67
+ where = where.delete :where
68
+ end
69
+ elsif where.is_a? String # Handle given Strings next.
70
+ where = where.to_s.dup
71
+ end
72
+ # ======================================================================= #
73
+ # `where` may still be a Hash at this point, so
74
+ # we convert it into a string.
75
+ # ======================================================================= #
76
+ if where.is_a? Hash and where.empty?
77
+ where = ''
78
+ end
79
+ _ = FindLineInFile.new(search_term, where)
80
+ return _.result # We will return a Fixnum here, or nil otherwise.
81
+ end; self.instance_eval { alias [] find } # === FindLineInFile[]
82
+
83
+ end
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # require 'find_line_in_file/constants.rb'
6
+ # =========================================================================== #
7
+ class FindLineInFile
8
+
9
+ # ========================================================================= #
10
+ # === DEFAULT_FILE
11
+ # ========================================================================= #
12
+ DEFAULT_FILE =
13
+ '/home/x/DATA/PROGRAMMING_LANGUAGES/RUBY/src/'\
14
+ 'find_line_in_file/test/testing_find_line_in_file.rb'
15
+
16
+ # ========================================================================= #
17
+ # === THIS_LINE
18
+ # ========================================================================= #
19
+ THIS_LINE = ''
20
+
21
+ # ========================================================================= #
22
+ # === NAME
23
+ # ========================================================================= #
24
+ NAME = 'FindLineInFile: '
25
+
26
+ # ========================================================================= #
27
+ # === ENCODING_ISO
28
+ # ========================================================================= #
29
+ ENCODING_ISO = 'ISO-8859-1'
30
+
31
+ # ========================================================================= #
32
+ # === ENCODING_UTF
33
+ # ========================================================================= #
34
+ ENCODING_UTF = 'UTF-8'
35
+
36
+ # ========================================================================= #
37
+ # === USE_THIS_ENCODING
38
+ #
39
+ # The class will default to UTF-8, unless specified otherwise. Thus,
40
+ # this is the default encoding to be used.
41
+ # ========================================================================= #
42
+ USE_THIS_ENCODING = ENCODING_UTF
43
+
44
+ # ========================================================================= #
45
+ # === MAIN_ENCODING
46
+ # ========================================================================= #
47
+ MAIN_ENCODING = USE_THIS_ENCODING
48
+
49
+ end
@@ -0,0 +1,254 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # === FindLineInFile
6
+ #
7
+ # The purpose of the class in this file is to find a specific line
8
+ # in a given file.
9
+ #
10
+ # If this file exists, then we will return a single integer
11
+ # which will denote the file number that contains our
12
+ # substring in question (starting at line number 1).
13
+ #
14
+ # Specific usage examples:
15
+ #
16
+ # require 'find_line_in_file'
17
+ # FindLineInFile.new
18
+ # FindLineInFile.new('include Colours')
19
+ # FindLineInFile.new('include Colours', 'att.rb')
20
+ # FindLineInFile['run_everything', '/Users/x/DATA/PROGRAMMING_LANGUAGES/RUBY/src/tools/to_png.rb']
21
+ #
22
+ # Here is a slightly more complex usage example of FindLineInFile:
23
+ #
24
+ # FindLineInFile[ what: 'run_everything', where: '/home/x/DATA/PROGRAMMING_LANGUAGES/RUBY/src/tools/to_png.rb']
25
+ #
26
+ # =========================================================================== #
27
+ # require 'find_line_in_file'; FindLineInFile.new
28
+ # =========================================================================== #
29
+ require 'colours'
30
+ require 'cliner'
31
+ require 'opn'
32
+ require 'find_line_in_file/class_methods.rb'
33
+ require 'find_line_in_file/constants.rb'
34
+ require 'find_line_in_file/version/version.rb'
35
+
36
+ class FindLineInFile # === FindLineInFile
37
+
38
+ include Colours::E
39
+
40
+ # ========================================================================= #
41
+ # === initialize
42
+ # ========================================================================= #
43
+ def initialize(
44
+ search_for_this_line = nil,
45
+ search_in_this_file = nil,
46
+ run_already = true
47
+ )
48
+ reset
49
+ # ======================================================================= #
50
+ # The second one has to come first, because the first argument may
51
+ # sometimes be a Hash. If it is a Hash then it may overrule the @file
52
+ # variable, so that is why the order is reverse.
53
+ # ======================================================================= #
54
+ set_search_in_this_file(
55
+ search_in_this_file # This file is grepped.
56
+ )
57
+ set_search_for_this_line(
58
+ search_for_this_line # This line is sought.
59
+ )
60
+ run if run_already
61
+ end
62
+
63
+ # ========================================================================= #
64
+ # === reset (reset tag)
65
+ # ========================================================================= #
66
+ def reset
67
+ # ======================================================================= #
68
+ # === @data
69
+ # ======================================================================= #
70
+ @data = nil
71
+ # ======================================================================= #
72
+ # === @result
73
+ # ======================================================================= #
74
+ @result = nil
75
+ # ======================================================================= #
76
+ # === @can_we_continue
77
+ # ======================================================================= #
78
+ @can_we_continue = true
79
+ # ======================================================================= #
80
+ # === @use_this_encoding
81
+ # ======================================================================= #
82
+ @use_this_encoding = USE_THIS_ENCODING
83
+ end
84
+
85
+ # ========================================================================= #
86
+ # === search_for_which_file?
87
+ # ========================================================================= #
88
+ def search_for_which_file?
89
+ @search_for_this_file
90
+ end; alias file? search_for_which_file? # === file?
91
+
92
+ # ========================================================================= #
93
+ # === clear_dataset
94
+ # ========================================================================= #
95
+ def clear_dataset # No more need for @data to contain anything.
96
+ @data = nil; remove_instance_variable(:@data)
97
+ end
98
+
99
+ # ========================================================================= #
100
+ # === can_we_continue?
101
+ # ========================================================================= #
102
+ def can_we_continue?
103
+ @can_we_continue
104
+ end
105
+
106
+ # ========================================================================= #
107
+ # === sfile
108
+ # ========================================================================= #
109
+ def sfile(i)
110
+ ::Colours.sfile(i)
111
+ end
112
+
113
+ # ========================================================================= #
114
+ # === ewarn
115
+ # ========================================================================= #
116
+ def ewarn(i)
117
+ ::Colours.ewarn(i)
118
+ end
119
+
120
+ # ========================================================================= #
121
+ # === simp
122
+ # ========================================================================= #
123
+ def simp(i)
124
+ ::Colours.simp(i)
125
+ end
126
+
127
+ # ========================================================================= #
128
+ # === swarn
129
+ # ========================================================================= #
130
+ def swarn(i)
131
+ ::Colours.swarn(i)
132
+ end
133
+
134
+ # ========================================================================= #
135
+ # === report_result
136
+ #
137
+ # We only report if we have found something.
138
+ # ========================================================================= #
139
+ def report_result
140
+ if can_we_continue?
141
+ opn; e "We will try to find something in "\
142
+ "#{sfile(search_for_which_file?)} now."
143
+ opn; e 'The variable @result (of class '+@result.class.to_s+') '+
144
+ 'is: '+simp(@result.to_s)
145
+ end
146
+ end; alias report report_result # === report
147
+
148
+ # ========================================================================= #
149
+ # === read_in_dataset
150
+ #
151
+ # This here makes use of File.readlines(), so the Encoding must be
152
+ # checked.
153
+ # ========================================================================= #
154
+ def read_in_dataset
155
+ @data = File.readlines(
156
+ search_for_which_file?,
157
+ encoding: @use_this_encoding # We must specify our default encoding to use.
158
+ )
159
+ end
160
+
161
+ # ========================================================================= #
162
+ # === number?
163
+ # ========================================================================= #
164
+ def number?
165
+ @result
166
+ end; alias line_number? number? # === line_number?
167
+
168
+ # ========================================================================= #
169
+ # === result?
170
+ # ========================================================================= #
171
+ def result?
172
+ @result
173
+ end; alias result result? # === result
174
+
175
+ # ========================================================================= #
176
+ # === set_search_in_this_file
177
+ # ========================================================================= #
178
+ def set_search_in_this_file(i = nil)
179
+ i = DEFAULT_FILE if i.nil?
180
+ i = i.first if i.is_a? Array
181
+ @search_for_this_file = i
182
+ end; alias set_this_file set_search_in_this_file # === set_this_file
183
+ alias use_this_file set_search_in_this_file # === use_this_file
184
+
185
+ # ========================================================================= #
186
+ # === set_search_for_this_line
187
+ # ========================================================================= #
188
+ def set_search_for_this_line(i = nil)
189
+ i = THIS_LINE if i.nil?
190
+ if i.is_a? Hash
191
+ if i.has_key? :this_file
192
+ set_this_file(i.delete(:this_file))
193
+ end
194
+ if i.has_key? :this_line
195
+ i = i.delete(:this_line)
196
+ end
197
+ end
198
+ i = i.to_s
199
+ @search_for_this_line = i
200
+ end; alias search_for set_search_for_this_line # === search_for
201
+
202
+ # ========================================================================= #
203
+ # === check_whether_the_file_exists
204
+ #
205
+ # We find out whether our target file exists or whether it does not.
206
+ # ========================================================================= #
207
+ def check_whether_the_file_exists
208
+ search_for_this_file = search_for_which_file?
209
+ unless File.exist? search_for_this_file
210
+ opn; ewarn 'The file `'+sfile(search_for_this_file)+
211
+ swarn('` does not exist.')
212
+ @can_we_continue = false
213
+ end
214
+ end
215
+
216
+ # ========================================================================= #
217
+ # === find_input_in_dataset
218
+ # ========================================================================= #
219
+ def find_input_in_dataset
220
+ # ======================================================================= #
221
+ # Must add +1 because files start at 1, whereas Arrays start at entry 0.
222
+ # ======================================================================= #
223
+ @result = @data.find_index {|entry|
224
+ entry.include? @search_for_this_line.to_s
225
+ }
226
+ if @result
227
+ @result += 1
228
+ else
229
+ opn; e "We may have not found anything for: `#{@search_for_this_line}`"
230
+ pp @search_for_this_line
231
+ opn; e 'Its encoding was: '+
232
+ simp(@search_for_this_line.to_s.encoding.to_s)
233
+ end
234
+ end
235
+
236
+ # ========================================================================= #
237
+ # === run (run tag)
238
+ # ========================================================================= #
239
+ def run
240
+ check_whether_the_file_exists
241
+ if can_we_continue?
242
+ read_in_dataset
243
+ find_input_in_dataset
244
+ clear_dataset
245
+ return @result
246
+ end
247
+ end
248
+
249
+ end
250
+
251
+ if __FILE__ == $PROGRAM_NAME
252
+ _ = FindLineInFile.new(ARGV.first, ARGV[1])
253
+ _.report_result
254
+ end # find_line_in_file
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ class FindLineInFile
6
+
7
+ # ========================================================================= #
8
+ # === FindLineInFile::VERSION
9
+ # ========================================================================= #
10
+ VERSION = '1.0.18'
11
+
12
+ # ========================================================================= #
13
+ # === FindLineInFile::LAST_UPDATE
14
+ # ========================================================================= #
15
+ LAST_UPDATE = '28.02.2020'
16
+
17
+ end
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ require 'colours/autoinclude'
6
+ require 'find_line_in_file'
7
+
8
+ INCLUDE_COLOUR_E = 'include Colours'
9
+
10
+ file = '/home/x/DATA/PROGRAMMING_LANGUAGES/RUBY/src/diamond_shell/lib/diamond_shell/standalone_classes/aliases.rb'
11
+ line = INCLUDE_COLOUR_E
12
+
13
+ p FindLineInFile[
14
+ what: INCLUDE_COLOUR_E,
15
+ where: file
16
+ ]
17
+ FindLineInFile.new
18
+ FindLineInFile.new(INCLUDE_COLOUR_E)
19
+ FindLineInFile.new(INCLUDE_COLOUR_E, file).report
20
+ FindLineInFile.new(INCLUDE_COLOUR_E, file).report
21
+ FindLineInFile['run_everything',
22
+ '/home/x/DATA/PROGRAMMING_LANGUAGES/RUBY/src/tools/to_png.rb']
23
+
24
+ find_line_in_file = FindLineInFile.new(
25
+ this_file: file,
26
+ this_line: line
27
+ )
28
+ pp find_line_in_file
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: find_line_in_file
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.18
5
+ platform: ruby
6
+ authors:
7
+ - Robert A. Heiler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colours
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: cliner
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: opn
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: |2+
56
+
57
+ This small class simply gives you the first line
58
+ found if you need to search a file for the occurance
59
+ of a given line.
60
+
61
+ This library is called find_line_in_file.
62
+
63
+ To use it, do something like:
64
+
65
+ FindLineInFile[]
66
+
67
+ email: shevegen@gmail.com
68
+ executables: []
69
+ extensions: []
70
+ extra_rdoc_files: []
71
+ files:
72
+ - find_line_in_file.gemspec
73
+ - lib/find_line_in_file.rb
74
+ - lib/find_line_in_file/class_methods.rb
75
+ - lib/find_line_in_file/constants.rb
76
+ - lib/find_line_in_file/find_line_in_file.rb
77
+ - lib/find_line_in_file/version/version.rb
78
+ - test/testing_find_line_in_file.rb
79
+ homepage: http://rubygems.org/gems/find_line_in_file
80
+ licenses:
81
+ - GPL-2.0
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 2.7.0
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 3.1.2
97
+ requirements: []
98
+ rubygems_version: 3.1.2
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: 'This small class simply gives you the first line found if you need to search
102
+ a file for the occurance of a given line. If you have specific suggestions to make
103
+ this gem more useful for others, please drop me an email at: shevegen@gmail.com Thank
104
+ you.'
105
+ test_files: []