Skip to content

Commit 7ec0085

Browse files
committed
Also report panic via debugprintln
1 parent 51adca0 commit 7ec0085

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

tools/tui/loop/api.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,7 @@ func (self *Loop) DebugPrintln(args ...any) {
297297
const limit = 2048
298298
msg := fmt.Sprintln(args...)
299299
for i := 0; i < len(msg); i += limit {
300-
end := i + limit
301-
if end > len(msg) {
302-
end = len(msg)
303-
}
300+
end := min(i+limit, len(msg))
304301
self.QueueWriteString("\x1bP@kitty-print|")
305302
self.QueueWriteString(base64.StdEncoding.EncodeToString([]byte(msg[i:end])))
306303
self.QueueWriteString("\x1b\\")
@@ -311,11 +308,12 @@ func (self *Loop) DebugPrintln(args ...any) {
311308
func (self *Loop) Run() (err error) {
312309
defer func() {
313310
if r := recover(); r != nil {
314-
pcs := make([]uintptr, 256)
311+
pcs := make([]uintptr, 512)
315312
n := runtime.Callers(2, pcs)
313+
lines := []string{}
316314
frames := runtime.CallersFrames(pcs[:n])
317315
err = fmt.Errorf("Panicked: %s", r)
318-
fmt.Fprintf(os.Stderr, "\r\nPanicked with error: %s\r\nStacktrace (most recent call first):\r\n", r)
316+
lines = append(lines, fmt.Sprintf("\r\nPanicked with error: %s\r\nStacktrace (most recent call first):\r\n", r))
319317
found_first_frame := false
320318
for frame, more := frames.Next(); more; frame, more = frames.Next() {
321319
if !found_first_frame {
@@ -324,8 +322,11 @@ func (self *Loop) Run() (err error) {
324322
}
325323
found_first_frame = true
326324
}
327-
fmt.Fprintf(os.Stderr, "%s\r\n\t%s:%d\r\n", frame.Function, frame.File, frame.Line)
325+
lines = append(lines, fmt.Sprintf("%s\r\n\t%s:%d\r\n", frame.Function, frame.File, frame.Line))
328326
}
327+
text := strings.Join(lines, "")
328+
os.Stderr.WriteString(text)
329+
tty.DebugPrintln(strings.TrimSpace(text))
329330
if self.terminal_options.Alternate_screen {
330331
term, err := tty.OpenControllingTerm(tty.SetRaw)
331332
if err == nil {

0 commit comments

Comments
 (0)