2014-01-15 03:11:12 +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.
|
|
|
|
|
2014-01-03 01:34:58 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-01-10 03:20:14 +08:00
|
|
|
"fmt"
|
2014-01-16 07:28:16 +08:00
|
|
|
"io/ioutil"
|
2014-01-03 01:34:58 +08:00
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/jroimartin/gocui"
|
|
|
|
)
|
|
|
|
|
2014-01-06 08:47:22 +08:00
|
|
|
func focusMain(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return g.SetCurrentView("main")
|
|
|
|
}
|
|
|
|
|
|
|
|
func focusSide(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return g.SetCurrentView("side")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func focusCmdLine(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return g.SetCurrentView("cmdline")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-01-06 20:56:28 +08:00
|
|
|
func showHideCursor(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
g.ShowCursor = !g.ShowCursor
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-01-07 01:36:38 +08:00
|
|
|
func cursorDown(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
if v != nil {
|
2014-01-17 06:01:53 +08:00
|
|
|
cx, cy := v.Cursor()
|
|
|
|
if err := v.SetCursor(cx, cy+1); err != nil {
|
|
|
|
ox, oy := v.Origin()
|
2014-01-18 19:31:53 +08:00
|
|
|
if err := v.SetOrigin(ox, oy+1); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-14 03:15:39 +08:00
|
|
|
}
|
2014-01-07 01:36:38 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cursorUp(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
if v != nil {
|
2014-01-17 06:01:53 +08:00
|
|
|
ox, oy := v.Origin()
|
|
|
|
cx, cy := v.Cursor()
|
|
|
|
if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 {
|
2014-01-18 19:31:53 +08:00
|
|
|
if err := v.SetOrigin(ox, oy-1); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-14 03:15:39 +08:00
|
|
|
}
|
2014-01-07 01:36:38 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cursorLeft(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
if v != nil {
|
2014-01-17 06:01:53 +08:00
|
|
|
ox, oy := v.Origin()
|
|
|
|
cx, cy := v.Cursor()
|
|
|
|
if err := v.SetCursor(cx-1, cy); err != nil && ox > 0 {
|
2014-01-18 19:31:53 +08:00
|
|
|
if err := v.SetOrigin(ox-1, oy); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-14 03:15:39 +08:00
|
|
|
}
|
2014-01-07 01:36:38 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cursorRight(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
if v != nil {
|
2014-01-17 06:01:53 +08:00
|
|
|
cx, cy := v.Cursor()
|
|
|
|
if err := v.SetCursor(cx+1, cy); err != nil {
|
|
|
|
ox, oy := v.Origin()
|
2014-01-18 19:31:53 +08:00
|
|
|
if err := v.SetOrigin(ox+1, oy); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-14 03:15:39 +08:00
|
|
|
}
|
2014-01-07 01:36:38 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-10 19:38:08 +08:00
|
|
|
func clear(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
if v != nil {
|
|
|
|
v.Clear()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeTest(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
if v != nil {
|
|
|
|
fmt.Fprintln(v, "This is a test")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-01-11 03:21:54 +08:00
|
|
|
func setLayout1(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
g.SetLayout(layout)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func setLayout2(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
g.SetLayout(layout2)
|
|
|
|
return nil
|
|
|
|
}
|
2014-01-10 19:38:08 +08:00
|
|
|
|
2014-01-06 08:47:22 +08:00
|
|
|
func keybindings(g *gocui.Gui) error {
|
2014-01-18 19:31:53 +08:00
|
|
|
if err := g.SetKeybinding("", gocui.KeyCtrlSpace, 0, focusMain); err != nil {
|
2014-01-06 08:47:22 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding("", gocui.KeyCtrlS, 0, focusSide); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding("", gocui.KeyCtrlL, 0, focusCmdLine); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-07 01:36:38 +08:00
|
|
|
if err := g.SetKeybinding("", 'c', gocui.ModAlt, showHideCursor); err != nil {
|
2014-01-06 08:47:22 +08:00
|
|
|
return err
|
|
|
|
}
|
2014-01-07 01:36:38 +08:00
|
|
|
if err := g.SetKeybinding("", 'j', 0, cursorDown); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding("", 'k', 0, cursorUp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding("", 'h', 0, cursorLeft); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding("", 'l', 0, cursorRight); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-10 04:55:23 +08:00
|
|
|
if err := g.SetKeybinding("", gocui.KeyCtrlC, 0, quit); err != nil {
|
2014-01-06 20:56:28 +08:00
|
|
|
return err
|
|
|
|
}
|
2014-01-06 08:47:22 +08:00
|
|
|
if err := g.SetKeybinding("main", 'q', 0, quit); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-10 19:38:08 +08:00
|
|
|
if err := g.SetKeybinding("", 'c', 0, clear); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding("", 't', 0, writeTest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-11 03:21:54 +08:00
|
|
|
if err := g.SetKeybinding("", '1', 0, setLayout1); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.SetKeybinding("", '2', 0, setLayout2); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-07 01:36:38 +08:00
|
|
|
|
2014-01-06 08:47:22 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-06 08:18:00 +08:00
|
|
|
func quit(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
return gocui.ErrorQuit
|
|
|
|
}
|
|
|
|
|
2014-01-10 19:38:08 +08:00
|
|
|
func layout(g *gocui.Gui) error {
|
|
|
|
maxX, maxY := g.Size()
|
2014-01-11 03:21:54 +08:00
|
|
|
if v, err := g.SetView("side", -1, -1, 30, maxY-5); err != nil {
|
|
|
|
if err != gocui.ErrorUnkView {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-10 19:38:08 +08:00
|
|
|
v.Highlight = true
|
|
|
|
fmt.Fprintln(v, "Item 1")
|
|
|
|
fmt.Fprintln(v, "Item 2")
|
|
|
|
fmt.Fprintln(v, "Item 3")
|
|
|
|
fmt.Fprintln(v, "Item 4")
|
|
|
|
}
|
2014-01-11 03:21:54 +08:00
|
|
|
if v, err := g.SetView("main", 30, -1, maxX, maxY-5); err != nil {
|
|
|
|
if err != gocui.ErrorUnkView {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-16 07:28:16 +08:00
|
|
|
b, err := ioutil.ReadFile("Mark.Twain-Tom.Sawyer.txt")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(v, "%s", b)
|
2014-01-11 03:21:54 +08:00
|
|
|
if err := g.SetCurrentView("main"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-10 19:38:08 +08:00
|
|
|
}
|
2014-01-11 03:21:54 +08:00
|
|
|
if v, err := g.SetView("cmdline", -1, maxY-5, maxX, maxY); err != nil {
|
|
|
|
if err != gocui.ErrorUnkView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintln(v, "Command line test")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func layout2(g *gocui.Gui) error {
|
|
|
|
maxX, maxY := g.Size()
|
|
|
|
if v, err := g.SetView("center", maxX/2-10, maxY/2-10, maxX/2+10, maxY/2+10); err != nil {
|
|
|
|
if err != gocui.ErrorUnkView {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintln(v, "Center view test")
|
2014-01-11 03:46:43 +08:00
|
|
|
if err := g.SetCurrentView("center"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-10 19:38:08 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-03 01:34:58 +08:00
|
|
|
func main() {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
g := gocui.NewGui()
|
|
|
|
if err := g.Init(); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
defer g.Close()
|
|
|
|
|
2014-01-11 03:21:54 +08:00
|
|
|
g.SetLayout(layout)
|
|
|
|
if err := keybindings(g); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
2014-01-10 19:38:08 +08:00
|
|
|
g.SelBgColor = gocui.ColorGreen
|
|
|
|
g.SelFgColor = gocui.ColorBlack
|
2014-01-11 03:21:54 +08:00
|
|
|
g.ShowCursor = true
|
2014-01-03 01:34:58 +08:00
|
|
|
|
|
|
|
err = g.MainLoop()
|
|
|
|
if err != nil && err != gocui.ErrorQuit {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
}
|