2016-10-17 21:50:03 +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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-10-18 06:24:16 +08:00
|
|
|
viewArr = []string{"v1", "v2", "v3", "v4"}
|
2016-10-17 21:50:03 +08:00
|
|
|
active = 0
|
|
|
|
)
|
|
|
|
|
2016-10-18 06:24:16 +08:00
|
|
|
func setCurrentViewOnTop(g *gocui.Gui, name string) (*gocui.View, error) {
|
|
|
|
if _, err := g.SetCurrentView(name); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return g.SetViewOnTop(name)
|
|
|
|
}
|
|
|
|
|
2016-10-17 21:50:03 +08:00
|
|
|
func nextView(g *gocui.Gui, v *gocui.View) error {
|
2016-10-18 06:24:16 +08:00
|
|
|
nextIndex := (active + 1) % len(viewArr)
|
2016-10-17 21:50:03 +08:00
|
|
|
name := viewArr[nextIndex]
|
|
|
|
|
2016-10-18 06:36:26 +08:00
|
|
|
out, err := g.View("v2")
|
2016-10-18 06:24:16 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-18 06:36:26 +08:00
|
|
|
fmt.Fprintln(out, "Going from view "+v.Name()+" to "+name)
|
2016-10-18 06:24:16 +08:00
|
|
|
|
|
|
|
if _, err := setCurrentViewOnTop(g, name); err != nil {
|
2016-10-17 21:50:03 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if nextIndex == 0 || nextIndex == 3 {
|
|
|
|
g.Cursor = true
|
|
|
|
} else {
|
|
|
|
g.Cursor = false
|
|
|
|
}
|
|
|
|
|
|
|
|
active = nextIndex
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func layout(g *gocui.Gui) error {
|
|
|
|
maxX, maxY := g.Size()
|
|
|
|
if v, err := g.SetView("v1", 0, 0, maxX/2-1, maxY/2-1); err != nil {
|
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Title = "v1 (editable)"
|
|
|
|
v.Editable = true
|
|
|
|
v.Wrap = true
|
2016-10-27 06:41:50 +08:00
|
|
|
|
|
|
|
if _, err = setCurrentViewOnTop(g, "v1"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-17 21:50:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if v, err := g.SetView("v2", maxX/2-1, 0, maxX-1, maxY/2-1); err != nil {
|
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Title = "v2"
|
|
|
|
v.Wrap = true
|
|
|
|
v.Autoscroll = true
|
|
|
|
}
|
|
|
|
if v, err := g.SetView("v3", 0, maxY/2-1, maxX/2-1, maxY-1); err != nil {
|
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Title = "v3"
|
|
|
|
v.Wrap = true
|
|
|
|
v.Autoscroll = true
|
2016-10-18 06:24:16 +08:00
|
|
|
fmt.Fprint(v, "Press TAB to change current view")
|
2016-10-17 21:50:03 +08:00
|
|
|
}
|
|
|
|
if v, err := g.SetView("v4", maxX/2, maxY/2, maxX-1, maxY-1); err != nil {
|
|
|
|
if err != gocui.ErrUnknownView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Title = "v4 (editable)"
|
|
|
|
v.Editable = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func quit(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return gocui.ErrQuit
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10-17 21:50:03 +08:00
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
defer g.Close()
|
|
|
|
|
2016-10-19 05:30:43 +08:00
|
|
|
g.Highlight = true
|
2016-10-27 06:41:50 +08:00
|
|
|
g.Cursor = true
|
2016-10-19 04:44:39 +08:00
|
|
|
g.SelFgColor = gocui.ColorGreen
|
2016-10-30 18:59:53 +08:00
|
|
|
|
2016-10-24 14:36:23 +08:00
|
|
|
g.SetManagerFunc(layout)
|
2016-10-17 21:50:03 +08:00
|
|
|
|
|
|
|
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, nextView); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
}
|