Skip to content

Commit

Permalink
Merge pull request #3 from y-yagi/gocui
Browse files Browse the repository at this point in the history
[WIP]show result with gocui
  • Loading branch information
y-yagi authored Aug 27, 2017
2 parents 7206043 + 5a4cbcf commit e19c7b8
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 3 deletions.
102 changes: 102 additions & 0 deletions gui.go
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
}
32 changes: 29 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ import (
yaml "gopkg.in/yaml.v2"

"github.com/dghubble/go-twitter/twitter"
"github.com/jroimartin/gocui"
runewidth "github.com/mattn/go-runewidth"
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
)

var tweets []twitter.Tweet

// Config manage config info
type Config struct {
TwitterConsumerKey string `yaml:"twitterConsumerKey"`
Expand Down Expand Up @@ -96,7 +99,7 @@ func updateStatus(egose *Egose) error {
return nil
}

func showTweets(tweets []twitter.Tweet) {
func showTweetsWithTable(tweets []twitter.Tweet) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"User", "Text", "URL"})
for _, tweet := range tweets {
Expand All @@ -105,6 +108,26 @@ func showTweets(tweets []twitter.Tweet) {
table.Render()
}

func showTweetsWithGui() error {
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
return errors.Wrap(err, "gui create error")
}
defer g.Close()

g.Cursor = true
g.SetManagerFunc(layout)

if err := keybindings(g); err != nil {
return errors.Wrap(err, "key bindings error")
}

if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
return errors.Wrap(err, "unexpected error")
}
return nil
}

func main() {
config, err := loadConfig()

Expand All @@ -117,7 +140,6 @@ func main() {
var user string
var count int
var status bool
var tweets []twitter.Tweet

flag.StringVar(&query, "q", "", "Search query")
flag.StringVar(&user, "u", "", "Show user timeline")
Expand Down Expand Up @@ -148,5 +170,9 @@ func main() {
fmt.Printf("twitter API Error:%v\n", err)
os.Exit(1)
}
showTweets(tweets)

if err = showTweetsWithGui(); err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
}

0 comments on commit e19c7b8

Please sign in to comment.