2017-01-07 16:30:31 +00:00
|
|
|
package tui
|
|
|
|
|
|
|
|
import (
|
2020-12-29 16:59:18 +00:00
|
|
|
"bytes"
|
2017-01-07 16:30:31 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2017-01-23 03:47:55 +00:00
|
|
|
"regexp"
|
2017-01-07 16:30:31 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
2021-05-14 02:43:32 +00:00
|
|
|
"github.com/mattn/go-runewidth"
|
|
|
|
"github.com/rivo/uniseg"
|
2017-01-15 04:10:59 +00:00
|
|
|
|
2021-03-20 05:38:34 +00:00
|
|
|
"golang.org/x/term"
|
2017-01-07 16:30:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultWidth = 80
|
|
|
|
defaultHeight = 24
|
|
|
|
|
2017-01-23 03:51:31 +00:00
|
|
|
defaultEscDelay = 100
|
2017-01-07 16:30:31 +00:00
|
|
|
escPollInterval = 5
|
2017-01-23 03:47:55 +00:00
|
|
|
offsetPollTries = 10
|
2022-03-09 08:02:06 +00:00
|
|
|
maxInputBuffer = 1024 * 1024
|
2017-01-07 16:30:31 +00:00
|
|
|
)
|
|
|
|
|
2017-01-15 04:10:59 +00:00
|
|
|
const consoleDevice string = "/dev/tty"
|
|
|
|
|
2018-09-26 17:35:44 +00:00
|
|
|
var offsetRegexp *regexp.Regexp = regexp.MustCompile("(.*)\x1b\\[([0-9]+);([0-9]+)R")
|
2020-07-03 10:45:58 +00:00
|
|
|
var offsetRegexpBegin *regexp.Regexp = regexp.MustCompile("^\x1b\\[[0-9]+;[0-9]+R")
|
2017-01-23 03:47:55 +00:00
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (r *LightRenderer) stderr(str string) {
|
2023-03-25 01:23:05 +00:00
|
|
|
r.stderrInternal(str, true, "")
|
2017-01-15 17:26:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-25 01:23:05 +00:00
|
|
|
const CR string = "\x1b[2m␍"
|
|
|
|
const LF string = "\x1b[2m␊"
|
|
|
|
|
|
|
|
func (r *LightRenderer) stderrInternal(str string, allowNLCR bool, resetCode string) {
|
2017-01-07 16:30:31 +00:00
|
|
|
bytes := []byte(str)
|
|
|
|
runes := []rune{}
|
|
|
|
for len(bytes) > 0 {
|
|
|
|
r, sz := utf8.DecodeRune(bytes)
|
2018-04-12 08:49:52 +00:00
|
|
|
nlcr := r == '\n' || r == '\r'
|
|
|
|
if r >= 32 || r == '\x1b' || nlcr {
|
2023-03-25 01:23:05 +00:00
|
|
|
if nlcr && !allowNLCR {
|
|
|
|
if r == '\r' {
|
|
|
|
runes = append(runes, []rune(CR+resetCode)...)
|
|
|
|
} else {
|
|
|
|
runes = append(runes, []rune(LF+resetCode)...)
|
|
|
|
}
|
|
|
|
} else if r != utf8.RuneError {
|
2018-04-12 08:49:52 +00:00
|
|
|
runes = append(runes, r)
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
bytes = bytes[sz:]
|
|
|
|
}
|
2021-05-14 02:43:32 +00:00
|
|
|
r.queued.WriteString(string(runes))
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-25 01:23:05 +00:00
|
|
|
func (r *LightRenderer) csi(code string) string {
|
|
|
|
fullcode := "\x1b[" + code
|
|
|
|
r.stderr(fullcode)
|
|
|
|
return fullcode
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LightRenderer) flush() {
|
2021-05-14 02:43:32 +00:00
|
|
|
if r.queued.Len() > 0 {
|
2022-04-04 13:06:16 +00:00
|
|
|
fmt.Fprint(os.Stderr, "\x1b[?25l"+r.queued.String()+"\x1b[?25h")
|
2021-05-14 02:43:32 +00:00
|
|
|
r.queued.Reset()
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Light renderer
|
|
|
|
type LightRenderer struct {
|
|
|
|
theme *ColorTheme
|
|
|
|
mouse bool
|
|
|
|
forceBlack bool
|
2017-03-04 02:29:31 +00:00
|
|
|
clearOnExit bool
|
2017-01-07 16:30:31 +00:00
|
|
|
prevDownTime time.Time
|
2023-01-02 16:21:40 +00:00
|
|
|
clicks [][2]int
|
2017-01-07 16:30:31 +00:00
|
|
|
ttyin *os.File
|
|
|
|
buffer []byte
|
2021-03-20 05:38:34 +00:00
|
|
|
origState *term.State
|
2017-01-07 16:30:31 +00:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
yoffset int
|
|
|
|
tabstop int
|
|
|
|
escDelay int
|
2017-01-21 20:26:38 +00:00
|
|
|
fullscreen bool
|
2017-01-07 16:30:31 +00:00
|
|
|
upOneLine bool
|
2021-05-14 02:43:32 +00:00
|
|
|
queued strings.Builder
|
2017-01-09 10:09:30 +00:00
|
|
|
y int
|
|
|
|
x int
|
2017-01-07 16:30:31 +00:00
|
|
|
maxHeightFunc func(int) int
|
2019-02-05 06:51:39 +00:00
|
|
|
|
|
|
|
// Windows only
|
|
|
|
ttyinChannel chan byte
|
|
|
|
inHandle uintptr
|
|
|
|
outHandle uintptr
|
|
|
|
origStateInput uint32
|
|
|
|
origStateOutput uint32
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type LightWindow struct {
|
|
|
|
renderer *LightRenderer
|
|
|
|
colored bool
|
2020-03-05 11:15:15 +00:00
|
|
|
preview bool
|
2017-02-04 12:51:22 +00:00
|
|
|
border BorderStyle
|
2017-01-07 16:30:31 +00:00
|
|
|
top int
|
|
|
|
left int
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
posx int
|
|
|
|
posy int
|
|
|
|
tabstop int
|
2017-09-08 09:33:17 +00:00
|
|
|
fg Color
|
2017-01-07 16:30:31 +00:00
|
|
|
bg Color
|
|
|
|
}
|
|
|
|
|
2017-03-04 05:09:36 +00:00
|
|
|
func NewLightRenderer(theme *ColorTheme, forceBlack bool, mouse bool, tabstop int, clearOnExit bool, fullscreen bool, maxHeightFunc func(int) int) Renderer {
|
2017-01-07 16:30:31 +00:00
|
|
|
r := LightRenderer{
|
|
|
|
theme: theme,
|
|
|
|
forceBlack: forceBlack,
|
|
|
|
mouse: mouse,
|
2017-03-04 02:29:31 +00:00
|
|
|
clearOnExit: clearOnExit,
|
2017-01-07 16:30:31 +00:00
|
|
|
ttyin: openTtyIn(),
|
2017-01-21 20:26:38 +00:00
|
|
|
yoffset: 0,
|
2017-01-07 16:30:31 +00:00
|
|
|
tabstop: tabstop,
|
2017-03-04 05:09:36 +00:00
|
|
|
fullscreen: fullscreen,
|
2017-01-07 16:30:31 +00:00
|
|
|
upOneLine: false,
|
|
|
|
maxHeightFunc: maxHeightFunc}
|
|
|
|
return &r
|
|
|
|
}
|
|
|
|
|
2019-03-28 17:11:03 +00:00
|
|
|
func repeat(r rune, times int) string {
|
2017-01-07 16:30:31 +00:00
|
|
|
if times > 0 {
|
2019-03-28 17:11:03 +00:00
|
|
|
return strings.Repeat(string(r), times)
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func atoi(s string, defaultValue int) int {
|
|
|
|
value, err := strconv.Atoi(s)
|
|
|
|
if err != nil {
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LightRenderer) Init() {
|
2017-01-23 03:51:31 +00:00
|
|
|
r.escDelay = atoi(os.Getenv("ESCDELAY"), defaultEscDelay)
|
2017-01-07 16:30:31 +00:00
|
|
|
|
2019-02-05 06:51:39 +00:00
|
|
|
if err := r.initPlatform(); err != nil {
|
2017-01-15 04:10:59 +00:00
|
|
|
errorExit(err.Error())
|
|
|
|
}
|
2017-03-04 05:09:36 +00:00
|
|
|
r.updateTerminalSize()
|
2017-01-07 16:30:31 +00:00
|
|
|
initTheme(r.theme, r.defaultTheme(), r.forceBlack)
|
|
|
|
|
2017-01-21 20:26:38 +00:00
|
|
|
if r.fullscreen {
|
|
|
|
r.smcup()
|
|
|
|
} else {
|
2017-07-18 11:50:38 +00:00
|
|
|
// We assume that --no-clear is used for repetitive relaunching of fzf.
|
|
|
|
// So we do not clear the lower bottom of the screen.
|
|
|
|
if r.clearOnExit {
|
|
|
|
r.csi("J")
|
|
|
|
}
|
2017-01-23 03:15:31 +00:00
|
|
|
y, x := r.findOffset()
|
|
|
|
r.mouse = r.mouse && y >= 0
|
2017-07-18 11:50:38 +00:00
|
|
|
// When --no-clear is used for repetitive relaunching, there is a small
|
|
|
|
// time frame between fzf processes where the user keystrokes are not
|
|
|
|
// captured by either of fzf process which can cause x offset to be
|
|
|
|
// increased and we're left with unwanted extra new line.
|
|
|
|
if x > 0 && r.clearOnExit {
|
2017-01-21 20:26:38 +00:00
|
|
|
r.upOneLine = true
|
|
|
|
r.makeSpace()
|
|
|
|
}
|
|
|
|
for i := 1; i < r.MaxY(); i++ {
|
|
|
|
r.makeSpace()
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-24 03:00:41 +00:00
|
|
|
r.enableMouse()
|
2017-01-07 16:30:31 +00:00
|
|
|
r.csi(fmt.Sprintf("%dA", r.MaxY()-1))
|
|
|
|
r.csi("G")
|
2017-01-21 18:19:50 +00:00
|
|
|
r.csi("K")
|
2017-08-08 16:58:09 +00:00
|
|
|
if !r.clearOnExit && !r.fullscreen {
|
|
|
|
r.csi("s")
|
|
|
|
}
|
2017-01-21 20:26:38 +00:00
|
|
|
if !r.fullscreen && r.mouse {
|
2017-01-10 13:55:55 +00:00
|
|
|
r.yoffset, _ = r.findOffset()
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 16:01:22 +00:00
|
|
|
func (r *LightRenderer) Resize(maxHeightFunc func(int) int) {
|
|
|
|
r.maxHeightFunc = maxHeightFunc
|
|
|
|
}
|
|
|
|
|
2017-01-21 18:19:50 +00:00
|
|
|
func (r *LightRenderer) makeSpace() {
|
|
|
|
r.stderr("\n")
|
|
|
|
r.csi("G")
|
|
|
|
}
|
|
|
|
|
2017-01-09 10:09:30 +00:00
|
|
|
func (r *LightRenderer) move(y int, x int) {
|
|
|
|
// w.csi("u")
|
|
|
|
if r.y < y {
|
|
|
|
r.csi(fmt.Sprintf("%dB", y-r.y))
|
|
|
|
} else if r.y > y {
|
|
|
|
r.csi(fmt.Sprintf("%dA", r.y-y))
|
|
|
|
}
|
|
|
|
r.stderr("\r")
|
|
|
|
if x > 0 {
|
|
|
|
r.csi(fmt.Sprintf("%dC", x))
|
|
|
|
}
|
|
|
|
r.y = y
|
|
|
|
r.x = x
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LightRenderer) origin() {
|
|
|
|
r.move(0, 0)
|
|
|
|
}
|
|
|
|
|
2017-01-15 04:10:59 +00:00
|
|
|
func getEnv(name string, defaultValue int) int {
|
|
|
|
env := os.Getenv(name)
|
|
|
|
if len(env) == 0 {
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
return atoi(env, defaultValue)
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (r *LightRenderer) getBytes() []byte {
|
2017-01-23 03:47:55 +00:00
|
|
|
return r.getBytesInternal(r.buffer, false)
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 03:47:55 +00:00
|
|
|
func (r *LightRenderer) getBytesInternal(buffer []byte, nonblock bool) []byte {
|
|
|
|
c, ok := r.getch(nonblock)
|
|
|
|
if !nonblock && !ok {
|
2017-01-10 17:12:32 +00:00
|
|
|
r.Close()
|
2017-01-15 04:10:59 +00:00
|
|
|
errorExit("Failed to read " + consoleDevice)
|
2017-01-10 17:12:32 +00:00
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
|
|
|
|
retries := 0
|
2020-12-29 16:59:18 +00:00
|
|
|
if c == ESC.Int() || nonblock {
|
2017-01-07 16:30:31 +00:00
|
|
|
retries = r.escDelay / escPollInterval
|
|
|
|
}
|
|
|
|
buffer = append(buffer, byte(c))
|
|
|
|
|
2018-08-08 06:43:55 +00:00
|
|
|
pc := c
|
2017-01-07 16:30:31 +00:00
|
|
|
for {
|
2017-01-10 17:12:32 +00:00
|
|
|
c, ok = r.getch(true)
|
|
|
|
if !ok {
|
2017-01-07 16:30:31 +00:00
|
|
|
if retries > 0 {
|
|
|
|
retries--
|
|
|
|
time.Sleep(escPollInterval * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
2020-12-29 16:59:18 +00:00
|
|
|
} else if c == ESC.Int() && pc != c {
|
2018-08-08 06:43:55 +00:00
|
|
|
retries = r.escDelay / escPollInterval
|
|
|
|
} else {
|
|
|
|
retries = 0
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
buffer = append(buffer, byte(c))
|
2018-08-08 06:43:55 +00:00
|
|
|
pc = c
|
2020-03-07 02:20:44 +00:00
|
|
|
|
|
|
|
// This should never happen under normal conditions,
|
|
|
|
// so terminate fzf immediately.
|
|
|
|
if len(buffer) > maxInputBuffer {
|
|
|
|
r.Close()
|
|
|
|
panic(fmt.Sprintf("Input buffer overflow (%d): %v", len(buffer), buffer))
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LightRenderer) GetChar() Event {
|
|
|
|
if len(r.buffer) == 0 {
|
|
|
|
r.buffer = r.getBytes()
|
|
|
|
}
|
|
|
|
if len(r.buffer) == 0 {
|
|
|
|
panic("Empty buffer")
|
|
|
|
}
|
|
|
|
|
|
|
|
sz := 1
|
|
|
|
defer func() {
|
|
|
|
r.buffer = r.buffer[sz:]
|
|
|
|
}()
|
|
|
|
|
|
|
|
switch r.buffer[0] {
|
2020-12-29 16:59:18 +00:00
|
|
|
case CtrlC.Byte():
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{CtrlC, 0, nil}
|
2020-12-29 16:59:18 +00:00
|
|
|
case CtrlG.Byte():
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{CtrlG, 0, nil}
|
2020-12-29 16:59:18 +00:00
|
|
|
case CtrlQ.Byte():
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{CtrlQ, 0, nil}
|
|
|
|
case 127:
|
|
|
|
return Event{BSpace, 0, nil}
|
2017-01-27 17:54:47 +00:00
|
|
|
case 0:
|
|
|
|
return Event{CtrlSpace, 0, nil}
|
2019-11-14 13:39:25 +00:00
|
|
|
case 28:
|
|
|
|
return Event{CtrlBackSlash, 0, nil}
|
|
|
|
case 29:
|
|
|
|
return Event{CtrlRightBracket, 0, nil}
|
|
|
|
case 30:
|
|
|
|
return Event{CtrlCaret, 0, nil}
|
|
|
|
case 31:
|
|
|
|
return Event{CtrlSlash, 0, nil}
|
2020-12-29 16:59:18 +00:00
|
|
|
case ESC.Byte():
|
2017-01-07 16:30:31 +00:00
|
|
|
ev := r.escSequence(&sz)
|
|
|
|
// Second chance
|
|
|
|
if ev.Type == Invalid {
|
|
|
|
r.buffer = r.getBytes()
|
|
|
|
ev = r.escSequence(&sz)
|
|
|
|
}
|
|
|
|
return ev
|
|
|
|
}
|
|
|
|
|
|
|
|
// CTRL-A ~ CTRL-Z
|
2020-12-29 16:59:18 +00:00
|
|
|
if r.buffer[0] <= CtrlZ.Byte() {
|
|
|
|
return Event{EventType(r.buffer[0]), 0, nil}
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
char, rsz := utf8.DecodeRune(r.buffer)
|
|
|
|
if char == utf8.RuneError {
|
|
|
|
return Event{ESC, 0, nil}
|
|
|
|
}
|
|
|
|
sz = rsz
|
|
|
|
return Event{Rune, char, nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LightRenderer) escSequence(sz *int) Event {
|
|
|
|
if len(r.buffer) < 2 {
|
|
|
|
return Event{ESC, 0, nil}
|
|
|
|
}
|
2020-07-03 10:45:58 +00:00
|
|
|
|
|
|
|
loc := offsetRegexpBegin.FindIndex(r.buffer)
|
|
|
|
if loc != nil && loc[0] == 0 {
|
|
|
|
*sz = loc[1]
|
|
|
|
return Event{Invalid, 0, nil}
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
*sz = 2
|
2017-04-27 17:36:36 +00:00
|
|
|
if r.buffer[1] >= 1 && r.buffer[1] <= 'z'-'a'+1 {
|
2020-12-29 16:59:18 +00:00
|
|
|
return CtrlAltKey(rune(r.buffer[1] + 'a' - 1))
|
2017-04-27 17:36:36 +00:00
|
|
|
}
|
2018-04-12 08:39:28 +00:00
|
|
|
alt := false
|
2020-12-29 16:59:18 +00:00
|
|
|
if len(r.buffer) > 2 && r.buffer[1] == ESC.Byte() {
|
2018-04-12 08:39:28 +00:00
|
|
|
r.buffer = r.buffer[1:]
|
|
|
|
alt = true
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
switch r.buffer[1] {
|
2020-12-29 16:59:18 +00:00
|
|
|
case ESC.Byte():
|
2018-09-27 06:54:04 +00:00
|
|
|
return Event{ESC, 0, nil}
|
2017-01-07 16:30:31 +00:00
|
|
|
case 127:
|
|
|
|
return Event{AltBS, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case '[', 'O':
|
2017-01-07 16:30:31 +00:00
|
|
|
if len(r.buffer) < 3 {
|
|
|
|
return Event{Invalid, 0, nil}
|
|
|
|
}
|
|
|
|
*sz = 3
|
|
|
|
switch r.buffer[2] {
|
2020-07-03 10:41:19 +00:00
|
|
|
case 'D':
|
2018-04-12 08:39:28 +00:00
|
|
|
if alt {
|
|
|
|
return Event{AltLeft, 0, nil}
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{Left, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case 'C':
|
2018-04-12 08:39:28 +00:00
|
|
|
if alt {
|
|
|
|
// Ugh..
|
|
|
|
return Event{AltRight, 0, nil}
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{Right, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case 'B':
|
2018-04-12 08:39:28 +00:00
|
|
|
if alt {
|
|
|
|
return Event{AltDown, 0, nil}
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{Down, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case 'A':
|
2018-04-12 08:39:28 +00:00
|
|
|
if alt {
|
|
|
|
return Event{AltUp, 0, nil}
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{Up, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case 'Z':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{BTab, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case 'H':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{Home, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case 'F':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{End, 0, nil}
|
2022-06-17 03:47:42 +00:00
|
|
|
case '<':
|
2017-01-07 16:30:31 +00:00
|
|
|
return r.mouseSequence(sz)
|
2020-07-03 10:41:19 +00:00
|
|
|
case 'P':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{F1, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case 'Q':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{F2, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case 'R':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{F3, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case 'S':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{F4, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case '1', '2', '3', '4', '5', '6':
|
2017-01-07 16:30:31 +00:00
|
|
|
if len(r.buffer) < 4 {
|
|
|
|
return Event{Invalid, 0, nil}
|
|
|
|
}
|
|
|
|
*sz = 4
|
|
|
|
switch r.buffer[2] {
|
2020-07-03 10:41:19 +00:00
|
|
|
case '2':
|
|
|
|
if r.buffer[3] == '~' {
|
2020-02-23 16:43:19 +00:00
|
|
|
return Event{Insert, 0, nil}
|
|
|
|
}
|
2020-07-03 10:41:19 +00:00
|
|
|
if len(r.buffer) > 4 && r.buffer[4] == '~' {
|
2017-01-07 16:30:31 +00:00
|
|
|
*sz = 5
|
|
|
|
switch r.buffer[3] {
|
2020-07-03 10:41:19 +00:00
|
|
|
case '0':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{F9, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case '1':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{F10, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case '3':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{F11, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case '4':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{F12, 0, nil}
|
|
|
|
}
|
|
|
|
}
|
2017-06-21 17:35:57 +00:00
|
|
|
// Bracketed paste mode: \e[200~ ... \e[201~
|
2020-02-23 16:37:08 +00:00
|
|
|
if len(r.buffer) > 5 && r.buffer[3] == '0' && (r.buffer[4] == '0' || r.buffer[4] == '1') && r.buffer[5] == '~' {
|
2017-06-21 17:35:57 +00:00
|
|
|
// Immediately discard the sequence from the buffer and reread input
|
|
|
|
r.buffer = r.buffer[6:]
|
|
|
|
*sz = 0
|
|
|
|
return r.GetChar()
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
return Event{Invalid, 0, nil} // INS
|
2020-07-03 10:41:19 +00:00
|
|
|
case '3':
|
2023-05-21 09:40:05 +00:00
|
|
|
if r.buffer[3] == '~' {
|
|
|
|
return Event{Del, 0, nil}
|
|
|
|
}
|
|
|
|
if len(r.buffer) == 6 && r.buffer[5] == '~' {
|
|
|
|
*sz = 6
|
|
|
|
switch r.buffer[4] {
|
|
|
|
case '5':
|
|
|
|
return Event{CtrlDelete, 0, nil}
|
|
|
|
case '2':
|
|
|
|
return Event{SDelete, 0, nil}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Event{Invalid, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case '4':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{End, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case '5':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{PgUp, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case '6':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{PgDn, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case '1':
|
2017-01-07 16:30:31 +00:00
|
|
|
switch r.buffer[3] {
|
2020-07-03 10:41:19 +00:00
|
|
|
case '~':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{Home, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case '1', '2', '3', '4', '5', '7', '8', '9':
|
|
|
|
if len(r.buffer) == 5 && r.buffer[4] == '~' {
|
2017-01-07 16:30:31 +00:00
|
|
|
*sz = 5
|
|
|
|
switch r.buffer[3] {
|
2020-07-03 10:41:19 +00:00
|
|
|
case '1':
|
2020-01-18 03:30:38 +00:00
|
|
|
return Event{F1, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case '2':
|
2020-01-18 03:30:38 +00:00
|
|
|
return Event{F2, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case '3':
|
2020-01-18 03:30:38 +00:00
|
|
|
return Event{F3, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case '4':
|
2020-01-18 03:30:38 +00:00
|
|
|
return Event{F4, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case '5':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{F5, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case '7':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{F6, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case '8':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{F7, 0, nil}
|
2020-07-03 10:41:19 +00:00
|
|
|
case '9':
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{F8, 0, nil}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Event{Invalid, 0, nil}
|
2018-02-15 10:56:11 +00:00
|
|
|
case ';':
|
2020-11-24 10:36:58 +00:00
|
|
|
if len(r.buffer) < 6 {
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{Invalid, 0, nil}
|
|
|
|
}
|
|
|
|
*sz = 6
|
|
|
|
switch r.buffer[4] {
|
2020-11-24 10:36:58 +00:00
|
|
|
case '1', '2', '3', '5':
|
|
|
|
alt := r.buffer[4] == '3'
|
|
|
|
altShift := r.buffer[4] == '1' && r.buffer[5] == '0'
|
|
|
|
char := r.buffer[5]
|
|
|
|
if altShift {
|
|
|
|
if len(r.buffer) < 7 {
|
|
|
|
return Event{Invalid, 0, nil}
|
|
|
|
}
|
|
|
|
*sz = 7
|
|
|
|
char = r.buffer[6]
|
|
|
|
}
|
|
|
|
switch char {
|
2018-02-15 10:56:11 +00:00
|
|
|
case 'A':
|
2020-11-24 10:36:58 +00:00
|
|
|
if alt {
|
|
|
|
return Event{AltUp, 0, nil}
|
|
|
|
}
|
|
|
|
if altShift {
|
|
|
|
return Event{AltSUp, 0, nil}
|
|
|
|
}
|
2018-02-15 10:56:11 +00:00
|
|
|
return Event{SUp, 0, nil}
|
|
|
|
case 'B':
|
2020-11-24 10:36:58 +00:00
|
|
|
if alt {
|
|
|
|
return Event{AltDown, 0, nil}
|
|
|
|
}
|
|
|
|
if altShift {
|
|
|
|
return Event{AltSDown, 0, nil}
|
|
|
|
}
|
2018-02-15 10:56:11 +00:00
|
|
|
return Event{SDown, 0, nil}
|
|
|
|
case 'C':
|
2020-11-24 10:36:58 +00:00
|
|
|
if alt {
|
|
|
|
return Event{AltRight, 0, nil}
|
|
|
|
}
|
|
|
|
if altShift {
|
|
|
|
return Event{AltSRight, 0, nil}
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{SRight, 0, nil}
|
2018-02-15 10:56:11 +00:00
|
|
|
case 'D':
|
2020-11-24 10:36:58 +00:00
|
|
|
if alt {
|
|
|
|
return Event{AltLeft, 0, nil}
|
|
|
|
}
|
|
|
|
if altShift {
|
|
|
|
return Event{AltSLeft, 0, nil}
|
|
|
|
}
|
2018-02-15 10:56:11 +00:00
|
|
|
return Event{SLeft, 0, nil}
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
} // r.buffer[4]
|
|
|
|
} // r.buffer[3]
|
|
|
|
} // r.buffer[2]
|
|
|
|
} // r.buffer[2]
|
|
|
|
} // r.buffer[1]
|
2020-12-29 16:59:18 +00:00
|
|
|
rest := bytes.NewBuffer(r.buffer[1:])
|
|
|
|
c, size, err := rest.ReadRune()
|
|
|
|
if err == nil {
|
|
|
|
*sz = 1 + size
|
|
|
|
return AltKey(c)
|
2019-08-06 05:06:58 +00:00
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{Invalid, 0, nil}
|
|
|
|
}
|
|
|
|
|
2022-06-17 03:47:42 +00:00
|
|
|
// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Mouse-Tracking
|
2017-01-07 16:30:31 +00:00
|
|
|
func (r *LightRenderer) mouseSequence(sz *int) Event {
|
2022-06-17 03:47:42 +00:00
|
|
|
// "\e[<0;0;0M"
|
|
|
|
if len(r.buffer) < 9 || !r.mouse {
|
2017-01-07 16:30:31 +00:00
|
|
|
return Event{Invalid, 0, nil}
|
|
|
|
}
|
2022-06-17 03:47:42 +00:00
|
|
|
|
|
|
|
rest := r.buffer[*sz:]
|
|
|
|
end := bytes.IndexAny(rest, "mM")
|
|
|
|
if end == -1 {
|
|
|
|
return Event{Invalid, 0, nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
elems := strings.SplitN(string(rest[:end]), ";", 3)
|
|
|
|
if len(elems) != 3 {
|
|
|
|
return Event{Invalid, 0, nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
t := atoi(elems[0], -1)
|
|
|
|
x := atoi(elems[1], -1) - 1
|
2022-07-29 06:42:44 +00:00
|
|
|
y := atoi(elems[2], -1) - 1 - r.yoffset
|
2022-11-18 11:31:12 +00:00
|
|
|
if t < 0 || x < 0 {
|
2022-06-17 03:47:42 +00:00
|
|
|
return Event{Invalid, 0, nil}
|
|
|
|
}
|
|
|
|
*sz += end + 1
|
|
|
|
|
|
|
|
down := rest[end] == 'M'
|
|
|
|
|
|
|
|
scroll := 0
|
|
|
|
if t >= 64 {
|
|
|
|
t -= 64
|
|
|
|
if t&0b1 == 1 {
|
|
|
|
scroll = -1
|
2017-01-07 16:30:31 +00:00
|
|
|
} else {
|
2022-06-17 03:47:42 +00:00
|
|
|
scroll = 1
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
2022-06-17 03:47:42 +00:00
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
|
2022-06-17 03:47:42 +00:00
|
|
|
// middle := t & 0b1
|
|
|
|
left := t&0b11 == 0
|
|
|
|
|
|
|
|
// shift := t & 0b100
|
|
|
|
// ctrl := t & 0b1000
|
|
|
|
mod := t&0b1100 > 0
|
|
|
|
|
2023-01-01 05:48:14 +00:00
|
|
|
drag := t&0b100000 > 0
|
|
|
|
|
2022-06-17 03:47:42 +00:00
|
|
|
if scroll != 0 {
|
|
|
|
return Event{Mouse, 0, &MouseEvent{y, x, scroll, false, false, false, mod}}
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
2022-06-17 03:47:42 +00:00
|
|
|
|
|
|
|
double := false
|
2023-01-01 05:48:14 +00:00
|
|
|
if down && !drag {
|
2022-06-17 03:47:42 +00:00
|
|
|
now := time.Now()
|
|
|
|
if !left { // Right double click is not allowed
|
2023-01-02 16:21:40 +00:00
|
|
|
r.clicks = [][2]int{}
|
2022-06-17 03:47:42 +00:00
|
|
|
} else if now.Sub(r.prevDownTime) < doubleClickDuration {
|
2023-01-02 16:21:40 +00:00
|
|
|
r.clicks = append(r.clicks, [2]int{x, y})
|
2022-06-17 03:47:42 +00:00
|
|
|
} else {
|
2023-01-02 16:21:40 +00:00
|
|
|
r.clicks = [][2]int{{x, y}}
|
2022-06-17 03:47:42 +00:00
|
|
|
}
|
|
|
|
r.prevDownTime = now
|
|
|
|
} else {
|
2023-01-02 16:39:16 +00:00
|
|
|
n := len(r.clicks)
|
|
|
|
if len(r.clicks) > 1 && r.clicks[n-2][0] == r.clicks[n-1][0] && r.clicks[n-2][1] == r.clicks[n-1][1] &&
|
2022-06-17 03:47:42 +00:00
|
|
|
time.Since(r.prevDownTime) < doubleClickDuration {
|
|
|
|
double = true
|
2023-01-02 16:36:51 +00:00
|
|
|
if double {
|
|
|
|
r.clicks = [][2]int{}
|
|
|
|
}
|
2022-06-17 03:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Event{Mouse, 0, &MouseEvent{y, x, 0, left, down, double, mod}}
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 20:26:38 +00:00
|
|
|
func (r *LightRenderer) smcup() {
|
|
|
|
r.csi("?1049h")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LightRenderer) rmcup() {
|
|
|
|
r.csi("?1049l")
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:58:08 +00:00
|
|
|
func (r *LightRenderer) Pause(clear bool) {
|
2023-01-24 03:41:40 +00:00
|
|
|
r.disableMouse()
|
2019-02-05 06:51:39 +00:00
|
|
|
r.restoreTerminal()
|
2017-04-28 13:58:08 +00:00
|
|
|
if clear {
|
|
|
|
if r.fullscreen {
|
|
|
|
r.rmcup()
|
|
|
|
} else {
|
|
|
|
r.smcup()
|
|
|
|
r.csi("H")
|
|
|
|
}
|
|
|
|
r.flush()
|
2017-01-21 20:26:38 +00:00
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-24 03:00:41 +00:00
|
|
|
func (r *LightRenderer) enableMouse() {
|
|
|
|
if r.mouse {
|
|
|
|
r.csi("?1000h")
|
|
|
|
r.csi("?1002h")
|
|
|
|
r.csi("?1006h")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LightRenderer) disableMouse() {
|
|
|
|
if r.mouse {
|
|
|
|
r.csi("?1000l")
|
|
|
|
r.csi("?1002l")
|
|
|
|
r.csi("?1006l")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 17:33:37 +00:00
|
|
|
func (r *LightRenderer) Resume(clear bool, sigcont bool) {
|
2019-02-05 06:51:39 +00:00
|
|
|
r.setupTerminal()
|
2017-04-28 13:58:08 +00:00
|
|
|
if clear {
|
|
|
|
if r.fullscreen {
|
|
|
|
r.smcup()
|
|
|
|
} else {
|
|
|
|
r.rmcup()
|
|
|
|
}
|
2023-01-24 03:00:41 +00:00
|
|
|
r.enableMouse()
|
2017-04-28 13:58:08 +00:00
|
|
|
r.flush()
|
2023-01-24 03:45:07 +00:00
|
|
|
} else if sigcont && !r.fullscreen && r.mouse {
|
2020-05-17 17:33:37 +00:00
|
|
|
// NOTE: SIGCONT (Coming back from CTRL-Z):
|
|
|
|
// It's highly likely that the offset we obtained at the beginning is
|
|
|
|
// no longer correct, so we simply disable mouse input.
|
2023-01-24 03:00:41 +00:00
|
|
|
r.disableMouse()
|
2023-01-24 03:41:40 +00:00
|
|
|
r.mouse = false
|
2017-01-21 20:26:38 +00:00
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LightRenderer) Clear() {
|
2017-02-14 13:30:09 +00:00
|
|
|
if r.fullscreen {
|
|
|
|
r.csi("H")
|
|
|
|
}
|
2017-01-09 10:09:30 +00:00
|
|
|
// r.csi("u")
|
|
|
|
r.origin()
|
2017-01-07 16:30:31 +00:00
|
|
|
r.csi("J")
|
|
|
|
r.flush()
|
|
|
|
}
|
|
|
|
|
2023-01-15 16:22:02 +00:00
|
|
|
func (r *LightRenderer) NeedScrollbarRedraw() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (r *LightRenderer) RefreshWindows(windows []Window) {
|
|
|
|
r.flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LightRenderer) Refresh() {
|
|
|
|
r.updateTerminalSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LightRenderer) Close() {
|
2017-01-09 10:09:30 +00:00
|
|
|
// r.csi("u")
|
2017-03-04 02:29:31 +00:00
|
|
|
if r.clearOnExit {
|
|
|
|
if r.fullscreen {
|
|
|
|
r.rmcup()
|
|
|
|
} else {
|
|
|
|
r.origin()
|
|
|
|
if r.upOneLine {
|
|
|
|
r.csi("A")
|
|
|
|
}
|
|
|
|
r.csi("J")
|
2017-01-21 20:26:38 +00:00
|
|
|
}
|
2017-07-18 11:50:38 +00:00
|
|
|
} else if !r.fullscreen {
|
|
|
|
r.csi("u")
|
2017-01-21 20:26:38 +00:00
|
|
|
}
|
2023-01-24 03:00:41 +00:00
|
|
|
r.disableMouse()
|
2017-01-07 16:30:31 +00:00
|
|
|
r.flush()
|
2019-02-05 06:51:39 +00:00
|
|
|
r.closePlatform()
|
|
|
|
r.restoreTerminal()
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LightRenderer) MaxX() int {
|
|
|
|
return r.width
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *LightRenderer) MaxY() int {
|
2022-09-07 16:01:22 +00:00
|
|
|
if r.height == 0 {
|
|
|
|
r.updateTerminalSize()
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
return r.height
|
|
|
|
}
|
|
|
|
|
2019-12-12 14:03:17 +00:00
|
|
|
func (r *LightRenderer) NewWindow(top int, left int, width int, height int, preview bool, borderStyle BorderStyle) Window {
|
2017-01-07 16:30:31 +00:00
|
|
|
w := &LightWindow{
|
|
|
|
renderer: r,
|
2020-10-25 10:29:37 +00:00
|
|
|
colored: r.theme.Colored,
|
2020-03-05 11:15:15 +00:00
|
|
|
preview: preview,
|
2017-02-04 12:51:22 +00:00
|
|
|
border: borderStyle,
|
2017-01-07 16:30:31 +00:00
|
|
|
top: top,
|
|
|
|
left: left,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
tabstop: r.tabstop,
|
2017-09-08 09:33:17 +00:00
|
|
|
fg: colDefault,
|
2017-01-07 16:30:31 +00:00
|
|
|
bg: colDefault}
|
2020-10-25 10:29:37 +00:00
|
|
|
if preview {
|
|
|
|
w.fg = r.theme.PreviewFg.Color
|
|
|
|
w.bg = r.theme.PreviewBg.Color
|
|
|
|
} else {
|
|
|
|
w.fg = r.theme.Fg.Color
|
|
|
|
w.bg = r.theme.Bg.Color
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
2023-01-21 16:56:29 +00:00
|
|
|
w.drawBorder(false)
|
2017-01-07 16:30:31 +00:00
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2023-01-21 16:56:29 +00:00
|
|
|
func (w *LightWindow) DrawHBorder() {
|
|
|
|
w.drawBorder(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *LightWindow) drawBorder(onlyHorizontal bool) {
|
2019-03-28 17:11:03 +00:00
|
|
|
switch w.border.shape {
|
2023-06-10 05:48:29 +00:00
|
|
|
case BorderRounded, BorderSharp, BorderBold, BorderBlock, BorderThinBlock, BorderDouble:
|
2023-01-21 16:56:29 +00:00
|
|
|
w.drawBorderAround(onlyHorizontal)
|
2017-02-04 12:51:22 +00:00
|
|
|
case BorderHorizontal:
|
2020-10-26 13:33:41 +00:00
|
|
|
w.drawBorderHorizontal(true, true)
|
|
|
|
case BorderVertical:
|
2023-01-21 16:56:29 +00:00
|
|
|
if onlyHorizontal {
|
|
|
|
return
|
|
|
|
}
|
2020-10-26 13:33:41 +00:00
|
|
|
w.drawBorderVertical(true, true)
|
|
|
|
case BorderTop:
|
|
|
|
w.drawBorderHorizontal(true, false)
|
|
|
|
case BorderBottom:
|
|
|
|
w.drawBorderHorizontal(false, true)
|
|
|
|
case BorderLeft:
|
2023-01-21 16:56:29 +00:00
|
|
|
if onlyHorizontal {
|
|
|
|
return
|
|
|
|
}
|
2020-10-26 13:33:41 +00:00
|
|
|
w.drawBorderVertical(true, false)
|
|
|
|
case BorderRight:
|
2023-01-21 16:56:29 +00:00
|
|
|
if onlyHorizontal {
|
|
|
|
return
|
|
|
|
}
|
2020-10-26 13:33:41 +00:00
|
|
|
w.drawBorderVertical(false, true)
|
2017-02-04 12:51:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 13:33:41 +00:00
|
|
|
func (w *LightWindow) drawBorderHorizontal(top, bottom bool) {
|
2021-04-06 09:01:02 +00:00
|
|
|
color := ColBorder
|
|
|
|
if w.preview {
|
|
|
|
color = ColPreviewBorder
|
|
|
|
}
|
2023-05-16 14:45:31 +00:00
|
|
|
hw := runewidth.RuneWidth(w.border.top)
|
2020-10-26 13:33:41 +00:00
|
|
|
if top {
|
|
|
|
w.Move(0, 0)
|
2023-05-16 14:45:31 +00:00
|
|
|
w.CPrint(color, repeat(w.border.top, w.width/hw))
|
2020-10-26 13:33:41 +00:00
|
|
|
}
|
|
|
|
if bottom {
|
|
|
|
w.Move(w.height-1, 0)
|
2023-05-16 14:45:31 +00:00
|
|
|
w.CPrint(color, repeat(w.border.bottom, w.width/hw))
|
2020-10-26 13:33:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *LightWindow) drawBorderVertical(left, right bool) {
|
|
|
|
width := w.width - 2
|
|
|
|
if !left || !right {
|
|
|
|
width++
|
|
|
|
}
|
2021-04-06 09:01:02 +00:00
|
|
|
color := ColBorder
|
|
|
|
if w.preview {
|
|
|
|
color = ColPreviewBorder
|
|
|
|
}
|
2020-10-26 13:33:41 +00:00
|
|
|
for y := 0; y < w.height; y++ {
|
|
|
|
w.Move(y, 0)
|
|
|
|
if left {
|
2023-05-16 14:45:31 +00:00
|
|
|
w.CPrint(color, string(w.border.left))
|
2020-10-26 13:33:41 +00:00
|
|
|
}
|
2021-04-06 09:01:02 +00:00
|
|
|
w.CPrint(color, repeat(' ', width))
|
2020-10-26 13:33:41 +00:00
|
|
|
if right {
|
2023-05-16 14:45:31 +00:00
|
|
|
w.CPrint(color, string(w.border.right))
|
2020-10-26 13:33:41 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-04 12:51:22 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 16:56:29 +00:00
|
|
|
func (w *LightWindow) drawBorderAround(onlyHorizontal bool) {
|
2017-01-07 16:30:31 +00:00
|
|
|
w.Move(0, 0)
|
2020-03-05 11:15:15 +00:00
|
|
|
color := ColBorder
|
|
|
|
if w.preview {
|
|
|
|
color = ColPreviewBorder
|
|
|
|
}
|
2023-05-16 14:45:31 +00:00
|
|
|
hw := runewidth.RuneWidth(w.border.top)
|
2023-01-15 16:22:02 +00:00
|
|
|
tcw := runewidth.RuneWidth(w.border.topLeft) + runewidth.RuneWidth(w.border.topRight)
|
|
|
|
bcw := runewidth.RuneWidth(w.border.bottomLeft) + runewidth.RuneWidth(w.border.bottomRight)
|
2023-01-16 10:55:44 +00:00
|
|
|
rem := (w.width - tcw) % hw
|
2023-05-16 14:45:31 +00:00
|
|
|
w.CPrint(color, string(w.border.topLeft)+repeat(w.border.top, (w.width-tcw)/hw)+repeat(' ', rem)+string(w.border.topRight))
|
2023-01-21 16:56:29 +00:00
|
|
|
if !onlyHorizontal {
|
2023-05-16 14:45:31 +00:00
|
|
|
vw := runewidth.RuneWidth(w.border.left)
|
2023-01-21 16:56:29 +00:00
|
|
|
for y := 1; y < w.height-1; y++ {
|
|
|
|
w.Move(y, 0)
|
2023-05-16 14:45:31 +00:00
|
|
|
w.CPrint(color, string(w.border.left))
|
2023-01-21 16:56:29 +00:00
|
|
|
w.CPrint(color, repeat(' ', w.width-vw*2))
|
2023-05-16 14:45:31 +00:00
|
|
|
w.CPrint(color, string(w.border.right))
|
2023-01-21 16:56:29 +00:00
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
w.Move(w.height-1, 0)
|
2023-01-16 10:55:44 +00:00
|
|
|
rem = (w.width - bcw) % hw
|
2023-05-16 14:45:31 +00:00
|
|
|
w.CPrint(color, string(w.border.bottomLeft)+repeat(w.border.bottom, (w.width-bcw)/hw)+repeat(' ', rem)+string(w.border.bottomRight))
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-25 01:23:05 +00:00
|
|
|
func (w *LightWindow) csi(code string) string {
|
|
|
|
return w.renderer.csi(code)
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-25 01:23:05 +00:00
|
|
|
func (w *LightWindow) stderrInternal(str string, allowNLCR bool, resetCode string) {
|
|
|
|
w.renderer.stderrInternal(str, allowNLCR, resetCode)
|
2017-01-15 17:26:36 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *LightWindow) Top() int {
|
|
|
|
return w.top
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *LightWindow) Left() int {
|
|
|
|
return w.left
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *LightWindow) Width() int {
|
|
|
|
return w.width
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *LightWindow) Height() int {
|
|
|
|
return w.height
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *LightWindow) Refresh() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *LightWindow) Close() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *LightWindow) X() int {
|
|
|
|
return w.posx
|
|
|
|
}
|
|
|
|
|
2017-07-19 13:46:16 +00:00
|
|
|
func (w *LightWindow) Y() int {
|
|
|
|
return w.posy
|
|
|
|
}
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
func (w *LightWindow) Enclose(y int, x int) bool {
|
|
|
|
return x >= w.left && x < (w.left+w.width) &&
|
|
|
|
y >= w.top && y < (w.top+w.height)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *LightWindow) Move(y int, x int) {
|
|
|
|
w.posx = x
|
|
|
|
w.posy = y
|
|
|
|
|
2017-01-09 10:09:30 +00:00
|
|
|
w.renderer.move(w.Top()+y, w.Left()+x)
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *LightWindow) MoveAndClear(y int, x int) {
|
|
|
|
w.Move(y, x)
|
|
|
|
// We should not delete preview window on the right
|
|
|
|
// csi("K")
|
2019-03-28 17:11:03 +00:00
|
|
|
w.Print(repeat(' ', w.width-x))
|
2017-01-07 16:30:31 +00:00
|
|
|
w.Move(y, x)
|
|
|
|
}
|
|
|
|
|
|
|
|
func attrCodes(attr Attr) []string {
|
|
|
|
codes := []string{}
|
2020-10-25 10:29:37 +00:00
|
|
|
if (attr & AttrClear) > 0 {
|
|
|
|
return codes
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
if (attr & Bold) > 0 {
|
|
|
|
codes = append(codes, "1")
|
|
|
|
}
|
|
|
|
if (attr & Dim) > 0 {
|
|
|
|
codes = append(codes, "2")
|
|
|
|
}
|
|
|
|
if (attr & Italic) > 0 {
|
|
|
|
codes = append(codes, "3")
|
|
|
|
}
|
|
|
|
if (attr & Underline) > 0 {
|
|
|
|
codes = append(codes, "4")
|
|
|
|
}
|
|
|
|
if (attr & Blink) > 0 {
|
|
|
|
codes = append(codes, "5")
|
|
|
|
}
|
|
|
|
if (attr & Reverse) > 0 {
|
|
|
|
codes = append(codes, "7")
|
|
|
|
}
|
2022-08-20 21:23:03 +00:00
|
|
|
if (attr & StrikeThrough) > 0 {
|
|
|
|
codes = append(codes, "9")
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
return codes
|
|
|
|
}
|
|
|
|
|
|
|
|
func colorCodes(fg Color, bg Color) []string {
|
|
|
|
codes := []string{}
|
|
|
|
appendCode := func(c Color, offset int) {
|
|
|
|
if c == colDefault {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c.is24() {
|
|
|
|
r := (c >> 16) & 0xff
|
|
|
|
g := (c >> 8) & 0xff
|
|
|
|
b := (c) & 0xff
|
|
|
|
codes = append(codes, fmt.Sprintf("%d;2;%d;%d;%d", 38+offset, r, g, b))
|
|
|
|
} else if c >= colBlack && c <= colWhite {
|
|
|
|
codes = append(codes, fmt.Sprintf("%d", int(c)+30+offset))
|
|
|
|
} else if c > colWhite && c < 16 {
|
|
|
|
codes = append(codes, fmt.Sprintf("%d", int(c)+90+offset-8))
|
|
|
|
} else if c >= 16 && c < 256 {
|
|
|
|
codes = append(codes, fmt.Sprintf("%d;5;%d", 38+offset, c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
appendCode(fg, 0)
|
|
|
|
appendCode(bg, 10)
|
|
|
|
return codes
|
|
|
|
}
|
|
|
|
|
2023-03-25 01:23:05 +00:00
|
|
|
func (w *LightWindow) csiColor(fg Color, bg Color, attr Attr) (bool, string) {
|
2017-01-07 16:30:31 +00:00
|
|
|
codes := append(attrCodes(attr), colorCodes(fg, bg)...)
|
2023-03-25 01:23:05 +00:00
|
|
|
code := w.csi(";" + strings.Join(codes, ";") + "m")
|
|
|
|
return len(codes) > 0, code
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *LightWindow) Print(text string) {
|
|
|
|
w.cprint2(colDefault, w.bg, AttrRegular, text)
|
|
|
|
}
|
|
|
|
|
2017-02-02 04:14:27 +00:00
|
|
|
func cleanse(str string) string {
|
2018-04-12 08:49:52 +00:00
|
|
|
return strings.Replace(str, "\x1b", "", -1)
|
2017-02-02 04:14:27 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 10:29:37 +00:00
|
|
|
func (w *LightWindow) CPrint(pair ColorPair, text string) {
|
2023-03-25 01:23:05 +00:00
|
|
|
_, code := w.csiColor(pair.Fg(), pair.Bg(), pair.Attr())
|
|
|
|
w.stderrInternal(cleanse(text), false, code)
|
2017-01-07 16:30:31 +00:00
|
|
|
w.csi("m")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *LightWindow) cprint2(fg Color, bg Color, attr Attr, text string) {
|
2023-03-25 01:23:05 +00:00
|
|
|
hasColors, code := w.csiColor(fg, bg, attr)
|
|
|
|
if hasColors {
|
2017-01-07 16:30:31 +00:00
|
|
|
defer w.csi("m")
|
|
|
|
}
|
2023-03-25 01:23:05 +00:00
|
|
|
w.stderrInternal(cleanse(text), false, code)
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type wrappedLine struct {
|
|
|
|
text string
|
|
|
|
displayWidth int
|
|
|
|
}
|
|
|
|
|
|
|
|
func wrapLine(input string, prefixLength int, max int, tabstop int) []wrappedLine {
|
|
|
|
lines := []wrappedLine{}
|
|
|
|
width := 0
|
|
|
|
line := ""
|
2021-05-14 02:43:32 +00:00
|
|
|
gr := uniseg.NewGraphemes(input)
|
|
|
|
for gr.Next() {
|
|
|
|
rs := gr.Runes()
|
|
|
|
str := string(rs)
|
|
|
|
var w int
|
|
|
|
if len(rs) == 1 && rs[0] == '\t' {
|
|
|
|
w = tabstop - (prefixLength+width)%tabstop
|
2019-03-28 17:11:03 +00:00
|
|
|
str = repeat(' ', w)
|
2023-03-25 01:23:05 +00:00
|
|
|
} else if rs[0] == '\r' {
|
|
|
|
w++
|
2021-05-14 02:43:32 +00:00
|
|
|
} else {
|
|
|
|
w = runewidth.StringWidth(str)
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
2021-05-14 02:43:32 +00:00
|
|
|
width += w
|
|
|
|
|
2017-01-07 16:30:31 +00:00
|
|
|
if prefixLength+width <= max {
|
|
|
|
line += str
|
|
|
|
} else {
|
|
|
|
lines = append(lines, wrappedLine{string(line), width - w})
|
|
|
|
line = str
|
|
|
|
prefixLength = 0
|
2021-05-14 02:43:32 +00:00
|
|
|
width = w
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
lines = append(lines, wrappedLine{string(line), width})
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2023-03-25 01:23:05 +00:00
|
|
|
func (w *LightWindow) fill(str string, resetCode string) FillReturn {
|
2017-01-07 16:30:31 +00:00
|
|
|
allLines := strings.Split(str, "\n")
|
|
|
|
for i, line := range allLines {
|
|
|
|
lines := wrapLine(line, w.posx, w.width, w.tabstop)
|
|
|
|
for j, wl := range lines {
|
2023-03-25 01:23:05 +00:00
|
|
|
w.stderrInternal(wl.text, false, resetCode)
|
2017-01-07 16:30:31 +00:00
|
|
|
w.posx += wl.displayWidth
|
2017-07-19 13:46:16 +00:00
|
|
|
|
|
|
|
// Wrap line
|
2017-01-07 16:30:31 +00:00
|
|
|
if j < len(lines)-1 || i < len(allLines)-1 {
|
|
|
|
if w.posy+1 >= w.height {
|
2017-01-11 13:13:40 +00:00
|
|
|
return FillSuspend
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
2017-07-19 13:46:16 +00:00
|
|
|
w.MoveAndClear(w.posy, w.posx)
|
|
|
|
w.Move(w.posy+1, 0)
|
2023-03-25 01:23:05 +00:00
|
|
|
w.renderer.stderr(resetCode)
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-12 12:23:16 +00:00
|
|
|
if w.posx+1 >= w.Width() {
|
|
|
|
if w.posy+1 >= w.height {
|
|
|
|
return FillSuspend
|
|
|
|
}
|
|
|
|
w.Move(w.posy+1, 0)
|
2023-03-25 01:23:05 +00:00
|
|
|
w.renderer.stderr(resetCode)
|
2021-03-12 10:56:24 +00:00
|
|
|
return FillNextLine
|
|
|
|
}
|
2017-01-11 13:13:40 +00:00
|
|
|
return FillContinue
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-25 01:23:05 +00:00
|
|
|
func (w *LightWindow) setBg() string {
|
2017-01-07 16:30:31 +00:00
|
|
|
if w.bg != colDefault {
|
2023-03-25 01:23:05 +00:00
|
|
|
_, code := w.csiColor(colDefault, w.bg, AttrRegular)
|
|
|
|
return code
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
2023-03-25 01:23:05 +00:00
|
|
|
// Should clear dim attribute after ␍ in the preview window
|
|
|
|
// e.g. printf "foo\rbar" | fzf --ansi --preview 'printf "foo\rbar"'
|
|
|
|
return "\x1b[m"
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 13:13:40 +00:00
|
|
|
func (w *LightWindow) Fill(text string) FillReturn {
|
2017-07-19 13:46:16 +00:00
|
|
|
w.Move(w.posy, w.posx)
|
2023-03-25 01:23:05 +00:00
|
|
|
code := w.setBg()
|
|
|
|
return w.fill(text, code)
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 13:13:40 +00:00
|
|
|
func (w *LightWindow) CFill(fg Color, bg Color, attr Attr, text string) FillReturn {
|
2017-07-19 13:46:16 +00:00
|
|
|
w.Move(w.posy, w.posx)
|
2017-09-08 09:33:17 +00:00
|
|
|
if fg == colDefault {
|
|
|
|
fg = w.fg
|
|
|
|
}
|
2017-01-07 16:30:31 +00:00
|
|
|
if bg == colDefault {
|
|
|
|
bg = w.bg
|
|
|
|
}
|
2023-03-25 01:23:05 +00:00
|
|
|
if hasColors, resetCode := w.csiColor(fg, bg, attr); hasColors {
|
2017-01-07 16:30:31 +00:00
|
|
|
defer w.csi("m")
|
2023-03-25 01:23:05 +00:00
|
|
|
return w.fill(text, resetCode)
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
2023-03-25 01:23:05 +00:00
|
|
|
return w.fill(text, w.setBg())
|
2017-01-07 16:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *LightWindow) FinishFill() {
|
2017-07-19 13:46:16 +00:00
|
|
|
w.MoveAndClear(w.posy, w.posx)
|
2017-01-07 16:30:31 +00:00
|
|
|
for y := w.posy + 1; y < w.height; y++ {
|
|
|
|
w.MoveAndClear(y, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *LightWindow) Erase() {
|
2023-01-21 16:56:29 +00:00
|
|
|
w.drawBorder(false)
|
2017-01-07 16:30:31 +00:00
|
|
|
// We don't erase the window here to avoid flickering during scroll
|
|
|
|
w.Move(0, 0)
|
|
|
|
}
|