From 6d0c23c1620862d306977c71228c53c1f4bc460c Mon Sep 17 00:00:00 2001 From: Daniel Konecny Date: Fri, 16 Jul 2021 00:03:25 +0200 Subject: [PATCH 1/2] Fix #92 Fprint into the View doesn't scroll correctly content if `Autoscroll` and `Wrap` are turned on. It's caused when checking buffer lines while using incorrect slice. --- _examples/wrap_autoscroll.go | 70 ++++++++++++++++++++++++++++++++++++ view.go | 2 +- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 _examples/wrap_autoscroll.go diff --git a/_examples/wrap_autoscroll.go b/_examples/wrap_autoscroll.go new file mode 100644 index 0000000..cd50c8a --- /dev/null +++ b/_examples/wrap_autoscroll.go @@ -0,0 +1,70 @@ +// Copyright 2014 The gocui Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "errors" + "fmt" + "log" + + "github.com/awesome-gocui/gocui" +) + +var tabCount = 0 + +func main() { + g, err := gocui.NewGui(gocui.OutputNormal, true) + if err != nil { + log.Panicln(err) + } + defer g.Close() + + g.SetManagerFunc(layout) + + if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { + log.Panicln(err) + } + if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, print); err != nil { + log.Panicln(err) + } + + if err := g.MainLoop(); err != nil && !errors.Is(err, gocui.ErrQuit) { + log.Panicln(err) + } +} + +func layout(g *gocui.Gui) error { + maxX, maxY := g.Size() + if v, err := g.SetView("main", maxX/2-7, maxY/2-1, maxX/2+7, maxY/2+4, 0); err != nil { + if !errors.Is(err, gocui.ErrUnknownView) { + return err + } + + if _, err := g.SetCurrentView("main"); err != nil { + return err + } + v.Clear() + v.Autoscroll = true + v.Wrap = true + + fmt.Fprintln(v, "Hello world!") + } + + return nil +} + +func print(g *gocui.Gui, v *gocui.View) error { + tabCount++ + if tabCount%10 == 0 { + fmt.Fprintln(v, tabCount, "Hello!") + } else { + fmt.Fprintln(v, tabCount, "Hello Cruel World !") + } + return nil +} + +func quit(g *gocui.Gui, v *gocui.View) error { + return gocui.ErrQuit +} diff --git a/view.go b/view.go index 4ba42e5..f22ddf0 100644 --- a/view.go +++ b/view.go @@ -572,7 +572,7 @@ func (v *View) draw() error { linesToRender := v.viewLines() if v.Autoscroll && len(linesToRender) > maxY { - v.oy = len(v.lines) - maxY + v.oy = len(linesToRender) - maxY - 1 } newCache := []cellCache{} From 4bc2c0081fcd0c4fe7948cab62021b1bcb841420 Mon Sep 17 00:00:00 2001 From: Daniel Konecny Date: Fri, 16 Jul 2021 14:28:00 +0200 Subject: [PATCH 2/2] Remove unnecessary example This example just test the autorscroll and wrap together. This can be recreated by using `active.go` or `custom_frames.go` example and resize terminal so the text in the second view will be forced to wrap. --- _examples/wrap_autoscroll.go | 70 ------------------------------------ 1 file changed, 70 deletions(-) delete mode 100644 _examples/wrap_autoscroll.go diff --git a/_examples/wrap_autoscroll.go b/_examples/wrap_autoscroll.go deleted file mode 100644 index cd50c8a..0000000 --- a/_examples/wrap_autoscroll.go +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2014 The gocui Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package main - -import ( - "errors" - "fmt" - "log" - - "github.com/awesome-gocui/gocui" -) - -var tabCount = 0 - -func main() { - g, err := gocui.NewGui(gocui.OutputNormal, true) - if err != nil { - log.Panicln(err) - } - defer g.Close() - - g.SetManagerFunc(layout) - - if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { - log.Panicln(err) - } - if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, print); err != nil { - log.Panicln(err) - } - - if err := g.MainLoop(); err != nil && !errors.Is(err, gocui.ErrQuit) { - log.Panicln(err) - } -} - -func layout(g *gocui.Gui) error { - maxX, maxY := g.Size() - if v, err := g.SetView("main", maxX/2-7, maxY/2-1, maxX/2+7, maxY/2+4, 0); err != nil { - if !errors.Is(err, gocui.ErrUnknownView) { - return err - } - - if _, err := g.SetCurrentView("main"); err != nil { - return err - } - v.Clear() - v.Autoscroll = true - v.Wrap = true - - fmt.Fprintln(v, "Hello world!") - } - - return nil -} - -func print(g *gocui.Gui, v *gocui.View) error { - tabCount++ - if tabCount%10 == 0 { - fmt.Fprintln(v, tabCount, "Hello!") - } else { - fmt.Fprintln(v, tabCount, "Hello Cruel World !") - } - return nil -} - -func quit(g *gocui.Gui, v *gocui.View) error { - return gocui.ErrQuit -}