mirror of
https://github.com/jesseduffield/lazygit.git
synced 2024-11-28 09:08:41 +02:00
14ecc15e71
The global counter approach is easy to understand but it's brittle and depends on implicit behaviour that is not very discoverable. With a global counter, if any goroutine accidentally decrements the counter twice, we'll think lazygit is idle when it's actually busy. Likewise if a goroutine accidentally increments the counter twice we'll think lazygit is busy when it's actually idle. With the new approach we have a map of tasks where each task can either be busy or not. We create a new task and add it to the map when we spawn a worker goroutine (among other things) and we remove it once the task is done. The task can also be paused and continued for situations where we switch back and forth between running a program and asking for user input. In order for this to work with `git push` (and other commands that require credentials) we need to obtain the task from gocui when we create the worker goroutine, and then pass it along to the commands package to pause/continue the task as required. This is MUCH more discoverable than the old approach which just decremented and incremented the global counter from within the commands package, but it's at the cost of expanding some function signatures (arguably a good thing). Likewise, whenever you want to call WithWaitingStatus or WithLoaderPanel the callback will now have access to the task for pausing/ continuing. We only need to actually make use of this functionality in a couple of places so it's a high price to pay, but I don't know if I want to introduce a WithWaitingStatusTask and WithLoaderPanelTask function (open to suggestions).
171 lines
4.3 KiB
Go
171 lines
4.3 KiB
Go
package gui
|
|
|
|
import (
|
|
"github.com/jesseduffield/gocui"
|
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
|
"github.com/jesseduffield/lazygit/pkg/config"
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
)
|
|
|
|
// hacking this by including the gui struct for now until we split more things out
|
|
type guiCommon struct {
|
|
gui *Gui
|
|
types.IPopupHandler
|
|
}
|
|
|
|
var _ types.IGuiCommon = &guiCommon{}
|
|
|
|
func (self *guiCommon) LogAction(msg string) {
|
|
self.gui.LogAction(msg)
|
|
}
|
|
|
|
func (self *guiCommon) LogCommand(cmdStr string, isCommandLine bool) {
|
|
self.gui.LogCommand(cmdStr, isCommandLine)
|
|
}
|
|
|
|
func (self *guiCommon) Refresh(opts types.RefreshOptions) error {
|
|
return self.gui.helpers.Refresh.Refresh(opts)
|
|
}
|
|
|
|
func (self *guiCommon) PostRefreshUpdate(context types.Context) error {
|
|
return self.gui.postRefreshUpdate(context)
|
|
}
|
|
|
|
func (self *guiCommon) RunSubprocessAndRefresh(cmdObj oscommands.ICmdObj) error {
|
|
return self.gui.runSubprocessWithSuspenseAndRefresh(cmdObj)
|
|
}
|
|
|
|
func (self *guiCommon) RunSubprocess(cmdObj oscommands.ICmdObj) (bool, error) {
|
|
return self.gui.runSubprocessWithSuspense(cmdObj)
|
|
}
|
|
|
|
func (self *guiCommon) PushContext(context types.Context, opts ...types.OnFocusOpts) error {
|
|
return self.gui.State.ContextMgr.Push(context, opts...)
|
|
}
|
|
|
|
func (self *guiCommon) PopContext() error {
|
|
return self.gui.State.ContextMgr.Pop()
|
|
}
|
|
|
|
func (self *guiCommon) ReplaceContext(context types.Context) error {
|
|
return self.gui.State.ContextMgr.Replace(context)
|
|
}
|
|
|
|
func (self *guiCommon) RemoveContexts(contexts []types.Context) error {
|
|
return self.gui.State.ContextMgr.RemoveContexts(contexts)
|
|
}
|
|
|
|
func (self *guiCommon) CurrentContext() types.Context {
|
|
return self.gui.State.ContextMgr.Current()
|
|
}
|
|
|
|
func (self *guiCommon) CurrentStaticContext() types.Context {
|
|
return self.gui.State.ContextMgr.CurrentStatic()
|
|
}
|
|
|
|
func (self *guiCommon) CurrentSideContext() types.Context {
|
|
return self.gui.State.ContextMgr.CurrentSide()
|
|
}
|
|
|
|
func (self *guiCommon) IsCurrentContext(c types.Context) bool {
|
|
return self.gui.State.ContextMgr.IsCurrent(c)
|
|
}
|
|
|
|
func (self *guiCommon) Context() types.IContextMgr {
|
|
return self.gui.State.ContextMgr
|
|
}
|
|
|
|
func (self *guiCommon) ActivateContext(context types.Context) error {
|
|
return self.gui.State.ContextMgr.ActivateContext(context, types.OnFocusOpts{})
|
|
}
|
|
|
|
func (self *guiCommon) GetAppState() *config.AppState {
|
|
return self.gui.Config.GetAppState()
|
|
}
|
|
|
|
func (self *guiCommon) SaveAppState() error {
|
|
return self.gui.Config.SaveAppState()
|
|
}
|
|
|
|
func (self *guiCommon) GetConfig() config.AppConfigurer {
|
|
return self.gui.Config
|
|
}
|
|
|
|
func (self *guiCommon) ResetViewOrigin(view *gocui.View) {
|
|
self.gui.resetViewOrigin(view)
|
|
}
|
|
|
|
func (self *guiCommon) SetViewContent(view *gocui.View, content string) {
|
|
self.gui.setViewContent(view, content)
|
|
}
|
|
|
|
func (self *guiCommon) Render() {
|
|
self.gui.render()
|
|
}
|
|
|
|
func (self *guiCommon) Views() types.Views {
|
|
return self.gui.Views
|
|
}
|
|
|
|
func (self *guiCommon) Git() *commands.GitCommand {
|
|
return self.gui.git
|
|
}
|
|
|
|
func (self *guiCommon) OS() *oscommands.OSCommand {
|
|
return self.gui.os
|
|
}
|
|
|
|
func (self *guiCommon) Modes() *types.Modes {
|
|
return self.gui.State.Modes
|
|
}
|
|
|
|
func (self *guiCommon) Model() *types.Model {
|
|
return self.gui.State.Model
|
|
}
|
|
|
|
func (self *guiCommon) Mutexes() types.Mutexes {
|
|
return self.gui.Mutexes
|
|
}
|
|
|
|
func (self *guiCommon) GocuiGui() *gocui.Gui {
|
|
return self.gui.g
|
|
}
|
|
|
|
func (self *guiCommon) OnUIThread(f func() error) {
|
|
self.gui.onUIThread(f)
|
|
}
|
|
|
|
func (self *guiCommon) OnWorker(f func(*gocui.Task)) {
|
|
self.gui.onWorker(f)
|
|
}
|
|
|
|
func (self *guiCommon) RenderToMainViews(opts types.RefreshMainOpts) error {
|
|
return self.gui.refreshMainViews(opts)
|
|
}
|
|
|
|
func (self *guiCommon) MainViewPairs() types.MainViewPairs {
|
|
return types.MainViewPairs{
|
|
Normal: self.gui.normalMainContextPair(),
|
|
Staging: self.gui.stagingMainContextPair(),
|
|
PatchBuilding: self.gui.patchBuildingMainContextPair(),
|
|
MergeConflicts: self.gui.mergingMainContextPair(),
|
|
}
|
|
}
|
|
|
|
func (self *guiCommon) State() types.IStateAccessor {
|
|
return self.gui.stateAccessor
|
|
}
|
|
|
|
func (self *guiCommon) KeybindingsOpts() types.KeybindingsOpts {
|
|
return self.gui.keybindingOpts()
|
|
}
|
|
|
|
func (self *guiCommon) IsAnyModeActive() bool {
|
|
return self.gui.helpers.Mode.IsAnyModeActive()
|
|
}
|
|
|
|
func (self *guiCommon) GetInitialKeybindingsWithCustomCommands() ([]*types.Binding, []*gocui.ViewMouseBinding) {
|
|
return self.gui.GetInitialKeybindingsWithCustomCommands()
|
|
}
|