summaryrefslogtreecommitdiffstats
path: root/pkg/gui
diff options
context:
space:
mode:
authorStefan Haller <stefan@haller-berlin.de>2023-03-10 13:35:19 +0100
committerStefan Haller <stefan@haller-berlin.de>2023-03-21 18:26:18 +0100
commit4adca84d68105f703f8c4755229c6e46914a99ca (patch)
tree47917284a40f2118325d1530377701f948b85bfa /pkg/gui
parent4b67a45a16196a388216a2dee63c971e32a8ff47 (diff)
Make sure scrollbars have the right size initially
We refresh the view after reading just enough to fill it, so that we see the initial content as quickly as possible, but then we continue reading enough lines so that we can tell how long the scrollbar needs to be, and then we refresh again. This can result in slight flicker of the scrollbar when it is first drawn with a bigger size and then jumps to a smaller size; however, that's a good tradeoff for a solution that provides both good speed and accuracy.
Diffstat (limited to 'pkg/gui')
-rw-r--r--pkg/gui/pty.go6
-rw-r--r--pkg/gui/tasks_adapter.go6
-rw-r--r--pkg/gui/view_helpers.go28
3 files changed, 32 insertions, 8 deletions
diff --git a/pkg/gui/pty.go b/pkg/gui/pty.go
index 4a87c98da..fe53697c8 100644
--- a/pkg/gui/pty.go
+++ b/pkg/gui/pty.go
@@ -55,9 +55,6 @@ func (gui *Gui) newPtyTask(view *gocui.View, cmd *exec.Cmd, prefix string) error
cmd.Env = append(cmd.Env, "GIT_PAGER="+pager)
- _, height := view.Size()
- _, oy := view.Origin()
-
manager := gui.getManager(view)
var ptmx *os.File
@@ -82,7 +79,8 @@ func (gui *Gui) newPtyTask(view *gocui.View, cmd *exec.Cmd, prefix string) error
gui.Mutexes.PtyMutex.Unlock()
}
- if err := manager.NewTask(manager.NewCmdTask(start, prefix, height+oy+10, onClose), cmdStr); err != nil {
+ linesToRead := gui.linesToReadFromCmdTask(view)
+ if err := manager.NewTask(manager.NewCmdTask(start, prefix, linesToRead, onClose), cmdStr); err != nil {
return err
}
diff --git a/pkg/gui/tasks_adapter.go b/pkg/gui/tasks_adapter.go
index 33aa09eb9..69b64d7b1 100644
--- a/pkg/gui/tasks_adapter.go
+++ b/pkg/gui/tasks_adapter.go
@@ -16,9 +16,6 @@ func (gui *Gui) newCmdTask(view *gocui.View, cmd *exec.Cmd, prefix string) error
cmdStr,
).Debug("RunCommand")
- _, height := view.Size()
- _, oy := view.Origin()
-
manager := gui.getManager(view)
start := func() (*exec.Cmd, io.Reader) {
@@ -35,7 +32,8 @@ func (gui *Gui) newCmdTask(view *gocui.View, cmd *exec.Cmd, prefix string) error
return cmd, r
}
- if err := manager.NewTask(manager.NewCmdTask(start, prefix, height+oy+10, nil), cmdStr); err != nil {
+ linesToRead := gui.linesToReadFromCmdTask(view)
+ if err := manager.NewTask(manager.NewCmdTask(start, prefix, linesToRead, nil), cmdStr); err != nil {
gui.c.Log.Error(err)
}
diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go
index c51942b03..8f2055245 100644
--- a/pkg/gui/view_helpers.go
+++ b/pkg/gui/view_helpers.go
@@ -6,6 +6,7 @@ import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
"github.com/jesseduffield/lazygit/pkg/gui/types"
+ "github.com/jesseduffield/lazygit/pkg/tasks"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/spkg/bom"
)
@@ -15,6 +16,33 @@ func (gui *Gui) resetOrigin(v *gocui.View) error {
return v.SetOrigin(0, 0)
}
+// Returns the number of lines that we should read initially from a cmd task so
+// that the scrollbar has the correct size, along with the number of lines after
+// which the view is filled and we can do a first refresh.
+func (gui *Gui) linesToReadFromCmdTask(v *gocui.View) tasks.LinesToRead {
+ _, height := v.Size()
+ _, oy := v.Origin()
+
+ linesForFirstRefresh := height + oy + 10
+
+ // We want to read as many lines initially as necessary to let the
+ // scrollbar go to its minimum height, so that the scrollbar thumb doesn't
+ // change size as you scroll down.
+ minScrollbarHeight := 2
+ linesToReadForAccurateScrollbar := height*(height-1)/minScrollbarHeight + oy
+
+ // However, cap it at some arbitrary max limit, so that we don't get
+ // performance problems for huge monitors or tiny font sizes
+ if linesToReadForAccurateScrollbar > 5000 {
+ linesToReadForAccurateScrollbar = 5000
+ }
+
+ return tasks.LinesToRead{
+ Total: linesToReadForAccurateScrollbar,
+ InitialRefreshAfter: linesForFirstRefresh,
+ }
+}
+
func (gui *Gui) cleanString(s string) string {
output := string(bom.Clean([]byte(s)))
return utils.NormalizeLinefeeds(output)