Skip to content

Commit d590c0d

Browse files
authored
Merge pull request #1830 from ksylvan/kayvan/newline-in-output-fix
Ensure final newline in model generated outputs
2 parents 7dacc07 + c936f8e commit d590c0d

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### PR [#1830](https://github.com/danielmiessler/Fabric/pull/1830) by [ksylvan](https://github.com/ksylvan): Ensure final newline in model generated outputs
2+
3+
- Feat: ensure newline in `CreateOutputFile` and improve tests
4+
- Add newline to `CreateOutputFile` if missing
5+
- Use `t.Cleanup` for file removal in tests
6+
- Add test for message with trailing newline
7+
- Introduce `printedStream` flag in `Chatter.Send`

internal/cli/output.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ func CreateOutputFile(message string, fileName string) (err error) {
2929
return
3030
}
3131
defer file.Close()
32+
if !strings.HasSuffix(message, "\n") {
33+
message += "\n"
34+
}
3235
if _, err = file.WriteString(message); err != nil {
3336
err = fmt.Errorf("%s", fmt.Sprintf(i18n.T("error_writing_to_file"), err))
3437
} else {

internal/cli/output_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,34 @@ func TestCreateOutputFile(t *testing.T) {
2424
t.Fatalf("CreateOutputFile() error = %v", err)
2525
}
2626

27-
defer os.Remove(fileName)
27+
t.Cleanup(func() { os.Remove(fileName) })
28+
29+
data, err := os.ReadFile(fileName)
30+
if err != nil {
31+
t.Fatalf("failed to read output file: %v", err)
32+
}
33+
34+
expected := message + "\n"
35+
if string(data) != expected {
36+
t.Fatalf("expected file contents %q, got %q", expected, data)
37+
}
38+
}
39+
40+
func TestCreateOutputFileMessageWithTrailingNewline(t *testing.T) {
41+
fileName := "test_output_with_newline.txt"
42+
message := "test message with newline\n"
43+
44+
if err := CreateOutputFile(message, fileName); err != nil {
45+
t.Fatalf("CreateOutputFile() error = %v", err)
46+
}
47+
t.Cleanup(func() { os.Remove(fileName) })
48+
49+
data, err := os.ReadFile(fileName)
50+
if err != nil {
51+
t.Fatalf("failed to read output file: %v", err)
52+
}
53+
54+
if string(data) != message {
55+
t.Fatalf("expected file contents %q, got %q", message, data)
56+
}
2857
}

internal/core/chatter.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func (o *Chatter) Send(request *domain.ChatRequest, opts *domain.ChatOptions) (s
6969
responseChan := make(chan string)
7070
errChan := make(chan error, 1)
7171
done := make(chan struct{})
72+
printedStream := false
7273

7374
go func() {
7475
defer close(done)
@@ -81,9 +82,14 @@ func (o *Chatter) Send(request *domain.ChatRequest, opts *domain.ChatOptions) (s
8182
message += response
8283
if !opts.SuppressThink {
8384
fmt.Print(response)
85+
printedStream = true
8486
}
8587
}
8688

89+
if printedStream && !opts.SuppressThink && !strings.HasSuffix(message, "\n") {
90+
fmt.Println()
91+
}
92+
8793
// Wait for goroutine to finish
8894
<-done
8995

0 commit comments

Comments
 (0)