2018-08-12 11:31:27 +02:00
package gui
2018-05-19 03:16:34 +02:00
import (
2019-05-26 03:24:01 +02:00
"fmt"
"io/ioutil"
2019-05-18 03:28:07 +02:00
"os"
2020-01-07 04:22:15 +02:00
"runtime"
2018-12-07 20:22:22 +02:00
"sync"
2018-05-26 05:23:39 +02:00
2018-07-21 07:51:18 +02:00
// "io"
// "io/ioutil"
2018-05-26 05:23:39 +02:00
2018-08-12 13:04:47 +02:00
"os/exec"
2018-07-21 10:37:00 +02:00
"strings"
2018-07-21 07:51:18 +02:00
"time"
2019-02-11 12:30:27 +02:00
"github.com/go-errors/errors"
2018-07-21 07:51:18 +02:00
// "strings"
2018-08-06 08:11:29 +02:00
2018-11-25 14:15:36 +02:00
"github.com/fatih/color"
2018-07-21 07:51:18 +02:00
"github.com/golang-collections/collections/stack"
"github.com/jesseduffield/gocui"
2018-08-13 12:26:02 +02:00
"github.com/jesseduffield/lazygit/pkg/commands"
2020-08-15 03:18:40 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/patch"
2018-08-18 05:22:05 +02:00
"github.com/jesseduffield/lazygit/pkg/config"
2018-08-14 15:26:25 +02:00
"github.com/jesseduffield/lazygit/pkg/i18n"
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.
2020-01-11 05:54:59 +02:00
"github.com/jesseduffield/lazygit/pkg/tasks"
2019-10-18 09:48:37 +02:00
"github.com/jesseduffield/lazygit/pkg/theme"
2018-08-19 15:28:29 +02:00
"github.com/jesseduffield/lazygit/pkg/updates"
2019-02-25 13:11:35 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
2020-01-07 04:22:15 +02:00
"github.com/mattn/go-runewidth"
2018-08-25 03:02:46 +02:00
"github.com/sirupsen/logrus"
2018-05-19 03:16:34 +02:00
)
2020-02-24 23:32:46 +02:00
const (
SCREEN_NORMAL int = iota
SCREEN_HALF
SCREEN_FULL
)
2019-11-10 13:07:45 +02:00
const StartupPopupVersion = 1
2018-08-05 14:18:59 +02:00
// OverlappingEdges determines if panel edges overlap
var OverlappingEdges = false
2018-08-05 14:02:19 +02:00
2018-08-14 15:47:14 +02:00
// SentinelErrors are the errors that have special meaning and need to be checked
// by calling functions. The less of these, the better
type SentinelErrors struct {
ErrSubProcess error
ErrNoFiles error
2018-09-07 01:41:15 +02:00
ErrSwitchRepo error
2020-03-28 07:28:35 +02:00
ErrRestart error
2018-08-14 15:47:14 +02:00
}
// GenerateSentinelErrors makes the sentinel errors for the gui. We're defining it here
// because we can't do package-scoped errors with localization, and also because
// it seems like package-scoped variables are bad in general
// https://dave.cheney.net/2017/06/11/go-without-package-scoped-variables
// In the future it would be good to implement some of the recommendations of
// that article. For now, if we don't need an error to be a sentinel, we will just
// define it inline. This has implications for error messages that pop up everywhere
// in that we'll be duplicating the default values. We may need to look at
// having a default localisation bundle defined, and just using keys-only when
// localising things in the code.
func ( gui * Gui ) GenerateSentinelErrors ( ) {
gui . Errors = SentinelErrors {
2018-08-16 07:16:32 +02:00
ErrSubProcess : errors . New ( gui . Tr . SLocalize ( "RunningSubprocess" ) ) ,
ErrNoFiles : errors . New ( gui . Tr . SLocalize ( "NoChangedFiles" ) ) ,
2018-09-07 01:41:15 +02:00
ErrSwitchRepo : errors . New ( "switching repo" ) ,
2020-03-28 07:28:35 +02:00
ErrRestart : errors . New ( "restarting" ) ,
2018-08-14 15:47:14 +02:00
}
}
2018-08-14 11:05:26 +02:00
2018-08-16 11:31:03 +02:00
// Teml is short for template used to make the required map[string]interface{} shorter when using gui.Tr.SLocalize and gui.Tr.TemplateLocalize
2018-08-16 13:35:04 +02:00
type Teml i18n . Teml
2018-08-12 13:04:47 +02:00
2018-08-13 12:26:02 +02:00
// Gui wraps the gocui Gui object which handles rendering and events
type Gui struct {
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.
2020-01-11 05:54:59 +02:00
g * gocui . Gui
Log * logrus . Entry
GitCommand * commands . GitCommand
OSCommand * commands . OSCommand
SubProcess * exec . Cmd
2020-03-09 02:34:10 +02:00
State * guiState
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.
2020-01-11 05:54:59 +02:00
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
2020-01-31 10:46:29 +02:00
stopChan chan struct { }
2020-08-16 14:49:37 +02:00
// when lazygit is opened outside a git directory we want to open to the most
// recent repo with the recent repos popup showing
2020-08-19 01:05:43 +02:00
showRecentRepos bool
Contexts ContextTree
2020-08-19 10:06:51 +02:00
ViewTabContextMap map [ string ] [ ] tabContext
2018-08-13 13:16:21 +02:00
}
2020-08-20 00:52:51 +02:00
type listPanelState struct {
2020-08-20 00:53:10 +02:00
SelectedLineIdx int
2020-08-19 13:51:50 +02:00
}
2020-08-20 00:52:51 +02:00
func ( h * listPanelState ) SetSelectedLineIdx ( value int ) {
2020-08-20 00:53:10 +02:00
h . SelectedLineIdx = value
2020-08-20 00:52:51 +02:00
}
func ( h * listPanelState ) GetSelectedLineIdx ( ) int {
2020-08-20 00:53:10 +02:00
return h . SelectedLineIdx
2020-08-20 00:52:51 +02:00
}
type IListPanelState interface {
SetSelectedLineIdx ( int )
GetSelectedLineIdx ( ) int
}
2018-12-07 09:52:31 +02:00
// for now the staging panel state, unlike the other panel states, is going to be
// non-mutative, so that we don't accidentally end up
// with mismatches of data. We might change this in the future
2019-11-05 05:21:19 +02:00
type lineByLinePanelState struct {
2019-11-04 10:47:25 +02:00
SelectedLineIdx int
FirstLineIdx int
LastLineIdx int
Diff string
2020-08-15 03:18:40 +02:00
PatchParser * patch . PatchParser
2019-11-04 10:47:25 +02:00
SelectMode int // one of LINE, HUNK, or RANGE
SecondaryFocused bool // this is for if we show the left or right panel
2018-12-04 10:50:11 +02:00
}
2018-12-08 07:54:54 +02:00
type mergingPanelState struct {
ConflictIndex int
ConflictTop bool
Conflicts [ ] commands . Conflict
EditHistory * stack . Stack
2020-05-19 10:29:56 +02:00
// UserScrolling tells us if the user has started scrolling through the file themselves
// in which case we won't auto-scroll to a conflict.
UserScrolling bool
2018-12-08 07:54:54 +02:00
}
2018-12-04 10:50:11 +02:00
type filePanelState struct {
2020-08-20 00:52:51 +02:00
listPanelState
2018-12-04 10:50:11 +02:00
}
2019-11-13 14:18:31 +02:00
// TODO: consider splitting this out into the window and the branches view
2018-12-04 10:50:11 +02:00
type branchPanelState struct {
2020-08-20 00:52:51 +02:00
listPanelState
2019-11-13 14:18:31 +02:00
}
type remotePanelState struct {
2020-08-20 00:52:51 +02:00
listPanelState
2018-12-04 10:50:11 +02:00
}
2019-11-16 08:35:59 +02:00
type remoteBranchesState struct {
2020-08-20 00:52:51 +02:00
listPanelState
2019-11-16 08:35:59 +02:00
}
2019-11-18 00:38:36 +02:00
type tagsPanelState struct {
2020-08-20 00:52:51 +02:00
listPanelState
2019-11-18 00:38:36 +02:00
}
2018-12-04 10:50:11 +02:00
type commitPanelState struct {
2020-08-20 00:52:51 +02:00
listPanelState
2020-08-19 13:51:50 +02:00
2020-03-29 05:34:17 +02:00
LimitCommits bool
2018-12-04 10:50:11 +02:00
}
2020-01-09 12:34:17 +02:00
type reflogCommitPanelState struct {
2020-08-20 00:52:51 +02:00
listPanelState
2020-01-09 12:34:17 +02:00
}
2020-08-22 00:49:02 +02:00
type subCommitPanelState struct {
listPanelState
// e.g. name of branch whose commits we're looking at
refName string
}
2018-12-04 10:50:11 +02:00
type stashPanelState struct {
2020-08-20 00:52:51 +02:00
listPanelState
2018-12-04 10:50:11 +02:00
}
2018-12-06 13:18:17 +02:00
type menuPanelState struct {
2020-08-20 00:52:51 +02:00
listPanelState
2020-08-19 13:51:50 +02:00
OnPress func ( g * gocui . Gui , v * gocui . View ) error
2018-12-06 13:18:17 +02:00
}
2019-03-09 16:42:10 +02:00
type commitFilesPanelState struct {
2020-08-20 00:52:51 +02:00
listPanelState
2020-08-21 01:12:45 +02:00
// this is the SHA of the commit or the stash index of the stash.
// Not sure if ref is actually the right word here
2020-08-22 10:50:37 +02:00
refName string
canRebase bool
2019-03-09 16:42:10 +02:00
}
2018-12-04 10:50:11 +02:00
type panelStates struct {
2019-11-16 08:35:59 +02:00
Files * filePanelState
Branches * branchPanelState
Remotes * remotePanelState
RemoteBranches * remoteBranchesState
2019-11-18 00:38:36 +02:00
Tags * tagsPanelState
2019-11-16 08:35:59 +02:00
Commits * commitPanelState
2020-01-09 12:34:17 +02:00
ReflogCommits * reflogCommitPanelState
2020-08-22 00:49:02 +02:00
SubCommits * subCommitPanelState
2019-11-16 08:35:59 +02:00
Stash * stashPanelState
Menu * menuPanelState
LineByLine * lineByLinePanelState
Merging * mergingPanelState
CommitFiles * commitFilesPanelState
2018-12-02 10:57:01 +02:00
}
2020-02-23 12:53:30 +02:00
type searchingState struct {
view * gocui . View
isSearching bool
searchString string
}
2020-03-28 02:22:11 +02:00
// startup stages so we don't need to load everything at once
const (
INITIAL = iota
COMPLETE
)
2020-03-29 04:56:03 +02:00
// if ref is blank we're not diffing anything
2020-08-22 03:05:37 +02:00
type Diffing struct {
2020-03-29 05:34:17 +02:00
Ref string
Reverse bool
2020-03-29 04:56:03 +02:00
}
2020-08-22 03:44:03 +02:00
func ( m * Diffing ) Active ( ) bool {
return m . Ref != ""
}
2020-08-22 03:05:37 +02:00
type Filtering struct {
Path string // the filename that gets passed to git log
}
2020-08-22 03:44:03 +02:00
func ( m * Filtering ) Active ( ) bool {
return m . Path != ""
}
2020-08-22 03:05:37 +02:00
type CherryPicking struct {
CherryPickedCommits [ ] * commands . Commit
2020-08-22 03:44:03 +02:00
// we only allow cherry picking from one context at a time, so you can't copy a commit from the local commits context and then also copy a commit in the reflog context
ContextKey string
}
func ( m * CherryPicking ) Active ( ) bool {
return len ( m . CherryPickedCommits ) > 0
2020-08-22 03:05:37 +02:00
}
type Modes struct {
Filtering Filtering
CherryPicking CherryPicking
Diffing Diffing
}
2018-08-13 13:16:21 +02:00
type guiState struct {
2020-03-29 02:06:46 +02:00
Files [ ] * commands . File
Branches [ ] * commands . Branch
Commits [ ] * commands . Commit
StashEntries [ ] * commands . StashEntry
CommitFiles [ ] * commands . CommitFile
// FilteredReflogCommits are the ones that appear in the reflog panel.
// when in filtering mode we only include the ones that match the given path
FilteredReflogCommits [ ] * commands . Commit
// ReflogCommits are the ones used by the branches panel to obtain recency values
// if we're not in filtering mode, CommitFiles and FilteredReflogCommits will be
// one and the same
2020-03-28 01:27:34 +02:00
ReflogCommits [ ] * commands . Commit
2020-08-22 00:49:02 +02:00
SubCommits [ ] * commands . Commit
2020-03-28 01:27:34 +02:00
Remotes [ ] * commands . Remote
RemoteBranches [ ] * commands . RemoteBranch
Tags [ ] * commands . Tag
2020-08-20 00:24:35 +02:00
MenuItems [ ] * menuItem
2020-03-28 01:27:34 +02:00
Updating bool
Panels * panelStates
MainContext string // used to keep the main and secondary views' contexts in sync
SplitMainPanel bool
RetainOriginalDir bool
IsRefreshingFiles bool
RefreshingFilesMutex sync . Mutex
RefreshingStatusMutex sync . Mutex
Searching searchingState
ScreenMode int
SideView * gocui . View
Ptmx * os . File
PrevMainWidth int
PrevMainHeight int
OldInformation string
2020-08-22 03:05:37 +02:00
StartupStage int // one of INITIAL and COMPLETE. Allows us to not load everything at once
Modes Modes
2020-08-16 02:05:45 +02:00
2020-08-16 05:58:29 +02:00
ContextStack [ ] Context
ViewContextMap map [ string ] Context
2020-08-23 01:27:42 +02:00
// WindowViewNameMap is a mapping of windows to the current view of that window.
// Some views move between windows for example the commitFiles view and when cycling through
// side windows we need to know which view to give focus to for a given window
2020-08-16 05:58:29 +02:00
WindowViewNameMap map [ string ] string
2018-08-13 12:26:02 +02:00
}
2020-03-29 01:20:57 +02:00
func ( gui * Gui ) resetState ( ) {
2020-03-29 05:34:17 +02:00
// we carry over the filter path and diff state
2020-08-23 01:30:33 +02:00
prevFiltering := Filtering {
Path : "" ,
}
2020-08-22 03:05:37 +02:00
prevDiff := Diffing { }
2020-08-23 01:30:33 +02:00
prevCherryPicking := CherryPicking {
CherryPickedCommits : make ( [ ] * commands . Commit , 0 ) ,
ContextKey : "" ,
}
2020-03-29 01:20:57 +02:00
if gui . State != nil {
2020-08-23 01:30:33 +02:00
prevFiltering = gui . State . Modes . Filtering
2020-08-22 03:05:37 +02:00
prevDiff = gui . State . Modes . Diffing
2020-08-23 01:30:33 +02:00
prevCherryPicking = gui . State . Modes . CherryPicking
2020-08-22 03:05:37 +02:00
}
modes := Modes {
2020-08-23 01:30:33 +02:00
Filtering : prevFiltering ,
CherryPicking : prevCherryPicking ,
Diffing : prevDiff ,
2020-03-29 01:20:57 +02:00
}
2018-08-27 12:35:55 +02:00
2020-03-29 01:20:57 +02:00
gui . State = & guiState {
2020-03-29 02:06:46 +02:00
Files : make ( [ ] * commands . File , 0 ) ,
Commits : make ( [ ] * commands . Commit , 0 ) ,
FilteredReflogCommits : make ( [ ] * commands . Commit , 0 ) ,
ReflogCommits : make ( [ ] * commands . Commit , 0 ) ,
StashEntries : make ( [ ] * commands . StashEntry , 0 ) ,
2018-12-04 10:50:11 +02:00
Panels : & panelStates {
2020-08-22 00:49:02 +02:00
// TODO: work out why some of these are -1 and some are 0. Last time I checked there was a good reason but I'm less certain now
2020-08-20 00:53:10 +02:00
Files : & filePanelState { listPanelState { SelectedLineIdx : - 1 } } ,
Branches : & branchPanelState { listPanelState { SelectedLineIdx : 0 } } ,
Remotes : & remotePanelState { listPanelState { SelectedLineIdx : 0 } } ,
RemoteBranches : & remoteBranchesState { listPanelState { SelectedLineIdx : - 1 } } ,
Tags : & tagsPanelState { listPanelState { SelectedLineIdx : - 1 } } ,
Commits : & commitPanelState { listPanelState : listPanelState { SelectedLineIdx : - 1 } , LimitCommits : true } ,
2020-08-22 00:49:02 +02:00
ReflogCommits : & reflogCommitPanelState { listPanelState { SelectedLineIdx : 0 } } ,
SubCommits : & subCommitPanelState { listPanelState : listPanelState { SelectedLineIdx : 0 } , refName : "" } ,
2020-08-21 01:12:45 +02:00
CommitFiles : & commitFilesPanelState { listPanelState : listPanelState { SelectedLineIdx : - 1 } , refName : "" } ,
2020-08-20 00:53:10 +02:00
Stash : & stashPanelState { listPanelState { SelectedLineIdx : - 1 } } ,
Menu : & menuPanelState { listPanelState : listPanelState { SelectedLineIdx : 0 } , OnPress : nil } ,
2018-12-08 07:54:54 +02:00
Merging : & mergingPanelState {
ConflictIndex : 0 ,
ConflictTop : true ,
Conflicts : [ ] commands . Conflict { } ,
EditHistory : stack . New ( ) ,
} ,
2018-12-04 10:50:11 +02:00
} ,
2020-08-19 00:26:22 +02:00
SideView : nil ,
Ptmx : nil ,
2020-08-22 03:05:37 +02:00
Modes : modes ,
2020-08-19 00:26:22 +02:00
ViewContextMap : gui . initialViewContextMap ( ) ,
2018-08-13 12:26:02 +02:00
}
2020-03-29 01:20:57 +02:00
}
2018-08-13 12:26:02 +02:00
2020-03-29 01:20:57 +02:00
// for now the split view will always be on
// NewGui builds a new gui handler
2020-08-16 14:49:37 +02:00
func NewGui ( log * logrus . Entry , gitCommand * commands . GitCommand , oSCommand * commands . OSCommand , tr * i18n . Localizer , config config . AppConfigurer , updater * updates . Updater , filterPath string , showRecentRepos bool ) ( * Gui , error ) {
2018-08-14 15:47:14 +02:00
gui := & Gui {
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.
2020-01-11 05:54:59 +02:00
Log : log ,
GitCommand : gitCommand ,
OSCommand : oSCommand ,
Config : config ,
Tr : tr ,
Updater : updater ,
statusManager : & statusManager { } ,
viewBufferManagerMap : map [ string ] * tasks . ViewBufferManager { } ,
2020-08-16 14:49:37 +02:00
showRecentRepos : showRecentRepos ,
2018-08-14 15:47:14 +02:00
}
2020-03-29 01:20:57 +02:00
gui . resetState ( )
2020-08-22 03:05:37 +02:00
gui . State . Modes . Filtering . Path = filterPath
2020-08-19 00:26:22 +02:00
gui . Contexts = gui . contextTree ( )
2020-08-19 01:05:43 +02:00
gui . ViewTabContextMap = gui . viewTabContextMap ( )
2020-03-29 01:20:57 +02:00
2019-11-12 13:19:20 +02:00
gui . watchFilesForChanges ( )
2018-08-14 15:47:14 +02:00
gui . GenerateSentinelErrors ( )
return gui , nil
2018-08-13 12:26:02 +02:00
}
// Run setup the gui with keybindings and start the mainloop
2018-08-13 13:16:21 +02:00
func ( gui * Gui ) Run ( ) error {
2020-03-29 01:35:12 +02:00
gui . resetState ( )
2020-03-01 03:30:48 +02:00
g , err := gocui . NewGui ( gocui . Output256 , OverlappingEdges )
2018-07-21 07:51:18 +02:00
if err != nil {
2018-08-13 13:16:21 +02:00
return err
2018-07-21 07:51:18 +02:00
}
defer g . Close ( )
2020-02-23 12:53:30 +02:00
2020-08-22 03:44:03 +02:00
if gui . State . Modes . Filtering . Active ( ) {
2020-03-28 07:28:35 +02:00
gui . State . ScreenMode = SCREEN_HALF
} else {
gui . State . ScreenMode = SCREEN_NORMAL
}
2020-02-23 12:53:30 +02:00
g . OnSearchEscape = gui . onSearchEscape
g . SearchEscapeKey = gui . getKey ( "universal.return" )
g . NextSearchMatchKey = gui . getKey ( "universal.nextMatch" )
g . PrevSearchMatchKey = gui . getKey ( "universal.prevMatch" )
2020-01-31 10:46:29 +02:00
gui . stopChan = make ( chan struct { } )
2018-05-19 09:04:33 +02:00
2020-01-07 04:22:15 +02:00
g . ASCII = runtime . GOOS == "windows" && runewidth . IsEastAsian ( )
2019-03-03 07:15:20 +02:00
if gui . Config . GetUserConfig ( ) . GetBool ( "gui.mouseEvents" ) {
g . Mouse = true
}
2019-02-25 13:11:35 +02:00
2018-08-14 00:33:40 +02:00
gui . g = g // TODO: always use gui.g rather than passing g around everywhere
2019-10-19 11:39:18 +02:00
if err := gui . setColorScheme ( ) ; err != nil {
2018-08-18 05:53:58 +02:00
return err
}
2018-08-11 07:09:37 +02:00
2019-11-10 13:07:45 +02:00
popupTasks := [ ] func ( chan struct { } ) error { }
configPopupVersion := gui . Config . GetUserConfig ( ) . GetInt ( "StartupPopupVersion" )
// -1 means we've disabled these popups
if configPopupVersion != - 1 && configPopupVersion < StartupPopupVersion {
2020-08-15 00:10:56 +02:00
popupTasks = append ( popupTasks , gui . showIntroPopupMessage )
2018-12-07 20:22:22 +02:00
}
2019-11-10 13:07:45 +02:00
gui . showInitialPopups ( popupTasks )
2018-12-07 20:22:22 +02:00
2019-11-10 13:07:45 +02:00
gui . waitForIntro . Add ( 1 )
2019-08-06 14:47:24 +02:00
if gui . Config . GetUserConfig ( ) . GetBool ( "git.autoFetch" ) {
2019-07-19 13:56:53 +02:00
go gui . startBackgroundFetch ( )
}
2019-11-10 13:07:45 +02:00
2020-01-31 10:46:29 +02:00
gui . goEvery ( time . Second * 10 , gui . stopChan , gui . refreshFiles )
2018-07-21 10:37:00 +02:00
2019-02-16 03:07:27 +02:00
g . SetManager ( gocui . ManagerFunc ( gui . layout ) , gocui . ManagerFunc ( gui . getFocusLayout ( ) ) )
2018-05-19 09:04:33 +02:00
2018-08-13 12:26:02 +02:00
if err = gui . keybindings ( g ) ; err != nil {
2018-08-13 13:16:21 +02:00
return err
2018-07-21 07:51:18 +02:00
}
2018-05-19 09:04:33 +02:00
2019-11-10 13:07:45 +02:00
gui . Log . Warn ( "starting main loop" )
2018-08-07 10:05:43 +02:00
err = g . MainLoop ( )
2018-08-13 13:16:21 +02:00
return err
2018-08-13 12:26:02 +02:00
}
// RunWithSubprocesses loops, instantiating a new gocui.Gui with each iteration
// if the error returned from a run is a ErrSubProcess, it runs the subprocess
// otherwise it handles the error, possibly by quitting the application
2019-02-18 10:42:23 +02:00
func ( gui * Gui ) RunWithSubprocesses ( ) error {
2018-08-13 12:26:02 +02:00
for {
if err := gui . Run ( ) ; err != nil {
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.
2020-01-11 05:54:59 +02:00
for _ , manager := range gui . viewBufferManagerMap {
manager . Close ( )
}
gui . viewBufferManagerMap = map [ string ] * tasks . ViewBufferManager { }
2020-01-31 10:46:29 +02:00
if ! gui . fileWatcher . Disabled {
gui . fileWatcher . Watcher . Close ( )
}
close ( gui . stopChan )
2018-08-13 12:26:02 +02:00
if err == gocui . ErrQuit {
2019-10-07 03:34:12 +02:00
if ! gui . State . RetainOriginalDir {
if err := gui . recordCurrentDirectory ( ) ; err != nil {
return err
}
}
2018-08-13 12:26:02 +02:00
break
2018-09-07 01:41:15 +02:00
} else if err == gui . Errors . ErrSwitchRepo {
continue
2020-03-28 07:28:35 +02:00
} else if err == gui . Errors . ErrRestart {
continue
2018-08-14 15:47:14 +02:00
} else if err == gui . Errors . ErrSubProcess {
2019-05-26 03:24:01 +02:00
if err := gui . runCommand ( ) ; err != nil {
2019-03-12 12:43:56 +02:00
return err
}
2018-08-13 12:26:02 +02:00
} else {
2019-02-18 10:42:23 +02:00
return err
2018-08-13 12:26:02 +02:00
}
}
}
2019-02-18 10:42:23 +02:00
return nil
2018-05-19 09:04:33 +02:00
}
2019-05-26 03:24:01 +02:00
func ( gui * Gui ) runCommand ( ) error {
gui . SubProcess . Stdout = os . Stdout
gui . SubProcess . Stderr = os . Stdout
gui . SubProcess . Stdin = os . Stdin
2019-03-12 12:43:56 +02:00
2019-05-26 03:24:01 +02:00
fmt . Fprintf ( os . Stdout , "\n%s\n\n" , utils . ColoredString ( "+ " + strings . Join ( gui . SubProcess . Args , " " ) , color . FgBlue ) )
if err := gui . SubProcess . Run ( ) ; err != nil {
2019-03-12 12:43:56 +02:00
// not handling the error explicitly because usually we're going to see it
// in the output anyway
gui . Log . Error ( err )
}
2019-05-26 03:24:01 +02:00
gui . SubProcess . Stdout = ioutil . Discard
gui . SubProcess . Stderr = ioutil . Discard
gui . SubProcess . Stdin = nil
gui . SubProcess = nil
fmt . Fprintf ( os . Stdout , "\n%s" , utils . ColoredString ( gui . Tr . SLocalize ( "pressEnterToReturn" ) , color . FgGreen ) )
fmt . Scanln ( ) // wait for enter press
return nil
2019-03-12 12:43:56 +02:00
}
2020-03-29 01:31:34 +02:00
func ( gui * Gui ) loadNewRepo ( ) error {
gui . Updater . CheckForNewUpdate ( gui . onBackgroundUpdateCheckFinish , false )
if err := gui . updateRecentRepoList ( ) ; err != nil {
return err
2019-03-03 05:28:16 +02:00
}
2020-03-29 01:31:34 +02:00
gui . waitForIntro . Done ( )
2019-03-03 05:28:16 +02:00
2020-03-29 01:31:34 +02:00
if err := gui . refreshSidePanels ( refreshOptions { mode : ASYNC } ) ; err != nil {
return err
2020-03-28 07:28:35 +02:00
}
return nil
2019-02-25 13:11:35 +02:00
}
2019-10-19 11:39:18 +02:00
2020-03-29 01:31:34 +02:00
func ( gui * Gui ) showInitialPopups ( tasks [ ] func ( chan struct { } ) error ) {
gui . waitForIntro . Add ( len ( tasks ) )
done := make ( chan struct { } )
2019-10-19 11:39:18 +02:00
2020-03-29 01:31:34 +02:00
go func ( ) {
for _ , task := range tasks {
go func ( ) {
if err := task ( done ) ; err != nil {
_ = gui . surfaceError ( err )
}
} ( )
2019-10-19 11:39:18 +02:00
2020-03-29 01:31:34 +02:00
<- done
gui . waitForIntro . Done ( )
}
} ( )
2019-10-19 11:39:18 +02:00
}
2019-11-10 07:20:35 +02:00
2020-08-15 00:10:56 +02:00
func ( gui * Gui ) showIntroPopupMessage ( done chan struct { } ) error {
2020-08-15 08:36:39 +02:00
onConfirm := func ( ) error {
2020-03-29 01:31:34 +02:00
done <- struct { } { }
return gui . Config . WriteToUserConfig ( "startupPopupVersion" , StartupPopupVersion )
2019-11-10 07:20:35 +02:00
}
2020-08-15 08:38:16 +02:00
return gui . ask ( askOpts {
2020-08-15 08:36:39 +02:00
returnToView : nil ,
returnFocusOnClose : true ,
2020-08-15 00:10:56 +02:00
title : "" ,
prompt : gui . Tr . SLocalize ( "IntroPopupMessage" ) ,
2020-08-15 08:36:39 +02:00
handleConfirm : onConfirm ,
handleClose : onConfirm ,
} )
2019-11-10 07:20:35 +02:00
}
2020-03-29 01:31:34 +02:00
func ( gui * Gui ) goEvery ( interval time . Duration , stop chan struct { } , function func ( ) error ) {
go func ( ) {
ticker := time . NewTicker ( interval )
defer ticker . Stop ( )
for {
select {
case <- ticker . C :
_ = function ( )
case <- stop :
return
}
}
} ( )
2020-03-28 07:28:35 +02:00
}
2020-03-29 01:31:34 +02:00
func ( gui * Gui ) startBackgroundFetch ( ) {
gui . waitForIntro . Wait ( )
isNew := gui . Config . GetIsNewRepo ( )
if ! isNew {
time . After ( 60 * time . Second )
}
2020-08-11 13:18:38 +02:00
err := gui . fetch ( false )
2020-03-29 01:31:34 +02:00
if err != nil && strings . Contains ( err . Error ( ) , "exit status 128" ) && isNew {
2020-08-15 08:38:16 +02:00
_ = gui . ask ( askOpts {
2020-08-15 08:36:39 +02:00
returnToView : gui . g . CurrentView ( ) ,
returnFocusOnClose : true ,
title : gui . Tr . SLocalize ( "NoAutomaticGitFetchTitle" ) ,
prompt : gui . Tr . SLocalize ( "NoAutomaticGitFetchBody" ) ,
} )
2020-03-29 01:31:34 +02:00
} else {
gui . goEvery ( time . Second * 60 , gui . stopChan , func ( ) error {
2020-08-11 13:18:38 +02:00
err := gui . fetch ( false )
2020-03-29 01:31:34 +02:00
return err
} )
2020-03-28 07:28:35 +02:00
}
}
2020-03-29 01:11:15 +02:00
2020-03-29 01:31:34 +02:00
// setColorScheme sets the color scheme for the app based on the user config
func ( gui * Gui ) setColorScheme ( ) error {
userConfig := gui . Config . GetUserConfig ( )
theme . UpdateTheme ( userConfig )
gui . g . FgColor = theme . InactiveBorderColor
gui . g . SelFgColor = theme . ActiveBorderColor
return nil
2020-03-29 01:11:15 +02:00
}