2018-08-14 11:05:26 +02:00
|
|
|
package gui
|
2018-05-26 05:23:39 +02:00
|
|
|
|
|
|
|
import (
|
2018-07-21 07:51:18 +02:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2018-05-26 05:23:39 +02:00
|
|
|
|
2018-07-21 07:51:18 +02:00
|
|
|
"github.com/jesseduffield/gocui"
|
2018-08-19 13:20:50 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2018-08-16 07:53:53 +02:00
|
|
|
"github.com/spkg/bom"
|
2018-05-26 05:23:39 +02:00
|
|
|
)
|
|
|
|
|
2018-08-18 06:52:01 +02:00
|
|
|
var cyclableViews = []string{"status", "files", "branches", "commits", "stash"}
|
2018-06-06 04:17:49 +02:00
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) refreshSidePanels(g *gocui.Gui) error {
|
|
|
|
gui.refreshBranches(g)
|
|
|
|
gui.refreshFiles(g)
|
|
|
|
gui.refreshCommits(g)
|
2018-07-21 07:51:18 +02:00
|
|
|
return nil
|
2018-06-06 04:17:49 +02:00
|
|
|
}
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) nextView(g *gocui.Gui, v *gocui.View) error {
|
2018-07-21 07:51:18 +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-08-15 11:49:43 +02:00
|
|
|
message := gui.Tr.TemplateLocalize(
|
|
|
|
"IssntListOfViews",
|
2018-08-16 11:31:50 +02:00
|
|
|
Teml{
|
2018-08-15 11:49:43 +02:00
|
|
|
"name": v.Name(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
gui.Log.Info(message)
|
2018-07-21 07:51:18 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
focusedView, err := g.View(focusedViewName)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-08-14 11:05:26 +02:00
|
|
|
return gui.switchFocus(g, v, focusedView)
|
2018-06-06 04:17:49 +02:00
|
|
|
}
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) previousView(g *gocui.Gui, v *gocui.View) error {
|
2018-07-22 04:07:36 +02:00
|
|
|
var focusedViewName string
|
2018-07-22 04:58:39 +02:00
|
|
|
if v == nil || v.Name() == cyclableViews[0] {
|
|
|
|
focusedViewName = cyclableViews[len(cyclableViews)-1]
|
2018-07-22 04:07:36 +02:00
|
|
|
} else {
|
|
|
|
for i := range cyclableViews {
|
|
|
|
if v.Name() == cyclableViews[i] {
|
|
|
|
focusedViewName = cyclableViews[i-1] // TODO: make this work properly
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if i == len(cyclableViews)-1 {
|
2018-08-15 11:49:43 +02:00
|
|
|
message := gui.Tr.TemplateLocalize(
|
|
|
|
"IssntListOfViews",
|
2018-08-16 11:31:50 +02:00
|
|
|
Teml{
|
2018-08-15 11:49:43 +02:00
|
|
|
"name": v.Name(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
gui.Log.Info(message)
|
2018-07-22 04:07:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
focusedView, err := g.View(focusedViewName)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-08-14 11:05:26 +02:00
|
|
|
return gui.switchFocus(g, v, focusedView)
|
2018-07-22 04:07:36 +02:00
|
|
|
}
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) newLineFocused(g *gocui.Gui, v *gocui.View) error {
|
2018-07-21 07:51:18 +02:00
|
|
|
mainView, _ := g.View("main")
|
|
|
|
mainView.SetOrigin(0, 0)
|
|
|
|
|
|
|
|
switch v.Name() {
|
2018-09-05 11:12:11 +02:00
|
|
|
case "menu":
|
|
|
|
return gui.handleMenuSelect(g, v)
|
2018-08-18 06:52:01 +02:00
|
|
|
case "status":
|
|
|
|
return gui.handleStatusSelect(g, v)
|
2018-07-21 07:51:18 +02:00
|
|
|
case "files":
|
2018-08-14 11:05:26 +02:00
|
|
|
return gui.handleFileSelect(g, v)
|
2018-07-21 07:51:18 +02:00
|
|
|
case "branches":
|
2018-08-14 11:05:26 +02:00
|
|
|
return gui.handleBranchSelect(g, v)
|
2018-07-21 07:51:18 +02:00
|
|
|
case "confirmation":
|
|
|
|
return nil
|
2018-08-11 07:09:37 +02:00
|
|
|
case "commitMessage":
|
2018-08-14 11:05:26 +02:00
|
|
|
return gui.handleCommitFocused(g, v)
|
2018-07-21 07:51:18 +02:00
|
|
|
case "main":
|
|
|
|
// TODO: pull this out into a 'view focused' function
|
2018-08-14 11:05:26 +02:00
|
|
|
gui.refreshMergePanel(g)
|
2018-07-21 07:51:18 +02:00
|
|
|
v.Highlight = false
|
|
|
|
return nil
|
|
|
|
case "commits":
|
2018-08-14 11:05:26 +02:00
|
|
|
return gui.handleCommitSelect(g, v)
|
2018-07-21 07:51:18 +02:00
|
|
|
case "stash":
|
2018-08-14 11:05:26 +02:00
|
|
|
return gui.handleStashEntrySelect(g, v)
|
2018-07-21 07:51:18 +02:00
|
|
|
default:
|
2018-08-16 07:16:32 +02:00
|
|
|
panic(gui.Tr.SLocalize("NoViewMachingNewLineFocusedSwitchStatement"))
|
2018-07-21 07:51:18 +02:00
|
|
|
}
|
2018-06-06 04:17:49 +02:00
|
|
|
}
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) returnFocus(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
previousView, err := g.View(gui.State.PreviousView)
|
2018-07-21 07:51:18 +02:00
|
|
|
if err != nil {
|
2018-08-23 10:43:16 +02:00
|
|
|
// always fall back to files view if there's no 'previous' view stored
|
|
|
|
previousView, err = g.View("files")
|
|
|
|
if err != nil {
|
|
|
|
gui.Log.Error(err)
|
|
|
|
}
|
2018-07-21 07:51:18 +02:00
|
|
|
}
|
2018-08-14 11:05:26 +02:00
|
|
|
return gui.switchFocus(g, v, previousView)
|
2018-05-26 05:23:39 +02:00
|
|
|
}
|
|
|
|
|
2018-06-09 11:06:33 +02:00
|
|
|
// pass in oldView = nil if you don't want to be able to return to your old view
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) switchFocus(g *gocui.Gui, oldView, newView *gocui.View) error {
|
2018-07-21 07:51:18 +02:00
|
|
|
// we assume we'll never want to return focus to a confirmation panel i.e.
|
|
|
|
// we should never stack confirmation panels
|
|
|
|
if oldView != nil && oldView.Name() != "confirmation" {
|
|
|
|
oldView.Highlight = false
|
2018-08-15 11:49:43 +02:00
|
|
|
message := gui.Tr.TemplateLocalize(
|
|
|
|
"settingPreviewsViewTo",
|
2018-08-16 11:31:50 +02:00
|
|
|
Teml{
|
2018-08-15 11:49:43 +02:00
|
|
|
"oldViewName": oldView.Name(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
gui.Log.Info(message)
|
2018-09-19 12:36:40 +02:00
|
|
|
|
|
|
|
// second class panels should never have focus restored to them because
|
|
|
|
// once they lose focus they are effectively 'destroyed'
|
|
|
|
secondClassPanels := []string{"confirmation", "menu"}
|
|
|
|
if !utils.IncludesString(secondClassPanels, oldView.Name()) {
|
|
|
|
gui.State.PreviousView = oldView.Name()
|
|
|
|
}
|
2018-07-21 07:51:18 +02:00
|
|
|
}
|
2018-09-19 12:36:40 +02:00
|
|
|
|
2018-07-21 07:51:18 +02:00
|
|
|
newView.Highlight = true
|
2018-08-15 11:49:43 +02:00
|
|
|
message := gui.Tr.TemplateLocalize(
|
|
|
|
"newFocusedViewIs",
|
2018-08-16 11:31:50 +02:00
|
|
|
Teml{
|
2018-08-15 11:49:43 +02:00
|
|
|
"newFocusedView": newView.Name(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
gui.Log.Info(message)
|
2018-07-21 07:51:18 +02:00
|
|
|
if _, err := g.SetCurrentView(newView.Name()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
g.Cursor = newView.Editable
|
2018-08-28 20:13:01 +02:00
|
|
|
|
2018-08-29 13:43:59 +02:00
|
|
|
return gui.newLineFocused(g, newView)
|
2018-05-26 05:23:39 +02:00
|
|
|
}
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) getItemPosition(v *gocui.View) int {
|
|
|
|
gui.correctCursor(v)
|
2018-07-21 07:51:18 +02:00
|
|
|
_, cy := v.Cursor()
|
|
|
|
_, oy := v.Origin()
|
|
|
|
return oy + cy
|
2018-05-26 05:23:39 +02:00
|
|
|
}
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) cursorUp(g *gocui.Gui, v *gocui.View) error {
|
2018-07-21 07:51:18 +02:00
|
|
|
// swallowing cursor movements in main
|
|
|
|
if v == nil || v.Name() == "main" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ox, oy := v.Origin()
|
|
|
|
cx, cy := v.Cursor()
|
|
|
|
if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 {
|
|
|
|
if err := v.SetOrigin(ox, oy-1); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
gui.newLineFocused(g, v)
|
2018-07-21 07:51:18 +02:00
|
|
|
return nil
|
2018-05-26 05:23:39 +02:00
|
|
|
}
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) cursorDown(g *gocui.Gui, v *gocui.View) error {
|
2018-07-21 07:51:18 +02:00
|
|
|
// swallowing cursor movements in main
|
|
|
|
if v == nil || v.Name() == "main" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cx, cy := v.Cursor()
|
|
|
|
ox, oy := v.Origin()
|
2018-09-22 05:44:48 +02:00
|
|
|
ly := v.LinesHeight() - 1
|
2018-09-10 12:17:39 +02:00
|
|
|
_, height := v.Size()
|
|
|
|
maxY := height - 1
|
|
|
|
|
|
|
|
// if we are at the end we just return
|
|
|
|
if cy+oy == ly {
|
2018-07-21 07:51:18 +02:00
|
|
|
return nil
|
|
|
|
}
|
2018-09-10 12:17:39 +02:00
|
|
|
|
|
|
|
var err error
|
|
|
|
if cy < maxY {
|
|
|
|
err = v.SetCursor(cx, cy+1)
|
|
|
|
} else {
|
|
|
|
err = v.SetOrigin(ox, oy+1)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-07-21 07:51:18 +02:00
|
|
|
}
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
gui.newLineFocused(g, v)
|
2018-07-21 07:51:18 +02:00
|
|
|
return nil
|
2018-05-26 05:23:39 +02:00
|
|
|
}
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) resetOrigin(v *gocui.View) error {
|
2018-07-21 07:51:18 +02:00
|
|
|
if err := v.SetCursor(0, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return v.SetOrigin(0, 0)
|
2018-06-09 11:06:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
// if the cursor down past the last item, move it to the last line
|
|
|
|
func (gui *Gui) correctCursor(v *gocui.View) error {
|
2018-07-21 07:51:18 +02:00
|
|
|
cx, cy := v.Cursor()
|
2018-09-10 12:17:39 +02:00
|
|
|
ox, oy := v.Origin()
|
|
|
|
_, height := v.Size()
|
|
|
|
maxY := height - 1
|
2018-09-22 05:44:48 +02:00
|
|
|
ly := v.LinesHeight() - 1
|
2018-09-10 12:17:39 +02:00
|
|
|
if oy+cy <= ly {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
newCy := utils.Min(ly, maxY)
|
|
|
|
if err := v.SetCursor(cx, newCy); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := v.SetOrigin(ox, ly-newCy); err != nil {
|
|
|
|
return err
|
2018-07-21 07:51:18 +02:00
|
|
|
}
|
|
|
|
return nil
|
2018-05-26 05:23:39 +02:00
|
|
|
}
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) renderString(g *gocui.Gui, viewName, s string) error {
|
2018-07-21 07:51:18 +02:00
|
|
|
g.Update(func(*gocui.Gui) error {
|
|
|
|
v, err := g.View(viewName)
|
2018-07-21 10:37:00 +02:00
|
|
|
// just in case the view disappeared as this function was called, we'll
|
|
|
|
// silently return if it's not found
|
2018-07-21 07:51:18 +02:00
|
|
|
if err != nil {
|
2018-07-21 10:37:00 +02:00
|
|
|
return nil
|
2018-07-21 07:51:18 +02:00
|
|
|
}
|
|
|
|
v.Clear()
|
2018-08-19 14:48:03 +02:00
|
|
|
output := string(bom.Clean([]byte(s)))
|
2018-08-19 14:52:08 +02:00
|
|
|
output = utils.NormalizeLinefeeds(output)
|
2018-08-19 08:19:19 +02:00
|
|
|
fmt.Fprint(v, output)
|
2018-07-21 07:51:18 +02:00
|
|
|
v.Wrap = true
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return nil
|
2018-05-26 05:23:39 +02:00
|
|
|
}
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) optionsMapToString(optionsMap map[string]string) string {
|
2018-07-21 07:51:18 +02:00
|
|
|
optionsArray := make([]string, 0)
|
|
|
|
for key, description := range optionsMap {
|
|
|
|
optionsArray = append(optionsArray, key+": "+description)
|
|
|
|
}
|
|
|
|
sort.Strings(optionsArray)
|
|
|
|
return strings.Join(optionsArray, ", ")
|
2018-06-09 11:06:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) renderOptionsMap(g *gocui.Gui, optionsMap map[string]string) error {
|
|
|
|
return gui.renderString(g, "options", gui.optionsMapToString(optionsMap))
|
2018-06-09 11:06:33 +02:00
|
|
|
}
|
2018-07-22 04:07:36 +02:00
|
|
|
|
2018-08-11 07:09:37 +02:00
|
|
|
// TODO: refactor properly
|
2018-09-03 18:45:52 +02:00
|
|
|
// i'm so sorry but had to add this getBranchesView
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) getFilesView(g *gocui.Gui) *gocui.View {
|
2018-08-11 07:09:37 +02:00
|
|
|
v, _ := g.View("files")
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) getCommitsView(g *gocui.Gui) *gocui.View {
|
2018-08-11 07:09:37 +02:00
|
|
|
v, _ := g.View("commits")
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) getCommitMessageView(g *gocui.Gui) *gocui.View {
|
2018-08-11 07:09:37 +02:00
|
|
|
v, _ := g.View("commitMessage")
|
|
|
|
return v
|
|
|
|
}
|
2018-08-14 11:05:26 +02:00
|
|
|
|
2018-09-03 18:45:52 +02:00
|
|
|
func (gui *Gui) getBranchesView(g *gocui.Gui) *gocui.View {
|
|
|
|
v, _ := g.View("branches")
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
func (gui *Gui) trimmedContent(v *gocui.View) string {
|
|
|
|
return strings.TrimSpace(v.Buffer())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) currentViewName(g *gocui.Gui) string {
|
|
|
|
currentView := g.CurrentView()
|
|
|
|
return currentView.Name()
|
|
|
|
}
|
2018-09-05 11:07:46 +02:00
|
|
|
|
|
|
|
func (gui *Gui) resizeCurrentPopupPanel(g *gocui.Gui) error {
|
|
|
|
v := g.CurrentView()
|
|
|
|
if v.Name() == "commitMessage" || v.Name() == "confirmation" {
|
|
|
|
return gui.resizePopupPanel(g, v)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) resizePopupPanel(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
// If the confirmation panel is already displayed, just resize the width,
|
|
|
|
// otherwise continue
|
|
|
|
content := v.Buffer()
|
|
|
|
x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(g, content)
|
|
|
|
vx0, vy0, vx1, vy1 := v.Dimensions()
|
|
|
|
if vx0 == x0 && vy0 == y0 && vx1 == x1 && vy1 == y1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
gui.Log.Info(gui.Tr.SLocalize("resizingPopupPanel"))
|
|
|
|
_, err := g.SetView(v.Name(), x0, y0, x1, y1, 0)
|
|
|
|
return err
|
|
|
|
}
|