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

320 lines
9.4 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
2018-05-19 09:04:33 +02:00
// "io"
// "io/ioutil"
2018-05-26 05:23:39 +02:00
2018-05-19 03:16:34 +02:00
"log"
2018-06-02 05:51:03 +02:00
"time"
2018-05-19 09:04:33 +02:00
// "strings"
2018-05-21 12:52:48 +02:00
"github.com/jroimartin/gocui"
2018-05-19 03:16:34 +02:00
)
2018-05-21 12:52:48 +02:00
type stateType struct {
2018-05-26 05:23:39 +02:00
GitFiles []GitFile
Branches []Branch
2018-05-27 08:32:09 +02:00
Commits []Commit
2018-05-26 05:23:39 +02:00
PreviousView string
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",
2018-05-27 08:32:09 +02:00
Commits: make([]Commit, 0),
2018-05-26 05:23:39 +02:00
}
2018-05-21 12:52:48 +02:00
2018-05-27 08:32:09 +02:00
var cyclableViews = []string{"files", "branches", "commits"}
func refreshSidePanels(g *gocui.Gui, v *gocui.View) error {
refreshBranches(g)
refreshFiles(g)
refreshCommits(g)
return nil
}
2018-05-19 09:04:33 +02:00
func nextView(g *gocui.Gui, v *gocui.View) error {
2018-05-21 12:52:48 +02:00
var focusedViewName string
if v == nil || v.Name() == cyclableViews[len(cyclableViews)-1] {
focusedViewName = cyclableViews[0]
} else {
for i := range cyclableViews {
if v.Name() == cyclableViews[i] {
focusedViewName = cyclableViews[i+1]
break
}
if i == len(cyclableViews)-1 {
2018-05-27 08:32:09 +02:00
devLog(v.Name() + " is not in the list of views")
return nil
2018-05-21 12:52:48 +02:00
}
}
}
focusedView, err := g.View(focusedViewName)
if err != nil {
panic(err)
2018-05-19 09:04:33 +02:00
return err
2018-05-19 03:16:34 +02:00
}
2018-05-21 14:34:02 +02:00
return switchFocus(g, v, focusedView)
}
2018-05-26 05:23:39 +02:00
func newLineFocused(g *gocui.Gui, v *gocui.View) error {
2018-05-21 12:52:48 +02:00
mainView, _ := g.View("main")
mainView.SetOrigin(0, 0)
switch v.Name() {
case "files":
return handleFileSelect(g, v)
case "branches":
return handleBranchSelect(g, v)
2018-05-26 05:23:39 +02:00
case "commit":
return handleCommitPromptFocus(g, v)
case "confirmation":
return nil
case "main":
2018-05-21 14:34:02 +02:00
return nil
2018-05-27 08:32:09 +02:00
case "commits":
return handleCommitSelect(g, v)
2018-05-21 12:52:48 +02:00
default:
2018-05-26 05:23:39 +02:00
panic("No view matching newLineFocused switch statement")
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 {
2018-05-21 12:52:48 +02:00
mainView, _ := g.View("main")
ox, oy := mainView.Origin()
if oy >= 1 {
return mainView.SetOrigin(ox, oy-1)
2018-05-19 03:16:34 +02:00
}
2018-05-21 12:52:48 +02:00
return nil
}
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 {
2018-05-21 12:52:48 +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
}
func keybindings(g *gocui.Gui) error {
2018-05-21 14:34:02 +02:00
if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, nextView); err != nil {
return err
}
if err := g.SetKeybinding("", 'q', gocui.ModNone, quit); err != nil {
return err
}
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
return err
}
if err := g.SetKeybinding("", gocui.KeyArrowDown, gocui.ModNone, cursorDown); err != nil {
return err
}
if err := g.SetKeybinding("", gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil {
return err
}
2018-05-26 05:23:39 +02:00
if err := g.SetKeybinding("", gocui.KeyPgup, gocui.ModNone, scrollUpMain); err != nil {
2018-05-21 14:34:02 +02:00
return err
}
2018-05-26 05:23:39 +02:00
if err := g.SetKeybinding("", gocui.KeyPgdn, gocui.ModNone, scrollDownMain); err != nil {
2018-05-21 14:34:02 +02:00
return err
}
2018-05-27 08:32:09 +02:00
if err := g.SetKeybinding("files", 'c', gocui.ModNone, handleCommitPress); err != nil {
2018-05-21 14:34:02 +02:00
return err
2018-05-19 09:04:33 +02:00
}
2018-05-21 12:52:48 +02:00
if err := g.SetKeybinding("files", gocui.KeySpace, gocui.ModNone, handleFilePress); err != nil {
2018-05-19 09:04:33 +02:00
return err
}
2018-05-27 08:32:09 +02:00
if err := g.SetKeybinding("files", 'r', gocui.ModNone, handleFileRemove); err != nil {
return err
}
if err := g.SetKeybinding("files", 'o', gocui.ModNone, handleFileOpen); err != nil {
return err
}
if err := g.SetKeybinding("files", 's', gocui.ModNone, handleSublimeFileOpen); err != nil {
2018-05-21 14:34:02 +02:00
return err
}
2018-06-01 15:23:31 +02:00
if err := g.SetKeybinding("", 'P', gocui.ModNone, pushFiles); err != nil {
2018-05-21 14:34:02 +02:00
return err
}
2018-06-01 15:23:31 +02:00
if err := g.SetKeybinding("", 'p', gocui.ModNone, pullFiles); err != nil {
return err
}
if err := g.SetKeybinding("files", 'i', gocui.ModNone, handleIgnoreFile); err != nil {
2018-05-21 14:34:02 +02:00
return err
}
2018-05-26 05:23:39 +02:00
if err := g.SetKeybinding("commit", gocui.KeyEsc, gocui.ModNone, closeCommitPrompt); err != nil {
2018-05-19 09:04:33 +02:00
return err
}
2018-05-26 05:23:39 +02:00
if err := g.SetKeybinding("commit", gocui.KeyEnter, gocui.ModNone, handleCommitSubmit); err != nil {
2018-05-21 12:52:48 +02:00
return err
}
2018-05-26 05:23:39 +02:00
if err := g.SetKeybinding("branches", gocui.KeySpace, gocui.ModNone, handleBranchPress); err != nil {
return err
2018-05-19 09:04:33 +02:00
}
2018-05-27 08:32:09 +02:00
if err := g.SetKeybinding("branches", 'F', gocui.ModNone, handleForceCheckout); err != nil {
return err
}
2018-06-02 05:51:03 +02:00
if err := g.SetKeybinding("branches", 'n', gocui.ModNone, handleNewBranch); err != nil {
return err
}
2018-05-27 08:32:09 +02:00
if err := g.SetKeybinding("commits", 's', gocui.ModNone, handleCommitSquashDown); err != nil {
return err
}
if err := g.SetKeybinding("commits", 'r', gocui.ModNone, handleRenameCommit); err != nil {
return err
}
2018-06-02 05:51:03 +02:00
if err := g.SetKeybinding("commits", 'g', gocui.ModNone, handleResetToCommit); err != nil {
return err
}
if err := g.SetKeybinding("", 'S', gocui.ModNone, genericTest); err != nil {
2018-05-26 08:08:08 +02:00
return err
}
return nil
}
2018-06-02 05:51:03 +02:00
func genericTest(g *gocui.Gui, v *gocui.View) error {
pushFiles(g, v)
2018-05-19 09:04:33 +02:00
return nil
2018-05-19 03:16:34 +02:00
}
func layout(g *gocui.Gui) error {
2018-05-26 05:23:39 +02:00
width, height := g.Size()
leftSideWidth := width / 3
logsBranchesBoundary := height - 10
filesBranchesBoundary := height - 20
2018-06-01 15:23:31 +02:00
statusFilesBoundary := 2
2018-05-21 12:52:48 +02:00
2018-06-01 15:23:31 +02:00
optionsTop := height - 2
2018-05-21 12:52:48 +02:00
// hiding options if there's not enough space
2018-05-26 05:23:39 +02:00
if height < 30 {
2018-06-01 15:23:31 +02:00
optionsTop = height - 1
2018-05-21 12:52:48 +02:00
}
2018-06-01 15:23:31 +02:00
sideView, err := g.SetView("files", 0, statusFilesBoundary+1, leftSideWidth, filesBranchesBoundary-1)
2018-05-19 09:04:33 +02:00
if err != nil {
2018-05-19 03:16:34 +02:00
if err != gocui.ErrUnknownView {
return err
}
2018-05-21 12:52:48 +02:00
sideView.Highlight = true
2018-05-19 09:04:33 +02:00
sideView.Title = "Files"
2018-05-21 12:52:48 +02:00
refreshFiles(g)
2018-05-19 03:16:34 +02:00
}
2018-06-01 15:23:31 +02:00
if v, err := g.SetView("status", 0, statusFilesBoundary-2, leftSideWidth, statusFilesBoundary); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = "Status"
}
if v, err := g.SetView("main", leftSideWidth+1, 0, width-1, optionsTop); err != nil {
2018-05-19 03:16:34 +02:00
if err != gocui.ErrUnknownView {
return err
}
2018-05-21 12:52:48 +02:00
v.Title = "Diff"
2018-05-19 09:04:33 +02:00
v.Wrap = true
2018-05-26 05:23:39 +02:00
switchFocus(g, nil, v)
handleFileSelect(g, sideView)
}
2018-06-01 15:23:31 +02:00
if v, err := g.SetView("branches", 0, filesBranchesBoundary, leftSideWidth, logsBranchesBoundary-1); err != nil {
2018-05-26 05:23:39 +02:00
if err != gocui.ErrUnknownView {
2018-05-21 12:52:48 +02:00
return err
}
2018-06-01 15:23:31 +02:00
v.Title = "Branches"
2018-05-26 05:23:39 +02:00
// these are only called once
2018-06-01 15:23:31 +02:00
refreshBranches(g)
nextView(g, nil)
2018-05-21 12:52:48 +02:00
}
2018-06-01 15:23:31 +02:00
if v, err := g.SetView("commits", 0, logsBranchesBoundary, leftSideWidth, optionsTop); err != nil {
2018-05-21 12:52:48 +02:00
if err != gocui.ErrUnknownView {
return err
}
2018-06-01 15:23:31 +02:00
v.Title = "Commits"
2018-05-21 12:52:48 +02:00
// these are only called once
2018-06-01 15:23:31 +02:00
refreshCommits(g)
2018-05-21 12:52:48 +02:00
}
2018-06-01 15:23:31 +02:00
if v, err := g.SetView("options", -1, optionsTop, width, optionsTop+2); err != nil {
2018-05-21 12:52:48 +02:00
if err != gocui.ErrUnknownView {
2018-05-19 09:04:33 +02:00
return err
}
2018-06-01 15:23:31 +02:00
v.BgColor = gocui.ColorBlue
v.Frame = false
2018-05-21 12:52:48 +02:00
v.Title = "Options"
2018-05-19 03:16:34 +02:00
}
return nil
}
2018-06-02 05:51:03 +02:00
func fetch(g *gocui.Gui) {
gitFetch()
refreshStatus(g)
}
2018-05-19 09:04:33 +02:00
func run() {
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
log.Panicln(err)
}
defer g.Close()
2018-06-02 05:51:03 +02:00
// periodically fetching to check for upstream differences
go func() {
for range time.Tick(time.Second * 60) {
fetch(g)
}
}()
2018-05-19 09:04:33 +02:00
g.SetManagerFunc(layout)
if err := keybindings(g); err != nil {
log.Panicln(err)
}
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)
}
}
2018-05-26 05:23:39 +02:00
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
2018-05-19 09:04:33 +02:00
// const mcRide = "
// `.-::-`
// -/o+oossys+:.
// `+o++++++osssyys/`
// ://-:+.` .::-. . `++oyyo/-/+oooosyhy.
// `-+sy::-:::/+o+yss+-... /s++ss/:/:+osyoosydh`
// `-:/+o/:/+:/+-s/:s/o+`/++s++/:--/+shds+++yd:
// `y+/+soy:+/-o++y+yhyyyo/---/oyhddo/::od-
// .+o-``-+syysy//o:-oo+oyyyo+oyhyddds/oshy
// `:o++o+/-....-:/+oooyyh+:ooshhhhhhdddssyyy`
// .:o+/++ooosso//:::+yo.::hs+++:yhhhhdddhoyhh:
// `-/+so///+osyso-.:://++-` `:hhhdsohddhhhdddssh+
// -+oso++ssoyys:.` ydddddddddddhho+yd+
// `:sysssssssydh:` `-:::-..-...`ydddddddddyso++shds
// `/syyysssyyhhdd+``..://+ooo/++ssssoyddddddhho/:::oyhdhs-`
// -syyyysssyhhddhyo++++/::+/+/-:::///+sddddhs//+o+/ososyhhs+/.`
// `+hhyyyyyyyhddhs+///://///+ooo/::+o++osyhyyys+--+//o//oosyys++++:..``
// .sddhyhyyyhddyso++/::://////+syo/:osssssyhsssoooosoo//+ossssyssooooo+++:.
// .hdhhhhhhhhhysssssysssssssyyyhddso+soyhhhsssooosyyssso+syysoososoo/++osyo/
// -syyyyyyyyyyyyyyyyyyo/::----:shdsyo+yysyyyssssosyysos+/+++/+ooo++:/+/ooss/
// `........----..`` odhyyyhhsysoss++oysso++s/++++syys++/:::/:+sy-
// `ydyssyysyoyyo+sysyys++s+++++ooo+osss+/+++syy
// /dysyssoyyoo+oyyshss//:---:/++++oshhysooosyh`
// .dhhhyysyyys++yyyyss+--:::/:///oshddhhyo+osy`
// yddhhyyssy+//ssyyso/-:://+ooosyhddhsoo+/+so
// +ddhhyysss+osyyysss:::/oyyhhyhddddds+///oy/
// /dddhhyyyssysssssss+++ooyhdddddddhdyo///yyo
// /dddhyyyyyysssoo+/:-/oshhdddddddssdds+//sys
// +ddhhyyhhy/oo+/:::::+syhddddddds -hdyo++ohh`
// sddhhysyysoys/:::::osyhdddddddy` sdhsosohh:
// `dddddhhhhhhhyo:-/ossoshddddhhd- .ddyssohh/"