diff options
| author | Jeff Carr <[email protected]> | 2025-02-06 16:19:36 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-02-06 16:19:36 -0600 |
| commit | e134ecb6a4285d576c45c3f86274703f32ab30b0 (patch) | |
| tree | 4c07d83ac6a4f195c708c50ca1b4b492931d662e /stdoutShow.go | |
| parent | 5bae8b7e41fc17a5020f4c8e58695ae59063cda4 (diff) | |
make stdout upside down
Diffstat (limited to 'stdoutShow.go')
| -rw-r--r-- | stdoutShow.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/stdoutShow.go b/stdoutShow.go index 94c7a1f..61f2246 100644 --- a/stdoutShow.go +++ b/stdoutShow.go @@ -6,6 +6,8 @@ package main import ( "errors" "fmt" + "slices" + "strings" "github.com/awesome-gocui/gocui" @@ -126,3 +128,50 @@ func (tk *guiWidget) relocateStdout(w int, h int) { me.baseGui.SetView("msg", w0, h0, w1, h1, 0) // me.baseGui.SetViewOnBottom("msg") } + +// from the gocui devs: +// Write appends a byte slice into the view's internal buffer. Because +// View implements the io.Writer interface, it can be passed as parameter +// of functions like fmt.Fprintf, fmt.Fprintln, io.Copy, etc. Clear must +// be called to clear the view's buffer. + +func (w *guiWidget) Write(p []byte) (n int, err error) { + w.tainted = true + me.writeMutex.Lock() + defer me.writeMutex.Unlock() + + tk := me.stdout.tk + + if tk.v == nil { + // optionally write the output to /tmp + s := fmt.Sprint(string(p)) + s = strings.TrimSuffix(s, "\n") + fmt.Fprintln(outf, s) + v, _ := me.baseGui.View("msg") + if v != nil { + // fmt.Fprintln(outf, "found msg") + tk.v = v + } + } else { + // display the output in the gocui window + lines := strings.Split(strings.TrimSpace(string(p)), "\n") + me.stdout.outputS = append(me.stdout.outputS, lines...) + + var cur []string + // chop off the last lines in the buffer + chop := len(me.stdout.outputS) - (me.stdout.h - 1) + if chop < 0 { + chop = 0 + } + if len(me.stdout.outputS) > chop { + cur = append(cur, me.stdout.outputS[chop:]...) + } else { + cur = append(cur, me.stdout.outputS...) + } + slices.Reverse(cur) + tk.v.Clear() + fmt.Fprintln(tk.v, strings.Join(cur, "\n")) + } + + return len(p), nil +} |
