Add method View.ViewBuffer

This commit is contained in:
Roi Martin 2015-08-16 18:08:34 +02:00
parent af4769f30e
commit e5bf60e36b
2 changed files with 28 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import (
"io"
"io/ioutil"
"log"
"strings"
"github.com/jroimartin/gocui"
)
@ -108,6 +109,9 @@ func keybindings(g *gocui.Gui) error {
if err := g.SetKeybinding("main", gocui.KeyCtrlS, gocui.ModNone, saveMain); err != nil {
return err
}
if err := g.SetKeybinding("main", gocui.KeyCtrlW, gocui.ModNone, saveVisualMain); err != nil {
return err
}
return nil
}
@ -137,6 +141,20 @@ func saveMain(g *gocui.Gui, v *gocui.View) error {
return nil
}
func saveVisualMain(g *gocui.Gui, v *gocui.View) error {
f, err := ioutil.TempFile("", "gocui_demo_")
if err != nil {
return err
}
defer f.Close()
vb := v.ViewBuffer()
if _, err := io.Copy(f, strings.NewReader(vb)); 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 {

10
view.go
View File

@ -428,6 +428,16 @@ func (v *View) Buffer() string {
return strings.Replace(str, "\x00", " ", -1)
}
// ViewBuffer returns a string with the contents of the view's buffer that is
// showed to the user
func (v *View) ViewBuffer() string {
str := ""
for _, l := range v.viewLines {
str += string(l.line) + "\n"
}
return strings.Replace(str, "\x00", " ", -1)
}
// Line returns a string with the line of the view's internal buffer
// at the position corresponding to the point (x, y).
func (v *View) Line(y int) (string, error) {