diff --git a/internal/text/querier.go b/internal/text/querier.go index 5ad0f55..8b5c209 100644 --- a/internal/text/querier.go +++ b/internal/text/querier.go @@ -176,6 +176,29 @@ func (q *Querier[C]) postProcessOutput(newSysMsg models.Message) { if q.termWidth > 0 { utils.UpdateMessageTerminalMetadata(q.fullMsg, &q.line, &q.lineCount, q.termWidth) + // Write the details of q to the file determined by the environment variable DEBUG_OUTPUT_FILE + if debugOutputFile := os.Getenv("DEBUG_OUTPUT_FILE"); debugOutputFile != "" { + file, err := os.Create(debugOutputFile) + if err != nil { + ancli.PrintErr(fmt.Sprintf("failed to create debug output file: %v\n", err)) + } else { + defer file.Close() + _, err = file.WriteString(debug.IndentedJsonFmt(struct { + FullMessage string + Line string + LineCount int + TermWidth int + }{ + FullMessage: q.fullMsg, + Line: q.line, + LineCount: q.lineCount, + TermWidth: q.termWidth, + })) + if err != nil { + ancli.PrintErr(fmt.Sprintf("failed to write to debug output file: %v\n", err)) + } + } + } utils.ClearTermTo(q.termWidth, q.lineCount-1) } else { fmt.Println() diff --git a/internal/utils/print.go b/internal/utils/print.go index 5b15115..dc51d48 100644 --- a/internal/utils/print.go +++ b/internal/utils/print.go @@ -39,6 +39,9 @@ func UpdateMessageTerminalMetadata(msg string, line *string, lineCount *int, ter for _, line := range newlineSplit { amNewlines += countNewLines(line, termWidth) } + if amNewlines == 1 { + amNewlines = 2 + } amNewlineChars := len(newlineSplit) if amNewlineChars == 1 { diff --git a/internal/utils/print_test.go b/internal/utils/print_test.go index 538f60a..138ed3f 100644 --- a/internal/utils/print_test.go +++ b/internal/utils/print_test.go @@ -75,6 +75,15 @@ func TestUpdateMessageTerminalMetadata(t *testing.T) { expectedLine: "66", expectedLineCount: 5, }, + { + name: "it should not fail on this edge case that I found", + msg: "Debugging involves systematically finding and resolving issues within your code or software. Start by identifying the problem, replicate the error, and use tools like breakpoints or logging to trace the source. Testing changes iteratively helps ensure the fix is successful and doesn't cause new issues.", + // This is not correct, but that's fine, the last line functionality isn't used anywhere anyways + expectedLine: "issues.", + lineCount: 0, + termWidth: 223, + expectedLineCount: 2, + }, } for _, tc := range testCases {