1
- // TODO implement 'mg' features
1
+ // TODO refactor
2
2
use std:: {
3
3
env, fs,
4
4
io:: { self , Write } ,
@@ -191,40 +191,20 @@ fn main() {
191
191
if !captures. is_empty ( ) {
192
192
search_hits += 1 ;
193
193
194
+ // check if grep_flag is set
194
195
if !grep_reg. as_str ( ) . is_empty ( ) {
195
196
let content =
196
197
fs:: read_to_string ( & fullpath) . unwrap_or_else ( |_| String :: new ( ) ) ;
197
198
198
- // TODO refactor
199
199
if grep_reg. is_match ( & content) {
200
200
grep_files += 1 ;
201
201
202
- // FIXME if count_flag is set, grep_patterns should be counted as well an printed out
203
- // FIXME currently grep_patterns are shown as '0' , because we don`t search files here
204
202
if !count_flag {
205
- let mut linenumber = 0 ;
206
-
207
203
if raw_flag {
208
204
// don't use "file://" to make the path clickable in Windows Terminal -> otherwise output can't be piped easily to another program
209
205
writeln ! ( handle, "{}" , fullpath) . unwrap_or_else ( |err| {
210
206
error ! ( "Error writing to stdout: {err}" ) ;
211
207
} ) ;
212
-
213
- for line in content. lines ( ) {
214
- linenumber += 1 ;
215
- if grep_reg. is_match ( & line) {
216
- grep_patterns += 1 ;
217
-
218
- writeln ! (
219
- handle,
220
- "{}" ,
221
- format!( " {}: {}" , linenumber, & line)
222
- )
223
- . unwrap_or_else ( |err| {
224
- error ! ( "Error writing to stdout: {err}" ) ;
225
- } ) ;
226
- }
227
- }
228
208
} else {
229
209
// highlight search pattern in filename
230
210
let mut highlighted_name = String :: new ( ) ;
@@ -235,17 +215,31 @@ fn main() {
235
215
let highlighted_path = parent + "/" + & highlighted_name;
236
216
// TODO check if terminal accepts clickable paths
237
217
println ! ( "file://{}" , highlighted_path) ;
218
+ }
219
+ }
238
220
239
- for line in content . lines ( ) {
240
- linenumber += 1 ;
241
- if grep_reg . is_match ( & line ) {
242
- grep_patterns += 1 ;
221
+ let mut linenumber = 0 ;
222
+ for line in content . lines ( ) {
223
+ linenumber += 1 ;
224
+ let grep_captures : Vec < _ > = grep_reg . find_iter ( & line ) . collect ( ) ;
243
225
244
- let grep_captures: Vec < _ > =
245
- grep_reg . find_iter ( & line ) . collect ( ) ;
226
+ if ! grep_captures. is_empty ( ) {
227
+ grep_patterns += grep_captures . len ( ) ;
246
228
229
+ if !count_flag {
230
+ if raw_flag {
231
+ writeln ! (
232
+ handle,
233
+ "{}" ,
234
+ format!( " {}: {}" , linenumber, & line)
235
+ )
236
+ . unwrap_or_else ( |err| {
237
+ error ! ( "Error writing to stdout: {err}" ) ;
238
+ } ) ;
239
+ } else {
247
240
let mut highlighted_line = String :: new ( ) ;
248
241
for grep_capture in & grep_captures {
242
+ // FIXME if more than one capture in line, first one doesn't get highlighted
249
243
highlighted_line = highlight_capture (
250
244
& line,
251
245
& grep_capture,
0 commit comments