// 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" "io" "io/ioutil" "log" "github.com/jroimartin/gocui" ) func nextView(g *gocui.Gui, v *gocui.View) error { currentView := g.CurrentView() if currentView == nil || currentView.Name() == "side" { return g.SetCurrentView("main") } return g.SetCurrentView("side") } func cursorDown(g *gocui.Gui, v *gocui.View) error { if v != nil { cx, cy := v.Cursor() if err := v.SetCursor(cx, cy+1); err != nil { ox, oy := v.Origin() if err := v.SetOrigin(ox, oy+1); err != nil { return err } } } return nil } func cursorUp(g *gocui.Gui, v *gocui.View) error { if v != nil { ox, oy := v.Origin() cx, cy := v.Cursor() if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 { if err := v.SetOrigin(ox, oy-1); err != nil { return err } } } return nil } func cursorLeft(g *gocui.Gui, v *gocui.View) error { if v != nil { ox, oy := v.Origin() cx, cy := v.Cursor() if err := v.SetCursor(cx-1, cy); err != nil && ox > 0 { if err := v.SetOrigin(ox-1, oy); err != nil { return err } } } return nil } func cursorRight(g *gocui.Gui, v *gocui.View) error { if v != nil { cx, cy := v.Cursor() if err := v.SetCursor(cx+1, cy); err != nil { ox, oy := v.Origin() if err := v.SetOrigin(ox+1, oy); err != nil { return err } } } return nil } 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 { l = "" } maxX, maxY := g.Size() if v, err := g.SetView("msg", maxX/2-30, maxY/2, maxX/2+30, maxY/2+2); err != nil { if err != gocui.ErrorUnkView { return err } fmt.Fprintln(v, l) if err := g.SetCurrentView("msg"); err != nil { return err } } return nil } func delMsg(g *gocui.Gui, v *gocui.View) error { if err := g.DeleteView("msg"); err != nil { return err } if err := g.SetCurrentView("side"); err != nil { return err } return nil } func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrorQuit } func keybindings(g *gocui.Gui) error { if err := g.SetKeybinding("side", gocui.KeyCtrlSpace, 0, nextView); err != nil { return err } if err := g.SetKeybinding("main", gocui.KeyCtrlSpace, 0, nextView); err != nil { return err } if err := g.SetKeybinding("", gocui.KeyArrowDown, 0, cursorDown); err != nil { return err } if err := g.SetKeybinding("", gocui.KeyArrowUp, 0, cursorUp); err != nil { return err } if err := g.SetKeybinding("", gocui.KeyArrowLeft, 0, cursorLeft); err != nil { return err } if err := g.SetKeybinding("", gocui.KeyArrowRight, 0, cursorRight); err != nil { return err } if err := g.SetKeybinding("", gocui.KeyCtrlC, 0, quit); err != nil { return err } if err := g.SetKeybinding("side", gocui.KeyEnter, 0, getLine); err != nil { return err } if err := g.SetKeybinding("msg", gocui.KeyEnter, 0, delMsg); err != nil { return err } if err := g.SetKeybinding("main", gocui.KeyCtrlS, 0, saveMain); err != nil { return err } 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 } } return nil } func layout(g *gocui.Gui) error { maxX, maxY := g.Size() if v, err := g.SetView("side", -1, -1, 30, maxY); err != nil { if err != gocui.ErrorUnkView { return err } v.Highlight = true fmt.Fprintln(v, "Item 1") fmt.Fprintln(v, "Item 2") fmt.Fprintln(v, "Item 3") fmt.Fprintln(v, "Item 4") } if v, err := g.SetView("main", 30, -1, maxX, maxY); err != nil { if err != gocui.ErrorUnkView { return err } b, err := ioutil.ReadFile("Mark.Twain-Tom.Sawyer.txt") if err != nil { panic(err) } fmt.Fprintf(v, "%s", b) v.Editable = true if err := g.SetCurrentView("main"); err != nil { return err } } return nil } func main() { var err error g := gocui.NewGui() if err := g.Init(); err != nil { log.Panicln(err) } defer g.Close() g.SetLayout(layout) if err := keybindings(g); err != nil { log.Panicln(err) } g.SelBgColor = gocui.ColorGreen g.SelFgColor = gocui.ColorBlack g.ShowCursor = true err = g.MainLoop() if err != nil && err != gocui.ErrorQuit { log.Panicln(err) } }