diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-01-11 14:54:59 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-01-12 11:17:20 +1100 |
commit | 23bcc191802eb1442f8edd0123051769ea9e978b (patch) | |
tree | e4786eed94218f41baea5a689f2a76dd9549463c /pkg/gui/gui.go | |
parent | 282f08df36eb2939d1a1caf12457680b38089e0a (diff) |
allow fast flicking through any list panel
Up till now our approach to rendering things like file diffs, branch logs, and
commit patches, has been to run a command on the command line, wait for it to
complete, take its output as a string, and then write that string to the main
view (or secondary view e.g. when showing both staged and unstaged changes of a
file).
This has caused various issues. For once, if you are flicking through a list of
files and an untracked file is particularly large, not only will this require
lazygit to load that whole file into memory (or more accurately it's equally
large diff), it also will slow down the UI thread while loading that file, and
if the user continued down the list, the original command might eventually
resolve and replace whatever the diff is for the newly selected file.
Following what we've done in lazydocker, I've added a tasks package for when you
need something done but you want it to cancel as soon as something newer comes
up. Given this typically involves running a command to display to a view, I've
added a viewBufferManagerMap struct to the Gui struct which allows you to define
these tasks on a per-view basis.
viewBufferManagers can run files and directly write the output to their view,
meaning we no longer need to use so much memory.
In the tasks package there is a helper method called NewCmdTask which takes a
command, an initial amount of lines to read, and then runs that command, reads
that number of lines, and allows for a readLines channel to tell it to read more
lines. We read more lines when we scroll or resize the window.
There is an adapter for the tasks package in a file called tasks_adapter which
wraps the functions from the tasks package in gui-specific stuff like clearing
the main view before starting the next task that wants to write to the main
view.
I've removed some small features as part of this work, namely the little headers
that were at the top of the main view for some situations. For example, we no
longer show the upstream of a selected branch. I want to re-introduce this in
the future, but I didn't want to make this tasks system too complicated, and in
order to facilitate a header section in the main view we'd need to have a task
that gets the upstream for the current branch, writes it to the header, then
tells another task to write the branch log to the main view, but without
clearing inbetween. So it would get messy. I'm thinking instead of having a
separate 'header' view atop the main view to render that kind of thing (which
can happen in another PR)
I've also simplified the 'git show' to just call 'git show' and not do anything
fancy when it comes to merge commits.
I considered using this tasks approach whenever we write to a view. The only
thing is that the renderString method currently resets the origin of a view and
I don't want to lose that. So I've left some in there that I consider harmless,
but we should probably be just using tasks now for all rendering, even if it's
just strings we can instantly make.
Diffstat (limited to 'pkg/gui/gui.go')
-rw-r--r-- | pkg/gui/gui.go | 80 |
1 files changed, 55 insertions, 25 deletions
diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 2bdb66e0f..d748b4d72 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -25,6 +25,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/i18n" + "github.com/jesseduffield/lazygit/pkg/tasks" "github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/updates" "github.com/jesseduffield/lazygit/pkg/utils" @@ -68,20 +69,21 @@ type Teml i18n.Teml // Gui wraps the gocui Gui object which handles rendering and events type Gui struct { - g *gocui.Gui - Log *logrus.Entry - GitCommand *commands.GitCommand - OSCommand *commands.OSCommand - SubProcess *exec.Cmd - State guiState - Config config.AppConfigurer - Tr *i18n.Localizer - Errors SentinelErrors - Updater *updates.Updater - statusManager *statusManager - credentials credentials - waitForIntro sync.WaitGroup - fileWatcher *fileWatcher + g *gocui.Gui + Log *logrus.Entry + GitCommand *commands.GitCommand + OSCommand *commands.OSCommand + SubProcess *exec.Cmd + State guiState + Config config.AppConfigurer + Tr *i18n.Localizer + Errors SentinelErrors + Updater *updates.Updater + statusManager *statusManager + credentials credentials + waitForIntro sync.WaitGroup + fileWatcher *fileWatcher + viewBufferManagerMap map[string]*tasks.ViewBufferManager } // for now the staging panel state, unlike the other panel states, is going to be @@ -229,14 +231,15 @@ func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *comma } gui := &Gui{ - Log: log, - GitCommand: gitCommand, - OSCommand: oSCommand, - State: initialState, - Config: config, - Tr: tr, - Updater: updater, - statusManager: &statusManager{}, + Log: log, + GitCommand: gitCommand, + OSCommand: oSCommand, + State: initialState, + Config: config, + Tr: tr, + Updater: updater, + statusManager: &statusManager{}, + viewBufferManagerMap: map[string]*tasks.ViewBufferManager{}, } gui.watchFilesForChanges() @@ -261,8 +264,14 @@ func (gui *Gui) scrollDownView(viewName string) error { _, sy := mainView.Size() y += sy } + scrollHeight := gui.Config.GetUserConfig().GetInt("gui.scrollHeight") if y < len(mainView.BufferLines()) { - return mainView.SetOrigin(ox, oy+gui.Config.GetUserConfig().GetInt("gui.scrollHeight")) + if err := mainView.SetOrigin(ox, oy+scrollHeight); err != nil { + return err + } + } + if manager, ok := gui.viewBufferManagerMap[viewName]; ok { + manager.ReadLines(scrollHeight) } return nil } @@ -465,7 +474,23 @@ func (gui *Gui) layout(g *gocui.Gui) error { secondary = "main" } - v, err := g.SetView(main, leftSideWidth+panelSpacing, 0, panelSplitX, height-2, gocui.LEFT) + // reading more lines into main view buffers upon resize + mainHeight := height - 2 + prevMainView, err := gui.g.View("main") + if err == nil { + _, prevMainHeight := prevMainView.Size() + heightDiff := mainHeight - 1 - prevMainHeight + if heightDiff > 0 { + if manager, ok := gui.viewBufferManagerMap["main"]; ok { + manager.ReadLines(heightDiff) + } + if manager, ok := gui.viewBufferManagerMap["secondary"]; ok { + manager.ReadLines(heightDiff) + } + } + } + + v, err := g.SetView(main, leftSideWidth+panelSpacing, 0, panelSplitX, mainHeight, gocui.LEFT) if err != nil { if err.Error() != "unknown view" { return err @@ -479,7 +504,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { if !gui.State.SplitMainPanel { hiddenViewOffset = 9999 } - secondaryView, err := g.SetView(secondary, panelSplitX+1+hiddenViewOffset, hiddenViewOffset, width-1+hiddenViewOffset, height-2+hiddenViewOffset, gocui.LEFT) + secondaryView, err := g.SetView(secondary, panelSplitX+1+hiddenViewOffset, hiddenViewOffset, width-1+hiddenViewOffset, mainHeight+hiddenViewOffset, gocui.LEFT) if err != nil { if err.Error() != "unknown view" { return err @@ -843,6 +868,11 @@ func (gui *Gui) Run() error { func (gui *Gui) RunWithSubprocesses() error { for { if err := gui.Run(); err != nil { + for _, manager := range gui.viewBufferManagerMap { + manager.Close() + } + gui.viewBufferManagerMap = map[string]*tasks.ViewBufferManager{} + if err == gocui.ErrQuit { if !gui.State.RetainOriginalDir { if err := gui.recordCurrentDirectory(); err != nil { |