summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike JS. Choi <mkchoi212@icloud.com>2018-05-22 20:01:21 -0500
committerMike JS. Choi <mkchoi212@icloud.com>2018-05-22 20:02:39 -0500
commitd93d37e9ccac9ce551e3fd9452bb414d6ffe4e3c (patch)
treea346f21adfc82198036db650aa63405c915fd6e9
parentf0822c975185cd8330ad4aa66f0aa8b2c4664831 (diff)
Fix external editor crash bug
-rw-r--r--layout.go2
-rw-r--r--main.go71
-rw-r--r--prompt.go23
-rw-r--r--prompt_test.go3
4 files changed, 64 insertions, 35 deletions
diff --git a/layout.go b/layout.go
index 1ec6ea3..697fa83 100644
--- a/layout.go
+++ b/layout.go
@@ -112,7 +112,7 @@ func makePrompt(g *gocui.Gui) error {
return err
}
v.Frame = false
- PrintPrompt(g, color.Green)
+ PrintPrompt(g)
}
// User input view
diff --git a/main.go b/main.go
index cdd396d..061e221 100644
--- a/main.go
+++ b/main.go
@@ -8,6 +8,7 @@ import (
"github.com/jroimartin/gocui"
"github.com/mkchoi212/fac/color"
"github.com/mkchoi212/fac/conflict"
+ "github.com/mkchoi212/fac/editor"
)
var (
@@ -33,25 +34,25 @@ func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
-func parseInput(g *gocui.Gui, v *gocui.View) (err error) {
+func parseInput(g *gocui.Gui, v *gocui.View) error {
in := strings.TrimSuffix(v.Buffer(), "\n")
v.Clear()
v.SetCursor(0, 0)
- if err = Evaluate(g, v, conflicts[cur], in); err != nil {
+ if err := Evaluate(g, v, conflicts[cur], in); err != nil {
if err == ErrUnknownCmd {
consecutiveError++
- PrintPrompt(g, color.Red)
-
if consecutiveError > 3 {
Select(conflicts[cur], g, true)
- consecutiveError = 0
}
} else {
- return
+ return err
}
+ } else {
+ consecutiveError = 0
}
+ PrintPrompt(g)
return nil
}
@@ -82,12 +83,13 @@ func Start() (err error) {
return
}
-func main() {
- // Find and parse conflicts
- cwd, _ := os.Getwd()
- files, err := conflict.Find(cwd)
+func findConflicts() (files []conflict.File, err error) {
+ cwd, err := os.Getwd()
if err != nil {
- fmt.Println(color.Red(color.Regular, err.Error()))
+ return
+ }
+
+ if files, err = conflict.Find(cwd); err != nil {
return
}
@@ -98,26 +100,55 @@ func main() {
}
}
- if len(conflicts) == 0 {
- fmt.Println(color.Green(color.Regular, "No conflicts detected 🎉"))
- return
- }
+ return
+}
- // Main GUI loop
+func runUI() error {
for {
if err := Start(); err != nil {
- if err == gocui.ErrQuit {
+ if err == ErrOpenEditor {
+ newLines, err := editor.Open(conflicts[cur])
+ if err != nil {
+ return err
+ }
+ if err = conflicts[cur].Update(newLines); err != nil {
+ consecutiveError++
+ }
+ } else if err == gocui.ErrQuit {
break
- } else if err == ErrNeedRefresh {
- continue
}
}
}
+ return nil
+}
+
+func die(err error) {
+ fmt.Println(color.Red(color.Regular, "fac: %s", strings.TrimSuffix(err.Error(), "\n")))
+ os.Exit(1)
+}
+
+func main() {
+ // Find and parse conflicts
+ files, err := findConflicts()
+ if err != nil {
+ die(err)
+ }
+
+ if len(conflicts) == 0 {
+ fmt.Println(color.Green(color.Regular, "No conflicts detected 🎉"))
+ os.Exit(0)
+ }
+
+ if err = runUI(); err != nil {
+ die(err)
+ }
+
for _, file := range files {
if err = file.WriteChanges(); err != nil {
- fmt.Println(color.Red(color.Underline, "%s\n", err))
+ die(err)
}
}
+
printSummary(conflicts)
}
diff --git a/prompt.go b/prompt.go
index bde999f..96046a9 100644
--- a/prompt.go
+++ b/prompt.go
@@ -7,20 +7,19 @@ import (
"github.com/jroimartin/gocui"
"github.com/mkchoi212/fac/color"
"github.com/mkchoi212/fac/conflict"
- "github.com/mkchoi212/fac/editor"
)
// ErrUnknownCmd is returned when user inputs an invalid character
var ErrUnknownCmd = errors.New("This person doesn't know whats going on")
-// ErrNeedRefresh is returned when the user opens vim to edit code
-// Note that a new instance of gocui must be created after vim is opened
-var ErrNeedRefresh = errors.New("Screen is tainted after opening vim")
+// ErrOpenEditor is returned when the user wants to open an editor
+// Note that the current instance of gocui must be destroyed before opening an editor
+var ErrOpenEditor = errors.New("Screen is tainted after opening vim")
// PrintPrompt prints the promptString on the bottom left corner of the screen
// Note that the prompt is composed of two seperate views,
// one that displays just the promptString, and another that takes input from the user
-func PrintPrompt(g *gocui.Gui, colorize func(style int, format string, a ...interface{}) string) {
+func PrintPrompt(g *gocui.Gui) {
promptString := "[w,a,s,d,e,?] >>"
g.Update(func(g *gocui.Gui) error {
@@ -30,7 +29,12 @@ func PrintPrompt(g *gocui.Gui, colorize func(style int, format string, a ...inte
}
v.Clear()
v.MoveCursor(0, 0, true)
- fmt.Fprintf(v, colorize(color.Regular, promptString))
+
+ if consecutiveError == 0 {
+ fmt.Fprintf(v, color.Green(color.Regular, promptString))
+ } else {
+ fmt.Fprintf(v, color.Red(color.Regular, promptString))
+ }
return nil
})
}
@@ -63,12 +67,7 @@ func Evaluate(g *gocui.Gui, v *gocui.View, conf *conflict.Conflict, input string
ViewOrientation = ^ViewOrientation
layout(g)
case 'e':
- newLines, err := editor.Open(conflicts[cur])
- if err != nil {
- return err
- }
- conflicts[cur].Update(newLines)
- return ErrNeedRefresh
+ return ErrOpenEditor
case 'h', '?':
Select(conflicts[cur], g, true)
case 'q':
diff --git a/prompt_test.go b/prompt_test.go
index 554283e..9ccc05a 100644
--- a/prompt_test.go
+++ b/prompt_test.go
@@ -4,13 +4,12 @@ import (
"testing"
"github.com/jroimartin/gocui"
- "github.com/mkchoi212/fac/color"
)
func TestPrintPrompt(t *testing.T) {
g := gocui.Gui{}
makePrompt(&g)
- PrintPrompt(&g, color.Red)
+ PrintPrompt(&g)
_, err := g.View(Prompt)
if err != nil {