diff --git a/_examples/demo.go b/_examples/demo.go index d956da4..755323d 100644 --- a/_examples/demo.go +++ b/_examples/demo.go @@ -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 { diff --git a/view.go b/view.go index 57de4f7..2c1842c 100644 --- a/view.go +++ b/view.go @@ -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) {