summaryrefslogtreecommitdiffstats
path: root/pkg/gui/remotes_panel.go
blob: c63b2f07d3fc9499e35a83261cf0ae7f818cc0e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package gui

import (
	"fmt"
	"strings"

	"github.com/fatih/color"
	"github.com/jesseduffield/gocui"
	"github.com/jesseduffield/lazygit/pkg/commands"
	"github.com/jesseduffield/lazygit/pkg/utils"
)

// list panel functions

func (gui *Gui) getSelectedRemote() *commands.Remote {
	selectedLine := gui.State.Panels.Remotes.SelectedLine
	if selectedLine == -1 {
		return nil
	}

	return gui.State.Remotes[selectedLine]
}

func (gui *Gui) handleRemotesClick(g *gocui.Gui, v *gocui.View) error {
	itemCount := len(gui.State.Remotes)
	handleSelect := gui.handleRemoteSelect
	selectedLine := &gui.State.Panels.Remotes.SelectedLine

	return gui.handleClick(v, itemCount, selectedLine, handleSelect)
}

func (gui *Gui) handleRemoteSelect(g *gocui.Gui, v *gocui.View) error {
	if gui.popupPanelFocused() {
		return nil
	}

	gui.State.SplitMainPanel = false

	if _, err := gui.g.SetCurrentView(v.Name()); err != nil {
		return err
	}

	gui.getMainView().Title = "Remote"

	remote := gui.getSelectedRemote()
	if err := gui.focusPoint(0, gui.State.Panels.Remotes.SelectedLine, len(gui.State.Remotes), v); err != nil {
		return err
	}

	return gui.renderString(g, "main", fmt.Sprintf("%s\nUrls:\n%s", utils.ColoredString(remote.Name, color.FgGreen), strings.Join(remote.Urls, "\n")))
}

// gui.refreshStatus is called at the end of this because that's when we can
// be sure there is a state.Remotes array to pick the current remote from
func (gui *Gui) refreshRemotes() error {
	remotes, err := gui.GitCommand.GetRemotes()
	if err != nil {
		return gui.createErrorPanel(gui.g, err.Error())
	}

	gui.State.Remotes = remotes

	gui.g.Update(func(g *gocui.Gui) error {
		gui.refreshSelectedLine(&gui.State.Panels.Remotes.SelectedLine, len(gui.State.Remotes))
		return nil
	})

	return nil
}

func (gui *Gui) handleRemoteEnter(g *gocui.Gui, v *gocui.View) error {
	// naive implementation: get the branches and render them to the list, change the context
	remote := gui.getSelectedRemote()

	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")
}