diff --git a/_demos/demo2.go b/_demos/demo2.go index dbc753d..ff4e018 100644 --- a/_demos/demo2.go +++ b/_demos/demo2.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "github.com/jroimartin/gocui" @@ -20,6 +21,29 @@ func layout(g *gocui.Gui) error { return nil } +func start(g *gocui.Gui) error { + if err := keybindings(g); err != nil { + return err + } + if err := g.SetCurrentView("main"); err != nil { + return err + } + if v := g.GetView("main"); v != nil { + fmt.Fprintln(v, "This is a test") + } + if v := g.GetView("side"); v != nil { + fmt.Fprintln(v, "Item 1") + fmt.Fprintln(v, "Item 2") + fmt.Fprintln(v, "Item 3") + fmt.Fprintln(v, "Item 4") + } + if v := g.GetView("cmdline"); v != nil { + fmt.Fprintln(v, "Buffer test") + } + g.ShowCursor = true + return nil +} + func focusMain(g *gocui.Gui, v *gocui.View) error { return g.SetCurrentView("main") } @@ -103,17 +127,6 @@ func keybindings(g *gocui.Gui) error { return nil } -func start(g *gocui.Gui) error { - if err := keybindings(g); err != nil { - return err - } - if err := g.SetCurrentView("main"); err != nil { - return err - } - g.ShowCursor = true - return nil -} - func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrorQuit } diff --git a/gui.go b/gui.go index cd16e29..9f367c7 100644 --- a/gui.go +++ b/gui.go @@ -198,17 +198,13 @@ func (g *Gui) draw() (err error) { } for _, v := range g.views { - if err := g.drawView(v); err != nil { + if err := v.Draw(); err != nil { return err } } return nil } -func (g *Gui) drawView(v *View) (err error) { - return nil -} - func (g *Gui) resize() (err error) { if g.Layout == nil { return errors.New("Null layout") diff --git a/view.go b/view.go index a50225a..f6d220a 100644 --- a/view.go +++ b/view.go @@ -1,12 +1,17 @@ package gocui import ( + "bufio" + "bytes" "errors" + "io" + "github.com/nsf/termbox-go" ) type View struct { Name string + buffer []rune X0, Y0, X1, Y1 int CX, CY int BgColor, FgColor termbox.Attribute @@ -28,8 +33,32 @@ func NewView(name string, x0, y0, x1, y1 int) (v *View) { return v } +func (v *View) Size() (x, y int) { + return v.X1 - v.X0 - 1, v.Y1 - v.Y0 - 1 +} + +func (v *View) SetRune(x, y int, ch rune) (err error) { + maxX, maxY := v.Size() + if x < 0 || x >= maxX || y < 0 || y >= maxY { + return errors.New("invalid point") + } + termbox.SetCell(v.X0+x+1, v.Y0+y+1, ch, v.FgColor, v.BgColor) + return nil +} + +func (v *View) GetRune(x, y int) (ch rune, err error) { + maxX, maxY := v.Size() + if x < 0 || x >= maxX || y < 0 || y >= maxY { + return 0, errors.New("invalid point") + } + h := v.Y1 - v.Y0 - 1 + c := v.buffer[y*h+x] + return c, nil +} + func (v *View) SetCursor(x, y int) (err error) { - if x < 0 || v.X0+x+1 >= v.X1 || y < 0 || v.Y0+y+1 >= v.Y1 { + maxX, maxY := v.Size() + if x < 0 || x >= maxX || y < 0 || y >= maxY { return errors.New("invalid point") } v.CX = x @@ -38,5 +67,29 @@ func (v *View) SetCursor(x, y int) (err error) { } func (v *View) Write(p []byte) (n int, err error) { - return 0, nil + pr := bytes.Runes(p) + v.buffer = append(v.buffer, pr...) + return len(pr), nil +} + +func (v *View) Draw() (err error) { + maxX, maxY := v.Size() + buf := bytes.NewBufferString(string(v.buffer)) + br := bufio.NewReader(buf) + for nl := 0; ; nl++ { + line, _, err := br.ReadLine() + if err == io.EOF { + break + } else if err != nil { + return err + } + for i, ch := range bytes.Runes(line) { + if i >= 0 && i < maxX && nl >= 0 && nl < maxY { + if err := v.SetRune(i, nl, ch); err != nil { + return err + } + } + } + } + return nil }