fortrangoingonforty/fortsh / dd59960

Browse files

debug visual_length: log bytes after newline to trace prompt_vlen=4 miscalculation [skip-pty]

Authored by espadonne
SHA
dd5996064efe7c8ef5f667e712407657f1156c95
Parents
46a780e
Tree
726e41a

2 changed files

StatusFile+-
M src/fortsh.f90 3 3
M src/io/readline.f90 23 0
src/fortsh.f90modified
@@ -324,14 +324,14 @@ program fortran_shell
324324
               char(27) // '[' // trim(col_str_buf) // 'G' // &
325325
               trim(rprompt_str) // &
326326
               prompt_str(newline_pos:len_trim(prompt_str))
327
-            call readline_enhanced(trim(embedded_prompt), input_line, iostat)
327
+            call readline_enhanced(trim(embedded_prompt), input_line, iostat, keep_raw=.true.)
328328
           else
329329
             ! Not enough space for RPROMPT — skip it
330
-            call readline_enhanced(trim(prompt_str), input_line, iostat)
330
+            call readline_enhanced(trim(prompt_str), input_line, iostat, keep_raw=.true.)
331331
           end if
332332
         else
333333
           ! Single-line prompt: pass RPROMPT to readline for its handling
334
-          call readline_enhanced(trim(prompt_str), input_line, iostat, trim(rprompt_str))
334
+          call readline_enhanced(trim(prompt_str), input_line, iostat, trim(rprompt_str), keep_raw=.true.)
335335
         end if
336336
       else
337337
         call readline_enhanced(trim(prompt_str), input_line, iostat, keep_raw=.true.)
src/io/readline.f90modified
@@ -6151,6 +6151,7 @@ contains
61516151
     integer :: i, slen
61526152
     integer :: state
61536153
     integer :: terminator_code
6154
+    integer :: last_newline_pos
61546155
 
61556156
     ! State machine for parsing escape sequences
61566157
     integer, parameter :: STATE_NORMAL = 0
@@ -6159,6 +6160,7 @@ contains
61596160
     integer, parameter :: STATE_OSC = 3
61606161
 
61616162
     vlen = 0
6163
+    last_newline_pos = 0
61626164
     slen = len_trim(str)
61636165
     state = STATE_NORMAL
61646166
 
@@ -6175,6 +6177,7 @@ contains
61756177
         else if (str(i:i) == char(10)) then  ! LF
61766178
           ! Newline resets visual position (for multi-line prompts)
61776179
           vlen = 0
6180
+          last_newline_pos = i
61786181
           i = i + 1
61796182
         else
61806183
           ! Regular character - count it (account for wide UTF-8 chars)
@@ -6221,6 +6224,26 @@ contains
62216224
         end if
62226225
       end select
62236226
     end do
6227
+
6228
+    ! Debug: log visual_length result for multi-line prompts
6229
+    if (last_newline_pos > 0 .and. slen > 10) then
6230
+      block
6231
+        integer :: dbu_vl
6232
+        open(newunit=dbu_vl, file='/tmp/fortsh_readline_debug.log', &
6233
+             status='unknown', position='append', action='write')
6234
+        write(dbu_vl, '(A,I0,A,I0,A,I0,A,I0)') &
6235
+          'VLEN: result=', vlen, ' slen=', slen, &
6236
+          ' newline_at=', last_newline_pos, ' chars_after_nl=', slen - last_newline_pos
6237
+        if (last_newline_pos < slen) then
6238
+          write(dbu_vl, '(A)', advance='no') '  after_nl_bytes=['
6239
+          do i = last_newline_pos + 1, min(slen, last_newline_pos + 20)
6240
+            write(dbu_vl, '(I0,A)', advance='no') iachar(str(i:i)), ' '
6241
+          end do
6242
+          write(dbu_vl, '(A)') ']'
6243
+        end if
6244
+        close(dbu_vl)
6245
+      end block
6246
+    end if
62246247
   end function
62256248
 
62266249
   ! ===========================================================================