2015-02-01 03:39:43 +08:00
|
|
|
// Copyright 2014 The gocui Authors. All rights reserved.
|
2015-01-25 21:07:14 +08:00
|
|
|
// 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"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/jroimartin/gocui"
|
|
|
|
)
|
|
|
|
|
2016-02-08 00:39:19 +08:00
|
|
|
const delta = 1
|
2015-01-25 21:07:14 +08:00
|
|
|
|
|
|
|
var (
|
|
|
|
views = []string{}
|
|
|
|
curView = -1
|
|
|
|
idxView = 0
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2015-01-25 21:07:14 +08:00
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
defer g.Close()
|
|
|
|
|
2016-10-19 05:30:43 +08:00
|
|
|
g.Highlight = true
|
2016-10-19 04:44:39 +08:00
|
|
|
g.SelFgColor = gocui.ColorRed
|
2016-10-18 06:36:26 +08:00
|
|
|
|
2016-10-30 18:59:53 +08:00
|
|
|
g.SetManagerFunc(layout)
|
|
|
|
|
2015-01-25 21:07:14 +08:00
|
|
|
if err := initKeybindings(g); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
2015-01-27 06:23:46 +08:00
|
|
|
if err := newView(g); err != nil {
|
2015-01-25 21:07:14 +08:00
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
|
2016-01-30 09:36:10 +08:00
|
|
|
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
|
2015-01-25 21:07:14 +08:00
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 06:23:46 +08:00
|
|
|
func layout(g *gocui.Gui) error {
|
|
|
|
maxX, _ := g.Size()
|
2017-08-16 02:34:30 +08:00
|
|
|
v, err := g.SetView("help", maxX-25, 0, maxX-1, 9)
|
2015-01-27 06:23:46 +08:00
|
|
|
if err != nil {
|
2016-01-23 23:07:42 +08:00
|
|
|
if err != gocui.ErrUnknownView {
|
2015-01-27 06:23:46 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintln(v, "KEYBINDINGS")
|
|
|
|
fmt.Fprintln(v, "Space: New View")
|
|
|
|
fmt.Fprintln(v, "Tab: Next View")
|
|
|
|
fmt.Fprintln(v, "← ↑ → ↓: Move View")
|
2016-01-23 23:07:42 +08:00
|
|
|
fmt.Fprintln(v, "Backspace: Delete View")
|
2016-02-07 23:43:50 +08:00
|
|
|
fmt.Fprintln(v, "t: Set view on top")
|
2017-08-16 02:34:30 +08:00
|
|
|
fmt.Fprintln(v, "b: Set view on bottom")
|
2015-01-27 06:23:46 +08:00
|
|
|
fmt.Fprintln(v, "^C: Exit")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-25 21:07:14 +08:00
|
|
|
func initKeybindings(g *gocui.Gui) error {
|
2017-08-16 02:34:30 +08:00
|
|
|
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone,
|
|
|
|
func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return gocui.ErrQuit
|
|
|
|
}); err != nil {
|
2015-01-25 21:07:14 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding("", gocui.KeySpace, gocui.ModNone,
|
|
|
|
func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return newView(g)
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding("", gocui.KeyBackspace2, gocui.ModNone,
|
|
|
|
func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return delView(g)
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone,
|
|
|
|
func(g *gocui.Gui, v *gocui.View) error {
|
2015-01-25 21:16:27 +08:00
|
|
|
return nextView(g, true)
|
2015-01-25 21:07:14 +08:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding("", gocui.KeyArrowLeft, gocui.ModNone,
|
|
|
|
func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return moveView(g, v, -delta, 0)
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding("", gocui.KeyArrowRight, gocui.ModNone,
|
|
|
|
func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return moveView(g, v, delta, 0)
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding("", gocui.KeyArrowDown, gocui.ModNone,
|
|
|
|
func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return moveView(g, v, 0, delta)
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding("", gocui.KeyArrowUp, gocui.ModNone,
|
|
|
|
func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return moveView(g, v, 0, -delta)
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-16 02:34:30 +08:00
|
|
|
if err := g.SetKeybinding("", 't', gocui.ModNone,
|
|
|
|
func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
_, err := g.SetViewOnTop(views[curView])
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding("", 'b', gocui.ModNone,
|
|
|
|
func(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
_, err := g.SetViewOnBottom(views[curView])
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
2016-02-07 23:43:50 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-01-25 21:07:14 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newView(g *gocui.Gui) error {
|
|
|
|
maxX, maxY := g.Size()
|
|
|
|
name := fmt.Sprintf("v%v", idxView)
|
|
|
|
v, err := g.SetView(name, maxX/2-5, maxY/2-5, maxX/2+5, maxY/2+5)
|
|
|
|
if err != nil {
|
2016-01-23 23:07:42 +08:00
|
|
|
if err != gocui.ErrUnknownView {
|
2015-01-25 21:07:14 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Wrap = true
|
|
|
|
fmt.Fprintln(v, strings.Repeat(name+" ", 30))
|
|
|
|
}
|
2016-10-18 06:24:16 +08:00
|
|
|
if _, err := g.SetCurrentView(name); err != nil {
|
2015-01-25 21:07:14 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
views = append(views, name)
|
|
|
|
curView = len(views) - 1
|
2015-01-25 21:16:27 +08:00
|
|
|
idxView += 1
|
2015-01-25 21:07:14 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func delView(g *gocui.Gui) error {
|
|
|
|
if len(views) <= 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.DeleteView(views[curView]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
views = append(views[:curView], views[curView+1:]...)
|
|
|
|
|
2015-01-25 21:16:27 +08:00
|
|
|
return nextView(g, false)
|
2015-01-25 21:07:14 +08:00
|
|
|
}
|
|
|
|
|
2015-01-25 21:16:27 +08:00
|
|
|
func nextView(g *gocui.Gui, disableCurrent bool) error {
|
2015-01-25 21:07:14 +08:00
|
|
|
next := curView + 1
|
|
|
|
if next > len(views)-1 {
|
|
|
|
next = 0
|
|
|
|
}
|
|
|
|
|
2016-10-18 06:24:16 +08:00
|
|
|
if _, err := g.SetCurrentView(views[next]); err != nil {
|
2015-01-25 21:07:14 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
curView = next
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func moveView(g *gocui.Gui, v *gocui.View, dx, dy int) error {
|
|
|
|
name := v.Name()
|
|
|
|
x0, y0, x1, y1, err := g.ViewPosition(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := g.SetView(name, x0+dx, y0+dy, x1+dx, y1+dy); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|