-
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.
Merge pull request #3 from y-yagi/gocui
[WIP]show result with gocui
- Loading branch information
Showing
2 changed files
with
131 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/jroimartin/gocui" | ||
) | ||
|
||
func keybindings(g *gocui.Gui) error { | ||
if err := g.SetKeybinding("", gocui.KeyArrowDown, gocui.ModNone, cursorDown); err != nil { | ||
return err | ||
} | ||
if err := g.SetKeybinding("", gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil { | ||
return err | ||
} | ||
if err := g.SetKeybinding("", gocui.KeyEnter, gocui.ModNone, getLine); err != nil { | ||
return err | ||
} | ||
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func layout(g *gocui.Gui) error { | ||
maxX, maxY := g.Size() | ||
if v, err := g.SetView("tweets", 0, 0, maxX-1, maxY-1); err != nil { | ||
if err != gocui.ErrUnknownView { | ||
return err | ||
} | ||
v.Title = "Tweets" | ||
v.Highlight = true | ||
v.SelBgColor = gocui.ColorGreen | ||
v.SelFgColor = gocui.ColorBlack | ||
|
||
for _, tweet := range tweets { | ||
fmt.Fprintln(v, tweet.Text) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func quit(g *gocui.Gui, v *gocui.View) error { | ||
return gocui.ErrQuit | ||
} | ||
|
||
func getLine(g *gocui.Gui, v *gocui.View) error { | ||
var l string | ||
var err error | ||
|
||
if v == nil { | ||
v = g.Views()[0] | ||
} | ||
|
||
_, cy := v.Cursor() | ||
if l, err = v.Line(cy); err != nil { | ||
l = "" | ||
} | ||
|
||
maxX, maxY := g.Size() | ||
if v, err := g.SetView("msg", maxX/2-30, maxY/2, maxX/2+30, maxY/2+2); err != nil { | ||
if err != gocui.ErrUnknownView { | ||
return err | ||
} | ||
fmt.Fprintln(v, l+":"+strconv.Itoa(cy)) | ||
if _, err := g.SetCurrentView("msg"); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func cursorDown(g *gocui.Gui, v *gocui.View) error { | ||
if v == nil { | ||
v = g.Views()[0] | ||
} | ||
|
||
cx, cy := v.Cursor() | ||
if err := v.SetCursor(cx, cy+1); err != nil { | ||
ox, oy := v.Origin() | ||
if err := v.SetOrigin(ox, oy+1); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func cursorUp(g *gocui.Gui, v *gocui.View) error { | ||
if v == nil { | ||
v = g.Views()[0] | ||
} | ||
|
||
ox, oy := v.Origin() | ||
cx, cy := v.Cursor() | ||
if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 { | ||
if err := v.SetOrigin(ox, oy-1); 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