elvish/edit/writer.go

282 lines
5.9 KiB
Go
Raw Normal View History

package edit
import (
"os"
"fmt"
"bytes"
2013-12-31 11:31:59 +08:00
"strings"
"unicode"
"unicode/utf8"
"./tty"
"../util"
)
// cell is an indivisible unit on the screen. It is not necessarily 1 column
// wide.
type cell struct {
rune
width byte
attr string
}
// pos is the position within a buffer.
type pos struct {
line, col int
}
2013-10-03 22:10:57 +08:00
// buffer is an internal reflection of the last few lines of the terminal, the
// part the line editor is concerned with.
type buffer struct {
2013-10-03 22:10:57 +08:00
cells [][]cell // cells reflect the last len(cells) lines of the terminal.
dot pos // dot is what the user perceives as the cursor.
}
func newBuffer(w int) *buffer {
return &buffer{cells: [][]cell{make([]cell, 0, w)}}
}
func (b *buffer) appendCell(c cell) {
n := len(b.cells)
b.cells[n-1] = append(b.cells[n-1], c)
}
func (b *buffer) appendLine(w int) {
b.cells = append(b.cells, make([]cell, 0, w))
}
// writer is the part of an Editor responsible for keeping the status of and
// updating the screen.
type writer struct {
file *os.File
oldBuf, buf *buffer
2013-10-03 21:44:37 +08:00
// Fields below are used when refreshing.
width, indent int
cursor pos
currentAttr string
}
2013-12-26 20:39:59 +08:00
func newWriter(f *os.File) *writer {
writer := &writer{file: f, oldBuf: newBuffer(0)}
return writer
}
func (w *writer) startBuffer() {
fd := int(w.file.Fd())
w.width = int(tty.GetWinsize(fd).Col)
2013-10-03 22:10:57 +08:00
w.indent = 0
w.cursor = pos{}
w.currentAttr = ""
2013-10-03 22:10:57 +08:00
w.buf = newBuffer(w.width)
}
2013-10-03 21:44:37 +08:00
// deltaPos calculates the escape sequence needed to move the cursor from one
// position to another.
func deltaPos(from, to pos) []byte {
buf := new(bytes.Buffer)
if from.line < to.line {
// move down
buf.WriteString(fmt.Sprintf("\033[%dB", to.line - from.line))
} else if from.line > to.line {
// move up
buf.WriteString(fmt.Sprintf("\033[%dA", from.line - to.line))
}
if from.col < to.col {
// move right
buf.WriteString(fmt.Sprintf("\033[%dC", to.col - from.col))
} else if from.col > to.col {
// move left
buf.WriteString(fmt.Sprintf("\033[%dD", from.col - to.col))
}
return buf.Bytes()
}
2013-10-03 21:44:37 +08:00
// commitBuffer updates the terminal display to reflect current buffer.
// TODO Instead of erasing w.oldBuf entirely and then draw w.buf, compute a
// delta between w.oldBuf and w.buf
func (w *writer) commitBuffer() error {
bytesBuf := new(bytes.Buffer)
pLine := w.oldBuf.dot.line
if pLine > 0 {
fmt.Fprintf(bytesBuf, "\033[%dA", pLine)
}
bytesBuf.WriteString("\r\033[J")
attr := ""
for _, line := range w.buf.cells {
for _, c := range line {
if c.width > 0 && c.attr != attr {
fmt.Fprintf(bytesBuf, "\033[m\033[%sm", c.attr)
attr = c.attr
}
bytesBuf.WriteString(string(c.rune))
}
}
if attr != "" {
bytesBuf.WriteString("\033[m")
}
bytesBuf.Write(deltaPos(w.cursor, w.buf.dot))
_, err := w.file.Write(bytesBuf.Bytes())
if err != nil {
return err
}
w.oldBuf = w.buf
return nil
}
func (w *writer) appendToLine(c cell) {
w.buf.appendCell(c)
w.cursor.col += int(c.width)
}
func (w *writer) newline() {
w.buf.appendCell(cell{rune: '\n'})
w.buf.appendLine(w.width)
w.cursor.line++
w.cursor.col = 0
if w.indent > 0 {
for i := 0; i < w.indent; i++ {
w.appendToLine(cell{rune: ' ', width: 1})
}
}
}
2013-10-03 21:44:37 +08:00
// write appends a single rune to w.buf.
func (w *writer) write(r rune) {
if r == '\n' {
w.newline()
return
} else if !unicode.IsPrint(r) {
// XXX unprintable runes are dropped silently
return
}
wd := wcwidth(r)
c := cell{r, byte(wd), w.currentAttr}
if w.cursor.col + wd > w.width {
w.newline()
w.appendToLine(c)
} else if w.cursor.col + wd == w.width {
w.appendToLine(c)
w.newline()
} else {
w.appendToLine(c)
}
}
2013-12-31 11:31:59 +08:00
func (w *writer) writes(s string) {
for _, r := range s {
w.write(r)
}
}
2013-10-03 22:10:57 +08:00
// refresh redraws the line editor. The dot is passed as an index into text;
// the corresponding position will be calculated.
func (w *writer) refresh(bs *bufferState) error {
w.startBuffer()
w.writes(bs.prompt)
if w.cursor.col * 2 < w.width {
w.indent = w.cursor.col
}
2013-12-27 09:13:56 +08:00
// i keeps track of number of bytes written.
i := 0
if bs.dot == 0 {
w.buf.dot = w.cursor
}
comp := bs.completion
2014-01-03 14:21:54 +08:00
var suppress = false
for _, token := range bs.tokens {
w.currentAttr = attrForType[token.Typ]
for _, r := range token.Val {
if suppress && i < comp.end {
2013-12-27 09:37:50 +08:00
// Silence the part that is being completed
} else {
w.write(r)
}
i += utf8.RuneLen(r)
if comp != nil && comp.current != -1 && i == comp.start {
// Put the current candidate and instruct text up to comp.end
// to be suppressed. The cursor should be placed correctly
// (i.e. right after the candidate)
for _, part := range comp.candidates[comp.current].parts {
w.currentAttr = attrForType[comp.typ]
if part.completed {
w.currentAttr += attrForCompleted
}
2013-12-31 11:31:59 +08:00
w.writes(part.text)
}
2014-01-03 14:21:54 +08:00
suppress = true
}
if bs.dot == i {
w.buf.dot = w.cursor
}
}
}
2013-12-24 19:01:01 +08:00
w.indent = 0
2014-01-03 14:47:07 +08:00
if bs.mode != ModeInsert {
w.newline()
w.currentAttr = attrForMode
switch bs.mode {
case ModeCommand:
w.writes("-- COMMAND --")
case ModeCompleting:
w.writes("-- COMPLETING --")
}
}
2013-09-23 16:34:00 +08:00
w.currentAttr = ""
if len(bs.tip) > 0 {
w.newline()
w.writes(bs.tip)
}
if comp != nil {
// Layout candidates in multiple columns
cands := comp.candidates
// First decide the shape (# of rows and columns)
colWidth := 0
colMargin := 2
for _, cand := range cands {
width := wcwidths(cand.text)
if colWidth < width {
colWidth = width
}
}
cols := (w.width + colMargin) / (colWidth + colMargin)
lines := util.CeilDiv(len(cands), cols)
for i := 0; i < lines; i++ {
w.newline()
for j := 0; j < cols; j++ {
k := j * lines + i
if k >= len(cands) {
continue
}
if k == comp.current {
w.currentAttr = attrForCurrentCompletion
} else {
w.currentAttr = ""
}
2013-12-31 11:31:59 +08:00
text := cands[k].text
w.writes(text)
w.writes(strings.Repeat(" ", colWidth - wcwidths(text)))
w.currentAttr = ""
2013-12-31 11:31:59 +08:00
w.writes(strings.Repeat(" ", colMargin))
}
2013-12-24 19:01:01 +08:00
}
}
return w.commitBuffer()
}