Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prettify examples: add main demo screen to select specific examples #117

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions _examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## Examples

Each of the samples in `_examples` demonstrates a specific aspect of the API.

Each can be run be alone or selected from the 'main' example.

### Build & run

```
gocui $ cd _examples/

_examples $ go run . --help
usage:
go run . : select a demo from the gui and run it
go run . <demo> : run the 'demo' argument

where 'demo' can be one of:
active
bufs
colors
colors-256
colors-true
custom-frames
demo
dynamic
flow-layout
goroutine
hello
keybinds
layout
mask
mouse
on-top
overlap
size
stdin
table
title
widgets
wrap

```

### Select a demo through the gui

<img src="demo_screen.png" alt="Demo Screen" width="500"/>

44 changes: 25 additions & 19 deletions _examples/active.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ import (
"github.com/awesome-gocui/gocui"
)

var (
viewArr = []string{"v1", "v2", "v3", "v4"}
active = 0
)
type demoActive struct {
viewArr []string
active int
}

func setCurrentViewOnTop(g *gocui.Gui, name string) (*gocui.View, error) {
func (d *demoActive) setCurrentViewOnTop(g *gocui.Gui, name string) (*gocui.View, error) {
if _, err := g.SetCurrentView(name); err != nil {
return nil, err
}
return g.SetViewOnTop(name)
}

func nextView(g *gocui.Gui, v *gocui.View) error {
nextIndex := (active + 1) % len(viewArr)
name := viewArr[nextIndex]
func (d *demoActive) nextView(g *gocui.Gui, v *gocui.View) error {
nextIndex := (d.active + 1) % len(d.viewArr)
name := d.viewArr[nextIndex]

out, err := g.View("v2")
if err != nil {
return err
}
fmt.Fprintln(out, "Going from view "+v.Name()+" to "+name)
_, _ = fmt.Fprintln(out, "Going from view "+v.Name()+" to "+name)

if _, err := setCurrentViewOnTop(g, name); err != nil {
if _, err := d.setCurrentViewOnTop(g, name); err != nil {
return err
}

Expand All @@ -44,11 +44,11 @@ func nextView(g *gocui.Gui, v *gocui.View) error {
g.Cursor = false
}

active = nextIndex
d.active = nextIndex
return nil
}

func layout(g *gocui.Gui) error {
func (d *demoActive) layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
if v, err := g.SetView("v1", 0, 0, maxX/2-1, maxY/2-1, 0); err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
Expand All @@ -58,7 +58,7 @@ func layout(g *gocui.Gui) error {
v.Editable = true
v.Wrap = true

if _, err = setCurrentViewOnTop(g, "v1"); err != nil {
if _, err = d.setCurrentViewOnTop(g, "v1"); err != nil {
return err
}
}
Expand All @@ -78,7 +78,7 @@ func layout(g *gocui.Gui) error {
v.Title = "v3"
v.Wrap = true
v.Autoscroll = true
fmt.Fprint(v, "Press TAB to change current view")
_, _ = fmt.Fprint(v, "Press TAB to change current view")
}
if v, err := g.SetView("v4", maxX/2, maxY/2, maxX-1, maxY-1, 0); err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
Expand All @@ -90,11 +90,17 @@ func layout(g *gocui.Gui) error {
return nil
}

func quit(g *gocui.Gui, v *gocui.View) error {
func (d *demoActive) quit(g *gocui.Gui, v *gocui.View) error {
_ = g
_ = v
return gocui.ErrQuit
}

func main() {
func mainActive() {
d := &demoActive{
viewArr: []string{"v1", "v2", "v3", "v4"},
active: 0,
}
g, err := gocui.NewGui(gocui.OutputNormal, true)
if err != nil {
log.Panicln(err)
Expand All @@ -105,12 +111,12 @@ func main() {
g.Cursor = true
g.SelFgColor = gocui.ColorGreen

g.SetManagerFunc(layout)
g.SetManagerFunc(d.layout)

if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, d.quit); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, nextView); err != nil {
if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, d.nextView); err != nil {
log.Panicln(err)
}

Expand Down
28 changes: 16 additions & 12 deletions _examples/bufs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ import (
"github.com/awesome-gocui/gocui"
)

var vbuf, buf string
type demoBufs struct {
vbuf, buf string
}

func quit(g *gocui.Gui, v *gocui.View) error {
vbuf = v.ViewBuffer()
buf = v.Buffer()
func (d *demoBufs) quit(g *gocui.Gui, v *gocui.View) error {
d.vbuf = v.ViewBuffer()
d.buf = v.Buffer()
return gocui.ErrQuit
}

func overwrite(g *gocui.Gui, v *gocui.View) error {
func (d *demoBufs) overwrite(g *gocui.Gui, v *gocui.View) error {
v.Overwrite = !v.Overwrite
return nil
}

func layout(g *gocui.Gui) error {
func (d *demoBufs) layout(g *gocui.Gui) error {
_, maxY := g.Size()
if v, err := g.SetView("main", 0, 0, 20, maxY-1, 0); err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
Expand All @@ -42,21 +44,23 @@ func layout(g *gocui.Gui) error {
return nil
}

func main() {
func mainBufs() {
g, err := gocui.NewGui(gocui.OutputNormal, true)
if err != nil {
log.Panicln(err)
}

d := &demoBufs{}

g.Cursor = true
g.Mouse = true

g.SetManagerFunc(layout)
g.SetManagerFunc(d.layout)

if err := g.SetKeybinding("main", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
if err := g.SetKeybinding("main", gocui.KeyCtrlC, gocui.ModNone, d.quit); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("main", gocui.KeyCtrlI, gocui.ModNone, overwrite); err != nil {
if err := g.SetKeybinding("main", gocui.KeyCtrlI, gocui.ModNone, d.overwrite); err != nil {
log.Panicln(err)
}

Expand All @@ -66,6 +70,6 @@ func main() {

g.Close()

fmt.Printf("VBUF:\n%s\n", vbuf)
fmt.Printf("BUF:\n%s\n", buf)
fmt.Printf("VBUF:\n%s\n", d.vbuf)
fmt.Printf("BUF:\n%s\n", d.buf)
}
15 changes: 10 additions & 5 deletions _examples/colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ import (
"github.com/awesome-gocui/gocui"
)

func main() {
type demoColors struct {
}

func mainColors() {
g, err := gocui.NewGui(gocui.OutputNormal, true)
if err != nil {
log.Panicln(err)
}
defer g.Close()

g.SetManagerFunc(layout)
d := &demoColors{}

g.SetManagerFunc(d.layout)

if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, d.quit); err != nil {
log.Panicln(err)
}

Expand All @@ -30,7 +35,7 @@ func main() {
}
}

func layout(g *gocui.Gui) error {
func (d *demoColors) layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
if v, err := g.SetView("colors", maxX/2-7, maxY/2-12, maxX/2+7, maxY/2+13, 0); err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
Expand All @@ -48,6 +53,6 @@ func layout(g *gocui.Gui) error {
return nil
}

func quit(g *gocui.Gui, v *gocui.View) error {
func (d *demoColors) quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
21 changes: 13 additions & 8 deletions _examples/colors256.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ import (
"github.com/awesome-gocui/gocui"
)

func main() {
type demoColors256 struct{}

func mainColors256() {
g, err := gocui.NewGui(gocui.Output256, true)

if err != nil {
log.Panicln(err)
}
defer g.Close()

g.SetManagerFunc(layout)
d := demoColors256{}
g.SetManagerFunc(d.layout)

if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, d.quit); err != nil {
log.Panicln(err)
}

Expand All @@ -31,7 +34,7 @@ func main() {
}
}

func layout(g *gocui.Gui) error {
func (d *demoColors256) layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
if v, err := g.SetView("colors", -1, -1, maxX, maxY, 0); err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
Expand All @@ -47,10 +50,10 @@ func layout(g *gocui.Gui) error {
str += "\n"
}

fmt.Fprint(v, str)
_, _ = fmt.Fprint(v, str)
}

fmt.Fprint(v, "\n\n")
_, _ = fmt.Fprint(v, "\n\n")

// 8-colors escape codes
ctr := 0
Expand All @@ -61,7 +64,7 @@ func layout(g *gocui.Gui) error {
str += "\n"
}

fmt.Fprint(v, str)
_, _ = fmt.Fprint(v, str)

ctr++
}
Expand All @@ -73,6 +76,8 @@ func layout(g *gocui.Gui) error {
return nil
}

func quit(g *gocui.Gui, v *gocui.View) error {
func (d *demoColors256) quit(g *gocui.Gui, v *gocui.View) error {
_ = g
_ = v
return gocui.ErrQuit
}