Implement view's buffer and draw

This commit is contained in:
Roi Martin (@nibble_ds) 2014-01-09 20:20:14 +01:00
parent 82ad9fa4ba
commit 10769ec99c
3 changed files with 80 additions and 18 deletions

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"log" "log"
"github.com/jroimartin/gocui" "github.com/jroimartin/gocui"
@ -20,6 +21,29 @@ func layout(g *gocui.Gui) error {
return nil 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 { func focusMain(g *gocui.Gui, v *gocui.View) error {
return g.SetCurrentView("main") return g.SetCurrentView("main")
} }
@ -103,17 +127,6 @@ func keybindings(g *gocui.Gui) error {
return nil 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 { func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrorQuit return gocui.ErrorQuit
} }

6
gui.go
View File

@ -198,17 +198,13 @@ func (g *Gui) draw() (err error) {
} }
for _, v := range g.views { for _, v := range g.views {
if err := g.drawView(v); err != nil { if err := v.Draw(); err != nil {
return err return err
} }
} }
return nil return nil
} }
func (g *Gui) drawView(v *View) (err error) {
return nil
}
func (g *Gui) resize() (err error) { func (g *Gui) resize() (err error) {
if g.Layout == nil { if g.Layout == nil {
return errors.New("Null layout") return errors.New("Null layout")

57
view.go
View File

@ -1,12 +1,17 @@
package gocui package gocui
import ( import (
"bufio"
"bytes"
"errors" "errors"
"io"
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
) )
type View struct { type View struct {
Name string Name string
buffer []rune
X0, Y0, X1, Y1 int X0, Y0, X1, Y1 int
CX, CY int CX, CY int
BgColor, FgColor termbox.Attribute BgColor, FgColor termbox.Attribute
@ -28,8 +33,32 @@ func NewView(name string, x0, y0, x1, y1 int) (v *View) {
return v 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) { 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") return errors.New("invalid point")
} }
v.CX = x 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) { 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
} }