2015-02-01 03:39:43 +08:00
|
|
|
// Copyright 2014 The gocui Authors. All rights reserved.
|
2014-01-15 03:11:12 +08:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-12-28 04:36:26 +08:00
|
|
|
package gocui
|
|
|
|
|
|
|
|
import (
|
2014-01-10 03:20:14 +08:00
|
|
|
"bytes"
|
2014-01-07 01:36:38 +08:00
|
|
|
"errors"
|
2014-05-01 20:32:30 +08:00
|
|
|
"io"
|
2014-01-24 06:10:13 +08:00
|
|
|
"strings"
|
2014-01-10 03:20:14 +08:00
|
|
|
|
2014-01-01 04:23:28 +08:00
|
|
|
"github.com/nsf/termbox-go"
|
2013-12-28 04:36:26 +08:00
|
|
|
)
|
|
|
|
|
2014-01-20 00:03:52 +08:00
|
|
|
// A View is a window. It maintains its own internal buffer and cursor
|
|
|
|
// position.
|
2013-12-28 04:36:26 +08:00
|
|
|
type View struct {
|
2014-05-03 21:20:46 +08:00
|
|
|
name string
|
|
|
|
x0, y0, x1, y1 int
|
|
|
|
ox, oy int
|
|
|
|
cx, cy int
|
2016-04-28 04:34:55 +08:00
|
|
|
lines [][]cell
|
2014-05-03 21:20:46 +08:00
|
|
|
readOffset int
|
|
|
|
readCache string
|
|
|
|
|
2015-02-04 01:29:38 +08:00
|
|
|
tainted bool // marks if the viewBuffer must be updated
|
|
|
|
viewLines []viewLine // internal representation of the view's buffer
|
2015-02-04 00:09:21 +08:00
|
|
|
|
2016-04-28 04:34:55 +08:00
|
|
|
ei *escapeInterpreter // used to decode ESC sequences on Write
|
|
|
|
|
2014-05-03 21:20:46 +08:00
|
|
|
// BgColor and FgColor allow to configure the background and foreground
|
|
|
|
// colors of the View.
|
|
|
|
BgColor, FgColor Attribute
|
|
|
|
|
|
|
|
// SelBgColor and SelFgColor are used to configure the background and
|
|
|
|
// foreground colors of the selected line, when it is highlighted.
|
|
|
|
SelBgColor, SelFgColor Attribute
|
2014-01-17 06:01:53 +08:00
|
|
|
|
2014-01-21 06:03:28 +08:00
|
|
|
// If Editable is true, keystrokes will be added to the view's internal
|
|
|
|
// buffer at the cursor position.
|
|
|
|
Editable bool
|
|
|
|
|
2016-10-27 06:26:59 +08:00
|
|
|
// Editor allows to define the editor that manages the edition mode,
|
|
|
|
// including keybindings or cursor behaviour. DefaultEditor is used by
|
|
|
|
// default.
|
|
|
|
Editor Editor
|
|
|
|
|
2015-02-24 07:26:26 +08:00
|
|
|
// Overwrite enables or disables the overwrite mode of the view.
|
|
|
|
Overwrite bool
|
|
|
|
|
2014-01-20 00:03:52 +08:00
|
|
|
// If Highlight is true, Sel{Bg,Fg}Colors will be used
|
|
|
|
// for the line under the cursor position.
|
2014-01-17 06:01:53 +08:00
|
|
|
Highlight bool
|
2014-10-18 05:22:28 +08:00
|
|
|
|
2014-11-15 20:50:56 +08:00
|
|
|
// If Frame is true, a border will be drawn around the view.
|
2014-10-18 05:22:28 +08:00
|
|
|
Frame bool
|
2014-11-12 00:10:55 +08:00
|
|
|
|
|
|
|
// If Wrap is true, the content that is written to this View is
|
2015-02-02 05:49:07 +08:00
|
|
|
// automatically wrapped when it is longer than its width. If true the
|
|
|
|
// view's x-origin will be ignored.
|
2014-11-12 00:10:55 +08:00
|
|
|
Wrap bool
|
|
|
|
|
2015-02-02 05:49:07 +08:00
|
|
|
// If Autoscroll is true, the View will automatically scroll down when the
|
|
|
|
// text overflows. If true the view's y-origin will be ignored.
|
|
|
|
Autoscroll bool
|
2016-02-06 21:50:30 +08:00
|
|
|
|
|
|
|
// If Frame is true, Title allows to configure a title for the view.
|
|
|
|
Title string
|
2016-03-15 17:42:42 +08:00
|
|
|
|
2016-04-20 20:34:09 +08:00
|
|
|
// If Mask is true, the View will display the mask instead of the real
|
|
|
|
// content
|
2016-03-15 17:42:42 +08:00
|
|
|
Mask rune
|
2013-12-28 04:36:26 +08:00
|
|
|
}
|
|
|
|
|
2015-02-04 01:29:38 +08:00
|
|
|
type viewLine struct {
|
2015-02-04 08:59:03 +08:00
|
|
|
linesX, linesY int // coordinates relative to v.lines
|
2016-04-28 04:34:55 +08:00
|
|
|
line []cell
|
|
|
|
}
|
|
|
|
|
|
|
|
type cell struct {
|
|
|
|
chr rune
|
|
|
|
bgColor, fgColor Attribute
|
|
|
|
}
|
|
|
|
|
|
|
|
type lineType []cell
|
|
|
|
|
|
|
|
// String returns a string from a given cell slice.
|
|
|
|
func (l lineType) String() string {
|
|
|
|
str := ""
|
|
|
|
for _, c := range l {
|
|
|
|
str += string(c.chr)
|
|
|
|
}
|
|
|
|
return str
|
2015-02-04 01:29:38 +08:00
|
|
|
}
|
|
|
|
|
2014-01-20 00:03:52 +08:00
|
|
|
// newView returns a new View object.
|
2016-11-06 03:08:48 +08:00
|
|
|
func newView(name string, x0, y0, x1, y1 int, mode OutputMode) *View {
|
2014-01-16 07:28:16 +08:00
|
|
|
v := &View{
|
2015-02-04 00:09:21 +08:00
|
|
|
name: name,
|
|
|
|
x0: x0,
|
|
|
|
y0: y0,
|
|
|
|
x1: x1,
|
|
|
|
y1: y1,
|
|
|
|
Frame: true,
|
2016-10-27 06:26:59 +08:00
|
|
|
Editor: DefaultEditor,
|
2015-02-04 00:09:21 +08:00
|
|
|
tainted: true,
|
2016-11-06 03:08:48 +08:00
|
|
|
ei: newEscapeInterpreter(mode),
|
2013-12-29 01:49:01 +08:00
|
|
|
}
|
|
|
|
return v
|
2013-12-28 04:36:26 +08:00
|
|
|
}
|
2014-01-07 01:36:38 +08:00
|
|
|
|
2014-01-20 00:03:52 +08:00
|
|
|
// Size returns the number of visible columns and rows in the View.
|
2014-01-10 03:20:14 +08:00
|
|
|
func (v *View) Size() (x, y int) {
|
2014-01-17 06:01:53 +08:00
|
|
|
return v.x1 - v.x0 - 1, v.y1 - v.y0 - 1
|
|
|
|
}
|
|
|
|
|
2014-01-20 00:03:52 +08:00
|
|
|
// Name returns the name of the view.
|
2014-01-17 06:01:53 +08:00
|
|
|
func (v *View) Name() string {
|
|
|
|
return v.name
|
2014-01-10 03:20:14 +08:00
|
|
|
}
|
|
|
|
|
2016-04-28 04:34:55 +08:00
|
|
|
// setRune sets a rune at the given point relative to the view. It applies the
|
|
|
|
// specified colors, taking into account if the cell must be highlighted. Also,
|
|
|
|
// it checks if the position is valid.
|
|
|
|
func (v *View) setRune(x, y int, ch rune, fgColor, bgColor Attribute) error {
|
2014-01-10 03:20:14 +08:00
|
|
|
maxX, maxY := v.Size()
|
|
|
|
if x < 0 || x >= maxX || y < 0 || y >= maxY {
|
|
|
|
return errors.New("invalid point")
|
|
|
|
}
|
2014-01-10 04:55:23 +08:00
|
|
|
|
2016-05-18 04:40:24 +08:00
|
|
|
var (
|
|
|
|
ry, rcy int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if v.Highlight {
|
|
|
|
_, ry, err = v.realPosition(x, y)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, rcy, err = v.realPosition(v.cx, v.cy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 04:34:55 +08:00
|
|
|
if v.Mask != 0 {
|
2014-05-03 21:20:46 +08:00
|
|
|
fgColor = v.FgColor
|
|
|
|
bgColor = v.BgColor
|
2016-03-15 17:42:42 +08:00
|
|
|
ch = v.Mask
|
2016-04-28 04:34:55 +08:00
|
|
|
} else if v.Highlight && ry == rcy {
|
|
|
|
fgColor = v.SelFgColor
|
|
|
|
bgColor = v.SelBgColor
|
2016-03-15 17:42:42 +08:00
|
|
|
}
|
2016-04-28 04:34:55 +08:00
|
|
|
|
2014-01-17 06:01:53 +08:00
|
|
|
termbox.SetCell(v.x0+x+1, v.y0+y+1, ch,
|
2014-01-10 04:55:23 +08:00
|
|
|
termbox.Attribute(fgColor), termbox.Attribute(bgColor))
|
2016-04-28 04:34:55 +08:00
|
|
|
|
2014-01-10 03:20:14 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-20 00:03:52 +08:00
|
|
|
// SetCursor sets the cursor position of the view at the given point,
|
|
|
|
// relative to the view. It checks if the position is valid.
|
2014-01-16 07:28:16 +08:00
|
|
|
func (v *View) SetCursor(x, y int) error {
|
2014-01-10 03:20:14 +08:00
|
|
|
maxX, maxY := v.Size()
|
|
|
|
if x < 0 || x >= maxX || y < 0 || y >= maxY {
|
2014-01-07 01:36:38 +08:00
|
|
|
return errors.New("invalid point")
|
|
|
|
}
|
2014-01-17 06:01:53 +08:00
|
|
|
v.cx = x
|
|
|
|
v.cy = y
|
2014-01-07 01:36:38 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-20 00:03:52 +08:00
|
|
|
// Cursor returns the cursor position of the view.
|
2014-01-17 06:01:53 +08:00
|
|
|
func (v *View) Cursor() (x, y int) {
|
|
|
|
return v.cx, v.cy
|
|
|
|
}
|
|
|
|
|
2014-01-20 00:03:52 +08:00
|
|
|
// SetOrigin sets the origin position of the view's internal buffer,
|
|
|
|
// so the buffer starts to be printed from this point, which means that
|
|
|
|
// it is linked with the origin point of view. It can be used to
|
|
|
|
// implement Horizontal and Vertical scrolling with just incrementing
|
|
|
|
// or decrementing ox and oy.
|
2014-01-18 19:31:53 +08:00
|
|
|
func (v *View) SetOrigin(x, y int) error {
|
|
|
|
if x < 0 || y < 0 {
|
|
|
|
return errors.New("invalid point")
|
|
|
|
}
|
2014-01-17 06:01:53 +08:00
|
|
|
v.ox = x
|
|
|
|
v.oy = y
|
2014-01-18 19:31:53 +08:00
|
|
|
return nil
|
2014-01-17 06:01:53 +08:00
|
|
|
}
|
|
|
|
|
2014-01-20 00:03:52 +08:00
|
|
|
// Origin returns the origin position of the view.
|
2014-01-17 06:01:53 +08:00
|
|
|
func (v *View) Origin() (x, y int) {
|
|
|
|
return v.ox, v.oy
|
2014-01-14 03:15:39 +08:00
|
|
|
}
|
|
|
|
|
2014-01-20 00:03:52 +08:00
|
|
|
// Write appends a byte slice into the view's internal buffer. Because
|
|
|
|
// View implements the io.Writer interface, it can be passed as parameter
|
2014-01-20 00:44:36 +08:00
|
|
|
// of functions like fmt.Fprintf, fmt.Fprintln, io.Copy, etc. Clear must
|
2014-01-20 00:03:52 +08:00
|
|
|
// be called to clear the view's buffer.
|
2014-01-07 01:36:38 +08:00
|
|
|
func (v *View) Write(p []byte) (n int, err error) {
|
2015-02-04 00:09:21 +08:00
|
|
|
v.tainted = true
|
|
|
|
|
2015-01-29 23:19:25 +08:00
|
|
|
for _, ch := range bytes.Runes(p) {
|
|
|
|
switch ch {
|
|
|
|
case '\n':
|
|
|
|
v.lines = append(v.lines, nil)
|
|
|
|
case '\r':
|
|
|
|
nl := len(v.lines)
|
|
|
|
if nl > 0 {
|
|
|
|
v.lines[nl-1] = nil
|
|
|
|
} else {
|
2016-04-28 04:34:55 +08:00
|
|
|
v.lines = make([][]cell, 1)
|
2015-01-29 23:19:25 +08:00
|
|
|
}
|
|
|
|
default:
|
2016-04-28 04:34:55 +08:00
|
|
|
cells := v.parseInput(ch)
|
|
|
|
if cells == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-01-29 23:19:25 +08:00
|
|
|
nl := len(v.lines)
|
|
|
|
if nl > 0 {
|
2016-04-28 04:34:55 +08:00
|
|
|
v.lines[nl-1] = append(v.lines[nl-1], cells...)
|
2015-01-29 23:19:25 +08:00
|
|
|
} else {
|
2016-04-28 04:34:55 +08:00
|
|
|
v.lines = append(v.lines, cells)
|
2015-01-29 23:19:25 +08:00
|
|
|
}
|
|
|
|
}
|
2014-01-18 19:31:53 +08:00
|
|
|
}
|
|
|
|
return len(p), nil
|
2014-01-10 03:20:14 +08:00
|
|
|
}
|
|
|
|
|
2016-04-28 04:34:55 +08:00
|
|
|
// parseInput parses char by char the input written to the View. It returns nil
|
|
|
|
// while processing ESC sequences. Otherwise, it returns a cell slice that
|
|
|
|
// contains the processed data.
|
|
|
|
func (v *View) parseInput(ch rune) []cell {
|
|
|
|
cells := []cell{}
|
|
|
|
|
|
|
|
isEscape, err := v.ei.parseOne(ch)
|
|
|
|
if err != nil {
|
|
|
|
for _, r := range v.ei.runes() {
|
|
|
|
c := cell{
|
|
|
|
fgColor: v.FgColor,
|
|
|
|
bgColor: v.BgColor,
|
|
|
|
chr: r,
|
|
|
|
}
|
|
|
|
cells = append(cells, c)
|
|
|
|
}
|
|
|
|
v.ei.reset()
|
|
|
|
} else {
|
|
|
|
if isEscape {
|
|
|
|
return nil
|
|
|
|
}
|
2016-10-11 14:52:03 +08:00
|
|
|
c := cell{
|
|
|
|
fgColor: v.ei.curFgColor,
|
|
|
|
bgColor: v.ei.curBgColor,
|
|
|
|
chr: ch,
|
|
|
|
}
|
|
|
|
cells = append(cells, c)
|
2016-04-28 04:34:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return cells
|
|
|
|
}
|
|
|
|
|
2014-05-01 20:32:30 +08:00
|
|
|
// Read reads data into p. It returns the number of bytes read into p.
|
2014-05-03 21:20:46 +08:00
|
|
|
// At EOF, err will be io.EOF. Calling Read() after Rewind() makes the
|
|
|
|
// cache to be refreshed with the contents of the view.
|
2014-05-01 20:32:30 +08:00
|
|
|
func (v *View) Read(p []byte) (n int, err error) {
|
|
|
|
if v.readOffset == 0 {
|
|
|
|
v.readCache = v.Buffer()
|
|
|
|
}
|
|
|
|
if v.readOffset < len(v.readCache) {
|
|
|
|
n = copy(p, v.readCache[v.readOffset:])
|
|
|
|
v.readOffset += n
|
|
|
|
} else {
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rewind sets the offset for the next Read to 0, which also refresh the
|
|
|
|
// read cache.
|
|
|
|
func (v *View) Rewind() {
|
|
|
|
v.readOffset = 0
|
|
|
|
}
|
|
|
|
|
2014-01-20 00:03:52 +08:00
|
|
|
// draw re-draws the view's contents.
|
2014-01-16 07:28:16 +08:00
|
|
|
func (v *View) draw() error {
|
2014-01-10 03:20:14 +08:00
|
|
|
maxX, maxY := v.Size()
|
2015-02-02 05:49:07 +08:00
|
|
|
|
|
|
|
if v.Wrap {
|
2015-02-04 17:37:15 +08:00
|
|
|
if maxX == 0 {
|
|
|
|
return errors.New("X size of the view cannot be 0")
|
2015-02-02 05:49:07 +08:00
|
|
|
}
|
|
|
|
v.ox = 0
|
|
|
|
}
|
2015-02-04 00:09:21 +08:00
|
|
|
if v.tainted {
|
2015-02-04 01:29:38 +08:00
|
|
|
v.viewLines = nil
|
|
|
|
for i, line := range v.lines {
|
2015-02-04 00:09:21 +08:00
|
|
|
if v.Wrap {
|
2017-08-18 06:19:02 +08:00
|
|
|
if len(line) < maxX {
|
2015-02-04 01:29:38 +08:00
|
|
|
vline := viewLine{linesX: 0, linesY: i, line: line}
|
|
|
|
v.viewLines = append(v.viewLines, vline)
|
2015-02-04 00:49:15 +08:00
|
|
|
continue
|
2015-02-04 00:09:21 +08:00
|
|
|
} else {
|
2017-08-18 06:19:02 +08:00
|
|
|
for n := 0; n <= len(line); n += maxX {
|
|
|
|
if len(line[n:]) <= maxX {
|
|
|
|
vline := viewLine{linesX: n, linesY: i, line: line[n:]}
|
|
|
|
v.viewLines = append(v.viewLines, vline)
|
|
|
|
} else {
|
|
|
|
vline := viewLine{linesX: n, linesY: i, line: line[n : n+maxX]}
|
|
|
|
v.viewLines = append(v.viewLines, vline)
|
|
|
|
}
|
2015-02-04 00:09:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-02-04 01:29:38 +08:00
|
|
|
vline := viewLine{linesX: 0, linesY: i, line: line}
|
|
|
|
v.viewLines = append(v.viewLines, vline)
|
2015-02-02 05:49:07 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-04 00:09:21 +08:00
|
|
|
v.tainted = false
|
2015-02-02 05:49:07 +08:00
|
|
|
}
|
|
|
|
|
2015-02-04 01:29:38 +08:00
|
|
|
if v.Autoscroll && len(v.viewLines) > maxY {
|
|
|
|
v.oy = len(v.viewLines) - maxY
|
2015-02-02 05:49:07 +08:00
|
|
|
}
|
2014-01-14 03:21:49 +08:00
|
|
|
y := 0
|
2015-02-04 01:29:38 +08:00
|
|
|
for i, vline := range v.viewLines {
|
2014-01-17 06:01:53 +08:00
|
|
|
if i < v.oy {
|
2014-01-14 03:15:39 +08:00
|
|
|
continue
|
|
|
|
}
|
2015-02-02 05:49:07 +08:00
|
|
|
if y >= maxY {
|
|
|
|
break
|
|
|
|
}
|
2015-02-04 00:49:15 +08:00
|
|
|
x := 0
|
2016-04-28 04:34:55 +08:00
|
|
|
for j, c := range vline.line {
|
2015-02-04 00:49:15 +08:00
|
|
|
if j < v.ox {
|
|
|
|
continue
|
|
|
|
}
|
2015-02-02 05:49:07 +08:00
|
|
|
if x >= maxX {
|
|
|
|
break
|
2014-01-14 03:15:39 +08:00
|
|
|
}
|
2016-10-04 01:37:08 +08:00
|
|
|
|
|
|
|
fgColor := c.fgColor
|
|
|
|
if fgColor == ColorDefault {
|
|
|
|
fgColor = v.FgColor
|
|
|
|
}
|
|
|
|
bgColor := c.bgColor
|
|
|
|
if bgColor == ColorDefault {
|
|
|
|
bgColor = v.BgColor
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := v.setRune(x, y, c.chr, fgColor, bgColor); err != nil {
|
2015-02-02 05:49:07 +08:00
|
|
|
return err
|
2014-01-10 03:20:14 +08:00
|
|
|
}
|
2015-02-04 00:49:15 +08:00
|
|
|
x++
|
2014-01-10 03:20:14 +08:00
|
|
|
}
|
2014-01-14 03:21:49 +08:00
|
|
|
y++
|
2014-01-10 03:20:14 +08:00
|
|
|
}
|
|
|
|
return nil
|
2014-01-07 01:36:38 +08:00
|
|
|
}
|
2014-01-10 19:38:08 +08:00
|
|
|
|
2015-02-04 08:59:03 +08:00
|
|
|
// realPosition returns the position in the internal buffer corresponding to the
|
|
|
|
// point (x, y) of the view.
|
|
|
|
func (v *View) realPosition(vx, vy int) (x, y int, err error) {
|
|
|
|
vx = v.ox + vx
|
|
|
|
vy = v.oy + vy
|
|
|
|
|
|
|
|
if vx < 0 || vy < 0 {
|
|
|
|
return 0, 0, errors.New("invalid point")
|
|
|
|
}
|
|
|
|
|
2015-02-04 16:25:25 +08:00
|
|
|
if len(v.viewLines) == 0 {
|
|
|
|
return vx, vy, nil
|
|
|
|
}
|
|
|
|
|
2015-02-04 08:59:03 +08:00
|
|
|
if vy < len(v.viewLines) {
|
|
|
|
vline := v.viewLines[vy]
|
2015-02-04 17:37:15 +08:00
|
|
|
x = vline.linesX + vx
|
2015-02-04 08:59:03 +08:00
|
|
|
y = vline.linesY
|
|
|
|
} else {
|
|
|
|
vline := v.viewLines[len(v.viewLines)-1]
|
|
|
|
x = vx
|
|
|
|
y = vline.linesY + vy - len(v.viewLines) + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return x, y, nil
|
|
|
|
}
|
|
|
|
|
2014-01-20 00:03:52 +08:00
|
|
|
// Clear empties the view's internal buffer.
|
2014-01-10 19:38:08 +08:00
|
|
|
func (v *View) Clear() {
|
2015-02-04 00:09:21 +08:00
|
|
|
v.tainted = true
|
|
|
|
|
2014-01-18 19:31:53 +08:00
|
|
|
v.lines = nil
|
2016-11-15 04:56:17 +08:00
|
|
|
v.viewLines = nil
|
2016-11-14 08:12:37 +08:00
|
|
|
v.readOffset = 0
|
2014-01-14 03:15:39 +08:00
|
|
|
v.clearRunes()
|
|
|
|
}
|
|
|
|
|
2014-01-20 00:03:52 +08:00
|
|
|
// clearRunes erases all the cells in the view.
|
2014-01-14 03:15:39 +08:00
|
|
|
func (v *View) clearRunes() {
|
2014-01-10 19:38:08 +08:00
|
|
|
maxX, maxY := v.Size()
|
|
|
|
for x := 0; x < maxX; x++ {
|
|
|
|
for y := 0; y < maxY; y++ {
|
2014-01-21 06:28:08 +08:00
|
|
|
termbox.SetCell(v.x0+x+1, v.y0+y+1, ' ',
|
2014-05-03 21:20:46 +08:00
|
|
|
termbox.Attribute(v.FgColor), termbox.Attribute(v.BgColor))
|
2014-01-10 19:38:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-21 06:03:28 +08:00
|
|
|
|
2018-04-01 22:03:12 +08:00
|
|
|
// BufferLines returns the lines in the view's internal
|
|
|
|
// buffer.
|
|
|
|
func (v *View) BufferLines() []string {
|
|
|
|
lines := make([]string, len(v.lines))
|
|
|
|
for i, l := range v.lines {
|
|
|
|
str := lineType(l).String()
|
|
|
|
str = strings.Replace(str, "\x00", " ", -1)
|
|
|
|
lines[i] = str
|
|
|
|
}
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2014-02-03 09:04:30 +08:00
|
|
|
// Buffer returns a string with the contents of the view's internal
|
2016-01-30 09:36:10 +08:00
|
|
|
// buffer.
|
2014-02-03 09:04:30 +08:00
|
|
|
func (v *View) Buffer() string {
|
|
|
|
str := ""
|
|
|
|
for _, l := range v.lines {
|
2016-04-28 04:34:55 +08:00
|
|
|
str += lineType(l).String() + "\n"
|
2014-02-03 09:04:30 +08:00
|
|
|
}
|
|
|
|
return strings.Replace(str, "\x00", " ", -1)
|
|
|
|
}
|
|
|
|
|
2018-04-11 23:50:10 +08:00
|
|
|
// ViewBufferLines returns the lines in the view's internal
|
|
|
|
// buffer that is shown to the user.
|
|
|
|
func (v *View) ViewBufferLines() []string {
|
|
|
|
lines := make([]string, len(v.viewLines))
|
|
|
|
for i, l := range v.viewLines {
|
|
|
|
str := lineType(l.line).String()
|
|
|
|
str = strings.Replace(str, "\x00", " ", -1)
|
|
|
|
lines[i] = str
|
|
|
|
}
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2015-08-17 00:08:34 +08:00
|
|
|
// ViewBuffer returns a string with the contents of the view's buffer that is
|
2016-01-30 09:36:10 +08:00
|
|
|
// shown to the user.
|
2015-08-17 00:08:34 +08:00
|
|
|
func (v *View) ViewBuffer() string {
|
|
|
|
str := ""
|
|
|
|
for _, l := range v.viewLines {
|
2016-04-28 04:34:55 +08:00
|
|
|
str += lineType(l.line).String() + "\n"
|
2015-08-17 00:08:34 +08:00
|
|
|
}
|
|
|
|
return strings.Replace(str, "\x00", " ", -1)
|
|
|
|
}
|
|
|
|
|
2014-01-24 06:14:11 +08:00
|
|
|
// Line returns a string with the line of the view's internal buffer
|
2014-01-24 06:10:13 +08:00
|
|
|
// at the position corresponding to the point (x, y).
|
|
|
|
func (v *View) Line(y int) (string, error) {
|
2015-02-04 08:59:03 +08:00
|
|
|
_, y, err := v.realPosition(0, y)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-01-24 06:10:13 +08:00
|
|
|
|
|
|
|
if y < 0 || y >= len(v.lines) {
|
|
|
|
return "", errors.New("invalid point")
|
|
|
|
}
|
2016-04-28 04:34:55 +08:00
|
|
|
|
|
|
|
return lineType(v.lines[y]).String(), nil
|
2014-01-24 06:10:13 +08:00
|
|
|
}
|
|
|
|
|
2014-01-24 06:14:11 +08:00
|
|
|
// Word returns a string with the word of the view's internal buffer
|
2014-01-24 06:10:13 +08:00
|
|
|
// at the position corresponding to the point (x, y).
|
|
|
|
func (v *View) Word(x, y int) (string, error) {
|
2015-02-04 08:59:03 +08:00
|
|
|
x, y, err := v.realPosition(x, y)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-01-24 06:10:13 +08:00
|
|
|
|
2015-02-23 07:34:41 +08:00
|
|
|
if x < 0 || y < 0 || y >= len(v.lines) || x >= len(v.lines[y]) {
|
2014-01-24 06:10:13 +08:00
|
|
|
return "", errors.New("invalid point")
|
|
|
|
}
|
2016-04-28 04:34:55 +08:00
|
|
|
|
|
|
|
str := lineType(v.lines[y]).String()
|
|
|
|
|
|
|
|
nl := strings.LastIndexFunc(str[:x], indexFunc)
|
2014-01-24 06:10:13 +08:00
|
|
|
if nl == -1 {
|
|
|
|
nl = 0
|
|
|
|
} else {
|
|
|
|
nl = nl + 1
|
|
|
|
}
|
2016-04-28 04:34:55 +08:00
|
|
|
nr := strings.IndexFunc(str[x:], indexFunc)
|
2014-01-24 06:10:13 +08:00
|
|
|
if nr == -1 {
|
2016-04-28 04:34:55 +08:00
|
|
|
nr = len(str)
|
2014-01-24 06:10:13 +08:00
|
|
|
} else {
|
|
|
|
nr = nr + x
|
|
|
|
}
|
2016-04-28 04:34:55 +08:00
|
|
|
return string(str[nl:nr]), nil
|
2014-01-24 06:10:13 +08:00
|
|
|
}
|
2014-01-24 06:42:52 +08:00
|
|
|
|
|
|
|
// indexFunc allows to split lines by words taking into account spaces
|
2015-02-04 00:09:21 +08:00
|
|
|
// and 0.
|
2014-01-24 06:42:52 +08:00
|
|
|
func indexFunc(r rune) bool {
|
|
|
|
return r == ' ' || r == 0
|
|
|
|
}
|