Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]show result with gocui #3

Merged
merged 1 commit into from
Aug 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -95,7 +98,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 @@ -104,6 +107,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 @@ -116,7 +139,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 @@ -147,5 +169,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)
}
}