From 5efdece4aff588ef50fa602df9d395ff9c9bf424 Mon Sep 17 00:00:00 2001 From: Roi Martin Date: Sat, 30 Jan 2016 02:54:26 +0100 Subject: [PATCH] Add _examples/hello.go and update README --- README.md | 76 +++++++++++++++++++++++++--------------------- _examples/hello.go | 45 +++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 35 deletions(-) create mode 100644 _examples/hello.go diff --git a/README.md b/README.md index bf01ac0..b163400 100644 --- a/README.md +++ b/README.md @@ -1,57 +1,71 @@ -# GOCUI - Go Console User Interface [![GoDoc](https://godoc.org/github.com/jroimartin/gocui?status.svg)](https://godoc.org/github.com/jroimartin/gocui) +# GOCUI - Go Console User Interface Minimalist Go package aimed at creating Console User Interfaces. ## Features * Minimalist API. -* Views (the "windows" in the GUI) implement the interface io.Writer. +* Views (the "windows" in the GUI) implement the interface io.ReadWriter. * Support for overlapping views. -* The GUI can be modified at runtime. +* The GUI can be modified at runtime (concurrent-safe). * Global and view-level keybindings. -* Edit mode. +* Mouse support. +* Customizable edition mode. + +## Installation + +`go get github.com/jroimartin/gocui` + +## Documentation + +`godoc github.com/jroimartin/gocui` [![GoDoc](https://godoc.org/github.com/jroimartin/gocui?status.svg)](https://godoc.org/github.com/jroimartin/gocui) ## Example ```go -func layout(g *gocui.Gui) error { - maxX, maxY := g.Size() - if v, err := g.SetView("center", maxX/2-10, maxY/2, maxX/2+10, maxY/2+2); err != nil { - if err != gocui.ErrUnknownView { - return err - } - fmt.Fprintln(v, "This is an example") - } - return nil -} -func quit(g *gocui.Gui, v *gocui.View) error { - return gocui.ErrQuit -} +package main + +import ( + "fmt" + "log" + + "github.com/jroimartin/gocui" +) + 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 := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { log.Panicln(err) } - err = g.MainLoop() - if err != nil && err != gocui.ErrQuit { + + if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { log.Panicln(err) } } + +func layout(g *gocui.Gui) error { + maxX, maxY := g.Size() + if v, err := g.SetView("hello", maxX/2-7, maxY/2, maxX/2+7, maxY/2+2); err != nil { + if err != gocui.ErrUnknownView { + return err + } + fmt.Fprintln(v, "Hello world!") + } + return nil +} + +func quit(g *gocui.Gui, v *gocui.View) error { + return gocui.ErrQuit +} ``` -## Concurrency - -Gocui implements mechanisms to be concurrent safe. Specifically, Gui and View -objects must be updated from a layout function or via *Gui.Execute. - -For more information, see _examples/goroutine.go - ## Screenshots _examples/demo.go: @@ -61,11 +75,3 @@ _examples/demo.go: _examples/delete.go: ![_examples/delete.go](https://cloud.githubusercontent.com/assets/1223476/5992751/76ad5cc2-aa36-11e4-8204-6a90269db827.png) - -## Installation - -`go get github.com/jroimartin/gocui` - -## Documentation - -`godoc github.com/jroimartin/gocui` diff --git a/_examples/hello.go b/_examples/hello.go new file mode 100644 index 0000000..c02183e --- /dev/null +++ b/_examples/hello.go @@ -0,0 +1,45 @@ +// 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" +) + +func main() { + g := gocui.NewGui() + if err := g.Init(); err != nil { + log.Panicln(err) + } + defer g.Close() + + g.SetLayout(layout) + + if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { + log.Panicln(err) + } + + if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { + log.Panicln(err) + } +} + +func layout(g *gocui.Gui) error { + maxX, maxY := g.Size() + if v, err := g.SetView("hello", maxX/2-7, maxY/2, maxX/2+7, maxY/2+2); err != nil { + if err != gocui.ErrUnknownView { + return err + } + fmt.Fprintln(v, "Hello world!") + } + return nil +} + +func quit(g *gocui.Gui, v *gocui.View) error { + return gocui.ErrQuit +}