2016-01-23 23:07:42 +08:00
|
|
|
// Copyright 2014 The gocui Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/jroimartin/gocui"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2016-11-06 03:08:48 +08:00
|
|
|
g, err := gocui.NewGui(gocui.OutputNormal)
|
2016-10-24 08:24:41 +08:00
|
|
|
if err != nil {
|
2016-01-23 23:07:42 +08:00
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
defer g.Close()
|
|
|
|
|
2016-10-30 18:59:53 +08:00
|
|
|
g.Cursor = true
|
|
|
|
g.Mouse = true
|
|
|
|
|
2016-10-24 14:36:23 +08:00
|
|
|
g.SetManagerFunc(layout)
|
2016-10-30 18:59:53 +08:00
|
|
|
|
2016-01-23 23:07:42 +08:00
|
|
|
if err := keybindings(g); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
|
2016-01-30 09:36:10 +08:00
|
|
|
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
|
2016-01-23 23:07:42 +08:00
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func layout(g *gocui.Gui) error {
|
2016-01-26 16:40:06 +08:00
|
|
|
if v, err := g.SetView("but1", 2, 2, 22, 7); err != nil {
|
2016-01-23 23:07:42 +08:00
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-26 16:40:06 +08:00
|
|
|
v.Highlight = true
|
2016-10-19 04:44:39 +08:00
|
|
|
v.SelBgColor = gocui.ColorGreen
|
|
|
|
v.SelFgColor = gocui.ColorBlack
|
2016-01-26 16:40:06 +08:00
|
|
|
fmt.Fprintln(v, "Button 1 - line 1")
|
|
|
|
fmt.Fprintln(v, "Button 1 - line 2")
|
|
|
|
fmt.Fprintln(v, "Button 1 - line 3")
|
|
|
|
fmt.Fprintln(v, "Button 1 - line 4")
|
2016-01-23 23:07:42 +08:00
|
|
|
}
|
2016-01-26 16:40:06 +08:00
|
|
|
if v, err := g.SetView("but2", 24, 2, 44, 4); err != nil {
|
2016-01-23 23:07:42 +08:00
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-26 16:40:06 +08:00
|
|
|
v.Highlight = true
|
2016-10-19 04:44:39 +08:00
|
|
|
v.SelBgColor = gocui.ColorGreen
|
|
|
|
v.SelFgColor = gocui.ColorBlack
|
2016-01-26 16:40:06 +08:00
|
|
|
fmt.Fprintln(v, "Button 2 - line 1")
|
2016-01-23 23:07:42 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func keybindings(g *gocui.Gui) error {
|
|
|
|
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, n := range []string{"but1", "but2"} {
|
|
|
|
if err := g.SetKeybinding(n, gocui.MouseLeft, gocui.ModNone, showMsg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding("msg", gocui.MouseLeft, gocui.ModNone, delMsg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func quit(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return gocui.ErrQuit
|
|
|
|
}
|
|
|
|
|
|
|
|
func showMsg(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
var l string
|
|
|
|
var err error
|
|
|
|
|
2016-10-18 06:24:16 +08:00
|
|
|
if _, err := g.SetCurrentView(v.Name()); err != nil {
|
2016-01-26 16:40:06 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-23 23:07:42 +08:00
|
|
|
_, cy := v.Cursor()
|
|
|
|
if l, err = v.Line(cy); err != nil {
|
|
|
|
l = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
maxX, maxY := g.Size()
|
2016-01-26 16:40:06 +08:00
|
|
|
if v, err := g.SetView("msg", maxX/2-10, maxY/2, maxX/2+10, maxY/2+2); err != nil {
|
2016-01-23 23:07:42 +08:00
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintln(v, l)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func delMsg(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
if err := g.DeleteView("msg"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|