Skip to content

Commit

Permalink
More stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed May 27, 2018
1 parent 787e5d1 commit ec78c79
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 89 deletions.
27 changes: 19 additions & 8 deletions branches_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ import (

func handleBranchPress(g *gocui.Gui, v *gocui.View) error {
branch := getSelectedBranch(v)
if err := gitCheckout(branch.Name, false); err != nil {
panic(err)
if output, err := gitCheckout(branch.Name, false); err != nil {
createSimpleConfirmationPanel(g, v, "Error", output)
}
refreshBranches(v)
refreshFiles(g)
refreshLogs(g)
return nil
return refreshSidePanels(g, v)
}

func handleForceCheckout(g *gocui.Gui, v *gocui.View) error {
branch := getSelectedBranch(v)
return createConfirmationPanel(g, v, "Force Checkout Branch", "Are you sure you want force checkout? You will lose all local changes (y/n)", func(g *gocui.Gui, v *gocui.View) error {
if output, err := gitCheckout(branch.Name, true); err != nil {
createSimpleConfirmationPanel(g, v, "Error", output)
}
return refreshSidePanels(g, v)
}, nil)
}

func getSelectedBranch(v *gocui.View) Branch {
Expand All @@ -34,7 +41,7 @@ func getSelectedBranch(v *gocui.View) Branch {
}

func handleBranchSelect(g *gocui.Gui, v *gocui.View) error {
renderString(g, "options", "space: checkout")
renderString(g, "options", "space: checkout, s: squash down")
lineNumber := getItemPosition(v)
branch := state.Branches[lineNumber]
diff, _ := getBranchDiff(branch.Name, branch.BaseBranch)
Expand All @@ -44,7 +51,11 @@ func handleBranchSelect(g *gocui.Gui, v *gocui.View) error {
return nil
}

func refreshBranches(v *gocui.View) error {
func refreshBranches(g *gocui.Gui) error {
v, err := g.View("branches")
if err != nil {
panic(err)
}
state.Branches = getGitBranches()
yellow := color.New(color.FgYellow)
red := color.New(color.FgRed)
Expand Down
4 changes: 2 additions & 2 deletions commit_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func handleCommitPress(g *gocui.Gui, currentView *gocui.View) error {
devLog(stagedFiles(state.GitFiles))
if len(stagedFiles(state.GitFiles)) == 0 {
return createConfirmationPanel(g, currentView, "Nothing to Commit", "There are no staged files to commit (enter)", nil, nil)
return createSimpleConfirmationPanel(g, currentView, "Nothing to Commit", "There are no staged files to commit (esc)")
}
maxX, maxY := g.Size()
if v, err := g.SetView("commit", maxX/2-30, maxY/2-1, maxX/2+30, maxY/2+1); err != nil {
Expand Down Expand Up @@ -44,7 +44,7 @@ func handleCommitSubmit(g *gocui.Gui, v *gocui.View) error {
panic(err)
}
refreshFiles(g)
refreshLogs(g)
refreshCommits(g)
return closeCommitPrompt(g, v)
}

Expand Down
79 changes: 79 additions & 0 deletions commits_panel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// lots of this has been directly ported from one of the example files, will brush up later

// 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

import (
"github.com/fatih/color"
"github.com/jroimartin/gocui"
)

func refreshCommits(g *gocui.Gui) error {
state.Commits = getCommits()
g.Update(func(*gocui.Gui) error {
v, err := g.View("commits")
if err != nil {
panic(err)
}
v.Clear()
yellow := color.New(color.FgYellow)
white := color.New(color.FgWhite)
for _, commit := range state.Commits {
yellow.Fprint(v, commit.Sha+" ")
white.Fprintln(v, commit.Name)
}
return nil
})
return nil
}

func handleCommitSelect(g *gocui.Gui, v *gocui.View) error {
commit := getSelectedCommit(v)
commitText := gitShow(commit.Sha)
devLog("commitText:", commitText)
return renderString(g, "main", commitText)
}

func handleCommitSquashDown(g *gocui.Gui, v *gocui.View) error {
if getItemPosition(v) != 0 {
return createSimpleConfirmationPanel(g, v, "Error", "Can only squash topmost commit")
}
commit := getSelectedCommit(v)
if output, err := gitSquashPreviousTwoCommits(commit.Name); err != nil {
return createSimpleConfirmationPanel(g, v, "Error", output)
}
if err := refreshCommits(g); err != nil {
panic(err)
}
return handleCommitSelect(g, v)
}

func handleRenameCommit(g *gocui.Gui, v *gocui.View) error {
if getItemPosition(v) != 0 {
return createSimpleConfirmationPanel(g, v, "Error", "Can only rename topmost commit")
}
createPromptPanel(g, v, "Rename Commit", func(g *gocui.Gui, v *gocui.View) error {
if output, err := gitRenameCommit(v.Buffer()); err != nil {
return createSimpleConfirmationPanel(g, v, "Error", output)
}
if err := refreshCommits(g); err != nil {
panic(err)
}
return handleCommitSelect(g, v)
})
return nil
}

func getSelectedCommit(v *gocui.View) Commit {
lineNumber := getItemPosition(v)
if len(state.Commits) == 0 {
return Commit{
Sha: "noCommit",
DisplayString: "none",
}
}
return state.Commits[lineNumber]
}
79 changes: 67 additions & 12 deletions confirmation_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// "io"
// "io/ioutil"

"math"
"strings"
// "strings"

"github.com/jroimartin/gocui"
Expand All @@ -24,39 +24,94 @@ func wrappedConfirmationFunction(function func(*gocui.Gui, *gocui.View) error) f
panic(err)
}
}
if err := returnFocus(g, v); err != nil {
panic(err)
}
g.DeleteKeybindings("confirmation")
return g.DeleteView("confirmation")
return closeConfirmationPrompt(g)
}
}

func closeConfirmationPrompt(g *gocui.Gui) error {
view, err := g.View("confirmation")
if err != nil {
panic(err)
}
if err := returnFocus(g, view); err != nil {
panic(err)
}
g.DeleteKeybindings("confirmation")
return g.DeleteView("confirmation")
}

func getMessageHeight(message string, width int) int {
lines := strings.Split(message, "\n")
lineCount := 0
for _, line := range lines {
lineCount += len(line)/width + 1
}
return lineCount
}

func getConfirmationPanelDimensions(g *gocui.Gui, prompt string) (int, int, int, int) {
width, height := g.Size()
panelWidth := 60
panelHeight := int(math.Ceil(float64(len(prompt)) / float64(panelWidth)))
// panelHeight := int(math.Ceil(float64(len(prompt)) / float64(panelWidth)))
panelHeight := getMessageHeight(prompt, panelWidth)
return width/2 - panelWidth/2,
height/2 - panelHeight/2 - panelHeight%2 - 1,
width/2 + panelWidth/2,
height/2 + panelHeight/2
}

func createConfirmationPanel(g *gocui.Gui, sourceView *gocui.View, title, prompt string, handleYes, handleNo func(*gocui.Gui, *gocui.View) error) error {
func createPromptPanel(g *gocui.Gui, v *gocui.View, title string, handleSubmit func(*gocui.Gui, *gocui.View) error) error {
// only need to fit one line
x0, y0, x1, y1 := getConfirmationPanelDimensions(g, "")
if confirmationView, err := g.SetView("confirmation", x0, y0, x1, y1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
confirmationView.Editable = true
g.Cursor = true
confirmationView.Title = title
switchFocus(g, v, confirmationView)
if err := g.SetKeybinding("confirmation", gocui.KeyEnter, gocui.ModNone, wrappedConfirmationFunction(handleSubmit)); err != nil {
return err
}
if err := g.SetKeybinding("confirmation", gocui.KeyEsc, gocui.ModNone, wrappedConfirmationFunction(nil)); err != nil {
return err
}
}
return nil
}

func createConfirmationPanel(g *gocui.Gui, v *gocui.View, title, prompt string, handleYes, handleNo func(*gocui.Gui, *gocui.View) error) error {
// delete the existing confirmation panel if it exists
if view, _ := g.View("confirmation"); view != nil {
if err := closeConfirmationPrompt(g); err != nil {
panic(err)
}
}
x0, y0, x1, y1 := getConfirmationPanelDimensions(g, prompt)
if v, err := g.SetView("confirmation", x0, y0, x1, y1); err != nil {
if confirmationView, err := g.SetView("confirmation", x0, y0, x1, y1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = title
renderString(g, "confirmation", prompt+" (y/n)")
switchFocus(g, sourceView, v)
confirmationView.Title = title
renderString(g, "confirmation", prompt)
switchFocus(g, v, confirmationView)
if err := g.SetKeybinding("confirmation", 'n', gocui.ModNone, wrappedConfirmationFunction(handleNo)); err != nil {
return err
}
if err := g.SetKeybinding("confirmation", gocui.KeyEsc, gocui.ModNone, wrappedConfirmationFunction(handleNo)); err != nil {
return err
}
if err := g.SetKeybinding("confirmation", 'y', gocui.ModNone, wrappedConfirmationFunction(handleYes)); err != nil {
return err
}
if err := g.SetKeybinding("confirmation", gocui.KeyEnter, gocui.ModNone, wrappedConfirmationFunction(handleYes)); err != nil {
return err
}
}
return nil
}

func createSimpleConfirmationPanel(g *gocui.Gui, v *gocui.View, title, prompt string) error {
return createConfirmationPanel(g, v, title, prompt, nil, nil)
}
33 changes: 27 additions & 6 deletions files_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func handleFilePress(g *gocui.Gui, v *gocui.View) error {
func getSelectedFile(v *gocui.View) GitFile {
lineNumber := getItemPosition(v)
if len(state.GitFiles) == 0 {
// find a way to not have to do this
return GitFile{
Name: "noFile",
DisplayString: "none",
Expand All @@ -71,7 +72,7 @@ func handleFileRemove(g *gocui.Gui, v *gocui.View) error {
} else {
deleteVerb = "delete"
}
return createConfirmationPanel(g, v, strings.Title(deleteVerb)+" file", "Are you sure you want to "+deleteVerb+" "+file.Name+" (you will lose your changes)?", func(g *gocui.Gui, v *gocui.View) error {
return createConfirmationPanel(g, v, strings.Title(deleteVerb)+" file", "Are you sure you want to "+deleteVerb+" "+file.Name+" (you will lose your changes)? (y/n)", func(g *gocui.Gui, v *gocui.View) error {
if err := removeFile(file); err != nil {
panic(err)
}
Expand Down Expand Up @@ -135,10 +136,30 @@ func refreshFiles(g *gocui.Gui) error {
return nil
}

func pullFiles(g *gocui.Gui, v *gocui.Gui) error {
if err := gitPull(); err != nil {
// should show error
panic(err)
}
func pullFiles(g *gocui.Gui, v *gocui.View) error {
devLog("pulling...")
createSimpleConfirmationPanel(g, v, "", "Pulling...")
go func() {
if output, err := gitPull(); err != nil {
createSimpleConfirmationPanel(g, v, "Error", output)
} else {
closeConfirmationPrompt(g)
}
}()
devLog("pulled.")
return refreshFiles(g)
}

func pushFiles(g *gocui.Gui, v *gocui.View) error {
devLog("pushing...")
createSimpleConfirmationPanel(g, v, "", "Pushing...")
go func() {
if output, err := gitPush(); err != nil {
createSimpleConfirmationPanel(g, v, "Error", output)
} else {
closeConfirmationPrompt(g)
}
}()
devLog("pushed.")
return nil
}
Loading

0 comments on commit ec78c79

Please sign in to comment.