Add OSC 8 support (#2243)
* Add `OSC 8` support * Rename functions * Update tests * Support grouped links * Update `CHANGELOG.md` * Move terminal sequence parsing to new file
This commit is contained in:
1 parent
830f072961
commit
e45412d363
10 files changed
+497
-306
No files matched your search
@@ -26,6 +26,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||
- The `lf_mode` environment variable will now be set to `compmenu` if the completion menu is active (#2146).
|
||||
- A `ruler` config file is added as an alternate method for customizing the ruler (#2186). This is intended to eventually replace the existing `rulerfmt`/`statfmt` options and must be enabled using the new `rulerfile` option. **This feature is currently experimental.**
|
||||
- A new option `preload` is added to enable calling the `previewer` to generate previews in advance (#2206). **This feature is currently experimental.**
|
||||
- `OSC 8` escape codes to render clickable hyperlinks are now supported (#2243).
|
||||
|
||||
### Fixed
|
||||
|
||||
|
||||
@@ -124,101 +124,6 @@ func parseColor(toks []string) (tcell.Color, int, error) {
|
||||
return tcell.ColorDefault, 0, fmt.Errorf("invalid args: %v", toks)
|
||||
}
|
||||
|
||||
func applyAnsiCodes(s string, st tcell.Style) tcell.Style {
|
||||
toks := strings.Split(s, ";")
|
||||
|
||||
// ECMA-48 details the standard
|
||||
tokslen := len(toks)
|
||||
|
||||
loop:
|
||||
for i := 0; i < tokslen; i++ {
|
||||
switch strings.TrimLeft(toks[i], "0") {
|
||||
case "":
|
||||
st = tcell.StyleDefault
|
||||
case "1":
|
||||
st = st.Bold(true)
|
||||
case "2":
|
||||
st = st.Dim(true)
|
||||
case "3":
|
||||
st = st.Italic(true)
|
||||
case "4:0":
|
||||
st = st.Underline(false)
|
||||
case "4", "4:1":
|
||||
st = st.Underline(true)
|
||||
case "4:2":
|
||||
st = st.Underline(tcell.UnderlineStyleDouble)
|
||||
case "4:3":
|
||||
st = st.Underline(tcell.UnderlineStyleCurly)
|
||||
case "4:4":
|
||||
st = st.Underline(tcell.UnderlineStyleDotted)
|
||||
case "4:5":
|
||||
st = st.Underline(tcell.UnderlineStyleDashed)
|
||||
case "5", "6":
|
||||
st = st.Blink(true)
|
||||
case "7":
|
||||
st = st.Reverse(true)
|
||||
case "8":
|
||||
// TODO: tcell PR for proper conceal
|
||||
_, bg, _ := st.Decompose()
|
||||
st = st.Foreground(bg)
|
||||
case "9":
|
||||
st = st.StrikeThrough(true)
|
||||
case "22":
|
||||
st = st.Bold(false).Dim(false)
|
||||
case "23":
|
||||
st = st.Italic(false)
|
||||
case "24":
|
||||
st = st.Underline(false)
|
||||
case "25":
|
||||
st = st.Blink(false)
|
||||
case "27":
|
||||
st = st.Reverse(false)
|
||||
case "29":
|
||||
st = st.StrikeThrough(false)
|
||||
case "30", "31", "32", "33", "34", "35", "36", "37":
|
||||
n, _ := strconv.Atoi(toks[i])
|
||||
st = st.Foreground(tcell.PaletteColor(n - 30))
|
||||
case "90", "91", "92", "93", "94", "95", "96", "97":
|
||||
n, _ := strconv.Atoi(toks[i])
|
||||
st = st.Foreground(tcell.PaletteColor(n - 82))
|
||||
case "38":
|
||||
color, offset, err := parseColor(toks[i+1:])
|
||||
if err != nil {
|
||||
log.Printf("error processing ansi code 38: %s", err)
|
||||
break loop
|
||||
}
|
||||
st = st.Foreground(color)
|
||||
i += offset
|
||||
case "40", "41", "42", "43", "44", "45", "46", "47":
|
||||
n, _ := strconv.Atoi(toks[i])
|
||||
st = st.Background(tcell.PaletteColor(n - 40))
|
||||
case "100", "101", "102", "103", "104", "105", "106", "107":
|
||||
n, _ := strconv.Atoi(toks[i])
|
||||
st = st.Background(tcell.PaletteColor(n - 92))
|
||||
case "48":
|
||||
color, offset, err := parseColor(toks[i+1:])
|
||||
if err != nil {
|
||||
log.Printf("error processing ansi code 48: %s", err)
|
||||
break loop
|
||||
}
|
||||
st = st.Background(color)
|
||||
i += offset
|
||||
case "58":
|
||||
color, offset, err := parseColor(toks[i+1:])
|
||||
if err != nil {
|
||||
log.Printf("error processing ansi code 58: %s", err)
|
||||
break loop
|
||||
}
|
||||
st = st.Underline(color)
|
||||
i += offset
|
||||
default:
|
||||
log.Printf("unknown ansi code: %s", toks[i])
|
||||
}
|
||||
}
|
||||
|
||||
return st
|
||||
}
|
||||
|
||||
func (sm styleMap) parseFile(path string) {
|
||||
log.Printf("reading file: %s", path)
|
||||
|
||||
@@ -271,7 +176,7 @@ func (sm *styleMap) parsePair(pair []string) {
|
||||
sm.useLinkTarget = true
|
||||
}
|
||||
|
||||
sm.styles[key] = applyAnsiCodes(val, tcell.StyleDefault)
|
||||
sm.styles[key] = applySGR(val, tcell.StyleDefault)
|
||||
}
|
||||
|
||||
// This function parses $LSCOLORS environment variable.
|
||||
|
||||
@@ -34,95 +34,3 @@ func TestParseColor(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyAnsiCodes(t *testing.T) {
|
||||
none := tcell.StyleDefault
|
||||
|
||||
tests := []struct {
|
||||
s string
|
||||
st tcell.Style
|
||||
stExp tcell.Style
|
||||
}{
|
||||
{"", none, none},
|
||||
{"", none.Foreground(tcell.ColorMaroon).Background(tcell.ColorMaroon), none},
|
||||
{"", none.Bold(true), none},
|
||||
{"", none.Foreground(tcell.ColorMaroon).Bold(true), none},
|
||||
|
||||
{"0", none, none},
|
||||
{"0", none.Foreground(tcell.ColorMaroon).Background(tcell.ColorMaroon), none},
|
||||
{"0", none.Bold(true), none},
|
||||
{"0", none.Foreground(tcell.ColorMaroon).Bold(true), none},
|
||||
|
||||
{"1", none, none.Bold(true)},
|
||||
{"4", none, none.Underline(true)},
|
||||
{"7", none, none.Reverse(true)},
|
||||
|
||||
{"1", none.Foreground(tcell.ColorMaroon), none.Foreground(tcell.ColorMaroon).Bold(true)},
|
||||
{"4", none.Foreground(tcell.ColorMaroon), none.Foreground(tcell.ColorMaroon).Underline(true)},
|
||||
{"7", none.Foreground(tcell.ColorMaroon), none.Foreground(tcell.ColorMaroon).Reverse(true)},
|
||||
|
||||
{"4", none.Bold(true), none.Bold(true).Underline(true)},
|
||||
{"4", none.Foreground(tcell.ColorMaroon).Bold(true), none.Foreground(tcell.ColorMaroon).Bold(true).Underline(true)},
|
||||
|
||||
{"4:0", none, none},
|
||||
{"4:0", none.Underline(true), none},
|
||||
{"4:1", none, none.Underline(true)},
|
||||
{"4:2", none, none.Underline(tcell.UnderlineStyleDouble)},
|
||||
{"4:3", none, none.Underline(tcell.UnderlineStyleCurly)},
|
||||
{"4:4", none, none.Underline(tcell.UnderlineStyleDotted)},
|
||||
{"4:5", none, none.Underline(tcell.UnderlineStyleDashed)},
|
||||
|
||||
{"22", none.Italic(true).Bold(true).Dim(true), none.Italic(true)},
|
||||
{"23", none.Bold(true).Italic(true), none.Bold(true)},
|
||||
{"24", none.Bold(true).Underline(true), none.Bold(true)},
|
||||
{"25", none.Bold(true).Blink(true), none.Bold(true)},
|
||||
{"27", none.Bold(true).Reverse(true), none.Bold(true)},
|
||||
{"29", none.Bold(true).StrikeThrough(true), none.Bold(true)},
|
||||
|
||||
{"31", none, none.Foreground(tcell.ColorMaroon)},
|
||||
{"31", none.Foreground(tcell.ColorGreen), none.Foreground(tcell.ColorMaroon)},
|
||||
{"31", none.Foreground(tcell.ColorGreen).Bold(true), none.Foreground(tcell.ColorMaroon).Bold(true)},
|
||||
|
||||
{"41", none, none.Background(tcell.ColorMaroon)},
|
||||
{"41", none.Background(tcell.ColorGreen), none.Background(tcell.ColorMaroon)},
|
||||
|
||||
{"1;31", none, none.Foreground(tcell.ColorMaroon).Bold(true)},
|
||||
{"1;31", none.Foreground(tcell.ColorGreen), none.Foreground(tcell.ColorMaroon).Bold(true)},
|
||||
|
||||
{"01;31", none, none.Foreground(tcell.ColorMaroon).Bold(true)},
|
||||
{"01;31", none.Foreground(tcell.ColorGreen), none.Foreground(tcell.ColorMaroon).Bold(true)},
|
||||
|
||||
{"38;5;0", none, none.Foreground(tcell.ColorBlack)},
|
||||
{"38;5;1", none, none.Foreground(tcell.ColorMaroon)},
|
||||
{"38;5;8", none, none.Foreground(tcell.ColorGray)},
|
||||
{"38;5;16", none, none.Foreground(tcell.Color16)},
|
||||
{"38;5;232", none, none.Foreground(tcell.Color232)},
|
||||
|
||||
{"38;5;1", none.Foreground(tcell.ColorGreen), none.Foreground(tcell.ColorMaroon)},
|
||||
{"38;5;1", none.Foreground(tcell.ColorGreen).Bold(true), none.Foreground(tcell.ColorMaroon).Bold(true)},
|
||||
|
||||
{"48;5;0", none, none.Background(tcell.ColorBlack)},
|
||||
{"48;5;1", none, none.Background(tcell.ColorMaroon)},
|
||||
{"48;5;8", none, none.Background(tcell.ColorGray)},
|
||||
{"48;5;16", none, none.Background(tcell.Color16)},
|
||||
{"48;5;232", none, none.Background(tcell.Color232)},
|
||||
|
||||
{"48;5;1", none.Background(tcell.ColorGreen), none.Background(tcell.ColorMaroon)},
|
||||
|
||||
{"1;38;5;1", none, none.Foreground(tcell.ColorMaroon).Bold(true)},
|
||||
{"1;38;5;1", none.Foreground(tcell.ColorGreen), none.Foreground(tcell.ColorMaroon).Bold(true)},
|
||||
|
||||
{"38;2;5;102;8", none, none.Foreground(tcell.NewRGBColor(5, 102, 8))},
|
||||
{"48;2;0;48;143", none, none.Background(tcell.NewRGBColor(0, 48, 143))},
|
||||
|
||||
// Fixes color construction issue: https://github.com/gokcehan/lf/pull/439#issuecomment-674409446
|
||||
{"38;5;34;1", none, none.Foreground(tcell.Color34).Bold(true)},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
if stGot := applyAnsiCodes(test.s, test.st); stGot != test.stExp {
|
||||
t.Errorf("at input '%s' with '%v' expected '%v' but got '%v'",
|
||||
test.s, test.st, test.stExp, stGot)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -123,7 +123,7 @@ func (im *iconMap) parseArray(arr []string) {
|
||||
}
|
||||
case 3:
|
||||
icon, color := arr[1], arr[2]
|
||||
im.icons[key] = iconWithStyle(icon, applyAnsiCodes(color, tcell.StyleDefault))
|
||||
im.icons[key] = iconWithStyle(icon, applySGR(color, tcell.StyleDefault))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,9 +13,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/mattn/go-runewidth"
|
||||
)
|
||||
|
||||
@@ -410,47 +408,6 @@ func optionToFmtstr(optstr string) string {
|
||||
}
|
||||
}
|
||||
|
||||
// This function takes an escape sequence option (e.g. `\033[1m`) and converts
|
||||
// it to a `tcell.Style` object.
|
||||
func parseEscapeSequence(s string) tcell.Style {
|
||||
s = strings.TrimPrefix(s, "\033[")
|
||||
if i := strings.IndexByte(s, 'm'); i >= 0 {
|
||||
s = s[:i]
|
||||
}
|
||||
return applyAnsiCodes(s, tcell.StyleDefault)
|
||||
}
|
||||
|
||||
// This function is used to remove style-related ANSI escape sequences from
|
||||
// a given string.
|
||||
//
|
||||
// *Note*: this function is based entirely on `printLength()` and strips only
|
||||
// style-related escape sequences and the `erase in line` sequence. Other codes
|
||||
// (e.g., cursor moves), as well as broken escape sequences, aren't removed.
|
||||
// This prevents mismatches between the two functions and avoids misalignment
|
||||
// when rendering the UI.
|
||||
func stripAnsi(s string) string {
|
||||
var b strings.Builder
|
||||
slen := len(s)
|
||||
for i := 0; i < slen; i++ {
|
||||
r, w := utf8.DecodeRuneInString(s[i:])
|
||||
|
||||
if r == gEscapeCode && i+1 < slen && s[i+1] == '[' {
|
||||
j := strings.IndexAny(s[i:min(slen, i+64)], "mK")
|
||||
if j == -1 {
|
||||
continue
|
||||
}
|
||||
|
||||
i += j
|
||||
continue
|
||||
}
|
||||
|
||||
i += w - 1
|
||||
b.WriteRune(r)
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// This function reads lines from a file to be displayed as a preview.
|
||||
// The number of lines to read is capped since files can be very large.
|
||||
// Lines are split on `\n` characters, and `\r` characters are discarded.
|
||||
|
||||
@@ -7,8 +7,6 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
)
|
||||
|
||||
func TestIsRoot(t *testing.T) {
|
||||
@@ -493,54 +491,6 @@ func TestOptionToFmtstr(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseEscapeSequence(t *testing.T) {
|
||||
tests := []struct {
|
||||
s string
|
||||
exp tcell.Style
|
||||
}{
|
||||
{"\033[1m", tcell.StyleDefault.Bold(true)},
|
||||
{"\033[1;7;31;42m", tcell.StyleDefault.Bold(true).Reverse(true).Foreground(tcell.ColorMaroon).Background(tcell.ColorGreen)},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
if got := parseEscapeSequence(test.s); got != test.exp {
|
||||
t.Errorf("at input %q expected '%v' but got '%v'", test.s, test.exp, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStripAnsi(t *testing.T) {
|
||||
tests := []struct {
|
||||
s string
|
||||
exp string
|
||||
}{
|
||||
{"", ""}, // empty
|
||||
{"foo bar", "foo bar"}, // plain text
|
||||
{"\033[31mRed\033[0m", "Red"}, // octal syntax
|
||||
{"\x1b[31mRed\x1b[0m", "Red"}, // hexadecimal syntax
|
||||
{"foo\x1b[31mRed", "fooRed"}, // no reset parameter
|
||||
{
|
||||
"foo\x1b[1;31;102mBoldRedGreen\x1b[0mbar",
|
||||
"fooBoldRedGreenbar",
|
||||
}, // multiple attributes
|
||||
{
|
||||
"misc.go:func \x1b[01;31m\x1b[KstripAnsi\x1b[m\x1b[K(s string) string {",
|
||||
"misc.go:func stripAnsi(s string) string {",
|
||||
}, // `grep` output containing `erase in line` sequence
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
if got := stripAnsi(test.s); got != test.exp {
|
||||
t.Errorf("at input %q expected %q but got %q", test.s, test.exp, got)
|
||||
}
|
||||
// we rely on both functions extracting the same runes
|
||||
// to avoid misalignment
|
||||
if printLength(test.s) != len(stripAnsi(test.s)) {
|
||||
t.Errorf("at input %q expected '%d' but got '%d'", test.s, printLength(test.s), len(stripAnsi(test.s)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadLines(t *testing.T) {
|
||||
tests := []struct {
|
||||
s string
|
||||
|
||||
@@ -331,8 +331,8 @@ func (dir *dir) sort() {
|
||||
})
|
||||
case customSort:
|
||||
applySort(func(f1, f2 *file) int {
|
||||
s1 := normalize(stripAnsi(f1.customInfo))
|
||||
s2 := normalize(stripAnsi(f2.customInfo))
|
||||
s1 := normalize(stripTermSequence(f1.customInfo))
|
||||
s2 := normalize(stripTermSequence(f2.customInfo))
|
||||
return naturalCmp(s1, s2)
|
||||
})
|
||||
}
|
||||
|
||||
+261
@@ -0,0 +1,261 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
)
|
||||
|
||||
// This function is used to remove style-related ANSI escape sequences from
|
||||
// a given string.
|
||||
//
|
||||
// *Note*: this function is based entirely on `printLength()` and strips only
|
||||
// style-related sequences, `erase in line` and `OSC 8` sequences. Other codes
|
||||
// (e.g., cursor moves), as well as broken escape sequences, aren't removed.
|
||||
// This prevents mismatches between the two functions and avoids misalignment
|
||||
// when rendering the UI.
|
||||
func stripTermSequence(s string) string {
|
||||
var b strings.Builder
|
||||
slen := len(s)
|
||||
for i := 0; i < slen; i++ {
|
||||
seq := readTermSequence(s[i:])
|
||||
if seq != "" {
|
||||
i += len(seq) - 1 // skip known sequence
|
||||
continue
|
||||
}
|
||||
|
||||
r, w := utf8.DecodeRuneInString(s[i:])
|
||||
i += w - 1
|
||||
b.WriteRune(r)
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// This function is used to extract and return a terminal sequence from a given
|
||||
// string. If no supported sequence could be found, an empty string is returned.
|
||||
//
|
||||
// CSI (Control Sequence Introducer):
|
||||
// - SGR (Select Graphic Rendition) `m`, used for text styling
|
||||
// - EL (Erase in Line) `K`, returned only so we can skip it
|
||||
//
|
||||
// OSC (Operating System Command):
|
||||
// - OSC 8, hyperlinks
|
||||
func readTermSequence(s string) string {
|
||||
slen := len(s)
|
||||
// must start with ESC
|
||||
if slen < 2 || s[0] != byte(gEscapeCode) {
|
||||
return ""
|
||||
}
|
||||
|
||||
switch s[1] {
|
||||
case '[': // CSI
|
||||
i := strings.IndexAny(s[:min(slen, 64)], "mK")
|
||||
if i == -1 {
|
||||
return ""
|
||||
}
|
||||
return s[:i+1]
|
||||
case ']': // OSC
|
||||
if slen < 4 || s[2] != '8' || s[3] != ';' {
|
||||
return ""
|
||||
}
|
||||
// find string terminator
|
||||
for i := 4; i < slen; i++ {
|
||||
b := s[i]
|
||||
// BEL (XTerm)
|
||||
if b == 0x07 {
|
||||
return s[:i+1]
|
||||
}
|
||||
// ESC\ (ECMA-48)
|
||||
if b == byte(gEscapeCode) && i+1 < slen && s[i+1] == '\\' {
|
||||
return s[:i+2]
|
||||
}
|
||||
}
|
||||
// TODO: C1 forms?
|
||||
return ""
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// This function takes an escape sequence option (e.g. `\033[1m`) and converts
|
||||
// it to a `tcell.Style` object.
|
||||
// Legacy function that only accepts SGR. Kept for convenience.
|
||||
func parseEscapeSequence(s string) tcell.Style {
|
||||
s = strings.TrimPrefix(s, "\033[")
|
||||
if i := strings.IndexByte(s, 'm'); i >= 0 {
|
||||
s = s[:i]
|
||||
}
|
||||
return applySGR(s, tcell.StyleDefault)
|
||||
}
|
||||
|
||||
// This function takes an escape sequence (e.g. `\033[1m`) and applies it
|
||||
// to the given `tcell.Style` object.
|
||||
// Accepts SGR and OSC sequences.
|
||||
func applyTermSequence(s string, st tcell.Style) tcell.Style {
|
||||
slen := len(s)
|
||||
if slen < 2 || s[0] != byte(gEscapeCode) {
|
||||
return st
|
||||
}
|
||||
switch s[1] {
|
||||
case '[':
|
||||
if s[slen-1] == 'm' {
|
||||
return applySGR(s[2:slen-1], st)
|
||||
}
|
||||
return st
|
||||
case ']':
|
||||
// trim terminator (BEL or ESC\), then parse body
|
||||
if s[slen-1] == 0x07 {
|
||||
return applyOSC(s[2:slen-1], st)
|
||||
} else if slen >= 2 && s[slen-2] == byte(gEscapeCode) && s[slen-1] == '\\' {
|
||||
return applyOSC(s[2:slen-2], st)
|
||||
}
|
||||
return st
|
||||
default:
|
||||
return st
|
||||
}
|
||||
}
|
||||
|
||||
func applySGR(s string, st tcell.Style) tcell.Style {
|
||||
toks := strings.Split(s, ";")
|
||||
|
||||
// ECMA-48 details the standard
|
||||
tokslen := len(toks)
|
||||
|
||||
loop:
|
||||
for i := 0; i < tokslen; i++ {
|
||||
switch strings.TrimLeft(toks[i], "0") {
|
||||
case "":
|
||||
st = tcell.StyleDefault
|
||||
case "1":
|
||||
st = st.Bold(true)
|
||||
case "2":
|
||||
st = st.Dim(true)
|
||||
case "3":
|
||||
st = st.Italic(true)
|
||||
case "4:0":
|
||||
st = st.Underline(false)
|
||||
case "4", "4:1":
|
||||
st = st.Underline(true)
|
||||
case "4:2":
|
||||
st = st.Underline(tcell.UnderlineStyleDouble)
|
||||
case "4:3":
|
||||
st = st.Underline(tcell.UnderlineStyleCurly)
|
||||
case "4:4":
|
||||
st = st.Underline(tcell.UnderlineStyleDotted)
|
||||
case "4:5":
|
||||
st = st.Underline(tcell.UnderlineStyleDashed)
|
||||
case "5", "6":
|
||||
st = st.Blink(true)
|
||||
case "7":
|
||||
st = st.Reverse(true)
|
||||
case "8":
|
||||
// TODO: tcell PR for proper conceal
|
||||
_, bg, _ := st.Decompose()
|
||||
st = st.Foreground(bg)
|
||||
case "9":
|
||||
st = st.StrikeThrough(true)
|
||||
case "22":
|
||||
st = st.Bold(false).Dim(false)
|
||||
case "23":
|
||||
st = st.Italic(false)
|
||||
case "24":
|
||||
st = st.Underline(false)
|
||||
case "25":
|
||||
st = st.Blink(false)
|
||||
case "27":
|
||||
st = st.Reverse(false)
|
||||
case "29":
|
||||
st = st.StrikeThrough(false)
|
||||
case "30", "31", "32", "33", "34", "35", "36", "37":
|
||||
n, _ := strconv.Atoi(toks[i])
|
||||
st = st.Foreground(tcell.PaletteColor(n - 30))
|
||||
case "90", "91", "92", "93", "94", "95", "96", "97":
|
||||
n, _ := strconv.Atoi(toks[i])
|
||||
st = st.Foreground(tcell.PaletteColor(n - 82))
|
||||
case "38":
|
||||
color, offset, err := parseColor(toks[i+1:])
|
||||
if err != nil {
|
||||
log.Printf("error processing ansi code 38: %s", err)
|
||||
break loop
|
||||
}
|
||||
st = st.Foreground(color)
|
||||
i += offset
|
||||
case "40", "41", "42", "43", "44", "45", "46", "47":
|
||||
n, _ := strconv.Atoi(toks[i])
|
||||
st = st.Background(tcell.PaletteColor(n - 40))
|
||||
case "100", "101", "102", "103", "104", "105", "106", "107":
|
||||
n, _ := strconv.Atoi(toks[i])
|
||||
st = st.Background(tcell.PaletteColor(n - 92))
|
||||
case "48":
|
||||
color, offset, err := parseColor(toks[i+1:])
|
||||
if err != nil {
|
||||
log.Printf("error processing ansi code 48: %s", err)
|
||||
break loop
|
||||
}
|
||||
st = st.Background(color)
|
||||
i += offset
|
||||
case "58":
|
||||
color, offset, err := parseColor(toks[i+1:])
|
||||
if err != nil {
|
||||
log.Printf("error processing ansi code 58: %s", err)
|
||||
break loop
|
||||
}
|
||||
st = st.Underline(color)
|
||||
i += offset
|
||||
default:
|
||||
log.Printf("unknown ansi code: %s", toks[i])
|
||||
}
|
||||
}
|
||||
|
||||
return st
|
||||
}
|
||||
|
||||
// https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
|
||||
func applyOSC(body string, st tcell.Style) tcell.Style {
|
||||
genAutoID := func(url string) string {
|
||||
sum := sha256.Sum256([]byte(url))
|
||||
return "lf_hyperlink_" + hex.EncodeToString(sum[:8])
|
||||
}
|
||||
extractID := func(params string) string {
|
||||
for seg := range strings.SplitSeq(params, ":") {
|
||||
if seg == "" {
|
||||
continue
|
||||
}
|
||||
if k, v, ok := strings.Cut(seg, "="); ok && k == "id" {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
toks := strings.SplitN(body, ";", 3)
|
||||
if len(toks) < 2 {
|
||||
return st
|
||||
}
|
||||
switch toks[0] {
|
||||
case "8":
|
||||
if len(toks) < 3 {
|
||||
return st
|
||||
}
|
||||
url := toks[2]
|
||||
if url == "" {
|
||||
return st
|
||||
}
|
||||
// Optional property used to identify grouped hyperlinks.
|
||||
// Use hash as a fallback to ensure a "unique" id.
|
||||
if id := extractID(toks[1]); id != "" {
|
||||
st = st.UrlId(id)
|
||||
} else {
|
||||
st = st.UrlId(genAutoID(url))
|
||||
}
|
||||
return st.Url(url)
|
||||
default:
|
||||
return st
|
||||
}
|
||||
}
|
||||
+221
@@ -0,0 +1,221 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
)
|
||||
|
||||
func TestStripTermSequence(t *testing.T) {
|
||||
tests := []struct {
|
||||
s string
|
||||
exp string
|
||||
}{
|
||||
{"", ""}, // empty
|
||||
{"foo bar", "foo bar"}, // plain text
|
||||
{"\033[31mRed\033[0m", "Red"}, // octal syntax
|
||||
{"\x1b[31mRed\x1b[0m", "Red"}, // hexadecimal syntax
|
||||
{"foo\x1b[31mRed", "fooRed"}, // no reset parameter
|
||||
{
|
||||
"foo\x1b[1;31;102mBoldRedGreen\x1b[0mbar",
|
||||
"fooBoldRedGreenbar",
|
||||
}, // multiple attributes
|
||||
{
|
||||
"misc.go:func \x1b[01;31m\x1b[KstripTermSequence\x1b[m\x1b[K(s string) string {",
|
||||
"misc.go:func stripTermSequence(s string) string {",
|
||||
}, // `grep` output containing `erase in line` sequence
|
||||
|
||||
// OSC 8 hyperlinks
|
||||
{
|
||||
"\x1b]8;;https://example.com\x1b\\example.com\x1b]8;;\x1b\\",
|
||||
"example.com",
|
||||
}, // open/close with ST (ESC\)
|
||||
{
|
||||
"\x1b]8;;https://example.com\x07example.com\x1b]8;;\x07",
|
||||
"example.com",
|
||||
}, // open/close with BEL
|
||||
{
|
||||
"\x1b]8;id=42;https://example.com\x1b\\label\x1b]8;;\x1b\\",
|
||||
"label",
|
||||
}, // params present
|
||||
{
|
||||
"\x1b]8;;https://example.com\x1b\\example.com",
|
||||
"example.com",
|
||||
}, // open without close
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
if got := stripTermSequence(test.s); got != test.exp {
|
||||
t.Errorf("at input %q expected %q but got %q", test.s, test.exp, got)
|
||||
}
|
||||
// we rely on both functions extracting the same runes
|
||||
// to avoid misalignment
|
||||
if printLength(test.s) != len(stripTermSequence(test.s)) {
|
||||
t.Errorf("at input %q expected '%d' but got '%d'", test.s, printLength(test.s), len(stripTermSequence(test.s)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadTermSequence(t *testing.T) {
|
||||
tests := []struct {
|
||||
s, exp string
|
||||
}{
|
||||
{"", ""}, // empty
|
||||
{"foo bar", ""}, // plain text
|
||||
{"\x1b", ""}, // lone ESC
|
||||
{"\x1bX", ""}, // unknown ESC sequence
|
||||
|
||||
{"\x1b[31m", "\x1b[31m"}, // CSI SGR
|
||||
{"\x1b[K", "\x1b[K"}, // CSI EL
|
||||
{"\x1b[1;31m", "\x1b[1;31m"}, // CSI SGR (multiple params)
|
||||
{"\x1b[31", ""}, // CSI incomplete (no terminator)
|
||||
{"foo\x1b[31m", ""}, // doesn't start with ESC
|
||||
|
||||
{"\x1b]8;;https://example.com\x1b\\", "\x1b]8;;https://example.com\x1b\\"}, // OSC 8 (ST terminator)
|
||||
{"\x1b]8;;https://example.com\x07", "\x1b]8;;https://example.com\x07"}, // OSC 8 (BEL terminator)
|
||||
{"\x1b]0;title\x07", ""}, // non-OSC8 OSC (ignored)
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
if got := readTermSequence(tc.s); got != tc.exp {
|
||||
t.Errorf("input %q: got %q, want %q", tc.s, got, tc.exp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseEscapeSequence(t *testing.T) {
|
||||
tests := []struct {
|
||||
s string
|
||||
exp tcell.Style
|
||||
}{
|
||||
{"\033[1m", tcell.StyleDefault.Bold(true)},
|
||||
{"\033[1;7;31;42m", tcell.StyleDefault.Bold(true).Reverse(true).Foreground(tcell.ColorMaroon).Background(tcell.ColorGreen)},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
if got := parseEscapeSequence(test.s); got != test.exp {
|
||||
t.Errorf("at input %q expected '%v' but got '%v'", test.s, test.exp, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyTermSequence(t *testing.T) {
|
||||
tests := []struct {
|
||||
s string
|
||||
exp tcell.Style
|
||||
}{
|
||||
{"", tcell.StyleDefault},
|
||||
{"\x1b[1m", tcell.StyleDefault.Bold(true)},
|
||||
{"\x1b[1;7;31;42m", tcell.StyleDefault.Bold(true).Reverse(true).Foreground(tcell.ColorMaroon).Background(tcell.ColorGreen)},
|
||||
{
|
||||
"\x1b]8;;https://example.com\x1b\\",
|
||||
tcell.StyleDefault.UrlId("lf_hyperlink_100680ad546ce6a5").Url("https://example.com"),
|
||||
}, // OSC 8 terminated with ST (ESC\), no `id` provided
|
||||
{
|
||||
"\x1b]8;;https://example.com\x07",
|
||||
tcell.StyleDefault.UrlId("lf_hyperlink_100680ad546ce6a5").Url("https://example.com"),
|
||||
}, // OSC 8 terminated with BEL, no `id` provided
|
||||
{
|
||||
"\x1b]8;id=42;https://example.com\x1b\\",
|
||||
tcell.StyleDefault.UrlId("42").Url("https://example.com"),
|
||||
}, // OSC 8, `id` provided
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
if got := applyTermSequence(test.s, tcell.StyleDefault); got != test.exp {
|
||||
t.Errorf("at input %q expected '%v' but got '%v'", test.s, test.exp, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplySGR(t *testing.T) {
|
||||
none := tcell.StyleDefault
|
||||
|
||||
tests := []struct {
|
||||
s string
|
||||
st tcell.Style
|
||||
stExp tcell.Style
|
||||
}{
|
||||
{"", none, none},
|
||||
{"", none.Foreground(tcell.ColorMaroon).Background(tcell.ColorMaroon), none},
|
||||
{"", none.Bold(true), none},
|
||||
{"", none.Foreground(tcell.ColorMaroon).Bold(true), none},
|
||||
|
||||
{"0", none, none},
|
||||
{"0", none.Foreground(tcell.ColorMaroon).Background(tcell.ColorMaroon), none},
|
||||
{"0", none.Bold(true), none},
|
||||
{"0", none.Foreground(tcell.ColorMaroon).Bold(true), none},
|
||||
|
||||
{"1", none, none.Bold(true)},
|
||||
{"4", none, none.Underline(true)},
|
||||
{"7", none, none.Reverse(true)},
|
||||
|
||||
{"1", none.Foreground(tcell.ColorMaroon), none.Foreground(tcell.ColorMaroon).Bold(true)},
|
||||
{"4", none.Foreground(tcell.ColorMaroon), none.Foreground(tcell.ColorMaroon).Underline(true)},
|
||||
{"7", none.Foreground(tcell.ColorMaroon), none.Foreground(tcell.ColorMaroon).Reverse(true)},
|
||||
|
||||
{"4", none.Bold(true), none.Bold(true).Underline(true)},
|
||||
{"4", none.Foreground(tcell.ColorMaroon).Bold(true), none.Foreground(tcell.ColorMaroon).Bold(true).Underline(true)},
|
||||
|
||||
{"4:0", none, none},
|
||||
{"4:0", none.Underline(true), none},
|
||||
{"4:1", none, none.Underline(true)},
|
||||
{"4:2", none, none.Underline(tcell.UnderlineStyleDouble)},
|
||||
{"4:3", none, none.Underline(tcell.UnderlineStyleCurly)},
|
||||
{"4:4", none, none.Underline(tcell.UnderlineStyleDotted)},
|
||||
{"4:5", none, none.Underline(tcell.UnderlineStyleDashed)},
|
||||
|
||||
{"22", none.Italic(true).Bold(true).Dim(true), none.Italic(true)},
|
||||
{"23", none.Bold(true).Italic(true), none.Bold(true)},
|
||||
{"24", none.Bold(true).Underline(true), none.Bold(true)},
|
||||
{"25", none.Bold(true).Blink(true), none.Bold(true)},
|
||||
{"27", none.Bold(true).Reverse(true), none.Bold(true)},
|
||||
{"29", none.Bold(true).StrikeThrough(true), none.Bold(true)},
|
||||
|
||||
{"31", none, none.Foreground(tcell.ColorMaroon)},
|
||||
{"31", none.Foreground(tcell.ColorGreen), none.Foreground(tcell.ColorMaroon)},
|
||||
{"31", none.Foreground(tcell.ColorGreen).Bold(true), none.Foreground(tcell.ColorMaroon).Bold(true)},
|
||||
|
||||
{"41", none, none.Background(tcell.ColorMaroon)},
|
||||
{"41", none.Background(tcell.ColorGreen), none.Background(tcell.ColorMaroon)},
|
||||
|
||||
{"1;31", none, none.Foreground(tcell.ColorMaroon).Bold(true)},
|
||||
{"1;31", none.Foreground(tcell.ColorGreen), none.Foreground(tcell.ColorMaroon).Bold(true)},
|
||||
|
||||
{"01;31", none, none.Foreground(tcell.ColorMaroon).Bold(true)},
|
||||
{"01;31", none.Foreground(tcell.ColorGreen), none.Foreground(tcell.ColorMaroon).Bold(true)},
|
||||
|
||||
{"38;5;0", none, none.Foreground(tcell.ColorBlack)},
|
||||
{"38;5;1", none, none.Foreground(tcell.ColorMaroon)},
|
||||
{"38;5;8", none, none.Foreground(tcell.ColorGray)},
|
||||
{"38;5;16", none, none.Foreground(tcell.Color16)},
|
||||
{"38;5;232", none, none.Foreground(tcell.Color232)},
|
||||
|
||||
{"38;5;1", none.Foreground(tcell.ColorGreen), none.Foreground(tcell.ColorMaroon)},
|
||||
{"38;5;1", none.Foreground(tcell.ColorGreen).Bold(true), none.Foreground(tcell.ColorMaroon).Bold(true)},
|
||||
|
||||
{"48;5;0", none, none.Background(tcell.ColorBlack)},
|
||||
{"48;5;1", none, none.Background(tcell.ColorMaroon)},
|
||||
{"48;5;8", none, none.Background(tcell.ColorGray)},
|
||||
{"48;5;16", none, none.Background(tcell.Color16)},
|
||||
{"48;5;232", none, none.Background(tcell.Color232)},
|
||||
|
||||
{"48;5;1", none.Background(tcell.ColorGreen), none.Background(tcell.ColorMaroon)},
|
||||
|
||||
{"1;38;5;1", none, none.Foreground(tcell.ColorMaroon).Bold(true)},
|
||||
{"1;38;5;1", none.Foreground(tcell.ColorGreen), none.Foreground(tcell.ColorMaroon).Bold(true)},
|
||||
|
||||
{"38;2;5;102;8", none, none.Foreground(tcell.NewRGBColor(5, 102, 8))},
|
||||
{"48;2;0;48;143", none, none.Background(tcell.NewRGBColor(0, 48, 143))},
|
||||
|
||||
// Fixes color construction issue: https://github.com/gokcehan/lf/pull/439#issuecomment-674409446
|
||||
{"38;5;34;1", none, none.Foreground(tcell.Color34).Bold(true)},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
if stGot := applySGR(test.s, test.st); stGot != test.stExp {
|
||||
t.Errorf("at input '%s' with '%v' expected '%v' but got '%v'",
|
||||
test.s, test.st, test.stExp, stGot)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -171,18 +171,13 @@ func printLength(s string) int {
|
||||
off := 0
|
||||
slen := len(s)
|
||||
for i := 0; i < slen; i++ {
|
||||
r, w := utf8.DecodeRuneInString(s[i:])
|
||||
|
||||
if r == gEscapeCode && i+1 < slen && s[i+1] == '[' {
|
||||
j := strings.IndexAny(s[i:min(slen, i+64)], "mK")
|
||||
if j == -1 {
|
||||
continue
|
||||
}
|
||||
|
||||
i += j
|
||||
seq := readTermSequence(s[i:])
|
||||
if seq != "" {
|
||||
i += len(seq) - 1
|
||||
continue
|
||||
}
|
||||
|
||||
r, w := utf8.DecodeRuneInString(s[i:])
|
||||
i += w - 1
|
||||
|
||||
if r == '\t' {
|
||||
@@ -200,21 +195,14 @@ func (win *win) print(screen tcell.Screen, x, y int, st tcell.Style, s string) t
|
||||
var comb []rune
|
||||
slen := len(s)
|
||||
for i := 0; i < slen; i++ {
|
||||
r, w := utf8.DecodeRuneInString(s[i:])
|
||||
|
||||
if r == gEscapeCode && i+1 < slen && s[i+1] == '[' {
|
||||
j := strings.IndexAny(s[i:min(slen, i+64)], "mK")
|
||||
if j == -1 {
|
||||
continue
|
||||
}
|
||||
if s[i+j] == 'm' {
|
||||
st = applyAnsiCodes(s[i+2:i+j], st)
|
||||
}
|
||||
|
||||
i += j
|
||||
seq := readTermSequence(s[i:])
|
||||
if seq != "" {
|
||||
st = applyTermSequence(seq, st)
|
||||
i += len(seq) - 1
|
||||
continue
|
||||
}
|
||||
|
||||
r, w := utf8.DecodeRuneInString(s[i:])
|
||||
for {
|
||||
rc, wc := utf8.DecodeRuneInString(s[i+w:])
|
||||
if !unicode.Is(unicode.Mn, rc) {
|
||||
@@ -539,7 +527,7 @@ func (win *win) printDir(ui *ui, dir *dir, context *dirContext, dirStyle *dirSty
|
||||
|
||||
// print over the empty space we reserved for the custom info
|
||||
if showInfo && custom != "" {
|
||||
win.print(ui.screen, off, i, st, fmt.Sprintf(cursorFmt, stripAnsi(custom)))
|
||||
win.print(ui.screen, off, i, st, fmt.Sprintf(cursorFmt, stripTermSequence(custom)))
|
||||
}
|
||||
} else {
|
||||
if tag == " " {
|
||||
|
||||
Reference in new issue
Block a user