1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-11-28 09:08:41 +02:00
lazygit/gui.go

268 lines
8.1 KiB
Go
Raw Normal View History

2018-05-21 12:52:48 +02:00
// lots of this has been directly ported from one of the example files, will brush up later
2018-05-19 09:04:33 +02:00
// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
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-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
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-21 12:52:48 +02:00
var cyclableViews = []string{"files", "branches"}
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 {
panic(v.Name() + " is not in the list of views")
}
}
}
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-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-26 05:23:39 +02:00
if err := g.SetKeybinding("", 'ç', 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-26 05:23:39 +02:00
if err := g.SetKeybinding("files", '®', gocui.ModNone, handleFileRemove); err != nil {
2018-05-21 14:34:02 +02:00
return err
}
2018-05-26 05:23:39 +02:00
if err := g.SetKeybinding("files", 'ø', gocui.ModNone, handleFileOpen); err != nil {
2018-05-21 14:34:02 +02:00
return err
}
2018-05-26 05:23:39 +02:00
if err := g.SetKeybinding("files", 'ß', gocui.ModNone, handleSublimeFileOpen); 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-26 08:08:08 +02:00
if err := g.SetKeybinding("", '∑', gocui.ModNone, handleLogState); err != nil {
return err
}
return nil
}
func handleLogState(g *gocui.Gui, v *gocui.View) error {
devLog("state is:", state)
devLog("previous view:", state.PreviousView)
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-05-21 12:52:48 +02:00
2018-05-26 05:23:39 +02:00
optionsTop := height - 3
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 {
optionsTop = height
2018-05-21 12:52:48 +02:00
}
sideView, err := g.SetView("files", 0, 0, 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-05-26 05:23:39 +02:00
if v, err := g.SetView("main", leftSideWidth+2, 0, width-1, optionsTop-1); 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)
}
if v, err := g.SetView("logs", 0, logsBranchesBoundary, leftSideWidth, optionsTop-1); err != nil {
if err != gocui.ErrUnknownView {
2018-05-21 12:52:48 +02:00
return err
}
2018-05-26 05:23:39 +02:00
v.Title = "Log"
// these are only called once
refreshLogs(g)
2018-05-21 12:52:48 +02:00
}
2018-05-26 05:23:39 +02:00
if v, err := g.SetView("branches", 0, filesBranchesBoundary, leftSideWidth, logsBranchesBoundary-1); err != nil {
2018-05-21 12:52:48 +02:00
if err != gocui.ErrUnknownView {
return err
}
v.Title = "Branches"
// these are only called once
refreshBranches(v)
nextView(g, nil)
}
2018-05-26 05:23:39 +02:00
if v, err := g.SetView("options", 0, optionsTop, width-1, 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-05-21 12:52:48 +02:00
v.Title = "Options"
2018-05-19 03:16:34 +02:00
}
return nil
}
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()
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/"