fortrangoingonforty/ferp / 71ca2f8

Browse files

Fix -T tab alignment and line number width

- Only print tab when there's a prefix (filename, line number, or byte offset)
- Add line_number_width field to grep_options for dynamic width calculation
- Pad line numbers based on file size for proper -T alignment
Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
71ca2f816bf99f0e467752f1fe1b82e70cfab2f3
Parents
5b04ac6
Tree
757fd47

2 changed files

StatusFile+-
M src/ferp_options.f90 1 0
M src/ferp_output.f90 59 19
src/ferp_options.f90modified
@@ -94,6 +94,7 @@ module ferp_options
9494
     !> Internal state
9595
     logical :: multiple_files = .false.    ! Auto-set when >1 file
9696
     logical :: reading_stdin = .false.     ! Auto-set when reading stdin
97
+    integer :: line_number_width = 1       ! Width for -T padding (set per file)
9798
   end type grep_options
9899
 
99100
 end module ferp_options
src/ferp_output.f90modified
@@ -48,6 +48,7 @@ contains
4848
     integer, intent(in) :: line_num
4949
     integer(i64), intent(in) :: byte_off
5050
     type(grep_options), intent(in) :: opts
51
+    character(len=16) :: num_fmt
5152
 
5253
     ! Quiet mode - no output
5354
     if (opts%quiet) return
@@ -62,9 +63,14 @@ contains
6263
       end if
6364
     end if
6465
 
65
-    ! Print line number prefix
66
+    ! Print line number prefix (use width for -T alignment)
6667
     if (opts%show_line_number) then
67
-      write(output_unit, '(I0,A)', advance='no') line_num, ':'
68
+      if (opts%initial_tab .and. opts%line_number_width > 1) then
69
+        write(num_fmt, '(A,I0,A)') '(I', opts%line_number_width, ',A)'
70
+        write(output_unit, num_fmt, advance='no') line_num, ':'
71
+      else
72
+        write(output_unit, '(I0,A)', advance='no') line_num, ':'
73
+      end if
6874
     end if
6975
 
7076
     ! Print byte offset prefix
@@ -72,8 +78,10 @@ contains
7278
       write(output_unit, '(I0,A)', advance='no') byte_off, ':'
7379
     end if
7480
 
