2015-02-01 03:39:43 +08:00
|
|
|
// Copyright 2014 The gocui Authors. All rights reserved.
|
2014-01-15 03:11:12 +08:00
|
|
|
// 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-05-01 20:32:30 +08:00
|
|
|
"io"
|
2014-01-16 07:28:16 +08:00
|
|
|
"io/ioutil"
|
2014-01-03 01:34:58 +08:00
|
|
|
"log"
|
2015-08-17 00:08:34 +08:00
|
|
|
"strings"
|
2014-01-03 01:34:58 +08:00
|
|
|
|
|
|
|
"github.com/jroimartin/gocui"
|
|
|
|
)
|
|
|
|
|
2014-01-28 05:40:46 +08:00
|
|
|
func nextView(g *gocui.Gui, v *gocui.View) error {
|
2015-01-24 22:03:03 +08:00
|
|
|
if v == nil || v.Name() == "side" {
|
2016-10-18 06:24:16 +08:00
|
|
|
_, err := g.SetCurrentView("main")
|
|
|
|
return err
|
2014-01-28 05:40:46 +08:00
|
|
|
}
|
2016-10-18 06:24:16 +08:00
|
|
|
_, err := g.SetCurrentView("side")
|
|
|
|
return err
|
2014-01-06 20:56:28 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-01-24 06:10:13 +08:00
|
|
|
func getLine(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
var l string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
_, cy := v.Cursor()
|
|
|
|
if l, err = v.Line(cy); err != nil {
|
2014-01-28 05:50:02 +08:00
|
|
|
l = ""
|
2014-01-24 06:10:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
maxX, maxY := g.Size()
|
|
|
|
if v, err := g.SetView("msg", maxX/2-30, maxY/2, maxX/2+30, maxY/2+2); err != nil {
|
2016-01-23 23:07:42 +08:00
|
|
|
if err != gocui.ErrUnknownView {
|
2014-01-24 06:10:13 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintln(v, l)
|
2016-10-18 06:24:16 +08:00
|
|
|
if _, err := g.SetCurrentView("msg"); err != nil {
|
2014-01-28 05:40:46 +08:00
|
|
|
return err
|
|
|
|
}
|
2014-01-24 06:10:13 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-28 05:40:46 +08:00
|
|
|
func delMsg(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
if err := g.DeleteView("msg"); err != nil {
|
2014-01-24 06:10:13 +08:00
|
|
|
return err
|
|
|
|
}
|
2016-10-18 06:24:16 +08:00
|
|
|
if _, err := g.SetCurrentView("side"); err != nil {
|
2014-01-28 05:40:46 +08:00
|
|
|
return err
|
2014-01-24 06:10:13 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-28 05:40:46 +08:00
|
|
|
func quit(g *gocui.Gui, v *gocui.View) error {
|
2016-01-23 23:07:42 +08:00
|
|
|
return gocui.ErrQuit
|
2014-01-28 05:40:46 +08:00
|
|
|
}
|
|
|
|
|
2014-01-06 08:47:22 +08:00
|
|
|
func keybindings(g *gocui.Gui) error {
|
2015-01-24 21:23:46 +08:00
|
|
|
if err := g.SetKeybinding("side", gocui.KeyCtrlSpace, gocui.ModNone, nextView); err != nil {
|
2014-01-06 08:47:22 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-01-24 21:23:46 +08:00
|
|
|
if err := g.SetKeybinding("main", gocui.KeyCtrlSpace, gocui.ModNone, nextView); err != nil {
|
2014-01-06 08:47:22 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-02-23 07:34:41 +08:00
|
|
|
if err := g.SetKeybinding("side", gocui.KeyArrowDown, gocui.ModNone, cursorDown); err != nil {
|
2014-01-07 01:36:38 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-02-23 07:34:41 +08:00
|
|
|
if err := g.SetKeybinding("side", gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil {
|
2014-01-07 01:36:38 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-01-24 21:23:46 +08:00
|
|
|
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
|
2014-01-06 20:56:28 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-01-24 21:23:46 +08:00
|
|
|
if err := g.SetKeybinding("side", gocui.KeyEnter, gocui.ModNone, getLine); err != nil {
|
2014-01-18 20:47:08 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-01-24 21:23:46 +08:00
|
|
|
if err := g.SetKeybinding("msg", gocui.KeyEnter, gocui.ModNone, delMsg); err != nil {
|
2014-01-24 06:10:13 +08:00
|
|
|
return err
|
|
|
|
}
|
2014-01-07 01:36:38 +08:00
|
|
|
|
2015-01-24 21:23:46 +08:00
|
|
|
if err := g.SetKeybinding("main", gocui.KeyCtrlS, gocui.ModNone, saveMain); err != nil {
|
2014-05-01 20:32:30 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-08-17 00:08:34 +08:00
|
|
|
if err := g.SetKeybinding("main", gocui.KeyCtrlW, gocui.ModNone, saveVisualMain); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-05-01 20:32:30 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveMain(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
f, err := ioutil.TempFile("", "gocui_demo_")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
p := make([]byte, 5)
|
|
|
|
v.Rewind()
|
|
|
|
for {
|
|
|
|
n, err := v.Read(p)
|
|
|
|
if n > 0 {
|
|
|
|
if _, err := f.Write(p[:n]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-01-06 08:47:22 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-17 00:08:34 +08:00
|
|
|
func saveVisualMain(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
f, err := ioutil.TempFile("", "gocui_demo_")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
vb := v.ViewBuffer()
|
|
|
|
if _, err := io.Copy(f, strings.NewReader(vb)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-10 19:38:08 +08:00
|
|
|
func layout(g *gocui.Gui) error {
|
|
|
|
maxX, maxY := g.Size()
|
2014-01-28 05:40:46 +08:00
|
|
|
if v, err := g.SetView("side", -1, -1, 30, maxY); err != nil {
|
2016-01-23 23:07:42 +08:00
|
|
|
if err != gocui.ErrUnknownView {
|
2014-01-11 03:21:54 +08:00
|
|
|
return err
|
|
|
|
}
|
2014-01-10 19:38:08 +08:00
|
|
|
v.Highlight = true
|
2016-10-19 04:44:39 +08:00
|
|
|
v.SelBgColor = gocui.ColorGreen
|
|
|
|
v.SelFgColor = gocui.ColorBlack
|
2014-01-10 19:38:08 +08:00
|
|
|
fmt.Fprintln(v, "Item 1")
|
|
|
|
fmt.Fprintln(v, "Item 2")
|
|
|
|
fmt.Fprintln(v, "Item 3")
|
2015-01-29 23:19:25 +08:00
|
|
|
fmt.Fprint(v, "\rWill be")
|
2015-02-02 08:32:15 +08:00
|
|
|
fmt.Fprint(v, "deleted\rItem 4\nItem 5")
|
2014-01-10 19:38:08 +08:00
|
|
|
}
|
2014-01-28 05:40:46 +08:00
|
|
|
if v, err := g.SetView("main", 30, -1, maxX, maxY); err != nil {
|
2016-01-23 23:07:42 +08:00
|
|
|
if err != gocui.ErrUnknownView {
|
2014-01-11 03:21:54 +08:00
|
|
|
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-21 06:03:28 +08:00
|
|
|
v.Editable = true
|
2015-02-04 00:09:21 +08:00
|
|
|
v.Wrap = true
|
2016-10-18 06:24:16 +08:00
|
|
|
if _, err := g.SetCurrentView("main"); err != nil {
|
2014-01-11 03:46:43 +08:00
|
|
|
return err
|
|
|
|
}
|
2014-01-10 19:38:08 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-03 01:34:58 +08:00
|
|
|
func main() {
|
2016-10-24 08:24:41 +08:00
|
|
|
g, err := gocui.NewGui()
|
|
|
|
if err != nil {
|
2014-01-03 01:34:58 +08:00
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
defer g.Close()
|
|
|
|
|
2016-10-24 14:36:23 +08:00
|
|
|
g.SetManagerFunc(layout)
|
2014-01-11 03:21:54 +08:00
|
|
|
if err := keybindings(g); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
2016-01-26 16:55:08 +08:00
|
|
|
g.Cursor = true
|
2014-01-03 01:34:58 +08:00
|
|
|
|
2016-01-30 09:36:10 +08:00
|
|
|
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
|
2014-01-03 01:34:58 +08:00
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
}
|