Update Dependencies (#390)

Co-authored-by: Norwin Roosen <git@nroo.de>
Co-authored-by: Norwin <git@nroo.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/390
Reviewed-by: 6543 <6543@obermui.de>
Reviewed-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: Norwin <noerw@noreply.gitea.io>
Co-committed-by: Norwin <noerw@noreply.gitea.io>
This commit is contained in:
Norwin
2021-08-30 23:18:50 +08:00
committed by Andrew Thornton
parent 4b9907fb54
commit d6df0a53b5
665 changed files with 29466 additions and 24547 deletions

7
vendor/github.com/muesli/reflow/ansi/ansi.go generated vendored Normal file
View File

@@ -0,0 +1,7 @@
package ansi
const Marker = '\x1B'
func IsTerminator(c rune) bool {
return (c >= 0x40 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a)
}

View File

@@ -11,26 +11,28 @@ type Buffer struct {
bytes.Buffer
}
// PrintableRuneWidth returns the width of all printable runes in the buffer.
// PrintableRuneWidth returns the cell width of all printable runes in the
// buffer.
func (w Buffer) PrintableRuneWidth() int {
return PrintableRuneWidth(w.String())
}
// PrintableRuneWidth returns the cell width of the given string.
func PrintableRuneWidth(s string) int {
var n int
var ansi bool
for _, c := range s {
if c == '\x1B' {
if c == Marker {
// ANSI escape sequence
ansi = true
} else if ansi {
if (c >= 0x40 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) {
if IsTerminator(c) {
// ANSI sequence terminated
ansi = false
}
} else {
n += runewidth.StringWidth(string(c))
n += runewidth.RuneWidth(c)
}
}

View File

@@ -1,45 +1,48 @@
package ansi
import (
"bytes"
"io"
"strings"
"unicode/utf8"
)
type Writer struct {
Forward io.Writer
ansi bool
ansiseq string
lastseq string
ansiseq bytes.Buffer
lastseq bytes.Buffer
seqchanged bool
runeBuf []byte
}
// Write is used to write content to the ANSI buffer.
func (w *Writer) Write(b []byte) (int, error) {
for _, c := range string(b) {
if c == '\x1B' {
if c == Marker {
// ANSI escape sequence
w.ansi = true
w.seqchanged = true
w.ansiseq += string(c)
_, _ = w.ansiseq.WriteRune(c)
} else if w.ansi {
w.ansiseq += string(c)
if (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) {
_, _ = w.ansiseq.WriteRune(c)
if IsTerminator(c) {
// ANSI sequence terminated
w.ansi = false
_, _ = w.Forward.Write([]byte(w.ansiseq))
if strings.HasSuffix(w.ansiseq, "[0m") {
if bytes.HasSuffix(w.ansiseq.Bytes(), []byte("[0m")) {
// reset sequence
w.lastseq = ""
} else if strings.HasSuffix(w.ansiseq, "m") {
w.lastseq.Reset()
w.seqchanged = false
} else if c == 'm' {
// color code
w.lastseq = w.ansiseq
_, _ = w.lastseq.Write(w.ansiseq.Bytes())
}
w.ansiseq = ""
_, _ = w.ansiseq.WriteTo(w.Forward)
}
} else {
_, err := w.Forward.Write([]byte(string(c)))
_, err := w.writeRune(c)
if err != nil {
return 0, err
}
@@ -49,8 +52,16 @@ func (w *Writer) Write(b []byte) (int, error) {
return len(b), nil
}
func (w *Writer) writeRune(r rune) (int, error) {
if w.runeBuf == nil {
w.runeBuf = make([]byte, utf8.UTFMax)
}
n := utf8.EncodeRune(w.runeBuf, r)
return w.Forward.Write(w.runeBuf[:n])
}
func (w *Writer) LastSequence() string {
return w.lastseq
return w.lastseq.String()
}
func (w *Writer) ResetAnsi() {
@@ -61,5 +72,5 @@ func (w *Writer) ResetAnsi() {
}
func (w *Writer) RestoreAnsi() {
_, _ = w.Forward.Write([]byte(w.lastseq))
_, _ = w.Forward.Write(w.lastseq.Bytes())
}

View File

@@ -6,7 +6,6 @@ import (
"strings"
"github.com/mattn/go-runewidth"
"github.com/muesli/reflow/ansi"
)
@@ -18,6 +17,7 @@ type Writer struct {
ansiWriter *ansi.Writer
buf bytes.Buffer
cache bytes.Buffer
lineLen int
ansi bool
}
@@ -48,7 +48,7 @@ func NewWriterPipe(forward io.Writer, width uint, paddingFunc PaddingFunc) *Writ
func Bytes(b []byte, width uint) []byte {
f := NewWriter(width, nil)
_, _ = f.Write(b)
f.Close()
_ = f.Flush()
return f.Bytes()
}
@@ -110,23 +110,34 @@ func (w *Writer) pad() error {
return nil
}
// Close will finish the padding operation. Always call it before trying to
// retrieve the final result.
func (w *Writer) Close() error {
// don't pad empty trailing lines
if w.lineLen == 0 {
return nil
}
return w.pad()
// Close will finish the padding operation.
func (w *Writer) Close() (err error) {
return w.Flush()
}
// Bytes returns the padded result as a byte slice.
func (w *Writer) Bytes() []byte {
return w.buf.Bytes()
return w.cache.Bytes()
}
// String returns the padded result as a string.
func (w *Writer) String() string {
return w.buf.String()
return w.cache.String()
}
// Flush will finish the padding operation. Always call it before trying to
// retrieve the final result.
func (w *Writer) Flush() (err error) {
if w.lineLen != 0 {
if err = w.pad(); err != nil {
return
}
}
w.cache.Reset()
_, err = w.buf.WriteTo(&w.cache)
w.lineLen = 0
w.ansi = false
return
}

View File

@@ -46,7 +46,7 @@ func NewWriter(limit int) *WordWrap {
func Bytes(b []byte, limit int) []byte {
f := NewWriter(limit)
_, _ = f.Write(b)
f.Close()
_ = f.Close()
return f.Bytes()
}
@@ -59,7 +59,7 @@ func String(s string, limit int) string {
func (w *WordWrap) addSpace() {
w.lineLen += w.space.Len()
w.buf.Write(w.space.Bytes())
_, _ = w.buf.Write(w.space.Bytes())
w.space.Reset()
}
@@ -67,13 +67,13 @@ func (w *WordWrap) addWord() {
if w.word.Len() > 0 {
w.addSpace()
w.lineLen += w.word.PrintableRuneWidth()
w.buf.Write(w.word.Bytes())
_, _ = w.buf.Write(w.word.Bytes())
w.word.Reset()
}
}
func (w *WordWrap) addNewLine() {
w.buf.WriteRune('\n')
_, _ = w.buf.WriteRune('\n')
w.lineLen = 0
w.space.Reset()
}
@@ -101,10 +101,10 @@ func (w *WordWrap) Write(b []byte) (int, error) {
for _, c := range s {
if c == '\x1B' {
// ANSI escape sequence
w.word.WriteRune(c)
_, _ = w.word.WriteRune(c)
w.ansi = true
} else if w.ansi {
w.word.WriteRune(c)
_, _ = w.word.WriteRune(c)
if (c >= 0x40 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) {
// ANSI sequence terminated
w.ansi = false
@@ -117,7 +117,7 @@ func (w *WordWrap) Write(b []byte) (int, error) {
w.lineLen = 0
} else {
// preserve whitespace
w.buf.Write(w.space.Bytes())
_, _ = w.buf.Write(w.space.Bytes())
}
w.space.Reset()
}
@@ -127,15 +127,15 @@ func (w *WordWrap) Write(b []byte) (int, error) {
} else if unicode.IsSpace(c) {
// end of current word
w.addWord()
w.space.WriteRune(c)
_, _ = w.space.WriteRune(c)
} else if inGroup(w.Breakpoints, c) {
// valid breakpoint
w.addSpace()
w.addWord()
w.buf.WriteRune(c)
_, _ = w.buf.WriteRune(c)
} else {
// any other character
w.word.WriteRune(c)
_, _ = w.word.WriteRune(c)
// add a line break if the current word would exceed the line's
// character limit

View File

@@ -5,7 +5,7 @@
<a href="https://godoc.org/github.com/muesli/termenv"><img src="https://godoc.org/github.com/golang/gddo?status.svg" alt="GoDoc"></a>
<a href="https://github.com/muesli/termenv/actions"><img src="https://github.com/muesli/termenv/workflows/build/badge.svg" alt="Build Status"></a>
<a href="https://coveralls.io/github/muesli/termenv?branch=master"><img src="https://coveralls.io/repos/github/muesli/termenv/badge.svg?branch=master" alt="Coverage Status"></a>
<a href="http://goreportcard.com/report/muesli/termenv"><img src="http://goreportcard.com/badge/muesli/termenv" alt="Go ReportCard"></a>
<a href="https://goreportcard.com/report/muesli/termenv"><img src="https://goreportcard.com/badge/muesli/termenv" alt="Go ReportCard"></a>
</p>
`termenv` lets you safely use advanced styling options on the terminal. It
@@ -16,26 +16,50 @@ color conversions.
![Example output](https://github.com/muesli/termenv/raw/master/examples/hello-world/hello-world.png)
## Features
- RGB/TrueColor support
- Detects the supported color range of your terminal
- Automatically converts colors to the best matching, available colors
- Terminal theme (light/dark) detection
- Chainable syntax
- Nested styles
## Installation
```bash
go get github.com/muesli/termenv
```
## Query Terminal Status
## Query Terminal Support
`termenv` can query the terminal it is running in, so you can safely use
advanced features, like RGB colors. `ColorProfile` returns the color profile
supported by the terminal:
```go
// returns supported color profile: Ascii, ANSI, ANSI256, or TrueColor
termenv.ColorProfile()
profile := termenv.ColorProfile()
```
// returns default foreground color
termenv.ForegroundColor()
This returns one of the supported color profiles:
// returns default background color
termenv.BackgroundColor()
- `termenv.Ascii` - no ANSI support detected, ASCII only
- `termenv.ANSI` - 16 color ANSI support
- `termenv.ANSI256` - Extended 256 color ANSI support
- `termenv.TrueColor` - RGB/TrueColor support
// returns whether terminal uses a dark-ish background
termenv.HasDarkBackground()
You can also query the terminal for its color scheme, so you know whether your
app is running in a light- or dark-themed environment:
```go
// Returns terminal's foreground color
color := termenv.ForegroundColor()
// Returns terminal's background color
color := termenv.BackgroundColor()
// Returns whether terminal uses a dark-ish background
darkTheme := termenv.HasDarkBackground()
```
## Colors
@@ -47,41 +71,49 @@ to the best matching available color in the desired profile:
`TrueColor` => `ANSI 256 Colors` => `ANSI 16 Colors` => `Ascii`
```go
out := termenv.String("Hello World")
s := termenv.String("Hello World")
// retrieve color profile supported by terminal
// Retrieve color profile supported by terminal
p := termenv.ColorProfile()
// supports hex values
// will automatically degrade colors on terminals not supporting RGB
out = out.Foreground(p.Color("#abcdef"))
// Supports hex values
// Will automatically degrade colors on terminals not supporting RGB
s.Foreground(p.Color("#abcdef"))
// but also supports ANSI colors (0-255)
out = out.Background(p.Color("69"))
s.Background(p.Color("69"))
// ...or the color.Color interface
s.Foreground(p.FromColor(color.RGBA{255, 128, 0, 255}))
fmt.Println(out)
// Combine fore- & background colors
s.Foreground(p.Color("#ffffff")).Background(p.Color("#0000ff"))
// Supports the fmt.Stringer interface
fmt.Println(s)
```
## Styles
You can use a chainable syntax to compose your own styles:
```go
out := termenv.String("foobar")
s := termenv.String("foobar")
// text styles
out.Bold()
out.Faint()
out.Italic()
out.CrossOut()
out.Underline()
out.Overline()
// Text styles
s.Bold()
s.Faint()
s.Italic()
s.CrossOut()
s.Underline()
s.Overline()
// reverse swaps current fore- & background colors
out.Reverse()
// Reverse swaps current fore- & background colors
s.Reverse()
// blinking text
out.Blink()
// Blinking text
s.Blink()
// combine multiple options
out.Bold().Underline()
// Combine multiple options
s.Bold().Underline()
```
## Template Helpers
@@ -103,11 +135,11 @@ bg := `{{ Background "#0000ff" "Blue Background" }}`
wrap := `{{ Bold (Underline "Hello World") }}`
// parse and render
tpl = tpl.Parse(bold)
tpl, err = tpl.Parse(bold)
var buf bytes.Buffer
tpl.Execute(&buf, nil)
fmt.Println(buf)
fmt.Println(&buf)
```
Other available helper functions are: `Faint`, `Italic`, `CrossOut`,
@@ -137,12 +169,24 @@ termenv.HideCursor()
// Show the cursor
termenv.ShowCursor()
// Save the cursor position
termenv.SaveCursorPosition()
// Restore a saved cursor position
termenv.RestoreCursorPosition()
// Move the cursor up a given number of lines
termenv.CursorUp(n)
// Move the cursor down a given number of lines
termenv.CursorDown(n)
// Move the cursor up a given number of lines
termenv.CursorForward(n)
// Move the cursor backwards a given number of cells
termenv.CursorBack(n)
// Move the cursor down a given number of lines and place it at the beginning
// of the line
termenv.CursorNextLine(n)
@@ -156,6 +200,51 @@ termenv.ClearLine()
// Clear a given number of lines
termenv.ClearLines(n)
// Set the scrolling region of the terminal
termenv.ChangeScrollingRegion(top, bottom)
// Insert the given number of lines at the top of the scrollable region, pushing
// lines below down
termenv.InsertLines(n)
// Delete the given number of lines, pulling any lines in the scrollable region
// below up
termenv.DeleteLines(n)
```
## Mouse
```go
// Enable X10 mouse mode, only button press events are sent
termenv.EnableMousePress()
// Disable X10 mouse mode
termenv.DisableMousePress()
// Enable Mouse Tracking mode
termenv.EnableMouse()
// Disable Mouse Tracking mode
termenv.DisableMouse()
// Enable Hilite Mouse Tracking mode
termenv.EnableMouseHilite()
// Disable Hilite Mouse Tracking mode
termenv.DisableMouseHilite()
// Enable Cell Motion Mouse Tracking mode
termenv.EnableMouseCellMotion()
// Disable Cell Motion Mouse Tracking mode
termenv.DisableMouseCellMotion()
// Enable All Motion Mouse mode
termenv.EnableMouseAllMotion()
// Disable All Motion Mouse mode
termenv.DisableMouseAllMotion()
```
## Color Chart
@@ -166,8 +255,9 @@ You can find the source code used to create this chart in `termenv`'s examples.
## Related Projects
Check out [Glow](https://github.com/charmbracelet/glow), a markdown renderer for
the command-line, which uses `termenv`.
- [reflow](https://github.com/muesli/reflow) - ANSI-aware text operations
- [Glow](https://github.com/charmbracelet/glow) - a markdown renderer for
the command-line, which uses `termenv`
## License

View File

@@ -3,6 +3,7 @@ package termenv
import (
"errors"
"fmt"
"image/color"
"math"
"strconv"
"strings"
@@ -106,6 +107,11 @@ func (p Profile) Color(s string) Color {
return p.Convert(c)
}
func (p Profile) FromColor(c color.Color) Color {
col, _ := colorful.MakeColor(c)
return p.Color(col.Hex())
}
func (c NoColor) Sequence(bg bool) string {
return ""
}
@@ -147,9 +153,21 @@ func (c RGBColor) Sequence(bg bool) string {
}
func xTermColor(s string) (RGBColor, error) {
if len(s) != 24 {
if len(s) < 24 || len(s) > 25 {
return RGBColor(""), ErrInvalidColor
}
switch {
case strings.HasSuffix(s, "\a"):
s = strings.TrimSuffix(s, "\a")
case strings.HasSuffix(s, "\033"):
s = strings.TrimSuffix(s, "\033")
case strings.HasSuffix(s, "\033\\"):
s = strings.TrimSuffix(s, "\033\\")
default:
return RGBColor(""), ErrInvalidColor
}
s = s[4:]
prefix := ";rgb:"
@@ -157,7 +175,6 @@ func xTermColor(s string) (RGBColor, error) {
return RGBColor(""), ErrInvalidColor
}
s = strings.TrimPrefix(s, prefix)
s = strings.TrimSuffix(s, "\a")
h := strings.Split(s, "/")
hex := fmt.Sprintf("#%s%s%s", h[0][:2], h[1][:2], h[2][:2])
@@ -171,7 +188,7 @@ func ansi256ToANSIColor(c ANSI256Color) ANSIColor {
h, _ := colorful.Hex(ansiHex[c])
for i := 0; i <= 15; i++ {
hb, _ := colorful.Hex(ansiHex[i])
d := h.DistanceLab(hb)
d := h.DistanceHSLuv(hb)
if d < md {
md = d
@@ -218,8 +235,8 @@ func hexToANSI256Color(c colorful.Color) ANSI256Color {
// Return the one which is nearer to the original input rgb value
c2 := colorful.Color{R: float64(cr) / 255.0, G: float64(cg) / 255.0, B: float64(cb) / 255.0}
g2 := colorful.Color{R: float64(gv) / 255.0, G: float64(gv) / 255.0, B: float64(gv) / 255.0}
colorDist := c.DistanceLab(c2)
grayDist := c.DistanceLab(g2)
colorDist := c.DistanceHSLuv(c2)
grayDist := c.DistanceHSLuv(g2)
if colorDist <= grayDist {
return ANSI256Color(16 + ci)

View File

@@ -3,8 +3,8 @@ module github.com/muesli/termenv
go 1.13
require (
github.com/lucasb-eyer/go-colorful v1.0.3
github.com/mattn/go-isatty v0.0.12
github.com/mattn/go-runewidth v0.0.9
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/mattn/go-isatty v0.0.13
github.com/mattn/go-runewidth v0.0.13
golang.org/x/sys v0.0.0-20200116001909-b77594299b42
)

View File

@@ -1,8 +1,10 @@
github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.13 h1:qdl+GuBjcsKKDco5BsxPJlId98mSWNKqYA+Co0SC1yA=
github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View File

@@ -24,6 +24,11 @@ const (
InsertLineSeq = "%dL"
DeleteLineSeq = "%dM"
// Explicit values for EraseLineSeq.
EraseLineRightSeq = "0K"
EraseLineLeftSeq = "1K"
EraseEntireLineSeq = "2K"
ShowCursorSeq = "?25h"
HideCursorSeq = "?25l"
EnableMousePressSeq = "?9h" // press only (X10)
@@ -122,7 +127,17 @@ func CursorPrevLine(n int) {
// ClearLine clears the current line.
func ClearLine() {
fmt.Printf(CSI+EraseLineSeq, 2)
fmt.Print(CSI + EraseEntireLineSeq)
}
// ClearLineLeft clears the line to the left of the cursor.
func ClearLineLeft() {
fmt.Print(CSI + EraseLineLeftSeq)
}
// ClearLineRight clears the line to the right of the cursor.
func ClearLineRight() {
fmt.Print(CSI + EraseLineRightSeq)
}
// ClearLines clears a given number of lines.
@@ -137,7 +152,7 @@ func ChangeScrollingRegion(top, bottom int) {
fmt.Printf(CSI+ChangeScrollingRegionSeq, top, bottom)
}
// InsertLines inserts the given number lines at the top of the scrollable
// InsertLines inserts the given number of lines at the top of the scrollable
// region, pushing lines below down.
func InsertLines(n int) {
fmt.Printf(CSI+InsertLineSeq, n)

View File

@@ -5,7 +5,6 @@ package termenv
import (
"fmt"
"os"
"runtime"
"strconv"
"strings"
@@ -84,53 +83,100 @@ func backgroundColor() Color {
return ANSIColor(0)
}
func readWithTimeout(f *os.File) (string, bool) {
var readfds unix.FdSet
fd := int(f.Fd())
readfds.Set(fd)
for {
// Use select to attempt to read from os.Stdout for 100 ms
_, err := unix.Select(fd+1, &readfds, nil, nil, &unix.Timeval{Usec: 100000})
if err == nil {
break
}
// On MacOS we can see EINTR here if the user
// pressed ^Z. Similar to issue https://github.com/golang/go/issues/22838
if runtime.GOOS == "darwin" && err == unix.EINTR {
continue
}
// log.Printf("select(read error): %v", err)
return "", false
func readNextByte(f *os.File) (byte, error) {
var b [1]byte
n, err := f.Read(b[:])
if err != nil {
return 0, err
}
if !readfds.IsSet(fd) {
// log.Print("select(read timeout)")
return "", false
if n == 0 {
panic("read returned no data")
}
// n > 0 => is readable
var data []byte
b := make([]byte, 1)
for {
_, err := f.Read(b)
return b[0], nil
}
// readNextResponse reads either an OSC response or a cursor position response:
// * OSC response: "\x1b]11;rgb:1111/1111/1111\x1b\\"
// * cursor position response: "\x1b[42;1R"
func readNextResponse(fd *os.File) (response string, isOSC bool, err error) {
start, err := readNextByte(fd)
if err != nil {
return "", false, err
}
// if we encounter a backslash, this is a left-over from the previous OSC
// response, which can be terminated by an optional backslash
if start == '\\' {
start, err = readNextByte(fd)
if err != nil {
// log.Printf("read(%d): %v %d", fd, err, n)
return "", false
return "", false, err
}
// log.Printf("read %d bytes from stdout: %s %d\n", n, data, data[len(data)-1])
}
data = append(data, b[0])
if b[0] == '\a' || (b[0] == '\\' && len(data) > 2) {
// first byte must be ESC
if start != '\033' {
return "", false, ErrStatusReport
}
response += string(start)
// next byte is either '[' (cursor position response) or ']' (OSC response)
tpe, err := readNextByte(fd)
if err != nil {
return "", false, err
}
response += string(tpe)
var oscResponse bool
switch tpe {
case '[':
oscResponse = false
case ']':
oscResponse = true
default:
return "", false, ErrStatusReport
}
for {
b, err := readNextByte(os.Stdout)
if err != nil {
return "", false, err
}
response += string(b)
if oscResponse {
// OSC can be terminated by BEL (\a) or ST (ESC)
if b == '\a' || strings.HasSuffix(response, "\033") {
return response, true, nil
}
} else {
// cursor position response is terminated by 'R'
if b == 'R' {
return response, false, nil
}
}
// both responses have less than 25 bytes, so if we read more, that's an error
if len(response) > 25 {
break
}
}
// fmt.Printf("read %d bytes from stdout: %s\n", n, data)
return string(data), true
return "", false, ErrStatusReport
}
func termStatusReport(sequence int) (string, error) {
// screen/tmux can't support OSC, because they can be connected to multiple
// terminals concurrently.
term := os.Getenv("TERM")
if strings.HasPrefix(term, "screen") {
return "", ErrStatusReport
}
t, err := unix.IoctlGetTermios(unix.Stdout, tcgetattr)
if err != nil {
return "", ErrStatusReport
@@ -144,11 +190,29 @@ func termStatusReport(sequence int) (string, error) {
return "", ErrStatusReport
}
fmt.Printf("\033]%d;?\007", sequence)
s, ok := readWithTimeout(os.Stdout)
if !ok {
// first, send OSC query, which is ignored by terminal which do not support it
fmt.Printf("\033]%d;?\033\\", sequence)
// then, query cursor position, should be supported by all terminals
fmt.Printf("\033[6n")
// read the next response
res, isOSC, err := readNextResponse(os.Stdout)
if err != nil {
return "", err
}
// if this is not OSC response, then the terminal does not support it
if !isOSC {
return "", ErrStatusReport
}
// fmt.Println("Rcvd", s[1:])
return s, nil
// read the cursor query response next and discard the result
_, _, err = readNextResponse(os.Stdout)
if err != nil {
return "", err
}
// fmt.Println("Rcvd", res[1:])
return res, nil
}