forked from jesseduffield/lazygit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe99e70
commit bb12309
Showing
8 changed files
with
520 additions
and
354 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
development.log | ||
commands.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// 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 ( | ||
"fmt" | ||
|
||
"github.com/jroimartin/gocui" | ||
) | ||
|
||
func handleCommitPress(g *gocui.Gui, currentView *gocui.View) error { | ||
devLog(stagedFiles(state.GitFiles)) | ||
if len(stagedFiles(state.GitFiles)) == 0 { | ||
return nil | ||
} | ||
maxX, maxY := g.Size() | ||
// var v *gocui.View | ||
if v, err := g.SetView("commit", maxX/2-30, maxY/2-1, maxX/2+30, maxY/2+1); err != nil { | ||
if err != gocui.ErrUnknownView { | ||
return err | ||
} | ||
v.Title = "Commit Message" | ||
v.Editable = true | ||
if _, err := g.SetCurrentView("commit"); err != nil { | ||
return err | ||
} | ||
switchFocus(g, currentView, v) | ||
} | ||
return nil | ||
} | ||
|
||
func handleCommitSubmit(g *gocui.Gui, v *gocui.View) error { | ||
if len(v.BufferLines()) == 0 { | ||
return closeCommitPrompt(g, v) | ||
} | ||
message := fmt.Sprint(v.BufferLines()[0]) | ||
// for whatever reason, a successful commit returns an error, so we're not | ||
// going to check for an error here | ||
if err := gitCommit(message); err != nil { | ||
devLog(err) | ||
panic(err) | ||
} | ||
refreshFiles(g) | ||
refreshLogs(g) | ||
return closeCommitPrompt(g, v) | ||
} | ||
|
||
func closeCommitPrompt(g *gocui.Gui, v *gocui.View) error { | ||
filesView, _ := g.View("files") | ||
switchFocus(g, v, filesView) | ||
devLog("test prompt close") | ||
if err := g.DeleteView("commit"); err != nil { | ||
return err | ||
} | ||
if _, err := g.SetCurrentView(state.PreviousView); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func handleCommitPromptFocus(g *gocui.Gui, v *gocui.View) error { | ||
return renderString(g, "options", "esc: close, enter: commit") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// 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 ( | ||
|
||
// "io" | ||
// "io/ioutil" | ||
|
||
"math" | ||
// "strings" | ||
|
||
"github.com/jroimartin/gocui" | ||
) | ||
|
||
func wrappedConfirmationFunction(function func(*gocui.Gui, *gocui.View) error) func(*gocui.Gui, *gocui.View) error { | ||
return func(g *gocui.Gui, v *gocui.View) error { | ||
if function != nil { | ||
if err := function(g, v); err != nil { | ||
panic(err) | ||
} | ||
} | ||
if err := returnFocus(g, v); err != nil { | ||
panic(err) | ||
} | ||
g.DeleteKeybindings("confirmation") | ||
return g.DeleteView("confirmation") | ||
} | ||
} | ||
|
||
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))) | ||
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 { | ||
x0, y0, x1, y1 := getConfirmationPanelDimensions(g, prompt) | ||
if v, 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) | ||
if err := g.SetKeybinding("confirmation", 'n', gocui.ModNone, wrappedConfirmationFunction(handleNo)); err != nil { | ||
return err | ||
} | ||
if err := g.SetKeybinding("confirmation", 'y', gocui.ModNone, wrappedConfirmationFunction(handleYes)); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// 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 ( | ||
|
||
// "io" | ||
// "io/ioutil" | ||
|
||
// "strings" | ||
|
||
"strings" | ||
|
||
"github.com/fatih/color" | ||
"github.com/jroimartin/gocui" | ||
) | ||
|
||
func stagedFiles(files []GitFile) []GitFile { | ||
result := make([]GitFile, 0) | ||
for _, file := range files { | ||
if file.HasStagedChanges { | ||
result = append(result, file) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
func handleFilePress(g *gocui.Gui, v *gocui.View) error { | ||
file := getSelectedFile(v) | ||
|
||
if file.HasUnstagedChanges { | ||
stageFile(file.Name) | ||
} else { | ||
unStageFile(file.Name) | ||
} | ||
|
||
if err := refreshFiles(g); err != nil { | ||
return err | ||
} | ||
if err := handleFileSelect(g, v); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getSelectedFile(v *gocui.View) GitFile { | ||
lineNumber := getItemPosition(v) | ||
if len(state.GitFiles) == 0 { | ||
return GitFile{ | ||
Name: "noFile", | ||
DisplayString: "none", | ||
HasStagedChanges: false, | ||
HasUnstagedChanges: false, | ||
Tracked: false, | ||
Deleted: false, | ||
} | ||
} | ||
return state.GitFiles[lineNumber] | ||
} | ||
|
||
func handleFileRemove(g *gocui.Gui, v *gocui.View) error { | ||
file := getSelectedFile(v) | ||
var deleteVerb string | ||
if file.Tracked { | ||
deleteVerb = "checkout" | ||
} 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 { | ||
if err := removeFile(file); err != nil { | ||
panic(err) | ||
} | ||
return refreshFiles(g) | ||
}, nil) | ||
} | ||
|
||
func handleFileSelect(g *gocui.Gui, v *gocui.View) error { | ||
item := getSelectedFile(v) | ||
var optionsString string | ||
baseString := "space: toggle staged, c: commit changes, option+o: open" | ||
if item.Tracked { | ||
optionsString = baseString + ", option+d: checkout" | ||
} else { | ||
optionsString = baseString + ", option+d: delete" | ||
} | ||
renderString(g, "options", optionsString) | ||
diff := getDiff(item) | ||
return renderString(g, "main", diff) | ||
} | ||
|
||
func handleFileOpen(g *gocui.Gui, v *gocui.View) error { | ||
file := getSelectedFile(v) | ||
_, err := openFile(file.Name) | ||
return err | ||
} | ||
|
||
func handleSublimeFileOpen(g *gocui.Gui, v *gocui.View) error { | ||
file := getSelectedFile(v) | ||
_, err := sublimeOpenFile(file.Name) | ||
return err | ||
} | ||
|
||
func refreshFiles(g *gocui.Gui) error { | ||
filesView, err := g.View("files") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// get files to stage | ||
gitFiles := getGitStatusFiles() | ||
state.GitFiles = mergeGitStatusFiles(state.GitFiles, gitFiles) | ||
|
||
filesView.Clear() | ||
red := color.New(color.FgRed) | ||
green := color.New(color.FgGreen) | ||
for _, gitFile := range state.GitFiles { | ||
if !gitFile.Tracked { | ||
red.Fprintln(filesView, gitFile.DisplayString) | ||
continue | ||
} | ||
green.Fprint(filesView, gitFile.DisplayString[0:1]) | ||
red.Fprint(filesView, gitFile.DisplayString[1:3]) | ||
if gitFile.HasUnstagedChanges { | ||
red.Fprintln(filesView, gitFile.Name) | ||
} else { | ||
green.Fprintln(filesView, gitFile.Name) | ||
} | ||
} | ||
correctCursor(filesView) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.