2019-11-16 07:38:38 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/fatih/color"
|
|
|
|
"github.com/jesseduffield/gocui"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
2020-02-25 11:55:36 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
|
2019-11-16 07:38:38 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// list panel functions
|
|
|
|
|
|
|
|
func (gui *Gui) getSelectedRemote() *commands.Remote {
|
|
|
|
selectedLine := gui.State.Panels.Remotes.SelectedLine
|
2019-11-17 04:47:47 +02:00
|
|
|
if selectedLine == -1 || len(gui.State.Remotes) == 0 {
|
2019-11-16 07:38:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return gui.State.Remotes[selectedLine]
|
|
|
|
}
|
|
|
|
|
2020-08-15 08:50:30 +02:00
|
|
|
func (gui *Gui) handleRemoteSelect() error {
|
2019-11-16 07:38:38 +02:00
|
|
|
if gui.popupPanelFocused() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
gui.State.SplitMainPanel = false
|
|
|
|
|
2020-08-15 08:50:30 +02:00
|
|
|
if _, err := gui.g.SetCurrentView("branches"); err != nil {
|
2019-11-16 07:38:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
gui.getMainView().Title = "Remote"
|
|
|
|
|
|
|
|
remote := gui.getSelectedRemote()
|
2019-11-18 00:38:36 +02:00
|
|
|
if remote == 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
|
|
|
return gui.newStringTask("main", "No remotes")
|
2019-11-18 00:38:36 +02:00
|
|
|
}
|
2020-03-29 05:34:17 +02:00
|
|
|
if gui.inDiffMode() {
|
|
|
|
return gui.renderDiff()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
return gui.newStringTask("main", fmt.Sprintf("%s\nUrls:\n%s", utils.ColoredString(remote.Name, color.FgGreen), strings.Join(remote.Urls, "\n")))
|
2019-11-16 07:38:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) refreshRemotes() error {
|
2019-11-17 04:47:47 +02:00
|
|
|
prevSelectedRemote := gui.getSelectedRemote()
|
|
|
|
|
2019-11-16 07:38:38 +02:00
|
|
|
remotes, err := gui.GitCommand.GetRemotes()
|
|
|
|
if err != nil {
|
2020-03-28 02:47:54 +02:00
|
|
|
return gui.surfaceError(err)
|
2019-11-16 07:38:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gui.State.Remotes = remotes
|
|
|
|
|
2019-11-17 04:47:47 +02:00
|
|
|
// we need to ensure our selected remote branches aren't now outdated
|
|
|
|
if prevSelectedRemote != nil && gui.State.RemoteBranches != nil {
|
|
|
|
// find remote now
|
|
|
|
for _, remote := range remotes {
|
|
|
|
if remote.Name == prevSelectedRemote.Name {
|
|
|
|
gui.State.RemoteBranches = remote.Branches
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: see if this works for deleting remote branches
|
|
|
|
switch gui.getBranchesView().Context {
|
|
|
|
case "remotes":
|
2019-11-17 03:02:39 +02:00
|
|
|
return gui.renderRemotesWithSelection()
|
2019-11-17 04:47:47 +02:00
|
|
|
case "remote-branches":
|
|
|
|
return gui.renderRemoteBranchesWithSelection()
|
2019-11-17 03:02:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) renderRemotesWithSelection() error {
|
|
|
|
branchesView := gui.getBranchesView()
|
|
|
|
|
|
|
|
gui.refreshSelectedLine(&gui.State.Panels.Remotes.SelectedLine, len(gui.State.Remotes))
|
2020-02-25 11:55:36 +02:00
|
|
|
|
2020-03-29 05:34:17 +02:00
|
|
|
displayStrings := presentation.GetRemoteListDisplayStrings(gui.State.Remotes, gui.State.Diff.Ref)
|
2020-02-25 11:55:36 +02:00
|
|
|
gui.renderDisplayStrings(branchesView, displayStrings)
|
|
|
|
|
2020-01-09 12:34:17 +02:00
|
|
|
if gui.g.CurrentView() == branchesView && branchesView.Context == "remotes" {
|
2020-08-15 08:50:30 +02:00
|
|
|
if err := gui.handleRemoteSelect(); err != nil {
|
2020-01-09 12:34:17 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-11-17 03:02:39 +02:00
|
|
|
}
|
2019-11-16 07:38:38 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-16 08:35:59 +02:00
|
|
|
|
2020-08-15 09:23:16 +02:00
|
|
|
func (gui *Gui) handleRemoteEnter() error {
|
2019-11-16 08:35:59 +02:00
|
|
|
// naive implementation: get the branches and render them to the list, change the context
|
|
|
|
remote := gui.getSelectedRemote()
|
2019-11-18 00:38:36 +02:00
|
|
|
if remote == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-16 08:35:59 +02:00
|
|
|
|
|
|
|
gui.State.RemoteBranches = remote.Branches
|
|
|
|
|
|
|
|
newSelectedLine := 0
|
|
|
|
if len(remote.Branches) == 0 {
|
|
|
|
newSelectedLine = -1
|
|
|
|
}
|
|
|
|
gui.State.Panels.RemoteBranches.SelectedLine = newSelectedLine
|
|
|
|
|
|
|
|
return gui.switchBranchesPanelContext("remote-branches")
|
|
|
|
}
|
2019-11-17 03:02:39 +02:00
|
|
|
|
|
|
|
func (gui *Gui) handleAddRemote(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
branchesView := gui.getBranchesView()
|
2020-08-15 08:38:16 +02:00
|
|
|
return gui.prompt(branchesView, gui.Tr.SLocalize("newRemoteName"), "", func(remoteName string) error {
|
|
|
|
return gui.prompt(branchesView, gui.Tr.SLocalize("newRemoteUrl"), "", func(remoteUrl string) error {
|
2019-11-17 03:02:39 +02:00
|
|
|
if err := gui.GitCommand.AddRemote(remoteName, remoteUrl); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-28 01:27:34 +02:00
|
|
|
return gui.refreshSidePanels(refreshOptions{scope: []int{REMOTES}})
|
2019-11-17 03:02:39 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleRemoveRemote(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
remote := gui.getSelectedRemote()
|
|
|
|
if remote == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-15 08:38:16 +02:00
|
|
|
return gui.ask(askOpts{
|
2020-08-15 08:36:39 +02:00
|
|
|
returnToView: v,
|
|
|
|
returnFocusOnClose: true,
|
|
|
|
title: gui.Tr.SLocalize("removeRemote"),
|
|
|
|
prompt: gui.Tr.SLocalize("removeRemotePrompt") + " '" + remote.Name + "'?",
|
|
|
|
handleConfirm: func() error {
|
|
|
|
if err := gui.GitCommand.RemoveRemote(remote.Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-17 03:02:39 +02:00
|
|
|
|
2020-08-15 08:36:39 +02:00
|
|
|
return gui.refreshSidePanels(refreshOptions{scope: []int{BRANCHES, REMOTES}})
|
|
|
|
},
|
|
|
|
})
|
2019-11-17 03:02:39 +02:00
|
|
|
}
|
2019-11-17 09:15:32 +02:00
|
|
|
|
|
|
|
func (gui *Gui) handleEditRemote(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
branchesView := gui.getBranchesView()
|
|
|
|
remote := gui.getSelectedRemote()
|
|
|
|
if remote == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
editNameMessage := gui.Tr.TemplateLocalize(
|
|
|
|
"editRemoteName",
|
|
|
|
Teml{
|
|
|
|
"remoteName": remote.Name,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-08-15 08:38:16 +02:00
|
|
|
return gui.prompt(branchesView, editNameMessage, "", func(updatedRemoteName string) error {
|
2019-11-17 09:15:32 +02:00
|
|
|
if updatedRemoteName != remote.Name {
|
|
|
|
if err := gui.GitCommand.RenameRemote(remote.Name, updatedRemoteName); err != nil {
|
2020-03-28 02:47:54 +02:00
|
|
|
return gui.surfaceError(err)
|
2019-11-17 09:15:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
editUrlMessage := gui.Tr.TemplateLocalize(
|
|
|
|
"editRemoteUrl",
|
|
|
|
Teml{
|
|
|
|
"remoteName": updatedRemoteName,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-08-15 08:38:16 +02:00
|
|
|
return gui.prompt(branchesView, editUrlMessage, "", func(updatedRemoteUrl string) error {
|
2019-11-17 09:15:32 +02:00
|
|
|
if err := gui.GitCommand.UpdateRemoteUrl(updatedRemoteName, updatedRemoteUrl); err != nil {
|
2020-03-28 02:47:54 +02:00
|
|
|
return gui.surfaceError(err)
|
2019-11-17 09:15:32 +02:00
|
|
|
}
|
2020-03-28 01:27:34 +02:00
|
|
|
return gui.refreshSidePanels(refreshOptions{scope: []int{BRANCHES, REMOTES}})
|
2019-11-17 09:15:32 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-12-07 07:20:22 +02:00
|
|
|
|
|
|
|
func (gui *Gui) handleFetchRemote(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
remote := gui.getSelectedRemote()
|
|
|
|
if remote == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return gui.WithWaitingStatus(gui.Tr.SLocalize("FetchingRemoteStatus"), func() error {
|
|
|
|
if err := gui.GitCommand.FetchRemote(remote.Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-28 01:27:34 +02:00
|
|
|
return gui.refreshSidePanels(refreshOptions{scope: []int{BRANCHES, REMOTES}})
|
2019-12-07 07:20:22 +02:00
|
|
|
})
|
|
|
|
}
|