2013-09-19 18:36:47 +08:00
|
|
|
package edit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"fmt"
|
|
|
|
"bytes"
|
2013-12-31 11:31:59 +08:00
|
|
|
"strings"
|
2013-10-03 21:29:46 +08:00
|
|
|
"unicode"
|
2013-10-03 21:52:46 +08:00
|
|
|
"unicode/utf8"
|
2013-09-19 18:36:47 +08:00
|
|
|
"./tty"
|
2013-12-31 11:28:05 +08:00
|
|
|
"../util"
|
2013-09-19 18:36:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// cell is an indivisible unit on the screen. It is not necessarily 1 column
|
|
|
|
// wide.
|
|
|
|
type cell struct {
|
|
|
|
rune
|
|
|
|
width byte
|
|
|
|
attr string
|
|
|
|
}
|
|
|
|
|
2013-09-20 09:28:32 +08:00
|
|
|
// 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.
|
2013-09-20 00:26:19 +08:00
|
|
|
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.
|
2013-09-20 00:26:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func newBuffer(w int) *buffer {
|
2013-09-20 09:28:32 +08:00
|
|
|
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))
|
2013-09-20 00:26:19 +08:00
|
|
|
}
|
2013-09-19 18:36:47 +08:00
|
|
|
|
|
|
|
// writer is the part of an Editor responsible for keeping the status of and
|
|
|
|
// updating the screen.
|
|
|
|
type writer struct {
|
2013-09-21 10:32:53 +08:00
|
|
|
file *os.File
|
2013-09-20 00:26:19 +08:00
|
|
|
oldBuf, buf *buffer
|
2013-10-03 21:44:37 +08:00
|
|
|
// Fields below are used when refreshing.
|
2013-09-20 09:28:32 +08:00
|
|
|
width, indent int
|
|
|
|
cursor pos
|
2013-09-19 18:36:47 +08:00
|
|
|
currentAttr string
|
|
|
|
}
|
|
|
|
|
2013-12-26 20:39:59 +08:00
|
|
|
func newWriter(f *os.File) *writer {
|
|
|
|
writer := &writer{file: f, oldBuf: newBuffer(0)}
|
2013-09-19 18:36:47 +08:00
|
|
|
return writer
|
|
|
|
}
|
|
|
|
|
2013-09-21 10:32:53 +08:00
|
|
|
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{}
|
2013-09-19 18:36:47 +08:00
|
|
|
w.currentAttr = ""
|
2013-10-03 22:10:57 +08:00
|
|
|
w.buf = newBuffer(w.width)
|
2013-09-19 18:36:47 +08:00
|
|
|
}
|
|
|
|
|
2013-10-03 21:44:37 +08:00
|
|
|
// deltaPos calculates the escape sequence needed to move the cursor from one
|
|
|
|
// position to another.
|
2013-09-20 09:28:32 +08:00
|
|
|
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
|
2013-09-21 10:32:53 +08:00
|
|
|
func (w *writer) commitBuffer() error {
|
2013-09-19 18:36:47 +08:00
|
|
|
bytesBuf := new(bytes.Buffer)
|
|
|
|
|
2013-10-03 21:57:44 +08:00
|
|
|
pLine := w.oldBuf.dot.line
|
2013-09-20 09:28:32 +08:00
|
|
|
if pLine > 0 {
|
|
|
|
fmt.Fprintf(bytesBuf, "\033[%dA", pLine)
|
2013-09-19 18:36:47 +08:00
|
|
|
}
|
|
|
|
bytesBuf.WriteString("\r\033[J")
|
|
|
|
|
|
|
|
attr := ""
|
2013-09-20 00:26:19 +08:00
|
|
|
for _, line := range w.buf.cells {
|
2013-09-19 18:36:47 +08:00
|
|
|
for _, c := range line {
|
|
|
|
if c.width > 0 && c.attr != attr {
|
2013-10-28 10:37:26 +08:00
|
|
|
fmt.Fprintf(bytesBuf, "\033[m\033[%sm", c.attr)
|
2013-09-19 18:36:47 +08:00
|
|
|
attr = c.attr
|
|
|
|
}
|
|
|
|
bytesBuf.WriteString(string(c.rune))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if attr != "" {
|
|
|
|
bytesBuf.WriteString("\033[m")
|
|
|
|
}
|
2013-10-03 21:57:44 +08:00
|
|
|
bytesBuf.Write(deltaPos(w.cursor, w.buf.dot))
|
2013-09-20 09:28:32 +08:00
|
|
|
|
2013-09-21 10:32:53 +08:00
|
|
|
_, err := w.file.Write(bytesBuf.Bytes())
|
2013-09-19 18:36:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.oldBuf = w.buf
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writer) appendToLine(c cell) {
|
2013-09-20 09:28:32 +08:00
|
|
|
w.buf.appendCell(c)
|
|
|
|
w.cursor.col += int(c.width)
|
2013-09-19 18:36:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writer) newline() {
|
2013-09-20 09:28:32 +08:00
|
|
|
w.buf.appendCell(cell{rune: '\n'})
|
|
|
|
w.buf.appendLine(w.width)
|
|
|
|
|
|
|
|
w.cursor.line++
|
|
|
|
w.cursor.col = 0
|
2013-09-19 18:36:47 +08:00
|
|
|
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.
|
2013-09-19 18:36:47 +08:00
|
|
|
func (w *writer) write(r rune) {
|
2013-10-03 21:29:46 +08:00
|
|
|
if r == '\n' {
|
|
|
|
w.newline()
|
|
|
|
return
|
|
|
|
} else if !unicode.IsPrint(r) {
|
|
|
|
// XXX unprintable runes are dropped silently
|
|
|
|
return
|
|
|
|
}
|
2013-09-19 18:36:47 +08:00
|
|
|
wd := wcwidth(r)
|
|
|
|
c := cell{r, byte(wd), w.currentAttr}
|
|
|
|
|
2013-09-20 09:28:32 +08:00
|
|
|
if w.cursor.col + wd > w.width {
|
2013-09-19 18:36:47 +08:00
|
|
|
w.newline()
|
|
|
|
w.appendToLine(c)
|
2013-09-20 09:28:32 +08:00
|
|
|
} else if w.cursor.col + wd == w.width {
|
2013-09-19 18:36:47 +08:00
|
|
|
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.
|
2014-01-03 14:21:25 +08:00
|
|
|
func (w *writer) refresh(bs *bufferState) error {
|
2013-09-21 10:32:53 +08:00
|
|
|
w.startBuffer()
|
2013-09-19 18:36:47 +08:00
|
|
|
|
2014-01-03 14:21:25 +08:00
|
|
|
w.writes(bs.prompt)
|
2013-09-19 18:36:47 +08:00
|
|
|
|
2013-09-20 09:28:32 +08:00
|
|
|
if w.cursor.col * 2 < w.width {
|
|
|
|
w.indent = w.cursor.col
|
2013-09-19 18:36:47 +08:00
|
|
|
}
|
|
|
|
|
2013-12-27 09:13:56 +08:00
|
|
|
// i keeps track of number of bytes written.
|
2013-10-03 21:37:07 +08:00
|
|
|
i := 0
|
2014-01-03 14:21:25 +08:00
|
|
|
if bs.dot == 0 {
|
2013-10-03 21:57:44 +08:00
|
|
|
w.buf.dot = w.cursor
|
2013-10-03 21:37:07 +08:00
|
|
|
}
|
2013-11-19 20:28:45 +08:00
|
|
|
|
2014-01-03 14:21:25 +08:00
|
|
|
comp := bs.completion
|
2014-01-03 14:21:54 +08:00
|
|
|
var suppress = false
|
2014-01-03 14:21:25 +08:00
|
|
|
for _, token := range bs.tokens {
|
2013-09-19 18:36:47 +08:00
|
|
|
w.currentAttr = attrForType[token.Typ]
|
|
|
|
for _, r := range token.Val {
|
2013-12-30 16:18:24 +08:00
|
|
|
if suppress && i < comp.end {
|
2013-12-27 09:37:50 +08:00
|
|
|
// Silence the part that is being completed
|
|
|
|
} else {
|
|
|
|
w.write(r)
|
|
|
|
}
|
2013-10-03 21:52:46 +08:00
|
|
|
i += utf8.RuneLen(r)
|
2013-12-30 16:18:24 +08:00
|
|
|
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)
|
2013-12-30 16:18:24 +08:00
|
|
|
}
|
2014-01-03 14:21:54 +08:00
|
|
|
suppress = true
|
2013-12-30 16:18:24 +08:00
|
|
|
}
|
2014-01-03 14:21:25 +08:00
|
|
|
if bs.dot == i {
|
2013-10-03 21:57:44 +08:00
|
|
|
w.buf.dot = w.cursor
|
2013-10-03 21:37:07 +08:00
|
|
|
}
|
2013-09-19 18:36:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 = ""
|
2014-01-03 14:21:25 +08:00
|
|
|
if len(bs.tip) > 0 {
|
2013-09-20 00:26:19 +08:00
|
|
|
w.newline()
|
2014-01-03 14:21:25 +08:00
|
|
|
w.writes(bs.tip)
|
2013-09-20 00:26:19 +08:00
|
|
|
}
|
|
|
|
|
2013-12-25 16:43:52 +08:00
|
|
|
if comp != nil {
|
2013-12-31 11:28:05 +08:00
|
|
|
// 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
|
2013-12-25 16:43:52 +08:00
|
|
|
}
|
2013-12-31 11:28:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cols := (w.width + colMargin) / (colWidth + colMargin)
|
|
|
|
lines := util.CeilDiv(len(cands), cols)
|
|
|
|
|
|
|
|
for i := 0; i < lines; i++ {
|
2013-12-25 16:43:52 +08:00
|
|
|
w.newline()
|
2013-12-31 11:28:05 +08:00
|
|
|
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)))
|
2013-12-31 11:28:05 +08:00
|
|
|
w.currentAttr = ""
|
2013-12-31 11:31:59 +08:00
|
|
|
w.writes(strings.Repeat(" ", colMargin))
|
2013-12-25 16:43:52 +08:00
|
|
|
}
|
2013-12-24 19:01:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-21 10:32:53 +08:00
|
|
|
return w.commitBuffer()
|
2013-09-19 18:36:47 +08:00
|
|
|
}
|