From 5a4cbcfff8985f22ff86c9fb0fdcf68cf3bd6927 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Wed, 8 Feb 2017 10:43:34 +0900 Subject: [PATCH] show result with gocui But need to wait to merge https://github.com/jroimartin/gocui/pull/74 --- gui.go | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 32 ++++++++++++++++-- 2 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 gui.go diff --git a/gui.go b/gui.go new file mode 100644 index 0000000..00603a7 --- /dev/null +++ b/gui.go @@ -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 +} diff --git a/main.go b/main.go index 8a18286..9f69606 100644 --- a/main.go +++ b/main.go @@ -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"` @@ -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 { @@ -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() @@ -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") @@ -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) + } }