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"
2021-04-05 00:52:09 +02:00
"log"
2019-05-18 03:28:07 +02:00
"os"
2018-07-21 10:37:00 +02:00
"strings"
2022-03-19 00:38:49 +02:00
"sync"
2018-07-21 07:51:18 +02:00
"time"
"github.com/jesseduffield/gocui"
2018-08-13 12:26:02 +02:00
"github.com/jesseduffield/lazygit/pkg/commands"
2022-01-19 09:32:27 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
2022-01-02 01:34:33 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/git_config"
2020-09-29 12:28:39 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
2020-09-29 11:10:57 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
2021-12-29 02:37:15 +02:00
"github.com/jesseduffield/lazygit/pkg/common"
2018-08-18 05:22:05 +02:00
"github.com/jesseduffield/lazygit/pkg/config"
2022-01-16 05:46:53 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/context"
2022-01-28 11:44:36 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/controllers"
2022-02-06 06:54:26 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
2021-04-18 08:30:34 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/lbl"
2021-04-18 10:07:10 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/mergeconflicts"
2021-06-05 07:08:36 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/modes/cherrypicking"
"github.com/jesseduffield/lazygit/pkg/gui/modes/diffing"
2021-04-03 02:32:14 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/modes/filtering"
2022-01-16 05:46:53 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/popup"
2022-01-29 07:23:57 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
2022-02-01 07:23:55 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/presentation/authors"
2021-11-02 23:23:47 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/presentation/graph"
2022-02-24 04:29:48 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/services/custom_commands"
2021-07-27 15:00:37 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/style"
2020-11-28 10:53:39 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
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"
2021-10-18 10:00:03 +02:00
"gopkg.in/ozeidan/fuzzy-patricia.v3/patricia"
2018-05-19 03:16:34 +02:00
)
2021-03-31 14:55:06 +02:00
// screen sizing determines how much space your selected window takes up (window
// as in panel, not your terminal's window). Sometimes you want a bit more space
// to see the contents of a panel, and this keeps track of how much maximisation
// you've set
type WindowMaximisation int
2020-02-24 23:32:46 +02:00
const (
2021-03-31 14:55:06 +02:00
SCREEN_NORMAL WindowMaximisation = iota
2020-02-24 23:32:46 +02:00
SCREEN_HALF
SCREEN_FULL
)
2021-04-20 10:34:47 +02:00
const StartupPopupVersion = 5
2019-11-10 13:07:45 +02:00
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
2021-04-03 04:43:43 +02:00
type ContextManager struct {
2022-01-16 05:46:53 +02:00
ContextStack [ ] types . Context
2021-04-06 08:01:07 +02:00
sync . RWMutex
2021-04-03 04:43:43 +02:00
}
2022-01-16 05:46:53 +02:00
func NewContextManager ( initialContext types . Context ) ContextManager {
2021-04-03 04:43:43 +02:00
return ContextManager {
2022-01-16 05:46:53 +02:00
ContextStack : [ ] types . Context { initialContext } ,
2021-04-06 08:01:07 +02:00
RWMutex : sync . RWMutex { } ,
2021-04-03 04:43:43 +02:00
}
}
2021-04-03 05:36:22 +02:00
type Repo string
2018-08-13 12:26:02 +02:00
// Gui wraps the gocui Gui object which handles rendering and events
type Gui struct {
2021-12-29 02:37:15 +02:00
* common . Common
2022-02-06 04:42:17 +02:00
g * gocui . Gui
git * commands . GitCommand
os * oscommands . OSCommand
2021-04-03 06:56:11 +02:00
// this is the state of the GUI for the current repo
2022-01-28 11:44:36 +02:00
State * GuiRepoState
2021-04-03 06:56:11 +02:00
2022-02-24 04:29:48 +02:00
CustomCommandsClient * custom_commands . Client
2021-04-03 06:56:11 +02:00
// this is a mapping of repos to gui states, so that we can restore the original
// gui state when returning from a subrepo
2022-01-28 11:44:36 +02:00
RepoStateMap map [ Repo ] * GuiRepoState
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
Updater * updates . Updater
statusManager * statusManager
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
2021-04-03 05:36:22 +02:00
showRecentRepos bool
2020-10-04 08:41:21 +02:00
2021-04-10 03:40:42 +02:00
Mutexes guiMutexes
2020-11-28 11:01:45 +02:00
// findSuggestions will take a string that the user has typed into a prompt
// and return a slice of suggestions which match that string.
findSuggestions func ( string ) [ ] * types . Suggestion
2021-04-03 04:43:43 +02:00
// when you enter into a submodule we'll append the superproject's path to this array
// so that you can return to the superproject
2022-01-28 11:44:36 +02:00
RepoPathStack * utils . StringStack
2021-04-03 06:56:11 +02:00
// this tells us whether our views have been initially set up
ViewsSetup bool
2021-04-04 15:51:59 +02:00
Views Views
2021-04-10 04:07:52 +02:00
// if we've suspended the gui (e.g. because we've switched to a subprocess)
// we typically want to pause some things that are running like background
// file refreshes
PauseBackgroundThreads bool
2021-04-10 05:08:51 +02:00
// Log of the commands that get run, to be displayed to the user.
2022-01-05 03:01:59 +02:00
CmdLog [ ] string
2021-04-11 04:12:54 +02:00
// the extras window contains things like the command log
ShowExtrasWindow bool
2021-10-18 10:00:03 +02:00
suggestionsAsyncHandler * tasks . AsyncHandler
2021-12-06 12:08:36 +02:00
2022-01-29 10:09:20 +02:00
PopupHandler types . IPopupHandler
2021-12-29 02:50:20 +02:00
IsNewRepo bool
2022-03-15 15:12:26 +02:00
2022-01-16 05:46:53 +02:00
// controllers define keybindings for a given context
2022-01-28 11:44:36 +02:00
Controllers Controllers
// flag as to whether or not the diff view should ignore whitespace
IgnoreWhitespaceInDiffView bool
2022-01-16 05:46:53 +02:00
// we use this to decide whether we'll return to the original directory that
// lazygit was opened in, or if we'll retain the one we're currently in.
2022-01-28 11:44:36 +02:00
RetainOriginalDir bool
PrevLayout PrevLayout
2022-03-17 09:42:44 +02:00
// this is the initial dir we are in upon opening lazygit. We hold onto this
// in case we want to restore it before quitting for users who have set up
// the feature for changing directory upon quit.
// The reason we don't just wait until quit time to handle changing directories
// is because some users want to keep track of the current lazygit directory in an outside
// process
InitialDir string
2022-01-30 01:40:48 +02:00
2022-02-06 06:54:26 +02:00
c * types . HelperCommon
helpers * helpers . Helpers
2020-10-04 08:41:21 +02:00
}
2022-01-28 11:44:36 +02:00
// we keep track of some stuff from one render to the next to see if certain
// things have changed
type PrevLayout struct {
Information string
MainWidth int
MainHeight int
}
type GuiRepoState struct {
2022-01-31 13:11:34 +02:00
Model * types . Model
2022-02-06 05:37:16 +02:00
Modes * types . Modes
2022-01-28 11:44:36 +02:00
// Suggestions will sometimes appear when typing into a prompt
2022-01-31 13:11:34 +02:00
Suggestions [ ] * types . Suggestion
2022-01-28 11:44:36 +02:00
Updating bool
Panels * panelStates
SplitMainPanel bool
2022-02-05 08:04:10 +02:00
LimitCommits bool
2022-01-28 11:44:36 +02:00
IsRefreshingFiles bool
Searching searchingState
Ptmx * os . File
StartupStage StartupStage // Allows us to not load everything at once
2022-01-31 13:11:34 +02:00
MainContext types . ContextKey // used to keep the main and secondary views' contexts in sync
2022-01-28 11:44:36 +02:00
ContextManager ContextManager
2022-01-31 13:20:28 +02:00
Contexts * context . ContextTree
2022-02-05 07:56:36 +02:00
ViewContextMap * context . ViewContextMap
2022-01-16 05:46:53 +02:00
ViewTabContextMap map [ string ] [ ] context . TabContext
2022-01-28 11:44:36 +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
WindowViewNameMap map [ string ] string
// tells us whether we've set up our views for the current repo. We'll need to
// do this whenever we switch back and forth between repos to get the views
// back in sync with the repo state
ViewsSetup bool
2022-02-22 12:16:00 +02:00
// we store a commit message in this field if we've escaped the commit message
// panel without committing or if our commit failed
savedCommitMessage string
2022-01-28 11:44:36 +02:00
ScreenMode WindowMaximisation
2022-02-23 11:02:40 +02:00
CurrentPopupOpts * types . CreatePopupPanelOpts
2022-01-28 11:44:36 +02:00
}
type Controllers struct {
2022-01-16 05:46:53 +02:00
Submodules * controllers . SubmodulesController
Tags * controllers . TagsController
LocalCommits * controllers . LocalCommitsController
Files * controllers . FilesController
Remotes * controllers . RemotesController
Menu * controllers . MenuController
Bisect * controllers . BisectController
Undo * controllers . UndoController
Sync * controllers . SyncController
2022-02-05 02:00:57 +02:00
Global * controllers . GlobalController
2022-01-28 11:44:36 +02:00
}
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
2021-04-18 08:30:34 +02:00
type LblPanelState struct {
* lbl . State
2019-11-04 10:47:25 +02:00
SecondaryFocused bool // this is for if we show the left or right panel
2018-12-04 10:50:11 +02:00
}
2021-04-18 10:07:10 +02:00
type MergingPanelState struct {
* mergeconflicts . State
2020-05-19 10:29:56 +02:00
2021-11-02 11:35:53 +02:00
// UserVerticalScrolling tells us if the user has started scrolling through the file themselves
2020-05-19 10:29:56 +02:00
// in which case we won't auto-scroll to a conflict.
2021-11-02 11:35:53 +02:00
UserVerticalScrolling bool
2018-12-08 07:54:54 +02:00
}
2022-01-30 07:38:07 +02:00
// as we move things to the new context approach we're going to eventually
// remove this struct altogether and store this state on the contexts.
2018-12-04 10:50:11 +02:00
type panelStates struct {
2022-02-05 08:04:10 +02:00
LineByLine * LblPanelState
Merging * MergingPanelState
2018-12-02 10:57:01 +02:00
}
2021-04-04 15:51:59 +02:00
type Views struct {
Status * gocui . View
Files * gocui . View
Branches * gocui . View
Commits * gocui . View
Stash * gocui . View
Main * gocui . View
Secondary * gocui . View
Options * gocui . View
Confirmation * gocui . View
Menu * gocui . View
CommitMessage * gocui . View
CommitFiles * gocui . View
2022-03-24 13:07:30 +02:00
SubCommits * gocui . View
2021-04-04 15:51:59 +02:00
Information * gocui . View
AppStatus * gocui . View
Search * gocui . View
SearchPrefix * gocui . View
Limit * gocui . View
Suggestions * gocui . View
2021-04-11 03:43:07 +02:00
Extras * gocui . View
2021-04-04 15:51:59 +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
2021-03-31 14:55:06 +02:00
type StartupStage int
2020-03-28 02:22:11 +02:00
const (
2021-03-31 14:55:06 +02:00
INITIAL StartupStage = iota
2020-03-28 02:22:11 +02:00
COMPLETE
)
2022-01-16 05:46:53 +02:00
// if you add a new mutex here be sure to instantiate it. We're using pointers to
// mutexes so that we can pass the mutexes to controllers.
2021-04-10 03:40:42 +02:00
type guiMutexes struct {
2022-01-16 05:46:53 +02:00
RefreshingFilesMutex * sync . Mutex
RefreshingStatusMutex * sync . Mutex
2022-01-29 02:22:35 +02:00
SyncMutex * sync . Mutex
2022-02-13 08:01:53 +02:00
LocalCommitsMutex * sync . Mutex
2022-01-16 05:46:53 +02:00
LineByLinePanelMutex * sync . Mutex
SubprocessMutex * sync . Mutex
2022-02-23 11:02:40 +02:00
PopupMutex * sync . Mutex
2020-10-01 23:56:14 +02:00
}
2022-01-31 13:11:34 +02:00
func ( gui * Gui ) onNewRepo ( filterPath string , reuseState bool ) error {
var err error
gui . git , err = commands . NewGitCommand (
gui . Common ,
2022-02-06 04:42:17 +02:00
gui . os ,
2022-01-31 13:11:34 +02:00
git_config . NewStdCachedGitConfig ( gui . Log ) ,
gui . Mutexes . SyncMutex ,
)
if err != nil {
return err
}
gui . resetState ( filterPath , reuseState )
gui . resetControllers ( )
if err := gui . resetKeybindings ( ) ; err != nil {
return err
}
return nil
}
2021-04-05 05:22:03 +02:00
// reuseState determines if we pull the repo state from our repo state map or
// just re-initialize it. For now we're only re-using state when we're going
// in and out of submodules, for the sake of having the cursor back on the submodule
// when we return.
//
// I tried out always reverting to the repo's original state but found that in fact
// it gets a bit confusing to land back in the status panel when visiting a repo
// you've already switched from. There's no doubt some easy way to make the UX
// optimal for all cases but I'm too lazy to think about what that is right now
func ( gui * Gui ) resetState ( filterPath string , reuseState bool ) {
2021-04-03 05:36:22 +02:00
currentDir , err := os . Getwd ( )
2021-04-05 05:22:03 +02:00
if reuseState {
if err == nil {
if state := gui . RepoStateMap [ Repo ( currentDir ) ] ; state != nil {
gui . State = state
gui . State . ViewsSetup = false
2022-02-23 11:02:40 +02:00
// setting this to nil so we don't get stuck based on a popup that was
// previously opened
gui . Mutexes . PopupMutex . Lock ( )
gui . State . CurrentPopupOpts = nil
gui . Mutexes . PopupMutex . Unlock ( )
2022-02-13 02:35:42 +02:00
gui . syncViewContexts ( )
return
2021-04-05 05:22:03 +02:00
}
} else {
2022-01-16 05:46:53 +02:00
gui . c . Log . Error ( err )
2021-04-03 05:36:22 +02:00
}
}
2022-02-05 07:56:36 +02:00
contextTree := gui . contextTree ( )
2021-04-05 05:07:44 +02:00
2021-04-03 04:43:43 +02:00
screenMode := SCREEN_NORMAL
2022-02-05 07:56:36 +02:00
var initialContext types . IListContext = contextTree . Files
2021-04-03 04:43:43 +02:00
if filterPath != "" {
screenMode = SCREEN_HALF
2022-02-13 08:01:53 +02:00
initialContext = contextTree . LocalCommits
2022-02-05 07:56:36 +02:00
}
viewContextMap := context . NewViewContextMap ( )
for viewName , context := range initialViewContextMapping ( contextTree ) {
viewContextMap . Set ( viewName , context )
2020-03-29 01:20:57 +02:00
}
2018-08-27 12:35:55 +02:00
2022-01-28 11:44:36 +02:00
gui . State = & GuiRepoState {
2022-01-31 13:11:34 +02:00
Model : & types . Model {
CommitFiles : nil ,
Files : make ( [ ] * models . File , 0 ) ,
Commits : make ( [ ] * models . Commit , 0 ) ,
StashEntries : make ( [ ] * models . StashEntry , 0 ) ,
FilteredReflogCommits : make ( [ ] * models . Commit , 0 ) ,
ReflogCommits : make ( [ ] * models . Commit , 0 ) ,
BisectInfo : git_commands . NewNullBisectInfo ( ) ,
FilesTrie : patricia . NewTrie ( ) ,
} ,
2018-12-04 10:50:11 +02:00
Panels : & panelStates {
2021-04-18 10:07:10 +02:00
Merging : & MergingPanelState {
2021-11-02 11:35:53 +02:00
State : mergeconflicts . NewState ( ) ,
UserVerticalScrolling : false ,
2018-12-08 07:54:54 +02:00
} ,
2018-12-04 10:50:11 +02:00
} ,
2022-02-06 05:37:16 +02:00
Ptmx : nil ,
Modes : & types . Modes {
2021-06-05 07:08:36 +02:00
Filtering : filtering . New ( filterPath ) ,
CherryPicking : cherrypicking . New ( ) ,
Diffing : diffing . New ( ) ,
2021-04-03 04:43:43 +02:00
} ,
2022-02-05 07:56:36 +02:00
ViewContextMap : viewContextMap ,
ViewTabContextMap : contextTree . InitialViewTabContextMap ( ) ,
2021-04-03 05:36:22 +02:00
ScreenMode : screenMode ,
2021-04-03 06:56:11 +02:00
// TODO: put contexts in the context manager
2021-04-05 05:07:44 +02:00
ContextManager : NewContextManager ( initialContext ) ,
2022-02-05 07:56:36 +02:00
Contexts : contextTree ,
2018-08-13 12:26:02 +02:00
}
2021-04-03 02:32:14 +02:00
2022-02-13 02:35:42 +02:00
gui . syncViewContexts ( )
2021-04-03 06:56:11 +02:00
gui . RepoStateMap [ Repo ( currentDir ) ] = gui . State
2020-03-29 01:20:57 +02:00
}
2018-08-13 12:26:02 +02:00
2022-02-13 02:35:42 +02:00
func ( gui * Gui ) syncViewContexts ( ) {
for viewName , context := range gui . State . ViewContextMap . Entries ( ) {
view , err := gui . g . View ( viewName )
if err != nil {
panic ( err )
}
view . Context = string ( context . GetKey ( ) )
}
}
2022-02-05 07:56:36 +02:00
func initialViewContextMapping ( contextTree * context . ContextTree ) map [ string ] types . Context {
return map [ string ] types . Context {
"status" : contextTree . Status ,
"files" : contextTree . Files ,
"branches" : contextTree . Branches ,
2022-02-13 08:01:53 +02:00
"commits" : contextTree . LocalCommits ,
2022-02-05 07:56:36 +02:00
"commitFiles" : contextTree . CommitFiles ,
2022-03-24 13:07:30 +02:00
"subCommits" : contextTree . SubCommits ,
2022-02-05 07:56:36 +02:00
"stash" : contextTree . Stash ,
"menu" : contextTree . Menu ,
"confirmation" : contextTree . Confirmation ,
"commitMessage" : contextTree . CommitMessage ,
"main" : contextTree . Normal ,
"secondary" : contextTree . Normal ,
"extras" : contextTree . CommandLog ,
}
}
2020-03-29 01:20:57 +02:00
// for now the split view will always be on
// NewGui builds a new gui handler
2022-01-02 01:34:33 +02:00
func NewGui (
cmn * common . Common ,
config config . AppConfigurer ,
gitConfig git_config . IGitConfig ,
updater * updates . Updater ,
showRecentRepos bool ,
2022-03-17 09:42:44 +02:00
initialDir string ,
2022-01-02 01:34:33 +02:00
) ( * Gui , error ) {
2018-08-14 15:47:14 +02:00
gui := & Gui {
2021-12-29 02:37:15 +02:00
Common : cmn ,
2021-10-18 10:00:03 +02:00
Config : config ,
Updater : updater ,
statusManager : & statusManager { } ,
viewBufferManagerMap : map [ string ] * tasks . ViewBufferManager { } ,
showRecentRepos : showRecentRepos ,
2022-01-28 11:44:36 +02:00
RepoPathStack : & utils . StringStack { } ,
RepoStateMap : map [ Repo ] * GuiRepoState { } ,
2021-10-18 10:00:03 +02:00
CmdLog : [ ] string { } ,
suggestionsAsyncHandler : tasks . NewAsyncHandler ( ) ,
2021-12-29 03:03:35 +02:00
// originally we could only hide the command log permanently via the config
// but now we do it via state. So we need to still support the config for the
// sake of backwards compatibility. We're making use of short circuiting here
ShowExtrasWindow : cmn . UserConfig . Gui . ShowCommandLog && ! config . GetAppState ( ) . HideCommandLog ,
2022-01-16 05:46:53 +02:00
Mutexes : guiMutexes {
RefreshingFilesMutex : & sync . Mutex { } ,
RefreshingStatusMutex : & sync . Mutex { } ,
2022-01-29 02:22:35 +02:00
SyncMutex : & sync . Mutex { } ,
2022-02-13 08:01:53 +02:00
LocalCommitsMutex : & sync . Mutex { } ,
2022-01-16 05:46:53 +02:00
LineByLinePanelMutex : & sync . Mutex { } ,
SubprocessMutex : & sync . Mutex { } ,
2022-02-23 11:02:40 +02:00
PopupMutex : & sync . Mutex { } ,
2022-01-16 05:46:53 +02:00
} ,
2022-03-17 09:42:44 +02:00
InitialDir : initialDir ,
2018-08-14 15:47:14 +02:00
}
2019-11-12 13:19:20 +02:00
gui . watchFilesForChanges ( )
2022-01-28 11:44:36 +02:00
gui . PopupHandler = popup . NewPopupHandler (
cmn ,
gui . createPopupPanel ,
2022-01-16 05:46:53 +02:00
func ( ) error { return gui . c . Refresh ( types . RefreshOptions { Mode : types . ASYNC } ) } ,
2022-01-28 11:44:36 +02:00
func ( ) error { return gui . closeConfirmationPrompt ( false ) } ,
gui . createMenu ,
gui . withWaitingStatus ,
2022-01-16 05:46:53 +02:00
gui . toast ,
func ( ) string { return gui . Views . Confirmation . TextArea . GetContent ( ) } ,
2022-01-28 11:44:36 +02:00
)
2021-04-10 08:01:46 +02:00
2022-01-16 05:46:53 +02:00
guiCommon := & guiCommon { gui : gui , IPopupHandler : gui . PopupHandler }
2022-02-06 06:54:26 +02:00
helperCommon := & types . HelperCommon { IGuiCommon : guiCommon , Common : cmn }
2022-01-16 05:46:53 +02:00
2022-02-23 10:44:48 +02:00
credentialsHelper := helpers . NewCredentialsHelper ( helperCommon )
guiIO := oscommands . NewGuiIO (
cmn . Log ,
gui . LogCommand ,
gui . getCmdWriter ,
credentialsHelper . PromptUserForCredential ,
)
osCommand := oscommands . NewOSCommand ( cmn , oscommands . GetPlatform ( ) , guiIO )
gui . os = osCommand
2022-01-16 05:46:53 +02:00
// storing this stuff on the gui for now to ease refactoring
// TODO: reset these controllers upon changing repos due to state changing
2022-02-06 06:54:26 +02:00
gui . c = helperCommon
2022-01-16 05:46:53 +02:00
2021-12-29 02:50:20 +02:00
authors . SetCustomAuthors ( gui . UserConfig . Gui . AuthorColors )
2022-02-01 07:23:55 +02:00
presentation . SetCustomBranches ( gui . UserConfig . Gui . BranchColors )
2021-10-30 09:00:28 +02:00
2022-01-23 05:40:28 +02:00
return gui , nil
}
2021-11-01 00:35:54 +02:00
var RuneReplacements = map [ rune ] string {
// for the commit graph
2021-11-02 23:23:47 +02:00
graph . MergeSymbol : "M" ,
graph . CommitSymbol : "o" ,
2021-11-01 00:35:54 +02:00
}
2022-01-31 13:11:34 +02:00
func ( gui * Gui ) initGocui ( ) ( * gocui . Gui , error ) {
2020-10-04 08:41:21 +02:00
recordEvents := recordingEvents ( )
2021-04-05 00:52:09 +02:00
playMode := gocui . NORMAL
if recordEvents {
playMode = gocui . RECORDING
} else if replaying ( ) {
playMode = gocui . REPLAYING
}
2020-10-04 08:41:21 +02:00
2021-11-01 00:35:54 +02:00
g , err := gocui . NewGui ( gocui . OutputTrue , OverlappingEdges , playMode , headless ( ) , RuneReplacements )
2022-01-31 13:11:34 +02:00
if err != nil {
return nil , err
}
return g , nil
}
// Run: setup the gui with keybindings and start the mainloop
func ( gui * Gui ) Run ( filterPath string ) error {
g , err := gui . initGocui ( )
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
}
2021-04-10 03:40:42 +02:00
2022-01-31 13:11:34 +02:00
gui . g = g
defer gui . g . Close ( )
2020-02-23 12:53:30 +02:00
2021-04-05 00:52:09 +02:00
if replaying ( ) {
2022-01-31 13:11:34 +02:00
gui . g . RecordingConfig = gocui . RecordingConfig {
2021-04-05 00:52:09 +02:00
Speed : getRecordingSpeed ( ) ,
2021-04-05 02:20:02 +02:00
Leeway : 100 ,
2021-04-05 00:52:09 +02:00
}
2022-01-31 13:11:34 +02:00
var err error
gui . g . Recording , err = gui . loadRecording ( )
2021-04-05 00:52:09 +02:00
if err != nil {
return err
}
go utils . Safe ( func ( ) {
2021-04-05 13:56:17 +02:00
time . Sleep ( time . Second * 40 )
log . Fatal ( "40 seconds is up, lazygit recording took too long to complete" )
2021-04-05 00:52:09 +02:00
} )
2020-10-04 08:41:21 +02:00
}
2022-01-31 13:11:34 +02:00
gui . g . OnSearchEscape = gui . onSearchEscape
2021-02-23 14:01:45 +02:00
if err := gui . Config . ReloadUserConfig ( ) ; err != nil {
return nil
}
2021-12-29 02:50:20 +02:00
userConfig := gui . UserConfig
2022-01-31 13:11:34 +02:00
gui . g . SearchEscapeKey = gui . getKey ( userConfig . Keybinding . Universal . Return )
gui . g . NextSearchMatchKey = gui . getKey ( userConfig . Keybinding . Universal . NextMatch )
gui . g . PrevSearchMatchKey = gui . getKey ( userConfig . Keybinding . Universal . PrevMatch )
2020-02-23 12:53:30 +02:00
2022-01-31 13:11:34 +02:00
gui . g . ShowListFooter = userConfig . Gui . ShowListFooter
2021-06-10 10:26:29 +02:00
2020-10-03 06:54:55 +02:00
if userConfig . Gui . MouseEvents {
2022-01-31 13:11:34 +02:00
gui . g . Mouse = true
2019-03-03 07:15:20 +02:00
}
2019-02-25 13:11:35 +02:00
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
2022-01-31 13:11:34 +02:00
gui . g . SetManager ( gocui . ManagerFunc ( gui . layout ) , gocui . ManagerFunc ( gui . getFocusLayout ( ) ) )
2022-02-05 07:56:36 +02:00
if err := gui . createAllViews ( ) ; err != nil {
return err
}
2022-01-31 13:11:34 +02:00
// onNewRepo must be called after g.SetManager because SetManager deletes keybindings
if err := gui . onNewRepo ( filterPath , false ) ; err != nil {
return err
}
2019-11-10 13:07:45 +02:00
gui . waitForIntro . Add ( 1 )
2022-01-16 05:46:53 +02:00
if gui . c . UserConfig . Git . AutoFetch {
2020-10-07 12:19:38 +02:00
go utils . Safe ( gui . startBackgroundFetch )
2019-07-19 13:56:53 +02:00
}
2019-11-10 13:07:45 +02:00
2021-04-03 12:56:42 +02:00
gui . goEvery ( time . Second * time . Duration ( userConfig . Refresher . RefreshInterval ) , gui . stopChan , gui . refreshFilesAndSubmodules )
2018-07-21 10:37:00 +02:00
2022-01-16 05:46:53 +02:00
gui . c . Log . Info ( "starting main loop" )
2019-11-10 13:07:45 +02:00
2022-01-31 13:11:34 +02:00
return gui . g . MainLoop ( )
2018-08-13 12:26:02 +02:00
}
2022-02-05 07:56:36 +02:00
func ( gui * Gui ) createAllViews ( ) error {
viewNameMappings := [ ] struct {
viewPtr * * gocui . View
name string
} {
{ viewPtr : & gui . Views . Status , name : "status" } ,
{ viewPtr : & gui . Views . Files , name : "files" } ,
{ viewPtr : & gui . Views . Branches , name : "branches" } ,
{ viewPtr : & gui . Views . Commits , name : "commits" } ,
{ viewPtr : & gui . Views . Stash , name : "stash" } ,
{ viewPtr : & gui . Views . CommitFiles , name : "commitFiles" } ,
2022-03-24 13:07:30 +02:00
{ viewPtr : & gui . Views . SubCommits , name : "subCommits" } ,
2022-02-05 07:56:36 +02:00
{ viewPtr : & gui . Views . Main , name : "main" } ,
{ viewPtr : & gui . Views . Secondary , name : "secondary" } ,
{ viewPtr : & gui . Views . Options , name : "options" } ,
{ viewPtr : & gui . Views . AppStatus , name : "appStatus" } ,
{ viewPtr : & gui . Views . Information , name : "information" } ,
{ viewPtr : & gui . Views . Search , name : "search" } ,
{ viewPtr : & gui . Views . SearchPrefix , name : "searchPrefix" } ,
{ viewPtr : & gui . Views . CommitMessage , name : "commitMessage" } ,
{ viewPtr : & gui . Views . Menu , name : "menu" } ,
{ viewPtr : & gui . Views . Suggestions , name : "suggestions" } ,
{ viewPtr : & gui . Views . Confirmation , name : "confirmation" } ,
{ viewPtr : & gui . Views . Limit , name : "limit" } ,
{ viewPtr : & gui . Views . Extras , name : "extras" } ,
}
var err error
for _ , mapping := range viewNameMappings {
* mapping . viewPtr , err = gui . prepareView ( mapping . name )
if err != nil && err . Error ( ) != UNKNOWN_VIEW_ERROR_MSG {
return err
}
}
gui . Views . Options . Frame = false
gui . Views . Options . FgColor = theme . OptionsColor
gui . Views . SearchPrefix . BgColor = gocui . ColorDefault
gui . Views . SearchPrefix . FgColor = gocui . ColorGreen
gui . Views . SearchPrefix . Frame = false
gui . setViewContent ( gui . Views . SearchPrefix , SEARCH_PREFIX )
gui . Views . Stash . Title = gui . c . Tr . StashTitle
gui . Views . Stash . FgColor = theme . GocuiDefaultTextColor
gui . Views . Commits . Title = gui . c . Tr . CommitsTitle
gui . Views . Commits . FgColor = theme . GocuiDefaultTextColor
gui . Views . CommitFiles . Title = gui . c . Tr . CommitFiles
gui . Views . CommitFiles . FgColor = theme . GocuiDefaultTextColor
2022-03-24 13:07:30 +02:00
gui . Views . SubCommits . FgColor = theme . GocuiDefaultTextColor
2022-02-05 07:56:36 +02:00
gui . Views . Branches . Title = gui . c . Tr . BranchesTitle
gui . Views . Branches . FgColor = theme . GocuiDefaultTextColor
gui . Views . Files . Highlight = true
gui . Views . Files . Title = gui . c . Tr . FilesTitle
gui . Views . Files . FgColor = theme . GocuiDefaultTextColor
gui . Views . Secondary . Title = gui . c . Tr . DiffTitle
gui . Views . Secondary . Wrap = true
gui . Views . Secondary . FgColor = theme . GocuiDefaultTextColor
gui . Views . Secondary . IgnoreCarriageReturns = true
gui . Views . Main . Title = gui . c . Tr . DiffTitle
gui . Views . Main . Wrap = true
gui . Views . Main . FgColor = theme . GocuiDefaultTextColor
gui . Views . Main . IgnoreCarriageReturns = true
gui . Views . Limit . Title = gui . c . Tr . NotEnoughSpace
gui . Views . Limit . Wrap = true
gui . Views . Status . Title = gui . c . Tr . StatusTitle
gui . Views . Status . FgColor = theme . GocuiDefaultTextColor
gui . Views . Search . BgColor = gocui . ColorDefault
gui . Views . Search . FgColor = gocui . ColorGreen
gui . Views . Search . Frame = false
gui . Views . Search . Editable = true
gui . Views . AppStatus . BgColor = gocui . ColorDefault
gui . Views . AppStatus . FgColor = gocui . ColorCyan
gui . Views . AppStatus . Frame = false
gui . Views . AppStatus . Visible = false
gui . Views . CommitMessage . Visible = false
gui . Views . CommitMessage . Title = gui . c . Tr . CommitMessage
gui . Views . CommitMessage . FgColor = theme . GocuiDefaultTextColor
gui . Views . CommitMessage . Editable = true
gui . Views . CommitMessage . Editor = gocui . EditorFunc ( gui . commitMessageEditor )
gui . Views . Confirmation . Visible = false
gui . Views . Suggestions . Visible = false
gui . Views . Menu . Visible = false
gui . Views . Information . BgColor = gocui . ColorDefault
gui . Views . Information . FgColor = gocui . ColorGreen
gui . Views . Information . Frame = false
gui . Views . Extras . Title = gui . c . Tr . CommandLog
gui . Views . Extras . FgColor = theme . GocuiDefaultTextColor
gui . Views . Extras . Autoscroll = true
gui . Views . Extras . Wrap = true
return nil
}
2022-01-31 13:11:34 +02:00
func ( gui * Gui ) RunAndHandleError ( filterPath string ) error {
2021-04-03 04:43:43 +02:00
gui . stopChan = make ( chan struct { } )
return utils . SafeWithError ( func ( ) error {
2022-01-31 13:11:34 +02:00
if err := gui . Run ( filterPath ) ; 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 ( )
}
2020-01-31 10:46:29 +02:00
if ! gui . fileWatcher . Disabled {
gui . fileWatcher . Watcher . Close ( )
}
close ( gui . stopChan )
2020-08-23 12:20:05 +02:00
switch err {
case gocui . ErrQuit :
2022-01-28 11:44:36 +02:00
if gui . RetainOriginalDir {
2022-03-17 09:42:44 +02:00
if err := gui . recordDirectory ( gui . InitialDir ) ; err != nil {
2022-03-15 15:12:26 +02:00
return err
}
} else {
2019-10-07 03:34:12 +02:00
if err := gui . recordCurrentDirectory ( ) ; err != nil {
return err
}
}
2021-04-05 00:52:09 +02:00
if err := gui . saveRecording ( gui . g . Recording ) ; err != nil {
2020-10-04 08:41:21 +02:00
return err
}
2020-08-23 12:20:05 +02:00
return nil
2021-04-03 04:43:43 +02:00
2020-08-23 12:20:05 +02:00
default :
2019-02-18 10:42:23 +02:00
return err
2018-08-13 12:26:02 +02:00
}
}
2021-04-03 04:43:43 +02:00
return nil
} )
2018-05-19 09:04:33 +02:00
}
2021-04-10 03:40:42 +02:00
// returns whether command exited without error or not
2021-12-07 12:59:36 +02:00
func ( gui * Gui ) runSubprocessWithSuspenseAndRefresh ( subprocess oscommands . ICmdObj ) error {
2021-04-10 03:40:42 +02:00
_ , err := gui . runSubprocessWithSuspense ( subprocess )
if err != nil {
return err
}
2022-01-16 05:46:53 +02:00
if err := gui . c . Refresh ( types . RefreshOptions { Mode : types . ASYNC } ) ; err != nil {
2021-04-10 03:40:42 +02:00
return err
}
return nil
}
// returns whether command exited without error or not
2021-12-07 12:59:36 +02:00
func ( gui * Gui ) runSubprocessWithSuspense ( subprocess oscommands . ICmdObj ) ( bool , error ) {
2021-04-10 03:40:42 +02:00
gui . Mutexes . SubprocessMutex . Lock ( )
defer gui . Mutexes . SubprocessMutex . Unlock ( )
2021-04-05 04:18:21 +02:00
if replaying ( ) {
// we do not yet support running subprocesses within integration tests. So if
// we're replaying an integration test and we're inside this method, something
// has gone wrong, so we should fail
log . Fatal ( "opening subprocesses not yet supported in integration tests. Chances are that this test is running too fast and a subprocess is accidentally opened" )
}
2021-04-10 03:40:42 +02:00
if err := gui . g . Suspend ( ) ; err != nil {
2022-01-16 05:46:53 +02:00
return false , gui . c . Error ( err )
2021-04-02 12:30:39 +02:00
}
2021-04-10 04:07:52 +02:00
gui . PauseBackgroundThreads = true
2021-04-02 12:30:39 +02:00
cmdErr := gui . runSubprocess ( subprocess )
2021-04-10 03:40:42 +02:00
if err := gui . g . Resume ( ) ; err != nil {
2021-04-10 04:07:52 +02:00
return false , err
2021-04-02 12:30:39 +02:00
}
2021-04-10 04:07:52 +02:00
gui . PauseBackgroundThreads = false
2022-01-16 05:31:28 +02:00
if cmdErr != nil {
2022-01-16 05:46:53 +02:00
return false , gui . c . Error ( cmdErr )
2022-01-16 05:31:28 +02:00
}
return true , nil
2021-04-02 12:30:39 +02:00
}
2022-01-08 06:46:35 +02:00
func ( gui * Gui ) runSubprocess ( cmdObj oscommands . ICmdObj ) error { //nolint:unparam
2022-01-16 05:46:53 +02:00
gui . LogCommand ( cmdObj . ToString ( ) , true )
2022-01-05 03:01:59 +02:00
2021-12-07 12:59:36 +02:00
subprocess := cmdObj . GetCmd ( )
2021-04-02 12:30:39 +02:00
subprocess . Stdout = os . Stdout
subprocess . Stderr = os . Stdout
subprocess . Stdin = os . Stdin
2019-03-12 12:43:56 +02:00
2021-07-27 15:00:37 +02:00
fmt . Fprintf ( os . Stdout , "\n%s\n\n" , style . FgBlue . Sprint ( "+ " + strings . Join ( subprocess . Args , " " ) ) )
2019-05-26 03:24:01 +02:00
2021-12-05 23:33:26 +02:00
err := subprocess . Run ( )
2019-03-12 12:43:56 +02:00
2021-04-02 12:30:39 +02:00
subprocess . Stdout = ioutil . Discard
subprocess . Stderr = ioutil . Discard
subprocess . Stdin = nil
2019-05-26 03:24:01 +02:00
2022-03-17 08:43:03 +02:00
if gui . Config . GetUserConfig ( ) . PromptToReturnFromSubprocess {
fmt . Fprintf ( os . Stdout , "\n%s" , style . FgGreen . Sprint ( gui . Tr . PressEnterToReturn ) )
fmt . Scanln ( ) // wait for enter press
}
2019-05-26 03:24:01 +02:00
2021-12-05 23:33:26 +02:00
return err
2019-03-12 12:43:56 +02:00
}
2020-03-29 01:31:34 +02:00
func ( gui * Gui ) loadNewRepo ( ) error {
if err := gui . updateRecentRepoList ( ) ; err != nil {
return err
2019-03-03 05:28:16 +02:00
}
2022-01-16 05:46:53 +02:00
if err := gui . c . Refresh ( types . RefreshOptions { Mode : types . ASYNC } ) ; err != nil {
2020-03-29 01:31:34 +02:00
return err
2020-03-28 07:28:35 +02:00
}
2022-02-06 04:42:17 +02:00
if err := gui . os . UpdateWindowTitle ( ) ; err != nil {
2022-01-20 17:02:03 +02:00
return err
}
2022-01-19 11:42:32 +02:00
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-10-07 12:19:38 +02:00
go utils . Safe ( func ( ) {
2020-03-29 01:31:34 +02:00
for _ , task := range tasks {
2020-11-16 11:38:26 +02:00
task := task
2020-10-07 12:19:38 +02:00
go utils . Safe ( func ( ) {
2020-03-29 01:31:34 +02:00
if err := task ( done ) ; err != nil {
2022-01-16 05:46:53 +02:00
_ = gui . c . Error ( err )
2020-03-29 01:31:34 +02:00
}
2020-10-07 12:19:38 +02:00
} )
2019-10-19 11:39:18 +02:00
2020-03-29 01:31:34 +02:00
<- done
gui . waitForIntro . Done ( )
}
2020-10-07 12:19:38 +02:00
} )
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 { } { }
2022-01-16 05:46:53 +02:00
gui . c . GetAppState ( ) . StartupPopupVersion = StartupPopupVersion
return gui . c . SaveAppState ( )
2019-11-10 07:20:35 +02:00
}
2022-01-29 10:09:20 +02:00
return gui . c . Ask ( types . AskOpts {
2022-01-28 11:44:36 +02:00
Title : "" ,
2022-01-16 05:46:53 +02:00
Prompt : gui . c . Tr . IntroPopupMessage ,
2022-01-28 11:44:36 +02:00
HandleConfirm : onConfirm ,
HandleClose : onConfirm ,
2020-08-15 08:36:39 +02:00
} )
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 ) {
2020-10-07 12:19:38 +02:00
go utils . Safe ( func ( ) {
2020-03-29 01:31:34 +02:00
ticker := time . NewTicker ( interval )
defer ticker . Stop ( )
for {
select {
case <- ticker . C :
2021-04-10 04:07:52 +02:00
if gui . PauseBackgroundThreads {
continue
}
2020-03-29 01:31:34 +02:00
_ = function ( )
case <- stop :
return
}
}
2020-10-07 12:19:38 +02:00
} )
2020-03-28 07:28:35 +02:00
}
2020-03-29 01:31:34 +02:00
func ( gui * Gui ) startBackgroundFetch ( ) {
gui . waitForIntro . Wait ( )
2021-12-29 02:50:20 +02:00
isNew := gui . IsNewRepo
userConfig := gui . UserConfig
2020-03-29 01:31:34 +02:00
if ! isNew {
2021-01-05 19:38:49 +02:00
time . After ( time . Duration ( userConfig . Refresher . FetchInterval ) * time . Second )
2020-03-29 01:31:34 +02:00
}
2022-01-06 13:05:18 +02:00
err := gui . backgroundFetch ( )
2020-03-29 01:31:34 +02:00
if err != nil && strings . Contains ( err . Error ( ) , "exit status 128" ) && isNew {
2022-01-29 10:09:20 +02:00
_ = gui . c . Ask ( types . AskOpts {
2022-01-16 05:46:53 +02:00
Title : gui . c . Tr . NoAutomaticGitFetchTitle ,
Prompt : gui . c . Tr . NoAutomaticGitFetchBody ,
2020-08-15 08:36:39 +02:00
} )
2020-03-29 01:31:34 +02:00
} else {
2021-04-03 12:56:42 +02:00
gui . goEvery ( time . Second * time . Duration ( userConfig . Refresher . FetchInterval ) , gui . stopChan , func ( ) error {
2022-01-06 13:05:18 +02:00
err := gui . backgroundFetch ( )
2021-11-01 00:35:54 +02:00
gui . render ( )
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 {
2021-12-29 02:50:20 +02:00
userConfig := gui . UserConfig
2020-10-03 06:54:55 +02:00
theme . UpdateTheme ( userConfig . Gui . Theme )
2020-03-29 01:31:34 +02:00
gui . g . FgColor = theme . InactiveBorderColor
gui . g . SelFgColor = theme . ActiveBorderColor
2020-12-24 05:45:59 +02:00
gui . g . FrameColor = theme . InactiveBorderColor
gui . g . SelFrameColor = theme . ActiveBorderColor
2020-03-29 01:31:34 +02:00
return nil
2020-03-29 01:11:15 +02:00
}
2022-01-15 03:04:00 +02:00
func ( gui * Gui ) OnUIThread ( f func ( ) error ) {
gui . g . Update ( func ( * gocui . Gui ) error {
return f ( )
} )
}