2018-08-12 11:31:27 +02:00
|
|
|
package gui
|
2018-05-19 03:16:34 +02:00
|
|
|
|
|
|
|
import (
|
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
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
2018-08-09 05:14:24 +02:00
|
|
|
"runtime"
|
2018-07-21 10:37:00 +02:00
|
|
|
"strings"
|
2018-07-21 07:51:18 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
// "strings"
|
2018-08-06 08:11:29 +02:00
|
|
|
|
2018-08-13 12:26:02 +02:00
|
|
|
"github.com/Sirupsen/logrus"
|
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"
|
2018-05-19 03:16:34 +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
|
|
|
|
2018-08-13 12:26:02 +02:00
|
|
|
// ErrSubProcess tells us we're switching to a subprocess so we need to
|
2018-08-12 13:04:47 +02:00
|
|
|
// close the Gui until it is finished
|
|
|
|
var (
|
2018-08-13 12:26:02 +02:00
|
|
|
ErrSubProcess = errors.New("running subprocess")
|
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 {
|
|
|
|
Gui *gocui.Gui
|
|
|
|
Log *logrus.Logger
|
|
|
|
GitCommand *commands.GitCommand
|
|
|
|
OSCommand *commands.OSCommand
|
|
|
|
Version string
|
|
|
|
SubProcess *exec.Cmd
|
2018-08-13 13:16:21 +02:00
|
|
|
State guiState
|
|
|
|
}
|
|
|
|
|
|
|
|
type guiState struct {
|
|
|
|
Files []commands.File
|
|
|
|
Branches []commands.Branch
|
|
|
|
Commits []commands.Commit
|
|
|
|
StashEntries []commands.StashEntry
|
|
|
|
PreviousView string
|
|
|
|
HasMergeConflicts bool
|
|
|
|
ConflictIndex int
|
|
|
|
ConflictTop bool
|
|
|
|
Conflicts []commands.Conflict
|
|
|
|
EditHistory *stack.Stack
|
|
|
|
Platform platform
|
|
|
|
Version string
|
2018-08-13 12:26:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewGui builds a new gui handler
|
|
|
|
func NewGui(log *logrus.Logger, gitCommand *commands.GitCommand, oSCommand *commands.OSCommand, version string) (*Gui, error) {
|
2018-08-13 13:16:21 +02:00
|
|
|
initialState := guiState{
|
2018-08-13 12:26:02 +02:00
|
|
|
Files: make([]commands.File, 0),
|
|
|
|
PreviousView: "files",
|
|
|
|
Commits: make([]commands.Commit, 0),
|
|
|
|
StashEntries: make([]commands.StashEntry, 0),
|
|
|
|
ConflictIndex: 0,
|
|
|
|
ConflictTop: true,
|
|
|
|
Conflicts: make([]commands.Conflict, 0),
|
|
|
|
EditHistory: stack.New(),
|
|
|
|
Platform: getPlatform(),
|
|
|
|
Version: "test version", // TODO: send version in
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Gui{
|
|
|
|
Log: log,
|
|
|
|
GitCommand: gitCommand,
|
|
|
|
OSCommand: oSCommand,
|
|
|
|
Version: version,
|
|
|
|
State: initialState,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-08-09 05:14:24 +02:00
|
|
|
type platform struct {
|
|
|
|
os string
|
|
|
|
shell string
|
|
|
|
shellArg string
|
|
|
|
escapedQuote string
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPlatform() platform {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
|
|
|
return platform{
|
|
|
|
os: "windows",
|
|
|
|
shell: "cmd",
|
|
|
|
shellArg: "/c",
|
|
|
|
escapedQuote: "\\\"",
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return platform{
|
|
|
|
os: runtime.GOOS,
|
|
|
|
shell: "bash",
|
|
|
|
shellArg: "-c",
|
|
|
|
escapedQuote: "\"",
|
|
|
|
}
|
|
|
|
}
|
2018-05-26 05:23:39 +02:00
|
|
|
}
|
2018-05-21 12:52:48 +02:00
|
|
|
|
2018-08-13 13:16:21 +02:00
|
|
|
func (gui *Gui) scrollUpMain(g *gocui.Gui, v *gocui.View) error {
|
2018-07-21 07:51:18 +02:00
|
|
|
mainView, _ := g.View("main")
|
|
|
|
ox, oy := mainView.Origin()
|
|
|
|
if oy >= 1 {
|
|
|
|
return mainView.SetOrigin(ox, oy-1)
|
|
|
|
}
|
|
|
|
return nil
|
2018-05-21 12:52:48 +02:00
|
|
|
}
|
2018-05-19 03:16:34 +02:00
|
|
|
|
2018-08-13 13:16:21 +02:00
|
|
|
func (gui *Gui) scrollDownMain(g *gocui.Gui, v *gocui.View) error {
|
2018-07-21 07:51:18 +02:00
|
|
|
mainView, _ := g.View("main")
|
|
|
|
ox, oy := mainView.Origin()
|
|
|
|
if oy < len(mainView.BufferLines()) {
|
|
|
|
return mainView.SetOrigin(ox, oy+1)
|
|
|
|
}
|
|
|
|
return nil
|
2018-05-19 09:04:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 13:16:21 +02:00
|
|
|
func (gui *Gui) handleRefresh(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return gui.refreshSidePanels(g)
|
2018-06-09 11:06:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 11:49:36 +02:00
|
|
|
func max(a, b int) int {
|
|
|
|
if a > b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// layout is called for every screen re-render e.g. when the screen is resized
|
2018-08-13 12:26:02 +02:00
|
|
|
func (gui *Gui) layout(g *gocui.Gui) error {
|
2018-07-21 07:51:18 +02:00
|
|
|
g.Highlight = true
|
2018-08-06 08:11:29 +02:00
|
|
|
g.SelFgColor = gocui.ColorWhite | gocui.AttrBold
|
2018-07-21 07:51:18 +02:00
|
|
|
width, height := g.Size()
|
|
|
|
leftSideWidth := width / 3
|
|
|
|
statusFilesBoundary := 2
|
|
|
|
filesBranchesBoundary := 2 * height / 5 // height - 20
|
|
|
|
commitsBranchesBoundary := 3 * height / 5 // height - 10
|
|
|
|
commitsStashBoundary := height - 5 // height - 5
|
2018-08-05 14:00:02 +02:00
|
|
|
minimumHeight := 16
|
2018-08-09 11:49:36 +02:00
|
|
|
minimumWidth := 10
|
2018-08-05 14:00:02 +02:00
|
|
|
|
2018-08-05 14:18:59 +02:00
|
|
|
panelSpacing := 1
|
|
|
|
if OverlappingEdges {
|
|
|
|
panelSpacing = 0
|
|
|
|
}
|
|
|
|
|
2018-08-09 11:49:36 +02:00
|
|
|
if height < minimumHeight || width < minimumWidth {
|
|
|
|
v, err := g.SetView("limit", 0, 0, max(width-1, 2), max(height-1, 2), 0)
|
2018-08-05 14:00:02 +02:00
|
|
|
if err != nil {
|
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Title = "Not enough space to render panels"
|
|
|
|
v.Wrap = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-21 12:52:48 +02:00
|
|
|
|
2018-08-06 07:41:59 +02:00
|
|
|
g.DeleteView("limit")
|
|
|
|
|
2018-07-21 07:51:18 +02:00
|
|
|
optionsTop := height - 2
|
|
|
|
// hiding options if there's not enough space
|
|
|
|
if height < 30 {
|
|
|
|
optionsTop = height - 1
|
|
|
|
}
|
2018-05-21 12:52:48 +02:00
|
|
|
|
2018-08-05 14:18:59 +02:00
|
|
|
v, err := g.SetView("main", leftSideWidth+panelSpacing, 0, width-1, optionsTop, gocui.LEFT)
|
2018-07-21 07:51:18 +02:00
|
|
|
if err != nil {
|
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-05 14:00:02 +02:00
|
|
|
v.Title = "Diff"
|
|
|
|
v.Wrap = true
|
2018-08-06 08:11:29 +02:00
|
|
|
v.FgColor = gocui.ColorWhite
|
2018-07-21 07:51:18 +02:00
|
|
|
}
|
2018-05-19 03:16:34 +02:00
|
|
|
|
2018-08-05 14:00:02 +02:00
|
|
|
if v, err := g.SetView("status", 0, 0, leftSideWidth, statusFilesBoundary, gocui.BOTTOM|gocui.RIGHT); err != nil {
|
2018-07-21 07:51:18 +02:00
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Title = "Status"
|
2018-08-06 08:11:29 +02:00
|
|
|
v.FgColor = gocui.ColorWhite
|
2018-07-21 07:51:18 +02:00
|
|
|
}
|
2018-06-01 15:23:31 +02:00
|
|
|
|
2018-08-05 14:18:59 +02:00
|
|
|
filesView, err := g.SetView("files", 0, statusFilesBoundary+panelSpacing, leftSideWidth, filesBranchesBoundary, gocui.TOP|gocui.BOTTOM)
|
2018-07-21 07:51:18 +02:00
|
|
|
if err != nil {
|
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-05 14:00:02 +02:00
|
|
|
filesView.Highlight = true
|
|
|
|
filesView.Title = "Files"
|
2018-08-06 08:11:29 +02:00
|
|
|
v.FgColor = gocui.ColorWhite
|
2018-07-21 07:51:18 +02:00
|
|
|
}
|
2018-05-26 05:23:39 +02:00
|
|
|
|
2018-08-05 14:18:59 +02:00
|
|
|
if v, err := g.SetView("branches", 0, filesBranchesBoundary+panelSpacing, leftSideWidth, commitsBranchesBoundary, gocui.TOP|gocui.BOTTOM); err != nil {
|
2018-07-21 07:51:18 +02:00
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Title = "Branches"
|
2018-08-06 08:11:29 +02:00
|
|
|
v.FgColor = gocui.ColorWhite
|
2018-07-21 07:51:18 +02:00
|
|
|
}
|
2018-05-21 12:52:48 +02:00
|
|
|
|
2018-08-05 14:18:59 +02:00
|
|
|
if v, err := g.SetView("commits", 0, commitsBranchesBoundary+panelSpacing, leftSideWidth, commitsStashBoundary, gocui.TOP|gocui.BOTTOM); err != nil {
|
2018-07-21 07:51:18 +02:00
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Title = "Commits"
|
2018-08-06 08:11:29 +02:00
|
|
|
v.FgColor = gocui.ColorWhite
|
2018-07-21 07:51:18 +02:00
|
|
|
}
|
2018-06-05 10:48:46 +02:00
|
|
|
|
2018-08-05 14:18:59 +02:00
|
|
|
if v, err := g.SetView("stash", 0, commitsStashBoundary+panelSpacing, leftSideWidth, optionsTop, gocui.TOP|gocui.RIGHT); err != nil {
|
2018-07-21 07:51:18 +02:00
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Title = "Stash"
|
2018-08-06 08:11:29 +02:00
|
|
|
v.FgColor = gocui.ColorWhite
|
2018-07-21 07:51:18 +02:00
|
|
|
}
|
2018-05-21 12:52:48 +02:00
|
|
|
|
2018-08-13 12:26:02 +02:00
|
|
|
if v, err := g.SetView("options", -1, optionsTop, width-len(gui.Version)-2, optionsTop+2, 0); err != nil {
|
2018-07-21 07:51:18 +02:00
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-06 08:31:24 +02:00
|
|
|
v.FgColor = gocui.ColorBlue
|
2018-07-21 07:51:18 +02:00
|
|
|
v.Frame = false
|
2018-08-08 00:32:52 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 12:26:02 +02:00
|
|
|
if gui.getCommitMessageView(g) == nil {
|
2018-08-11 07:09:37 +02:00
|
|
|
// doesn't matter where this view starts because it will be hidden
|
|
|
|
if commitMessageView, err := g.SetView("commitMessage", 0, 0, width, height, 0); err != nil {
|
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
g.SetViewOnBottom("commitMessage")
|
|
|
|
commitMessageView.Title = "Commit message"
|
|
|
|
commitMessageView.FgColor = gocui.ColorWhite
|
|
|
|
commitMessageView.Editable = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 12:26:02 +02:00
|
|
|
if v, err := g.SetView("version", width-len(gui.Version)-1, optionsTop, width, optionsTop+2, 0); err != nil {
|
2018-08-08 00:32:52 +02:00
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.BgColor = gocui.ColorDefault
|
|
|
|
v.FgColor = gocui.ColorGreen
|
|
|
|
v.Frame = false
|
2018-08-13 12:26:02 +02:00
|
|
|
gui.renderString(g, "version", gui.Version)
|
2018-06-05 10:48:46 +02:00
|
|
|
|
2018-07-21 07:51:18 +02:00
|
|
|
// these are only called once
|
2018-08-13 12:26:02 +02:00
|
|
|
gui.handleFileSelect(g, filesView)
|
|
|
|
gui.refreshFiles(g)
|
2018-08-13 13:16:21 +02:00
|
|
|
gui.refreshBranches(g)
|
|
|
|
gui.refreshCommits(g)
|
|
|
|
gui.refreshStashEntries(g)
|
|
|
|
gui.nextView(g, nil)
|
2018-07-21 07:51:18 +02:00
|
|
|
}
|
2018-05-19 03:16:34 +02:00
|
|
|
|
2018-08-13 13:16:21 +02:00
|
|
|
gui.resizePopupPanels(g)
|
2018-08-12 04:00:30 +02:00
|
|
|
|
2018-07-21 07:51:18 +02:00
|
|
|
return nil
|
2018-05-19 03:16:34 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 13:16:21 +02:00
|
|
|
func (gui *Gui) fetch(g *gocui.Gui) error {
|
|
|
|
gui.GitCommand.Fetch()
|
|
|
|
gui.refreshStatus(g)
|
2018-08-09 11:01:42 +02:00
|
|
|
return nil
|
2018-06-02 05:51:03 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 13:16:21 +02:00
|
|
|
func (gui *Gui) updateLoader(g *gocui.Gui) error {
|
2018-07-21 10:37:00 +02:00
|
|
|
if confirmationView, _ := g.View("confirmation"); confirmationView != nil {
|
2018-08-13 12:26:02 +02:00
|
|
|
content := gui.trimmedContent(confirmationView)
|
2018-07-21 10:37:00 +02:00
|
|
|
if strings.Contains(content, "...") {
|
|
|
|
staticContent := strings.Split(content, "...")[0] + "..."
|
2018-08-13 13:16:21 +02:00
|
|
|
gui.renderString(g, "confirmation", staticContent+" "+gui.loader())
|
2018-07-21 10:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-09 11:01:42 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-13 13:16:21 +02:00
|
|
|
func (gui *Gui) goEvery(g *gocui.Gui, interval time.Duration, function func(*gocui.Gui) error) {
|
2018-08-09 11:01:42 +02:00
|
|
|
go func() {
|
|
|
|
for range time.Tick(interval) {
|
|
|
|
function(g)
|
|
|
|
}
|
|
|
|
}()
|
2018-07-21 10:37:00 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 13:16:21 +02:00
|
|
|
func (gui *Gui) resizePopupPanels(g *gocui.Gui) error {
|
2018-08-12 04:00:30 +02:00
|
|
|
v := g.CurrentView()
|
|
|
|
if v.Name() == "commitMessage" || v.Name() == "confirmation" {
|
2018-08-13 13:16:21 +02:00
|
|
|
return gui.resizePopupPanel(g, v)
|
2018-08-12 04:00:30 +02:00
|
|
|
}
|
|
|
|
return 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 {
|
2018-08-05 14:18:59 +02:00
|
|
|
g, err := gocui.NewGui(gocui.OutputNormal, 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()
|
2018-05-19 09:04:33 +02:00
|
|
|
|
2018-08-11 07:09:37 +02:00
|
|
|
g.FgColor = gocui.ColorDefault
|
|
|
|
|
2018-08-13 13:16:21 +02:00
|
|
|
gui.goEvery(g, time.Second*60, gui.fetch)
|
|
|
|
gui.goEvery(g, time.Second*10, gui.refreshFiles)
|
|
|
|
gui.goEvery(g, time.Millisecond*10, gui.updateLoader)
|
2018-07-21 10:37:00 +02:00
|
|
|
|
2018-08-13 12:26:02 +02:00
|
|
|
g.SetManagerFunc(gui.layout)
|
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
|
|
|
|
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
|
|
|
|
func (gui *Gui) RunWithSubprocesses() {
|
|
|
|
for {
|
|
|
|
if err := gui.Run(); err != nil {
|
|
|
|
if err == gocui.ErrQuit {
|
|
|
|
break
|
|
|
|
} else if err == ErrSubProcess {
|
|
|
|
gui.SubProcess.Run()
|
|
|
|
} else {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-19 09:04:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 13:16:21 +02:00
|
|
|
func (gui *Gui) quit(g *gocui.Gui, v *gocui.View) error {
|
2018-07-21 07:51:18 +02:00
|
|
|
return gocui.ErrQuit
|
2018-05-26 05:23:39 +02:00
|
|
|
}
|