elvish/edit/writer.go

454 lines
11 KiB
Go
Raw Normal View History

package edit
import (
"bytes"
2014-01-16 09:24:14 +08:00
"fmt"
"os"
2013-12-31 11:31:59 +08:00
"strings"
"unicode"
"unicode/utf8"
2014-02-10 11:33:53 +08:00
"github.com/xiaq/elvish/edit/tty"
"github.com/xiaq/elvish/util"
)
// cell is an indivisible unit on the screen. It is not necessarily 1 column
// wide.
type cell struct {
rune
width byte
2014-01-16 09:24:14 +08:00
attr string
}
// pos is the position within a buffer.
type pos struct {
line, col int
}
2014-01-08 16:26:03 +08:00
// buffer reflects a continuous range of lines on the terminal. The Unix
// terminal API provides only awkward ways of querying the terminal buffer, so
// we keep an internal reflection and do one-way synchronizations (buffer ->
// terminal, and not the other way around). This requires us to exactly match
// the terminal's idea of the width of characters (wcwidth) and where to
// insert soft carriage returns, so there could be bugs.
type buffer struct {
width, col, indent int
2014-01-16 09:24:14 +08:00
newlineWhenFull bool
cells [][]cell // cells reflect len(cells) lines on the terminal.
dot pos // dot is what the user perceives as the cursor.
}
func newBuffer(width int) *buffer {
return &buffer{width: width, cells: [][]cell{make([]cell, 0, width)}}
}
func (b *buffer) appendCell(c cell) {
n := len(b.cells)
b.cells[n-1] = append(b.cells[n-1], c)
b.col += int(c.width)
}
func (b *buffer) appendLine() {
b.cells = append(b.cells, make([]cell, 0, b.width))
b.col = 0
}
func (b *buffer) newline() {
b.appendLine()
if b.indent > 0 {
for i := 0; i < b.indent; i++ {
b.appendCell(cell{rune: ' ', width: 1})
}
}
}
2014-01-09 20:16:27 +08:00
func (b *buffer) extend(b2 *buffer) {
if b2 != nil && b2.cells != nil {
b.cells = append(b.cells, b2.cells...)
b.col = b2.col
}
}
// write appends a single rune to a buffer.
func (b *buffer) write(r rune, attr string) {
if r == '\n' {
b.newline()
return
} else if !unicode.IsPrint(r) {
// BUG(xiaq): buffer.write drops unprintable runes silently
return
}
wd := wcwidth(r)
c := cell{r, byte(wd), attr}
2014-01-16 09:24:14 +08:00
if b.col+wd > b.width {
b.newline()
b.appendCell(c)
} else {
b.appendCell(c)
if b.col == b.width && b.newlineWhenFull {
b.newline()
}
}
}
func (b *buffer) writes(s string, attr string) {
for _, r := range s {
b.write(r, attr)
}
}
2014-01-09 20:23:54 +08:00
func (b *buffer) writePadding(w int, attr string) {
b.writes(strings.Repeat(" ", w), attr)
}
func (b *buffer) line() int {
return len(b.cells) - 1
}
func (b *buffer) cursor() pos {
return pos{len(b.cells) - 1, b.col}
}
func (b *buffer) trimToLines(low, high int) {
for i := 0; i < low; i++ {
b.cells[i] = nil
}
for i := high; i < len(b.cells); i++ {
b.cells[i] = nil
}
b.cells = b.cells[low:high]
b.dot.line -= low
}
// writer is the part of an Editor responsible for keeping the status of and
// updating the screen.
type writer struct {
2014-01-16 09:24:14 +08:00
file *os.File
2014-01-09 20:16:27 +08:00
oldBuf *buffer
}
2013-12-26 20:39:59 +08:00
func newWriter(f *os.File) *writer {
writer := &writer{file: f, oldBuf: newBuffer(0)}
return writer
}
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
fmt.Fprintf(buf, "\033[%dB", to.line-from.line)
} else if from.line > to.line {
// move up
fmt.Fprintf(buf, "\033[%dA", from.line-to.line)
}
2014-02-08 22:36:05 +08:00
fmt.Fprintf(buf, "\033[%dG", to.col+1)
return buf.Bytes()
}
2013-10-03 21:44:37 +08:00
// commitBuffer updates the terminal display to reflect current buffer.
2014-01-09 20:16:27 +08:00
// TODO Instead of erasing w.oldBuf entirely and then draw buf, compute a
// delta between w.oldBuf and buf
func (w *writer) commitBuffer(buf *buffer) 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 := ""
2014-01-09 20:18:58 +08:00
for i, line := range buf.cells {
if i > 0 {
bytesBuf.WriteString("\n")
}
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")
}
cursor := buf.cursor()
if cursor.col == buf.width {
cursor.col--
}
bytesBuf.Write(deltaPos(cursor, buf.dot))
_, err := w.file.Write(bytesBuf.Bytes())
if err != nil {
return err
}
2014-01-09 20:16:27 +08:00
w.oldBuf = buf
return nil
}
2014-01-16 09:24:14 +08:00
func lines(bufs ...*buffer) (l int) {
for _, buf := range bufs {
if buf != nil {
l += len(buf.cells)
}
}
return
}
2014-03-06 17:27:03 +08:00
// findWindow finds a window of lines around the selected line in a total
// number of height lines, that is at most max lines.
func findWindow(height, selected, max int) (low, high int) {
if height > max {
low = selected - max/2
high = low + max
switch {
case low < 0:
// Near top of the list, move the window down
low = 0
high = low + max
case high > height:
// Near bottom of the list, move the window down
high = height
low = high - max
}
return
} else {
return 0, height
}
}
func trimToWindow(s []string, selected, max int) ([]string, int) {
low, high := findWindow(len(s), selected, max)
return s[low:high], low
}
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-08 16:11:38 +08:00
func (w *writer) refresh(bs *editorState) error {
winsize := tty.GetWinsize(int(w.file.Fd()))
width, height := int(winsize.Col), int(winsize.Row)
2014-01-09 20:16:27 +08:00
var bufLine, bufMode, bufTips, bufListing, buf *buffer
2014-01-09 20:16:27 +08:00
// bufLine
b := newBuffer(width)
bufLine = b
b.newlineWhenFull = true
b.writes(bs.prompt, attrForPrompt)
2014-01-16 09:24:14 +08:00
if b.line() == 0 && b.col*2 < b.width {
b.indent = b.col
}
2013-12-27 09:13:56 +08:00
// i keeps track of number of bytes written.
i := 0
if bs.dot == 0 {
b.dot = b.cursor()
}
comp := bs.completion
2014-01-03 14:21:54 +08:00
var suppress = false
2014-02-09 19:53:21 +08:00
tokens:
for _, token := range bs.tokens {
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 {
b.write(r, attrForType[token.Typ])
2013-12-27 09:37:50 +08:00
}
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 {
attr := attrForType[comp.typ]
if part.completed {
attr += attrForCompleted
}
b.writes(part.text, attr)
}
2014-01-03 14:21:54 +08:00
suppress = true
}
2014-02-10 11:33:53 +08:00
if bs.mode == modeHistory && i == len(bs.history.prefix) {
2014-02-09 19:53:21 +08:00
break tokens
}
if bs.dot == i {
b.dot = b.cursor()
}
}
}
2014-02-10 11:33:53 +08:00
if bs.mode == modeHistory {
// Put the rest of current history, position the cursor at the
// end of the line, and finish writing
h := bs.history
b.writes(h.items[h.current][len(h.prefix):], attrForCompletedHistory)
b.dot = b.cursor()
}
2014-01-04 22:45:35 +08:00
// Write rprompt
padding := b.width - b.col - wcwidths(bs.rprompt)
2014-01-04 22:45:35 +08:00
if padding >= 1 {
b.newlineWhenFull = false
2014-01-09 20:23:54 +08:00
b.writePadding(padding, "")
b.writes(bs.rprompt, attrForRprompt)
2014-01-04 22:45:35 +08:00
}
2014-01-09 20:16:27 +08:00
// bufMode
2014-01-31 20:01:07 +08:00
if bs.mode != modeInsert {
2014-01-09 20:16:27 +08:00
b := newBuffer(width)
bufMode = b
text := ""
2014-01-03 14:47:07 +08:00
switch bs.mode {
2014-01-31 20:01:07 +08:00
case modeCommand:
text = "Command"
b.writes(trimWcwidth("Command", width), attrForMode)
2014-03-05 11:22:09 +08:00
case modeCompletion:
text = fmt.Sprintf("Completing %s", bs.line[comp.start:comp.end])
case modeNavigation:
text = "Navigating"
2014-02-09 19:53:21 +08:00
case modeHistory:
text = fmt.Sprintf("History #%d", bs.history.current)
2014-01-03 14:47:07 +08:00
}
b.writes(trimWcwidth(text, width), attrForMode)
2014-01-03 14:47:07 +08:00
}
2014-01-09 20:16:27 +08:00
// bufTips
2014-01-09 23:51:35 +08:00
// TODO tips is assumed to contain no newlines.
if len(bs.tips) > 0 {
2014-01-09 20:16:27 +08:00
b := newBuffer(width)
bufTips = b
2014-01-09 23:51:35 +08:00
b.writes(trimWcwidth(strings.Join(bs.tips, ", "), width), attrForTip)
}
2014-03-06 17:27:03 +08:00
listingHeight := 0
// Trim lines and determine the maximum height for bufListing
switch {
case height >= lines(bufLine, bufMode, bufTips):
listingHeight = height - lines(bufLine, bufMode, bufTips)
case height >= lines(bufLine, bufTips):
bufMode, bufListing = nil, nil
case height >= lines(bufLine):
bufTips, bufMode, bufListing = nil, nil, nil
case height >= 1:
bufTips, bufMode, bufListing = nil, nil, nil
dotLine := bufLine.dot.line
bufLine.trimToLines(dotLine+1-height, dotLine+1)
default:
bufLine, bufTips, bufMode, bufListing = nil, nil, nil, nil
}
// Render bufListing under the maximum height constraint
nav := bs.navigation
if listingHeight > 0 && comp != nil || nav != nil {
2014-01-09 20:16:27 +08:00
b := newBuffer(width)
bufListing = b
2014-03-06 17:27:03 +08:00
// Completion listing
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
}
}
2014-03-06 17:27:03 +08:00
cols := (b.width + colMargin) / (colWidth + colMargin)
if cols == 0 {
cols = 1
2014-01-09 20:16:27 +08:00
}
2014-03-06 17:27:03 +08:00
lines := util.CeilDiv(len(cands), cols)
bs.completionLines = lines
// Determine the window to show.
low, high := findWindow(lines, comp.current%lines, listingHeight)
2014-03-06 17:27:03 +08:00
for i := low; i < high; i++ {
if i > low {
2014-03-06 17:27:03 +08:00
b.newline()
}
2014-03-06 17:27:03 +08:00
for j := 0; j < cols; j++ {
k := j*lines + i
if k >= len(cands) {
continue
}
attr := ""
2014-03-06 17:27:03 +08:00
if k == comp.current {
attr = attrForCurrentCompletion
}
text := cands[k].text
b.writes(text, attr)
b.writePadding(colWidth-wcwidths(text), attr)
b.writePadding(colMargin, "")
}
}
2013-12-24 19:01:01 +08:00
}
2014-03-06 17:27:03 +08:00
// Navigation listing
if nav != nil {
b := newBuffer(width)
bufListing = b
2014-03-06 17:43:34 +08:00
filenames, low := trimToWindow(nav.current.names, nav.current.selected, listingHeight)
parentFilenames, parentLow := trimToWindow(nav.parent.names, nav.parent.selected, listingHeight)
2014-03-06 17:27:03 +08:00
// TODO(xiaq): When laying out the navigation listing, determine
// the width of two columns more intelligently instead of
// allocating half of screen for each. Maybe the algorithm used by
// ranger could be pirated.
colMargin := 1
parentWidth := (width + colMargin) / 2
currentWidth := width - colMargin - parentWidth
for i := 0; i < len(filenames) || i < len(parentFilenames); i++ {
if i > 0 {
b.newline()
}
text, attr := "", ""
if i < len(parentFilenames) {
text = parentFilenames[i]
}
2014-03-06 17:43:34 +08:00
if i+parentLow == nav.parent.selected {
attr = attrForSelectedFile
}
2014-03-06 17:27:03 +08:00
b.writes(trimWcwidth(text, parentWidth), attr)
b.writePadding(parentWidth-wcwidths(text), attr)
b.writePadding(colMargin, "")
if i < len(filenames) {
attr := ""
2014-03-06 17:43:34 +08:00
if i+low == nav.current.selected {
2014-03-06 17:27:03 +08:00
attr = attrForSelectedFile
}
text := filenames[i]
b.writes(trimWcwidth(text, currentWidth), attr)
b.writePadding(currentWidth-wcwidths(text), attr)
}
}
}
2014-03-06 17:27:03 +08:00
// Trim bufListing.
// XXX This algorithm only works for completion listing.
}
// Combine buffers (reusing bufLine)
2014-01-09 20:16:27 +08:00
buf = bufLine
buf.extend(bufMode)
buf.extend(bufTips)
buf.extend(bufListing)
2014-01-09 20:16:27 +08:00
return w.commitBuffer(buf)
}