Skip to content

Commit

Permalink
Refactor enter detection
Browse files Browse the repository at this point in the history
  • Loading branch information
mkchoi212 committed Jun 11, 2018
1 parent 9eeca61 commit 8e39330
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
9 changes: 3 additions & 6 deletions layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func makePrompt(g *gocui.Gui) error {

// Select selects conflict `c` as the current conflict displayed on the screen
// When selecting a conflict, it updates the side panel, and the code view
func Select(g *gocui.Gui, c *conflict.Conflict, showHelp bool) error {
func Select(g *gocui.Gui, c *conflict.Conflict, showHelp bool) {
// Update side panel
g.Update(func(g *gocui.Gui) error {
v, err := g.View(Panel)
Expand Down Expand Up @@ -220,22 +220,20 @@ func Select(g *gocui.Gui, c *conflict.Conflict, showHelp bool) error {

return nil
})
return nil
}

// Resolve resolves the provided conflict and moves to the next conflict
// in the queue
func Resolve(g *gocui.Gui, v *gocui.View, c *conflict.Conflict, version int) error {
func Resolve(g *gocui.Gui, v *gocui.View, c *conflict.Conflict, version int) {
g.Update(func(g *gocui.Gui) error {
c.Choice = version
Move(g, v, Down)
return nil
})
return nil
}

// Move goes to the next conflict in the list in the provided `direction`
func Move(g *gocui.Gui, v *gocui.View, direction int) error {
func Move(g *gocui.Gui, v *gocui.View, direction int) {
originalCur := cur

for {
Expand All @@ -262,7 +260,6 @@ func Move(g *gocui.Gui, v *gocui.View, direction int) error {
}

Select(g, conflicts[cur], false)
return nil
}

// Scroll scrolls the two code view panels in `direction` by one line
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ func runUI() (err error) {
return
}

if keyBinding[binding.ContinuousEvaluation] == "false" {
if err = g.SetKeybinding("", gocui.KeyEnter, gocui.ModNone, ParseInput); err != nil {
return
}
}

Select(g, conflicts[cur], false)

if err = g.MainLoop(); err != nil {
Expand Down
14 changes: 7 additions & 7 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func Evaluate(g *gocui.Gui, v *gocui.View, conf *conflict.Conflict, input string
// It `evaluate`s the user's query and reflects the state on the UI
func ParseInput(g *gocui.Gui, v *gocui.View) error {
in := strings.TrimSuffix(v.Buffer(), "\n")
if keyBinding[binding.ContinuousEvaluation] == "false" {
v.Clear()
_ = v.SetCursor(0, 0)
}

if err := Evaluate(g, v, conflicts[cur], in); err != nil {
if err == ErrUnknownCmd {
Expand All @@ -110,19 +114,15 @@ func PromptEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
if keyBinding[binding.ContinuousEvaluation] == "true" {
v.Clear()
v.EditWrite(ch)
ParseInput(g, v)
v.SetCursor(0, 0)
_ = ParseInput(g, v)
_ = v.SetCursor(0, 0)
} else {
v.EditWrite(ch)
}
return
}

switch key {
case gocui.KeyEnter:
ParseInput(g, v)
v.Clear()
v.SetCursor(0, 0)
case gocui.KeySpace:
v.EditWrite(' ')
case gocui.KeyBackspace, gocui.KeyBackspace2:
Expand All @@ -132,7 +132,7 @@ func PromptEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
case gocui.KeyInsert:
v.Overwrite = !v.Overwrite
case gocui.KeyArrowDown:
v.SetCursor(len(v.Buffer())-1, 0)
_ = v.SetCursor(len(v.Buffer())-1, 0)
case gocui.KeyArrowUp:
v.MoveCursor(0, -1, false)
case gocui.KeyArrowLeft:
Expand Down

0 comments on commit 8e39330

Please sign in to comment.