1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-11-30 09:16:47 +02:00
lazygit/gui.go

267 lines
5.8 KiB
Go
Raw Normal View History

2018-05-19 09:04:33 +02:00
package main
2018-05-19 03:16:34 +02:00
import (
2018-05-26 05:23:39 +02:00
// "io"
// "io/ioutil"
2018-05-26 05:23:39 +02:00
"runtime"
"strings"
"time"
// "strings"
2018-08-06 08:11:29 +02:00
"github.com/golang-collections/collections/stack"
"github.com/jesseduffield/gocui"
2018-05-19 03:16:34 +02:00
)
// OverlappingEdges determines if panel edges overlap
var OverlappingEdges = false
2018-05-21 12:52:48 +02:00
type stateType struct {
GitFiles []GitFile
Branches []Branch
Commits []Commit
StashEntries []StashEntry
PreviousView string
HasMergeConflicts bool
ConflictIndex int
ConflictTop bool
Conflicts []conflict
EditHistory *stack.Stack
Platform platform
2018-06-09 11:06:33 +02:00
}
type conflict struct {
start int
middle int
end int
2018-05-19 09:04:33 +02:00
}
2018-05-26 05:23:39 +02:00
var state = stateType{
GitFiles: make([]GitFile, 0),
PreviousView: "files",
Commits: make([]Commit, 0),
StashEntries: make([]StashEntry, 0),
ConflictIndex: 0,
ConflictTop: true,
Conflicts: make([]conflict, 0),
EditHistory: stack.New(),
Platform: getPlatform(),
}
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-05-26 05:23:39 +02:00
func scrollUpMain(g *gocui.Gui, v *gocui.View) error {
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-05-26 05:23:39 +02:00
func scrollDownMain(g *gocui.Gui, v *gocui.View) error {
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-06-09 11:06:33 +02:00
func handleRefresh(g *gocui.Gui, v *gocui.View) error {
return refreshSidePanels(g)
2018-06-09 11:06:33 +02:00
}
2018-05-19 03:16:34 +02:00
func layout(g *gocui.Gui) error {
g.Highlight = true
2018-08-06 08:11:29 +02:00
g.SelFgColor = gocui.ColorWhite | gocui.AttrBold
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
panelSpacing := 1
if OverlappingEdges {
panelSpacing = 0
}
2018-08-05 14:00:02 +02:00
if height < minimumHeight {
v, err := g.SetView("limit", 0, 0, width-1, height-1, 0)
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")
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
v, err := g.SetView("main", leftSideWidth+panelSpacing, 0, width-1, optionsTop, gocui.LEFT)
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-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 {
if err != gocui.ErrUnknownView {
return err
}
v.Title = "Status"
2018-08-06 08:11:29 +02:00
v.FgColor = gocui.ColorWhite
}
2018-06-01 15:23:31 +02:00
filesView, err := g.SetView("files", 0, statusFilesBoundary+panelSpacing, leftSideWidth, filesBranchesBoundary, gocui.TOP|gocui.BOTTOM)
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-05-26 05:23:39 +02:00
if v, err := g.SetView("branches", 0, filesBranchesBoundary+panelSpacing, leftSideWidth, commitsBranchesBoundary, gocui.TOP|gocui.BOTTOM); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = "Branches"
2018-08-06 08:11:29 +02:00
v.FgColor = gocui.ColorWhite
}
2018-05-21 12:52:48 +02:00
if v, err := g.SetView("commits", 0, commitsBranchesBoundary+panelSpacing, leftSideWidth, commitsStashBoundary, gocui.TOP|gocui.BOTTOM); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = "Commits"
2018-08-06 08:11:29 +02:00
v.FgColor = gocui.ColorWhite
}
2018-06-05 10:48:46 +02:00
if v, err := g.SetView("stash", 0, commitsStashBoundary+panelSpacing, leftSideWidth, optionsTop, gocui.TOP|gocui.RIGHT); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = "Stash"
2018-08-06 08:11:29 +02:00
v.FgColor = gocui.ColorWhite
}
2018-05-21 12:52:48 +02:00
2018-08-08 14:51:18 +02:00
if v, err := g.SetView("options", -1, optionsTop, width-len(version)-2, optionsTop+2, 0); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.BgColor = gocui.ColorDefault
v.FgColor = gocui.ColorBlue
v.Frame = false
2018-08-08 00:32:52 +02:00
}
2018-08-08 14:51:18 +02:00
if v, err := g.SetView("version", width-len(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-08 14:51:18 +02:00
renderString(g, "version", version)
2018-06-05 10:48:46 +02:00
// these are only called once
handleFileSelect(g, filesView)
refreshFiles(g)
refreshBranches(g)
refreshCommits(g)
refreshStashEntries(g)
nextView(g, nil)
}
2018-05-19 03:16:34 +02:00
return nil
2018-05-19 03:16:34 +02:00
}
2018-06-02 05:51:03 +02:00
func fetch(g *gocui.Gui) {
gitFetch()
refreshStatus(g)
2018-06-02 05:51:03 +02:00
}
func updateLoader(g *gocui.Gui) {
if confirmationView, _ := g.View("confirmation"); confirmationView != nil {
content := trimmedContent(confirmationView)
if strings.Contains(content, "...") {
staticContent := strings.Split(content, "...")[0] + "..."
renderString(g, "confirmation", staticContent+" "+loader())
}
}
}
2018-08-07 10:05:43 +02:00
func run() (err error) {
g, err := gocui.NewGui(gocui.OutputNormal, OverlappingEdges)
if err != nil {
2018-08-07 10:05:43 +02:00
return
}
defer g.Close()
2018-05-19 09:04:33 +02:00
// periodically fetching to check for upstream differences
go func() {
for range time.Tick(time.Second * 60) {
fetch(g)
}
}()
2018-06-02 05:51:03 +02:00
go func() {
for range time.Tick(time.Millisecond * 10) {
updateLoader(g)
}
}()
g.SetManagerFunc(layout)
2018-05-19 09:04:33 +02:00
2018-08-07 10:05:43 +02:00
if err = keybindings(g); err != nil {
return
}
2018-05-19 09:04:33 +02:00
2018-08-07 10:05:43 +02:00
err = g.MainLoop()
return
2018-05-19 09:04:33 +02:00
}
2018-05-26 05:23:39 +02:00
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
2018-05-26 05:23:39 +02:00
}