Skip to content

Commit 85582bd

Browse files
bug: fix repeating content in scrollback buffer
Also synchronize paint operations for slow systems like powershell
1 parent 038d3ee commit 85582bd

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

display.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"atomicgo.dev/cursor"
9+
"github.com/pterm/pterm"
910
)
1011

1112
var (
@@ -23,6 +24,7 @@ type display struct {
2324
displayState
2425
prompter *prompter
2526
contentLock sync.Mutex
27+
paintLock sync.Mutex
2628
closer func()
2729
}
2830

@@ -49,6 +51,8 @@ func newDisplay(tool string) (*display, error) {
4951

5052
func (a *display) readline(f func() (string, bool)) (string, bool) {
5153
a.paint()
54+
a.paintLock.Lock()
55+
defer a.paintLock.Unlock()
5256
cursor.Show()
5357
defer cursor.Hide()
5458
return f()
@@ -68,7 +72,7 @@ func (a *display) setMultiLinePrompt(text string) {
6872
if len(lines) > 1 {
6973
a.contentLock.Lock()
7074
defer a.contentLock.Unlock()
71-
a.content = a.area.content + "\n" + strings.Join(lines[:len(lines)-1], "\n") + "\n"
75+
a.content = a.content + "\n" + strings.Join(lines[:len(lines)-1], "\n") + "\n"
7276
}
7377
}
7478

@@ -104,10 +108,12 @@ func (a *display) Prompt(text string) (string, bool) {
104108
}
105109

106110
func (a *display) paint() {
111+
a.paintLock.Lock()
112+
defer a.paintLock.Unlock()
113+
107114
a.contentLock.Lock()
108115
if a.finish {
109116
a.area.Update(a.content)
110-
cursor.Show()
111117
a.displayState = displayState{}
112118
a.contentLock.Unlock()
113119
return
@@ -120,7 +126,12 @@ func (a *display) paint() {
120126
return
121127
}
122128

123-
a.area.Update(newContent)
129+
lines := strings.Split(newContent, "\n")
130+
height := pterm.GetTerminalHeight()
131+
if len(lines) > height {
132+
lines = lines[len(lines)-height:]
133+
}
134+
a.area.Update(strings.Join(lines, "\n"))
124135
a.lastPrint = newContent
125136
}
126137

run.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ func Run(ctx context.Context, tool string, opts ...RunOptions) error {
308308
}
309309

310310
input = line
311+
ui.Progress(render(input, nil))
312+
311313
run, err = run.NextChat(localCtx, input)
312314
if err != nil {
313315
fmt.Println(color.RedString("%v", run.Err()))
@@ -349,10 +351,13 @@ func render(input string, run *gptscript.Run) string {
349351

350352
if input != "" {
351353
buf.WriteString(color.GreenString(splitAtTerm("> "+input+"\n", pterm.GetTerminalWidth())))
354+
buf.WriteString("\n")
352355
}
353356

354-
if call, ok := run.ParentCallFrame(); ok {
355-
printCall(buf, run.Calls(), call, nil)
357+
if run != nil {
358+
if call, ok := run.ParentCallFrame(); ok {
359+
printCall(buf, run.Calls(), call, nil)
360+
}
356361
}
357362

358363
return buf.String()

0 commit comments

Comments
 (0)