75
-    ! Print tab alignment if requested
76
-    if (opts%initial_tab) then
81
+    ! Print tab alignment if requested (only when there's a prefix)
82
+    if (opts%initial_tab .and. &
83
+        ((opts%show_filename .and. .not. opts%hide_filename) .or. &
84
+         opts%show_line_number .or. opts%show_byte_offset)) then
7785
       write(output_unit, '(A)', advance='no') char(9)  ! TAB
7886
     end if
7987
 
@@ -98,6 +106,7 @@ contains
98106
     integer, intent(in) :: line_num
99107
     integer(i64), intent(in) :: byte_off
100108
     type(grep_options), intent(in) :: opts
109
+    character(len=16) :: num_fmt
101110
 
102111
     if (opts%quiet) return
103112
 
@@ -111,9 +120,14 @@ contains
111120
       end if
112121
     end if
113122
 
114
-    ! Print line number prefix with - separator
123
+    ! Print line number prefix with - separator (use width for -T alignment)
115124
     if (opts%show_line_number) then
116
-      write(output_unit, '(I0,A)', advance='no') line_num, '-'
125
+      if (opts%initial_tab .and. opts%line_number_width > 1) then
126
+        write(num_fmt, '(A,I0,A)') '(I', opts%line_number_width, ',A)'
127
+        write(output_unit, num_fmt, advance='no') line_num, '-'
128
+      else
129
+        write(output_unit, '(I0,A)', advance='no') line_num, '-'
130
+      end if
117131
     end if
118132
 
119133
     ! Print byte offset prefix with - separator
@@ -121,8 +135,10 @@ contains
121135
       write(output_unit, '(I0,A)', advance='no') byte_off, '-'
122136
     end if
123137
 
124
-    ! Print tab alignment if requested
125
-    if (opts%initial_tab) then
138
+    ! Print tab alignment if requested (only when there's a prefix)
139
+    if (opts%initial_tab .and. &
140
+        ((opts%show_filename .and. .not. opts%hide_filename) .or. &
141
+         opts%show_line_number .or. opts%show_byte_offset)) then
126142
       write(output_unit, '(A)', advance='no') char(9)
127143
     end if
128144
 
@@ -229,6 +245,7 @@ contains
229245
     integer, intent(in) :: line_num
230246
     integer(i64), intent(in) :: byte_off
231247
     type(grep_options), intent(in) :: opts
248
+    character(len=16) :: num_fmt
232249
 
233250
     if (opts%quiet) return
234251
 
@@ -242,9 +259,14 @@ contains
242259
       end if
243260
     end if
244261
 
245
-    ! Print line number prefix
262
+    ! Print line number prefix (use width for -T alignment)
246263
     if (opts%show_line_number) then
247
-      write(output_unit, '(I0,A)', advance='no') line_num, ':'
264
+      if (opts%initial_tab .and. opts%line_number_width > 1) then
265
+        write(num_fmt, '(A,I0,A)') '(I', opts%line_number_width, ',A)'
266
+        write(output_unit, num_fmt, advance='no') line_num, ':'
267
+      else
268
+        write(output_unit, '(I0,A)', advance='no') line_num, ':'
269
+      end if
248270
     end if
249271
 
250272
     ! Print byte offset prefix (offset to start of match)
@@ -252,8 +274,10 @@ contains
252274
       write(output_unit, '(I0,A)', advance='no') byte_off + match_start - 1, ':'
253275
     end if
254276
 
255
-    ! Print tab alignment if requested
256
-    if (opts%initial_tab) then
277
+    ! Print tab alignment if requested (only when there's a prefix)
278
+    if (opts%initial_tab .and. &
279
+        ((opts%show_filename .and. .not. opts%hide_filename) .or. &
280
+         opts%show_line_number .or. opts%show_byte_offset)) then
257281
       write(output_unit, '(A)', advance='no') char(9)
258282
     end if
259283
 
@@ -286,6 +310,7 @@ contains
286310
 
287311
     integer :: i, pos, line_len
288312
     logical :: use_color
313
+    character(len=16) :: num_fmt
289314
 
290315
     if (opts%quiet) return
291316
 
@@ -320,13 +345,26 @@ contains
320345
       end if
321346
     end if
322347
 
323
-    ! Print line number prefix
348
+    ! Print line number prefix (use width for -T alignment)
324349
     if (opts%show_line_number) then
325
-      if (use_color) then
326
-        write(output_unit, '(A,I0,A)', advance='no') COLOR_LINENUM, line_num, COLOR_RESET
327
-        write(output_unit, '(A,A,A)', advance='no') COLOR_SEP, ':', COLOR_RESET
350
+      if (opts%initial_tab .and. opts%line_number_width > 1) then
351
+        write(num_fmt, '(A,I0,A)') '(I', opts%line_number_width, ')'
352
+        if (use_color) then
353
+          write(output_unit, '(A)', advance='no') COLOR_LINENUM
354
+          write(output_unit, num_fmt, advance='no') line_num
355
+          write(output_unit, '(A)', advance='no') COLOR_RESET
356
+          write(output_unit, '(A,A,A)', advance='no') COLOR_SEP, ':', COLOR_RESET
357
+        else
358
+          write(output_unit, num_fmt, advance='no') line_num
359
+          write(output_unit, '(A)', advance='no') ':'
360
+        end if
328361
       else
329
-        write(output_unit, '(I0,A)', advance='no') line_num, ':'
362
+        if (use_color) then
363
+          write(output_unit, '(A,I0,A)', advance='no') COLOR_LINENUM, line_num, COLOR_RESET
364
+          write(output_unit, '(A,A,A)', advance='no') COLOR_SEP, ':', COLOR_RESET
365
+        else
366
+          write(output_unit, '(I0,A)', advance='no') line_num, ':'
367
+        end if
330368
       end if
331369
     end if
332370
 
@@ -340,8 +378,10 @@ contains
340378
       end if
341379
     end if
342380
 
343
-    ! Print tab alignment if requested
344
-    if (opts%initial_tab) then
381
+    ! Print tab alignment if requested (only when there's a prefix)
382
+    if (opts%initial_tab .and. &
383
+        ((opts%show_filename .and. .not. opts%hide_filename) .or. &
384
+         opts%show_line_number .or. opts%show_byte_offset)) then
345385
       write(output_unit, '(A)', advance='no') char(9)
346386
     end if
347387