2019-04-07 22:37:28 -04:00
|
|
|
// Copyright 2019 Google Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package textinput
|
|
|
|
|
|
|
|
// editor.go contains data types that edit the content of the text input field.
|
|
|
|
|
|
|
|
import (
|
2019-04-07 23:31:41 -04:00
|
|
|
"fmt"
|
2019-04-17 23:59:33 -04:00
|
|
|
"strings"
|
2019-04-08 23:32:00 -04:00
|
|
|
|
2019-04-10 23:42:49 -04:00
|
|
|
"github.com/mum4k/termdash/internal/numbers"
|
2019-04-08 23:32:00 -04:00
|
|
|
"github.com/mum4k/termdash/internal/runewidth"
|
2019-04-07 22:37:28 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// fieldData are the data currently present inside the text input field.
|
|
|
|
type fieldData []rune
|
|
|
|
|
|
|
|
// String implements fmt.Stringer.
|
|
|
|
func (fd fieldData) String() string {
|
2019-04-17 23:59:33 -04:00
|
|
|
var b strings.Builder
|
2019-04-07 22:37:28 -04:00
|
|
|
for _, r := range fd {
|
|
|
|
b.WriteRune(r)
|
|
|
|
}
|
2019-04-17 23:59:33 -04:00
|
|
|
return fmt.Sprintf("%q", b.String())
|
2019-04-07 22:37:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// insertAt inserts rune at the specified index.
|
|
|
|
func (fd *fieldData) insertAt(idx int, r rune) {
|
|
|
|
*fd = append(
|
|
|
|
(*fd)[:idx],
|
|
|
|
append(fieldData{r}, (*fd)[idx:]...)...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteAt deletes rune at the specified index.
|
|
|
|
func (fd *fieldData) deleteAt(idx int) {
|
|
|
|
*fd = append((*fd)[:idx], (*fd)[idx+1:]...)
|
|
|
|
}
|
2019-04-07 23:31:41 -04:00
|
|
|
|
2019-04-10 00:09:46 -04:00
|
|
|
// cellsBefore given an endIdx calculates startIdx that results in range that
|
|
|
|
// will take at most the provided number of cells to print on the screen.
|
|
|
|
func (fd *fieldData) cellsBefore(cells, endIdx int) int {
|
|
|
|
if endIdx == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
usedCells := 0
|
|
|
|
for i := endIdx; i > 0; i-- {
|
|
|
|
prev := (*fd)[i-1]
|
|
|
|
width := runewidth.RuneWidth(prev)
|
|
|
|
if usedCells+width > cells {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
usedCells += width
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-04-10 23:42:49 -04:00
|
|
|
// cellsAfter given a startIdx calculates endIdx that results in range that
|
|
|
|
// will take at most the provided number of cells to print on the screen.
|
|
|
|
func (fd *fieldData) cellsAfter(cells, startIdx int) int {
|
|
|
|
if startIdx >= len(*fd) || cells == 0 {
|
|
|
|
return startIdx
|
|
|
|
}
|
|
|
|
|
|
|
|
first := (*fd)[startIdx]
|
|
|
|
usedCells := runewidth.RuneWidth(first)
|
|
|
|
for i := startIdx + 1; i < len(*fd); i++ {
|
|
|
|
r := (*fd)[i]
|
|
|
|
width := runewidth.RuneWidth(r)
|
|
|
|
if usedCells+width > cells {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
usedCells += width
|
|
|
|
}
|
|
|
|
return len(*fd)
|
|
|
|
}
|
|
|
|
|
2019-04-17 23:59:33 -04:00
|
|
|
// startVisible asserts whether the first rune is within the visible range.
|
|
|
|
func (fd *fieldData) startVisible(vr *visibleRange) bool {
|
|
|
|
return vr.startIdx == 0
|
2019-04-10 00:09:46 -04:00
|
|
|
}
|
|
|
|
|
2019-04-17 23:59:33 -04:00
|
|
|
// endVisible asserts whether the last rune is within the visible range.
|
|
|
|
// The last position in the visible range is reserved for the cursor or an
|
|
|
|
// arrow.
|
|
|
|
func (fd *fieldData) endVisible(vr *visibleRange) bool {
|
|
|
|
return vr.endIdx-1 >= len(*fd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// minForArrows is the smallest number of cells in the window where we can
|
|
|
|
// indicate hidden text with left and right arrow.
|
|
|
|
const minForArrows = 3
|
|
|
|
|
2019-04-10 00:09:46 -04:00
|
|
|
// runesIn returns runes that are in the visible range.
|
|
|
|
func (fd *fieldData) runesIn(vr *visibleRange) string {
|
2019-04-10 23:42:49 -04:00
|
|
|
var runes []rune
|
2019-04-10 00:09:46 -04:00
|
|
|
for i, r := range (*fd)[vr.startIdx:] {
|
2019-04-17 23:59:33 -04:00
|
|
|
if i+vr.startIdx > vr.endIdx-2 {
|
2019-04-10 00:09:46 -04:00
|
|
|
break
|
|
|
|
}
|
2019-04-10 23:42:49 -04:00
|
|
|
runes = append(runes, r)
|
|
|
|
}
|
|
|
|
|
2019-04-17 23:59:33 -04:00
|
|
|
useArrows := vr.cells() >= minForArrows
|
|
|
|
var b strings.Builder
|
2019-04-10 23:42:49 -04:00
|
|
|
for i, r := range runes {
|
|
|
|
switch {
|
2019-04-17 23:59:33 -04:00
|
|
|
case useArrows && i == 0 && !fd.startVisible(vr):
|
2019-04-10 23:42:49 -04:00
|
|
|
b.WriteRune('⇦')
|
|
|
|
|
|
|
|
default:
|
|
|
|
b.WriteRune(r)
|
|
|
|
}
|
2019-04-10 00:09:46 -04:00
|
|
|
}
|
2019-04-17 23:59:33 -04:00
|
|
|
|
|
|
|
if useArrows && !fd.endVisible(vr) {
|
|
|
|
b.WriteRune('⇨')
|
|
|
|
}
|
2019-04-10 00:09:46 -04:00
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2019-04-17 23:59:33 -04:00
|
|
|
// visibleRange represents a range of currently visible cells.
|
|
|
|
// Visible cells are all cells whose index falls within:
|
2019-04-08 23:32:00 -04:00
|
|
|
// startIdx <= idx < endIdx
|
2019-04-17 23:59:33 -04:00
|
|
|
// Not all of these cells are available for runes, the last cell is reserved
|
|
|
|
// for the cursor to append data or for an arrow indicating that the text is
|
|
|
|
// scrolling. See forRunes().
|
2019-04-08 23:32:00 -04:00
|
|
|
type visibleRange struct {
|
|
|
|
startIdx int
|
|
|
|
endIdx int
|
|
|
|
}
|
|
|
|
|
2019-04-17 23:59:33 -04:00
|
|
|
// forRunes returns the number of cells that are usable for runes.
|
|
|
|
// Part of the visible range is reserved for the cursor at the end of the data.
|
|
|
|
func (vr *visibleRange) forRunes() int {
|
|
|
|
cells := vr.cells()
|
|
|
|
if cells < 1 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return cells - 1 // One cell reserved for the cursor at the end.
|
|
|
|
}
|
|
|
|
|
|
|
|
// cells returns the number of cells in the range.
|
|
|
|
func (vr *visibleRange) cells() int {
|
2019-04-10 00:09:46 -04:00
|
|
|
return vr.endIdx - vr.startIdx
|
|
|
|
}
|
|
|
|
|
2019-04-17 23:59:33 -04:00
|
|
|
// contains asserts whether the provided index is in the range.
|
|
|
|
func (vr *visibleRange) contains(idx int) bool {
|
|
|
|
return idx >= vr.startIdx && idx < vr.endIdx
|
2019-04-10 00:09:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// set sets the visible range from the start to the end index.
|
|
|
|
func (vr *visibleRange) set(startIdx, endIdx int) {
|
|
|
|
vr.startIdx = startIdx
|
|
|
|
vr.endIdx = endIdx
|
|
|
|
}
|
|
|
|
|
2019-04-17 23:59:33 -04:00
|
|
|
// curMinIdx returns the lowest acceptable index for cursor position that is
|
|
|
|
// still within the visible range.
|
|
|
|
func (vr *visibleRange) curMinIdx() int {
|
|
|
|
if vr.cells() == 0 {
|
|
|
|
return vr.startIdx
|
|
|
|
}
|
|
|
|
|
|
|
|
if vr.startIdx == 0 || vr.cells() < minForArrows {
|
|
|
|
// The very first rune is visible, so the cursor can go all the way to
|
|
|
|
// the start.
|
|
|
|
return vr.startIdx
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the first rune isn't visible, the cursor cannot go on the first
|
|
|
|
// cell in the visible range since it contains the left arrow.
|
|
|
|
return vr.startIdx + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// curMaxIdx returns the highest acceptable index for cursor position that is
|
|
|
|
// still within the visible range given the number of runes in data.
|
|
|
|
func (vr *visibleRange) curMaxIdx(runeCount int) int {
|
|
|
|
if vr.cells() == 0 {
|
|
|
|
return vr.startIdx
|
|
|
|
}
|
|
|
|
|
|
|
|
if vr.endIdx == runeCount || vr.endIdx == runeCount+1 || vr.cells() < minForArrows {
|
|
|
|
// The last rune is visible, so the cursor can go all the way to the
|
|
|
|
// end.
|
|
|
|
return vr.endIdx - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the last rune isn't visible, the cursor cannot go on the last cell
|
|
|
|
// in the window that is reserved for appending text, since it contains the
|
|
|
|
// right arrow.
|
|
|
|
return vr.endIdx - 2
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalizeToWidth normalizes the visible range, handles cases where the width of the
|
|
|
|
// text input field changed (terminal resize).
|
|
|
|
func (vr *visibleRange) normalizeToWidth(width int) {
|
|
|
|
switch {
|
|
|
|
case width < vr.cells():
|
|
|
|
diff := vr.cells() - width
|
|
|
|
vr.startIdx += diff
|
|
|
|
|
|
|
|
case width > vr.cells():
|
|
|
|
diff := width - vr.cells()
|
|
|
|
vr.startIdx -= diff
|
|
|
|
}
|
|
|
|
|
|
|
|
if vr.startIdx < 0 {
|
|
|
|
vr.endIdx += -1 * vr.startIdx
|
|
|
|
vr.startIdx = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalizeToiData normalizes the visible range, handles cases where the
|
|
|
|
// length of the data decreased due to deletion of some runes.
|
2019-04-18 23:57:24 -04:00
|
|
|
func (vr *visibleRange) normalizeToData(fd fieldData) {
|
|
|
|
if vr.endIdx <= len(fd) || vr.startIdx == 0 {
|
|
|
|
// Nothing to do when data fills the range or the range already starts
|
|
|
|
// all the way left.
|
2019-04-17 23:59:33 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-18 23:57:24 -04:00
|
|
|
endIdx := len(fd)
|
|
|
|
startIdx := fd.cellsBefore(vr.forRunes(), endIdx)
|
|
|
|
endIdx++ // Space for the cursor within the visible range.
|
|
|
|
vr.set(startIdx, endIdx)
|
2019-04-17 23:59:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// curRelative returns the relative position of the cursor within the visible
|
|
|
|
// range. Returns an error if the cursos isn't inside the visible range.
|
|
|
|
func (vr *visibleRange) curRelative(curDataPos int) (int, error) {
|
|
|
|
if !vr.contains(curDataPos) {
|
|
|
|
return 0, fmt.Errorf("curDataPos %d isn't inside %#v", curDataPos, *vr)
|
|
|
|
}
|
|
|
|
return curDataPos - vr.startIdx, nil
|
|
|
|
}
|
|
|
|
|
2019-04-07 23:31:41 -04:00
|
|
|
// fieldEditor maintains the cursor position and allows editing of the data in
|
|
|
|
// the text input field.
|
|
|
|
// This object isn't thread-safe.
|
|
|
|
type fieldEditor struct {
|
|
|
|
// data are the data currently present in the text input field.
|
|
|
|
data fieldData
|
2019-04-08 23:32:00 -04:00
|
|
|
|
2019-04-17 23:59:33 -04:00
|
|
|
// curDataPos is the current position of the cursor within the data.
|
|
|
|
// The cursor is allowed to go one cell beyond the data so appending is
|
|
|
|
// possible.
|
|
|
|
curDataPos int
|
2019-04-08 23:32:00 -04:00
|
|
|
|
|
|
|
// visible is the currently visible range.
|
2019-04-10 00:09:46 -04:00
|
|
|
visible *visibleRange
|
2019-04-07 23:31:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// newFieldEditor returns a new fieldEditor instance.
|
|
|
|
func newFieldEditor() *fieldEditor {
|
2019-04-10 00:09:46 -04:00
|
|
|
return &fieldEditor{
|
|
|
|
visible: &visibleRange{},
|
|
|
|
}
|
2019-04-07 23:31:41 -04:00
|
|
|
}
|
|
|
|
|
2019-04-17 23:59:33 -04:00
|
|
|
// shiftLeft shifts the visible range left so that it again contains the
|
|
|
|
// cursor.
|
|
|
|
func (fe *fieldEditor) shiftLeft() {
|
|
|
|
var startIdx int
|
|
|
|
switch {
|
|
|
|
case fe.curDataPos == 0 || fe.visible.cells() < minForArrows:
|
|
|
|
startIdx = fe.curDataPos
|
2019-04-10 00:09:46 -04:00
|
|
|
|
2019-04-17 23:59:33 -04:00
|
|
|
default:
|
|
|
|
startIdx = fe.curDataPos - 1
|
2019-04-10 00:09:46 -04:00
|
|
|
}
|
2019-04-17 23:59:33 -04:00
|
|
|
endIdx := fe.data.cellsAfter(fe.visible.forRunes(), startIdx)
|
|
|
|
endIdx++ // Space for the cursor.
|
|
|
|
|
|
|
|
gotCells := endIdx - startIdx
|
|
|
|
if fe.visible.cells() >= minForArrows && gotCells < minForArrows {
|
|
|
|
// The plan was to hide the first rune under an arrow.
|
|
|
|
// However after looking at the actual runes in the range, some took
|
|
|
|
// more space than one cell (full-width runes) and we have lost the
|
|
|
|
// space for the arrow, so shift the range by one.
|
|
|
|
startIdx++
|
|
|
|
endIdx++
|
2019-04-10 23:42:49 -04:00
|
|
|
}
|
2019-04-17 23:59:33 -04:00
|
|
|
fe.visible.set(startIdx, endIdx)
|
|
|
|
}
|
2019-04-10 23:42:49 -04:00
|
|
|
|
2019-04-17 23:59:33 -04:00
|
|
|
// shiftRight shifts the visible range right so that it again contains the
|
|
|
|
// cursor.
|
|
|
|
func (fe *fieldEditor) shiftRight() {
|
|
|
|
var endIdx int
|
|
|
|
switch dataLen := len(fe.data); {
|
|
|
|
case fe.curDataPos == dataLen:
|
|
|
|
// Cursor is in the empty space after the data.
|
|
|
|
// Print all runes until the end of data.
|
|
|
|
endIdx = dataLen
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Cursor is within the data, print all runes including the one the
|
|
|
|
// cursor is on.
|
|
|
|
endIdx = fe.curDataPos + 1
|
2019-04-10 00:09:46 -04:00
|
|
|
}
|
2019-04-07 23:31:41 -04:00
|
|
|
|
2019-04-17 23:59:33 -04:00
|
|
|
startIdx := fe.data.cellsBefore(fe.visible.forRunes(), endIdx)
|
|
|
|
endIdx++ // Space for the cursor within the visible range.
|
|
|
|
fe.visible.set(startIdx, endIdx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// toCursor shifts the visible range to the cursor if it scrolled out of view.
|
|
|
|
// This is a no-op if the cursor is inside the range.
|
|
|
|
func (fe *fieldEditor) toCursor() {
|
|
|
|
switch {
|
|
|
|
case fe.curDataPos < fe.visible.curMinIdx():
|
|
|
|
fe.shiftLeft()
|
|
|
|
case fe.curDataPos > fe.visible.curMaxIdx(len(fe.data)):
|
|
|
|
fe.shiftRight()
|
|
|
|
}
|
|
|
|
}
|
2019-04-07 23:31:41 -04:00
|
|
|
|
2019-04-17 23:59:33 -04:00
|
|
|
// viewFor returns the currently visible data inside a text field with the
|
|
|
|
// specified width and the cursor position within the field.
|
|
|
|
func (fe *fieldEditor) viewFor(width int) (string, int, error) {
|
|
|
|
if min := 4; width < min { // One for left arrow, two for one full-width rune and one for the cursor.
|
|
|
|
return "", -1, fmt.Errorf("width %d is too small, the minimum is %d", width, min)
|
2019-04-10 23:42:49 -04:00
|
|
|
}
|
2019-04-17 23:59:33 -04:00
|
|
|
fe.visible.normalizeToWidth(width)
|
2019-04-18 23:57:24 -04:00
|
|
|
fe.visible.normalizeToData(fe.data)
|
2019-04-17 23:59:33 -04:00
|
|
|
fe.toCursor()
|
2019-04-07 23:31:41 -04:00
|
|
|
|
2019-04-17 23:59:33 -04:00
|
|
|
cur, err := fe.visible.curRelative(fe.curDataPos)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
|
|
|
return fe.data.runesIn(fe.visible), cur, nil
|
2019-04-07 23:31:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// insert inserts the rune at the current position of the cursor.
|
|
|
|
func (fe *fieldEditor) insert(r rune) {
|
2019-04-17 23:59:33 -04:00
|
|
|
fe.data.insertAt(fe.curDataPos, r)
|
|
|
|
fe.curDataPos++
|
2019-04-07 23:31:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete deletes the rune at the current position of the cursor.
|
2019-04-17 23:59:33 -04:00
|
|
|
func (fe *fieldEditor) delete() {
|
|
|
|
if fe.curDataPos >= len(fe.data) {
|
|
|
|
// Cursor not on a rune, nothing to do.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fe.data.deleteAt(fe.curDataPos)
|
|
|
|
}
|
2019-04-07 23:31:41 -04:00
|
|
|
|
|
|
|
// deleteBefore deletes the rune that is immediately to the left of the cursor.
|
2019-04-17 23:59:33 -04:00
|
|
|
func (fe *fieldEditor) deleteBefore() {
|
|
|
|
if fe.curDataPos == 0 {
|
|
|
|
// Cursor at the beginning, nothing to do.
|
2019-04-18 23:57:24 -04:00
|
|
|
return
|
2019-04-17 23:59:33 -04:00
|
|
|
}
|
|
|
|
fe.cursorLeft()
|
|
|
|
fe.delete()
|
|
|
|
}
|
2019-04-07 23:31:41 -04:00
|
|
|
|
|
|
|
// cursorRight moves the cursor one position to the right.
|
2019-04-08 23:32:00 -04:00
|
|
|
func (fe *fieldEditor) cursorRight() {
|
2019-04-17 23:59:33 -04:00
|
|
|
fe.curDataPos, _ = numbers.MinMaxInts([]int{fe.curDataPos + 1, len(fe.data)})
|
2019-04-08 23:32:00 -04:00
|
|
|
}
|
2019-04-07 23:31:41 -04:00
|
|
|
|
2019-04-08 23:32:00 -04:00
|
|
|
// cursorLeft moves the cursor one position to the left.
|
|
|
|
func (fe *fieldEditor) cursorLeft() {
|
2019-04-17 23:59:33 -04:00
|
|
|
_, fe.curDataPos = numbers.MinMaxInts([]int{fe.curDataPos - 1, 0})
|
2019-04-08 23:32:00 -04:00
|
|
|
}
|
2019-04-07 23:31:41 -04:00
|
|
|
|
2019-04-17 23:59:33 -04:00
|
|
|
// cursorStart moves the cursor to the beginning of the data.
|
|
|
|
func (fe *fieldEditor) cursorStart() {
|
|
|
|
fe.curDataPos = 0
|
|
|
|
}
|
2019-04-07 23:31:41 -04:00
|
|
|
|
|
|
|
// cursorEnd moves the cursor to the end of the data.
|
2019-04-17 23:59:33 -04:00
|
|
|
func (fe *fieldEditor) cursorEnd() {
|
|
|
|
fe.curDataPos = len(fe.data)
|
|
|
|
}
|