Skip to content

Commit

Permalink
Create layout for command prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
mkchoi212 committed Dec 29, 2017
1 parent 2fc3f18 commit 300358a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 38 deletions.
57 changes: 57 additions & 0 deletions layout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"fmt"

"github.com/jroimartin/gocui"
)

func layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
inputHeight := 2
viewHeight := maxY - inputHeight
branchViewWidth := (maxX / 5) * 2

if _, err := g.SetView("current", 0, 0, branchViewWidth, viewHeight); err != nil {
if err != gocui.ErrUnknownView {
return err
}
}

if _, err := g.SetView("foreign", branchViewWidth, 0, branchViewWidth*2, viewHeight); err != nil {
if err != gocui.ErrUnknownView {
return err
}
}

if v, err := g.SetView("panel", branchViewWidth*2, 0, maxX-2, viewHeight); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = "Conflicts"
}

if v, err := g.SetView("input prompt", 1, viewHeight, 15, viewHeight+inputHeight); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Frame = false
prompt := fmt.Sprintf("\033[3%d;%dm[a | d] >> \033[0m", 2, 1)
v.Write([]byte(prompt))
v.MoveCursor(11, 0, true)
}

if v, err := g.SetView("input", 12, viewHeight, maxX, viewHeight+inputHeight); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Frame = false
v.Editable = true
v.Wrap = false
v.Editor = gocui.EditorFunc(promptEditor)
if _, err := g.SetCurrentView("input"); err != nil {
return err
}
}
return nil
}
51 changes: 13 additions & 38 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package main

import (
"bytes"
"fmt"
"log"

Expand All @@ -17,33 +18,6 @@ var (
conflicts = []Conflict{}
)

func layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
height := maxY - 2
branchViewWidth := (maxX / 5) * 2

if _, err := g.SetView("current", 0, 0, branchViewWidth, height); err != nil {
if err != gocui.ErrUnknownView {
return err
}
}

if _, err := g.SetView("foreign", branchViewWidth, 0, branchViewWidth*2, height); err != nil {
if err != gocui.ErrUnknownView {
return err
}
}

if v, err := g.SetView("panel", branchViewWidth*2, 0, maxX-2, height); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = "Conflicts"
}

return nil
}

func printLines(v *gocui.View, lines []string) {
v.Clear()
for _, line := range lines {
Expand Down Expand Up @@ -84,14 +58,22 @@ func selectConflict(i int, g *gocui.Gui) error {
if err != nil {
return err
}
v.Title = conf.CurrentName
var buf bytes.Buffer
buf.WriteString(conf.CurrentName)
buf.WriteString(" (Current Change) ")
v.Title = buf.String()

printLines(v, conf.ColoredCurrentLines)

v, err = g.View("foreign")
if err != nil {
return err
}
v.Title = conf.ForeignName
buf.Reset()
buf.WriteString(conf.ForeignName)
buf.WriteString(" (Incoming Change) ")
v.Title = buf.String()

printLines(v, conf.ColoredForeignLines)
return nil
})
Expand Down Expand Up @@ -126,15 +108,6 @@ func keyBindings(g *gocui.Gui) error {
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
return err
}

if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, nextConflict); err != nil {
return err
}

if err := g.SetKeybinding("", gocui.KeyEnter, gocui.ModNone, resolveConflict); err != nil {
return err
}

return nil
}

Expand All @@ -145,6 +118,7 @@ func main() {
log.Panicln("No conflicts found")
}
conflictCount = len(conflicts)
conflicts[0].Diff()

g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
Expand All @@ -154,6 +128,7 @@ func main() {

g.SetManagerFunc(layout)

g.Cursor = true
if err := keyBindings(g); err != nil {
log.Panicln(err)
}
Expand Down

0 comments on commit 300358a

Please sign in to comment.