forked from toolshed/abra
build: go 1.24
We were running behind and there were quite some deprecations to update. This was mostly in the upstream copy/pasta package but seems quite minimal.
This commit is contained in:
2
vendor/github.com/charmbracelet/x/ansi/color.go
generated
vendored
2
vendor/github.com/charmbracelet/x/ansi/color.go
generated
vendored
@ -178,7 +178,7 @@ func ansiToRGB(ansi uint32) (uint32, uint32, uint32) {
|
||||
//
|
||||
// r, g, b := hexToRGB(0x0000FF)
|
||||
func hexToRGB(hex uint32) (uint32, uint32, uint32) {
|
||||
return hex >> 16, hex >> 8 & 0xff, hex & 0xff
|
||||
return hex >> 16 & 0xff, hex >> 8 & 0xff, hex & 0xff
|
||||
}
|
||||
|
||||
// toRGBA converts an RGB 8-bit color values to 32-bit color values suitable
|
||||
|
120
vendor/github.com/charmbracelet/x/ansi/csi.go
generated
vendored
120
vendor/github.com/charmbracelet/x/ansi/csi.go
generated
vendored
@ -1,120 +0,0 @@
|
||||
package ansi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// CsiSequence represents a control sequence introducer (CSI) sequence.
|
||||
//
|
||||
// The sequence starts with a CSI sequence, CSI (0x9B) in a 8-bit environment
|
||||
// or ESC [ (0x1B 0x5B) in a 7-bit environment, followed by any number of
|
||||
// parameters in the range of 0x30-0x3F, then by any number of intermediate
|
||||
// byte in the range of 0x20-0x2F, then finally with a single final byte in the
|
||||
// range of 0x20-0x7E.
|
||||
//
|
||||
// CSI P..P I..I F
|
||||
//
|
||||
// See ECMA-48 § 5.4.
|
||||
type CsiSequence struct {
|
||||
// Params contains the raw parameters of the sequence.
|
||||
// This is a slice of integers, where each integer is a 32-bit integer
|
||||
// containing the parameter value in the lower 31 bits and a flag in the
|
||||
// most significant bit indicating whether there are more sub-parameters.
|
||||
Params []Parameter
|
||||
|
||||
// Cmd contains the raw command of the sequence.
|
||||
// The command is a 32-bit integer containing the CSI command byte in the
|
||||
// lower 8 bits, the private marker in the next 8 bits, and the intermediate
|
||||
// byte in the next 8 bits.
|
||||
//
|
||||
// CSI ? u
|
||||
//
|
||||
// Is represented as:
|
||||
//
|
||||
// 'u' | '?' << 8
|
||||
Cmd Command
|
||||
}
|
||||
|
||||
var _ Sequence = CsiSequence{}
|
||||
|
||||
// Clone returns a deep copy of the CSI sequence.
|
||||
func (s CsiSequence) Clone() Sequence {
|
||||
return CsiSequence{
|
||||
Params: append([]Parameter(nil), s.Params...),
|
||||
Cmd: s.Cmd,
|
||||
}
|
||||
}
|
||||
|
||||
// Marker returns the marker byte of the CSI sequence.
|
||||
// This is always gonna be one of the following '<' '=' '>' '?' and in the
|
||||
// range of 0x3C-0x3F.
|
||||
// Zero is returned if the sequence does not have a marker.
|
||||
func (s CsiSequence) Marker() int {
|
||||
return s.Cmd.Marker()
|
||||
}
|
||||
|
||||
// Intermediate returns the intermediate byte of the CSI sequence.
|
||||
// An intermediate byte is in the range of 0x20-0x2F. This includes these
|
||||
// characters from ' ', '!', '"', '#', '$', '%', '&', ”', '(', ')', '*', '+',
|
||||
// ',', '-', '.', '/'.
|
||||
// Zero is returned if the sequence does not have an intermediate byte.
|
||||
func (s CsiSequence) Intermediate() int {
|
||||
return s.Cmd.Intermediate()
|
||||
}
|
||||
|
||||
// Command returns the command byte of the CSI sequence.
|
||||
func (s CsiSequence) Command() int {
|
||||
return s.Cmd.Command()
|
||||
}
|
||||
|
||||
// Param is a helper that returns the parameter at the given index and falls
|
||||
// back to the default value if the parameter is missing. If the index is out
|
||||
// of bounds, it returns the default value and false.
|
||||
func (s CsiSequence) Param(i, def int) (int, bool) {
|
||||
if i < 0 || i >= len(s.Params) {
|
||||
return def, false
|
||||
}
|
||||
return s.Params[i].Param(def), true
|
||||
}
|
||||
|
||||
// String returns a string representation of the sequence.
|
||||
// The string will always be in the 7-bit format i.e (ESC [ P..P I..I F).
|
||||
func (s CsiSequence) String() string {
|
||||
return s.buffer().String()
|
||||
}
|
||||
|
||||
// buffer returns a buffer containing the sequence.
|
||||
func (s CsiSequence) buffer() *bytes.Buffer {
|
||||
var b bytes.Buffer
|
||||
b.WriteString("\x1b[")
|
||||
if m := s.Marker(); m != 0 {
|
||||
b.WriteByte(byte(m))
|
||||
}
|
||||
for i, p := range s.Params {
|
||||
param := p.Param(-1)
|
||||
if param >= 0 {
|
||||
b.WriteString(strconv.Itoa(param))
|
||||
}
|
||||
if i < len(s.Params)-1 {
|
||||
if p.HasMore() {
|
||||
b.WriteByte(':')
|
||||
} else {
|
||||
b.WriteByte(';')
|
||||
}
|
||||
}
|
||||
}
|
||||
if i := s.Intermediate(); i != 0 {
|
||||
b.WriteByte(byte(i))
|
||||
}
|
||||
if cmd := s.Command(); cmd != 0 {
|
||||
b.WriteByte(byte(cmd))
|
||||
}
|
||||
return &b
|
||||
}
|
||||
|
||||
// Bytes returns the byte representation of the sequence.
|
||||
// The bytes will always be in the 7-bit format i.e (ESC [ P..P I..I F).
|
||||
func (s CsiSequence) Bytes() []byte {
|
||||
return s.buffer().Bytes()
|
||||
}
|
25
vendor/github.com/charmbracelet/x/ansi/ctrl.go
generated
vendored
25
vendor/github.com/charmbracelet/x/ansi/ctrl.go
generated
vendored
@ -14,7 +14,7 @@ import (
|
||||
//
|
||||
// See https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-PC-Style-Function-Keys
|
||||
const (
|
||||
RequestNameVersion = "\x1b[>0q"
|
||||
RequestNameVersion = "\x1b[>q"
|
||||
XTVERSION = RequestNameVersion
|
||||
)
|
||||
|
||||
@ -24,6 +24,7 @@ const (
|
||||
// DCS > | text ST
|
||||
//
|
||||
// See https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-PC-Style-Function-Keys
|
||||
//
|
||||
// Deprecated: use [RequestNameVersion] instead.
|
||||
const RequestXTVersion = RequestNameVersion
|
||||
|
||||
@ -40,7 +41,7 @@ const RequestXTVersion = RequestNameVersion
|
||||
// See https://vt100.net/docs/vt510-rm/DA1.html
|
||||
func PrimaryDeviceAttributes(attrs ...int) string {
|
||||
if len(attrs) == 0 {
|
||||
return "\x1b[c"
|
||||
return RequestPrimaryDeviceAttributes
|
||||
} else if len(attrs) == 1 && attrs[0] == 0 {
|
||||
return "\x1b[0c"
|
||||
}
|
||||
@ -75,7 +76,7 @@ const RequestPrimaryDeviceAttributes = "\x1b[c"
|
||||
// See https://vt100.net/docs/vt510-rm/DA2.html
|
||||
func SecondaryDeviceAttributes(attrs ...int) string {
|
||||
if len(attrs) == 0 {
|
||||
return "\x1b[>c"
|
||||
return RequestSecondaryDeviceAttributes
|
||||
}
|
||||
|
||||
as := make([]string, len(attrs))
|
||||
@ -90,6 +91,14 @@ func DA2(attrs ...int) string {
|
||||
return SecondaryDeviceAttributes(attrs...)
|
||||
}
|
||||
|
||||
// RequestSecondaryDeviceAttributes is a control sequence that requests the
|
||||
// terminal's secondary device attributes (DA2).
|
||||
//
|
||||
// CSI > c
|
||||
//
|
||||
// See https://vt100.net/docs/vt510-rm/DA2.html
|
||||
const RequestSecondaryDeviceAttributes = "\x1b[>c"
|
||||
|
||||
// TertiaryDeviceAttributes (DA3) is a control sequence that reports the
|
||||
// terminal's tertiary device attributes.
|
||||
//
|
||||
@ -106,7 +115,7 @@ func DA2(attrs ...int) string {
|
||||
func TertiaryDeviceAttributes(unitID string) string {
|
||||
switch unitID {
|
||||
case "":
|
||||
return "\x1b[=c"
|
||||
return RequestTertiaryDeviceAttributes
|
||||
case "0":
|
||||
return "\x1b[=0c"
|
||||
}
|
||||
@ -118,3 +127,11 @@ func TertiaryDeviceAttributes(unitID string) string {
|
||||
func DA3(unitID string) string {
|
||||
return TertiaryDeviceAttributes(unitID)
|
||||
}
|
||||
|
||||
// RequestTertiaryDeviceAttributes is a control sequence that requests the
|
||||
// terminal's tertiary device attributes (DA3).
|
||||
//
|
||||
// CSI = c
|
||||
//
|
||||
// See https://vt100.net/docs/vt510-rm/DA3.html
|
||||
const RequestTertiaryDeviceAttributes = "\x1b[=c"
|
||||
|
4
vendor/github.com/charmbracelet/x/ansi/cursor.go
generated
vendored
4
vendor/github.com/charmbracelet/x/ansi/cursor.go
generated
vendored
@ -36,6 +36,8 @@ const (
|
||||
//
|
||||
// Where Pl is the line number and Pc is the column number.
|
||||
// See: https://vt100.net/docs/vt510-rm/CPR.html
|
||||
//
|
||||
// Deprecated: use [RequestCursorPositionReport] instead.
|
||||
const RequestCursorPosition = "\x1b[6n"
|
||||
|
||||
// RequestExtendedCursorPosition (DECXCPR) is a sequence for requesting the
|
||||
@ -51,6 +53,8 @@ const RequestCursorPosition = "\x1b[6n"
|
||||
// Where Pl is the line number, Pc is the column number, and Pp is the page
|
||||
// number.
|
||||
// See: https://vt100.net/docs/vt510-rm/DECXCPR.html
|
||||
//
|
||||
// Deprecated: use [RequestExtendedCursorPositionReport] instead.
|
||||
const RequestExtendedCursorPosition = "\x1b[?6n"
|
||||
|
||||
// CursorUp (CUU) returns a sequence for moving the cursor up n cells.
|
||||
|
133
vendor/github.com/charmbracelet/x/ansi/dcs.go
generated
vendored
133
vendor/github.com/charmbracelet/x/ansi/dcs.go
generated
vendored
@ -1,133 +0,0 @@
|
||||
package ansi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DcsSequence represents a Device Control String (DCS) escape sequence.
|
||||
//
|
||||
// The DCS sequence is used to send device control strings to the terminal. The
|
||||
// sequence starts with the C1 control code character DCS (0x9B) or ESC P in
|
||||
// 7-bit environments, followed by parameter bytes, intermediate bytes, a
|
||||
// command byte, followed by data bytes, and ends with the C1 control code
|
||||
// character ST (0x9C) or ESC \ in 7-bit environments.
|
||||
//
|
||||
// This follows the parameter string format.
|
||||
// See ECMA-48 § 5.4.1
|
||||
type DcsSequence struct {
|
||||
// Params contains the raw parameters of the sequence.
|
||||
// This is a slice of integers, where each integer is a 32-bit integer
|
||||
// containing the parameter value in the lower 31 bits and a flag in the
|
||||
// most significant bit indicating whether there are more sub-parameters.
|
||||
Params []Parameter
|
||||
|
||||
// Data contains the string raw data of the sequence.
|
||||
// This is the data between the final byte and the escape sequence terminator.
|
||||
Data []byte
|
||||
|
||||
// Cmd contains the raw command of the sequence.
|
||||
// The command is a 32-bit integer containing the DCS command byte in the
|
||||
// lower 8 bits, the private marker in the next 8 bits, and the intermediate
|
||||
// byte in the next 8 bits.
|
||||
//
|
||||
// DCS > 0 ; 1 $ r <data> ST
|
||||
//
|
||||
// Is represented as:
|
||||
//
|
||||
// 'r' | '>' << 8 | '$' << 16
|
||||
Cmd Command
|
||||
}
|
||||
|
||||
var _ Sequence = DcsSequence{}
|
||||
|
||||
// Clone returns a deep copy of the DCS sequence.
|
||||
func (s DcsSequence) Clone() Sequence {
|
||||
return DcsSequence{
|
||||
Params: append([]Parameter(nil), s.Params...),
|
||||
Data: append([]byte(nil), s.Data...),
|
||||
Cmd: s.Cmd,
|
||||
}
|
||||
}
|
||||
|
||||
// Split returns a slice of data split by the semicolon.
|
||||
func (s DcsSequence) Split() []string {
|
||||
return strings.Split(string(s.Data), ";")
|
||||
}
|
||||
|
||||
// Marker returns the marker byte of the DCS sequence.
|
||||
// This is always gonna be one of the following '<' '=' '>' '?' and in the
|
||||
// range of 0x3C-0x3F.
|
||||
// Zero is returned if the sequence does not have a marker.
|
||||
func (s DcsSequence) Marker() int {
|
||||
return s.Cmd.Marker()
|
||||
}
|
||||
|
||||
// Intermediate returns the intermediate byte of the DCS sequence.
|
||||
// An intermediate byte is in the range of 0x20-0x2F. This includes these
|
||||
// characters from ' ', '!', '"', '#', '$', '%', '&', ”', '(', ')', '*', '+',
|
||||
// ',', '-', '.', '/'.
|
||||
// Zero is returned if the sequence does not have an intermediate byte.
|
||||
func (s DcsSequence) Intermediate() int {
|
||||
return s.Cmd.Intermediate()
|
||||
}
|
||||
|
||||
// Command returns the command byte of the CSI sequence.
|
||||
func (s DcsSequence) Command() int {
|
||||
return s.Cmd.Command()
|
||||
}
|
||||
|
||||
// Param is a helper that returns the parameter at the given index and falls
|
||||
// back to the default value if the parameter is missing. If the index is out
|
||||
// of bounds, it returns the default value and false.
|
||||
func (s DcsSequence) Param(i, def int) (int, bool) {
|
||||
if i < 0 || i >= len(s.Params) {
|
||||
return def, false
|
||||
}
|
||||
return s.Params[i].Param(def), true
|
||||
}
|
||||
|
||||
// String returns a string representation of the sequence.
|
||||
// The string will always be in the 7-bit format i.e (ESC P p..p i..i f <data> ESC \).
|
||||
func (s DcsSequence) String() string {
|
||||
return s.buffer().String()
|
||||
}
|
||||
|
||||
// buffer returns a buffer containing the sequence.
|
||||
func (s DcsSequence) buffer() *bytes.Buffer {
|
||||
var b bytes.Buffer
|
||||
b.WriteString("\x1bP")
|
||||
if m := s.Marker(); m != 0 {
|
||||
b.WriteByte(byte(m))
|
||||
}
|
||||
for i, p := range s.Params {
|
||||
param := p.Param(-1)
|
||||
if param >= 0 {
|
||||
b.WriteString(strconv.Itoa(param))
|
||||
}
|
||||
if i < len(s.Params)-1 {
|
||||
if p.HasMore() {
|
||||
b.WriteByte(':')
|
||||
} else {
|
||||
b.WriteByte(';')
|
||||
}
|
||||
}
|
||||
}
|
||||
if i := s.Intermediate(); i != 0 {
|
||||
b.WriteByte(byte(i))
|
||||
}
|
||||
if cmd := s.Command(); cmd != 0 {
|
||||
b.WriteByte(byte(cmd))
|
||||
}
|
||||
b.Write(s.Data)
|
||||
b.WriteByte(ESC)
|
||||
b.WriteByte('\\')
|
||||
return &b
|
||||
}
|
||||
|
||||
// Bytes returns the byte representation of the sequence.
|
||||
// The bytes will always be in the 7-bit format i.e (ESC P p..p i..i F <data> ESC \).
|
||||
func (s DcsSequence) Bytes() []byte {
|
||||
return s.buffer().Bytes()
|
||||
}
|
199
vendor/github.com/charmbracelet/x/ansi/graphics.go
generated
vendored
Normal file
199
vendor/github.com/charmbracelet/x/ansi/graphics.go
generated
vendored
Normal file
@ -0,0 +1,199 @@
|
||||
package ansi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/x/ansi/kitty"
|
||||
)
|
||||
|
||||
// KittyGraphics returns a sequence that encodes the given image in the Kitty
|
||||
// graphics protocol.
|
||||
//
|
||||
// APC G [comma separated options] ; [base64 encoded payload] ST
|
||||
//
|
||||
// See https://sw.kovidgoyal.net/kitty/graphics-protocol/
|
||||
func KittyGraphics(payload []byte, opts ...string) string {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString("\x1b_G")
|
||||
buf.WriteString(strings.Join(opts, ","))
|
||||
if len(payload) > 0 {
|
||||
buf.WriteString(";")
|
||||
buf.Write(payload)
|
||||
}
|
||||
buf.WriteString("\x1b\\")
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
var (
|
||||
// KittyGraphicsTempDir is the directory where temporary files are stored.
|
||||
// This is used in [WriteKittyGraphics] along with [os.CreateTemp].
|
||||
KittyGraphicsTempDir = ""
|
||||
|
||||
// KittyGraphicsTempPattern is the pattern used to create temporary files.
|
||||
// This is used in [WriteKittyGraphics] along with [os.CreateTemp].
|
||||
// The Kitty Graphics protocol requires the file path to contain the
|
||||
// substring "tty-graphics-protocol".
|
||||
KittyGraphicsTempPattern = "tty-graphics-protocol-*"
|
||||
)
|
||||
|
||||
// WriteKittyGraphics writes an image using the Kitty Graphics protocol with
|
||||
// the given options to w. It chunks the written data if o.Chunk is true.
|
||||
//
|
||||
// You can omit m and use nil when rendering an image from a file. In this
|
||||
// case, you must provide a file path in o.File and use o.Transmission =
|
||||
// [kitty.File]. You can also use o.Transmission = [kitty.TempFile] to write
|
||||
// the image to a temporary file. In that case, the file path is ignored, and
|
||||
// the image is written to a temporary file that is automatically deleted by
|
||||
// the terminal.
|
||||
//
|
||||
// See https://sw.kovidgoyal.net/kitty/graphics-protocol/
|
||||
func WriteKittyGraphics(w io.Writer, m image.Image, o *kitty.Options) error {
|
||||
if o == nil {
|
||||
o = &kitty.Options{}
|
||||
}
|
||||
|
||||
if o.Transmission == 0 && len(o.File) != 0 {
|
||||
o.Transmission = kitty.File
|
||||
}
|
||||
|
||||
var data bytes.Buffer // the data to be encoded into base64
|
||||
e := &kitty.Encoder{
|
||||
Compress: o.Compression == kitty.Zlib,
|
||||
Format: o.Format,
|
||||
}
|
||||
|
||||
switch o.Transmission {
|
||||
case kitty.Direct:
|
||||
if err := e.Encode(&data, m); err != nil {
|
||||
return fmt.Errorf("failed to encode direct image: %w", err)
|
||||
}
|
||||
|
||||
case kitty.SharedMemory:
|
||||
// TODO: Implement shared memory
|
||||
return fmt.Errorf("shared memory transmission is not yet implemented")
|
||||
|
||||
case kitty.File:
|
||||
if len(o.File) == 0 {
|
||||
return kitty.ErrMissingFile
|
||||
}
|
||||
|
||||
f, err := os.Open(o.File)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open file: %w", err)
|
||||
}
|
||||
|
||||
defer f.Close() //nolint:errcheck
|
||||
|
||||
stat, err := f.Stat()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get file info: %w", err)
|
||||
}
|
||||
|
||||
mode := stat.Mode()
|
||||
if !mode.IsRegular() {
|
||||
return fmt.Errorf("file is not a regular file")
|
||||
}
|
||||
|
||||
// Write the file path to the buffer
|
||||
if _, err := data.WriteString(f.Name()); err != nil {
|
||||
return fmt.Errorf("failed to write file path to buffer: %w", err)
|
||||
}
|
||||
|
||||
case kitty.TempFile:
|
||||
f, err := os.CreateTemp(KittyGraphicsTempDir, KittyGraphicsTempPattern)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create file: %w", err)
|
||||
}
|
||||
|
||||
defer f.Close() //nolint:errcheck
|
||||
|
||||
if err := e.Encode(f, m); err != nil {
|
||||
return fmt.Errorf("failed to encode image to file: %w", err)
|
||||
}
|
||||
|
||||
// Write the file path to the buffer
|
||||
if _, err := data.WriteString(f.Name()); err != nil {
|
||||
return fmt.Errorf("failed to write file path to buffer: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Encode image to base64
|
||||
var payload bytes.Buffer // the base64 encoded image to be written to w
|
||||
b64 := base64.NewEncoder(base64.StdEncoding, &payload)
|
||||
if _, err := data.WriteTo(b64); err != nil {
|
||||
return fmt.Errorf("failed to write base64 encoded image to payload: %w", err)
|
||||
}
|
||||
if err := b64.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// If not chunking, write all at once
|
||||
if !o.Chunk {
|
||||
_, err := io.WriteString(w, KittyGraphics(payload.Bytes(), o.Options()...))
|
||||
return err
|
||||
}
|
||||
|
||||
// Write in chunks
|
||||
var (
|
||||
err error
|
||||
n int
|
||||
)
|
||||
chunk := make([]byte, kitty.MaxChunkSize)
|
||||
isFirstChunk := true
|
||||
|
||||
for {
|
||||
// Stop if we read less than the chunk size [kitty.MaxChunkSize].
|
||||
n, err = io.ReadFull(&payload, chunk)
|
||||
if errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, io.EOF) {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read chunk: %w", err)
|
||||
}
|
||||
|
||||
opts := buildChunkOptions(o, isFirstChunk, false)
|
||||
if _, err := io.WriteString(w, KittyGraphics(chunk[:n], opts...)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
isFirstChunk = false
|
||||
}
|
||||
|
||||
// Write the last chunk
|
||||
opts := buildChunkOptions(o, isFirstChunk, true)
|
||||
_, err = io.WriteString(w, KittyGraphics(chunk[:n], opts...))
|
||||
return err
|
||||
}
|
||||
|
||||
// buildChunkOptions creates the options slice for a chunk
|
||||
func buildChunkOptions(o *kitty.Options, isFirstChunk, isLastChunk bool) []string {
|
||||
var opts []string
|
||||
if isFirstChunk {
|
||||
opts = o.Options()
|
||||
} else {
|
||||
// These options are allowed in subsequent chunks
|
||||
if o.Quite > 0 {
|
||||
opts = append(opts, fmt.Sprintf("q=%d", o.Quite))
|
||||
}
|
||||
if o.Action == kitty.Frame {
|
||||
opts = append(opts, "a=f")
|
||||
}
|
||||
}
|
||||
|
||||
if !isFirstChunk || !isLastChunk {
|
||||
// We don't need to encode the (m=) option when we only have one chunk.
|
||||
if isLastChunk {
|
||||
opts = append(opts, "m=0")
|
||||
} else {
|
||||
opts = append(opts, "m=1")
|
||||
}
|
||||
}
|
||||
return opts
|
||||
}
|
18
vendor/github.com/charmbracelet/x/ansi/iterm2.go
generated
vendored
Normal file
18
vendor/github.com/charmbracelet/x/ansi/iterm2.go
generated
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
package ansi
|
||||
|
||||
import "fmt"
|
||||
|
||||
// ITerm2 returns a sequence that uses the iTerm2 proprietary protocol. Use the
|
||||
// iterm2 package for a more convenient API.
|
||||
//
|
||||
// OSC 1337 ; key = value ST
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// ITerm2(iterm2.File{...})
|
||||
//
|
||||
// See https://iterm2.com/documentation-escape-codes.html
|
||||
// See https://iterm2.com/documentation-images.html
|
||||
func ITerm2(data any) string {
|
||||
return "\x1b]1337;" + fmt.Sprint(data) + "\x07"
|
||||
}
|
2
vendor/github.com/charmbracelet/x/ansi/kitty.go
generated
vendored
2
vendor/github.com/charmbracelet/x/ansi/kitty.go
generated
vendored
@ -72,7 +72,7 @@ func PushKittyKeyboard(flags int) string {
|
||||
// Keyboard stack to disable the protocol.
|
||||
//
|
||||
// This is equivalent to PushKittyKeyboard(0).
|
||||
const DisableKittyKeyboard = "\x1b[>0u"
|
||||
const DisableKittyKeyboard = "\x1b[>u"
|
||||
|
||||
// PopKittyKeyboard returns a sequence to pop n number of flags from the
|
||||
// terminal Kitty Keyboard stack.
|
||||
|
85
vendor/github.com/charmbracelet/x/ansi/kitty/decoder.go
generated
vendored
Normal file
85
vendor/github.com/charmbracelet/x/ansi/kitty/decoder.go
generated
vendored
Normal file
@ -0,0 +1,85 @@
|
||||
package kitty
|
||||
|
||||
import (
|
||||
"compress/zlib"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"io"
|
||||
)
|
||||
|
||||
// Decoder is a decoder for the Kitty graphics protocol. It supports decoding
|
||||
// images in the 24-bit [RGB], 32-bit [RGBA], and [PNG] formats. It can also
|
||||
// decompress data using zlib.
|
||||
// The default format is 32-bit [RGBA].
|
||||
type Decoder struct {
|
||||
// Uses zlib decompression.
|
||||
Decompress bool
|
||||
|
||||
// Can be one of [RGB], [RGBA], or [PNG].
|
||||
Format int
|
||||
|
||||
// Width of the image in pixels. This can be omitted if the image is [PNG]
|
||||
// formatted.
|
||||
Width int
|
||||
|
||||
// Height of the image in pixels. This can be omitted if the image is [PNG]
|
||||
// formatted.
|
||||
Height int
|
||||
}
|
||||
|
||||
// Decode decodes the image data from r in the specified format.
|
||||
func (d *Decoder) Decode(r io.Reader) (image.Image, error) {
|
||||
if d.Decompress {
|
||||
zr, err := zlib.NewReader(r)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create zlib reader: %w", err)
|
||||
}
|
||||
|
||||
defer zr.Close() //nolint:errcheck
|
||||
r = zr
|
||||
}
|
||||
|
||||
if d.Format == 0 {
|
||||
d.Format = RGBA
|
||||
}
|
||||
|
||||
switch d.Format {
|
||||
case RGBA, RGB:
|
||||
return d.decodeRGBA(r, d.Format == RGBA)
|
||||
|
||||
case PNG:
|
||||
return png.Decode(r)
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported format: %d", d.Format)
|
||||
}
|
||||
}
|
||||
|
||||
// decodeRGBA decodes the image data in 32-bit RGBA or 24-bit RGB formats.
|
||||
func (d *Decoder) decodeRGBA(r io.Reader, alpha bool) (image.Image, error) {
|
||||
m := image.NewRGBA(image.Rect(0, 0, d.Width, d.Height))
|
||||
|
||||
var buf []byte
|
||||
if alpha {
|
||||
buf = make([]byte, 4)
|
||||
} else {
|
||||
buf = make([]byte, 3)
|
||||
}
|
||||
|
||||
for y := 0; y < d.Height; y++ {
|
||||
for x := 0; x < d.Width; x++ {
|
||||
if _, err := io.ReadFull(r, buf[:]); err != nil {
|
||||
return nil, fmt.Errorf("failed to read pixel data: %w", err)
|
||||
}
|
||||
if alpha {
|
||||
m.SetRGBA(x, y, color.RGBA{buf[0], buf[1], buf[2], buf[3]})
|
||||
} else {
|
||||
m.SetRGBA(x, y, color.RGBA{buf[0], buf[1], buf[2], 0xff})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
64
vendor/github.com/charmbracelet/x/ansi/kitty/encoder.go
generated
vendored
Normal file
64
vendor/github.com/charmbracelet/x/ansi/kitty/encoder.go
generated
vendored
Normal file
@ -0,0 +1,64 @@
|
||||
package kitty
|
||||
|
||||
import (
|
||||
"compress/zlib"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/png"
|
||||
"io"
|
||||
)
|
||||
|
||||
// Encoder is an encoder for the Kitty graphics protocol. It supports encoding
|
||||
// images in the 24-bit [RGB], 32-bit [RGBA], and [PNG] formats, and
|
||||
// compressing the data using zlib.
|
||||
// The default format is 32-bit [RGBA].
|
||||
type Encoder struct {
|
||||
// Uses zlib compression.
|
||||
Compress bool
|
||||
|
||||
// Can be one of [RGBA], [RGB], or [PNG].
|
||||
Format int
|
||||
}
|
||||
|
||||
// Encode encodes the image data in the specified format and writes it to w.
|
||||
func (e *Encoder) Encode(w io.Writer, m image.Image) error {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if e.Compress {
|
||||
zw := zlib.NewWriter(w)
|
||||
defer zw.Close() //nolint:errcheck
|
||||
w = zw
|
||||
}
|
||||
|
||||
if e.Format == 0 {
|
||||
e.Format = RGBA
|
||||
}
|
||||
|
||||
switch e.Format {
|
||||
case RGBA, RGB:
|
||||
bounds := m.Bounds()
|
||||
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
||||
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
||||
r, g, b, a := m.At(x, y).RGBA()
|
||||
switch e.Format {
|
||||
case RGBA:
|
||||
w.Write([]byte{byte(r >> 8), byte(g >> 8), byte(b >> 8), byte(a >> 8)}) //nolint:errcheck
|
||||
case RGB:
|
||||
w.Write([]byte{byte(r >> 8), byte(g >> 8), byte(b >> 8)}) //nolint:errcheck
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case PNG:
|
||||
if err := png.Encode(w, m); err != nil {
|
||||
return fmt.Errorf("failed to encode PNG: %w", err)
|
||||
}
|
||||
|
||||
default:
|
||||
return fmt.Errorf("unsupported format: %d", e.Format)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
414
vendor/github.com/charmbracelet/x/ansi/kitty/graphics.go
generated
vendored
Normal file
414
vendor/github.com/charmbracelet/x/ansi/kitty/graphics.go
generated
vendored
Normal file
@ -0,0 +1,414 @@
|
||||
package kitty
|
||||
|
||||
import "errors"
|
||||
|
||||
// ErrMissingFile is returned when the file path is missing.
|
||||
var ErrMissingFile = errors.New("missing file path")
|
||||
|
||||
// MaxChunkSize is the maximum chunk size for the image data.
|
||||
const MaxChunkSize = 1024 * 4
|
||||
|
||||
// Placeholder is a special Unicode character that can be used as a placeholder
|
||||
// for an image.
|
||||
const Placeholder = '\U0010EEEE'
|
||||
|
||||
// Graphics image format.
|
||||
const (
|
||||
// 32-bit RGBA format.
|
||||
RGBA = 32
|
||||
|
||||
// 24-bit RGB format.
|
||||
RGB = 24
|
||||
|
||||
// PNG format.
|
||||
PNG = 100
|
||||
)
|
||||
|
||||
// Compression types.
|
||||
const (
|
||||
Zlib = 'z'
|
||||
)
|
||||
|
||||
// Transmission types.
|
||||
const (
|
||||
// The data transmitted directly in the escape sequence.
|
||||
Direct = 'd'
|
||||
|
||||
// The data transmitted in a regular file.
|
||||
File = 'f'
|
||||
|
||||
// A temporary file is used and deleted after transmission.
|
||||
TempFile = 't'
|
||||
|
||||
// A shared memory object.
|
||||
// For POSIX see https://pubs.opengroup.org/onlinepubs/9699919799/functions/shm_open.html
|
||||
// For Windows see https://docs.microsoft.com/en-us/windows/win32/memory/creating-named-shared-memory
|
||||
SharedMemory = 's'
|
||||
)
|
||||
|
||||
// Action types.
|
||||
const (
|
||||
// Transmit image data.
|
||||
Transmit = 't'
|
||||
// TransmitAndPut transmit image data and display (put) it.
|
||||
TransmitAndPut = 'T'
|
||||
// Query terminal for image info.
|
||||
Query = 'q'
|
||||
// Put (display) previously transmitted image.
|
||||
Put = 'p'
|
||||
// Delete image.
|
||||
Delete = 'd'
|
||||
// Frame transmits data for animation frames.
|
||||
Frame = 'f'
|
||||
// Animate controls animation.
|
||||
Animate = 'a'
|
||||
// Compose composes animation frames.
|
||||
Compose = 'c'
|
||||
)
|
||||
|
||||
// Delete types.
|
||||
const (
|
||||
// Delete all placements visible on screen
|
||||
DeleteAll = 'a'
|
||||
// Delete all images with the specified id, specified using the i key. If
|
||||
// you specify a p key for the placement id as well, then only the
|
||||
// placement with the specified image id and placement id will be deleted.
|
||||
DeleteID = 'i'
|
||||
// Delete newest image with the specified number, specified using the I
|
||||
// key. If you specify a p key for the placement id as well, then only the
|
||||
// placement with the specified number and placement id will be deleted.
|
||||
DeleteNumber = 'n'
|
||||
// Delete all placements that intersect with the current cursor position.
|
||||
DeleteCursor = 'c'
|
||||
// Delete animation frames.
|
||||
DeleteFrames = 'f'
|
||||
// Delete all placements that intersect a specific cell, the cell is
|
||||
// specified using the x and y keys
|
||||
DeleteCell = 'p'
|
||||
// Delete all placements that intersect a specific cell having a specific
|
||||
// z-index. The cell and z-index is specified using the x, y and z keys.
|
||||
DeleteCellZ = 'q'
|
||||
// Delete all images whose id is greater than or equal to the value of the x
|
||||
// key and less than or equal to the value of the y.
|
||||
DeleteRange = 'r'
|
||||
// Delete all placements that intersect the specified column, specified using
|
||||
// the x key.
|
||||
DeleteColumn = 'x'
|
||||
// Delete all placements that intersect the specified row, specified using
|
||||
// the y key.
|
||||
DeleteRow = 'y'
|
||||
// Delete all placements that have the specified z-index, specified using the
|
||||
// z key.
|
||||
DeleteZ = 'z'
|
||||
)
|
||||
|
||||
// Diacritic returns the diacritic rune at the specified index. If the index is
|
||||
// out of bounds, the first diacritic rune is returned.
|
||||
func Diacritic(i int) rune {
|
||||
if i < 0 || i >= len(diacritics) {
|
||||
return diacritics[0]
|
||||
}
|
||||
return diacritics[i]
|
||||
}
|
||||
|
||||
// From https://sw.kovidgoyal.net/kitty/_downloads/f0a0de9ec8d9ff4456206db8e0814937/rowcolumn-diacritics.txt
|
||||
// See https://sw.kovidgoyal.net/kitty/graphics-protocol/#unicode-placeholders for further explanation.
|
||||
var diacritics = []rune{
|
||||
'\u0305',
|
||||
'\u030D',
|
||||
'\u030E',
|
||||
'\u0310',
|
||||
'\u0312',
|
||||
'\u033D',
|
||||
'\u033E',
|
||||
'\u033F',
|
||||
'\u0346',
|
||||
'\u034A',
|
||||
'\u034B',
|
||||
'\u034C',
|
||||
'\u0350',
|
||||
'\u0351',
|
||||
'\u0352',
|
||||
'\u0357',
|
||||
'\u035B',
|
||||
'\u0363',
|
||||
'\u0364',
|
||||
'\u0365',
|
||||
'\u0366',
|
||||
'\u0367',
|
||||
'\u0368',
|
||||
'\u0369',
|
||||
'\u036A',
|
||||
'\u036B',
|
||||
'\u036C',
|
||||
'\u036D',
|
||||
'\u036E',
|
||||
'\u036F',
|
||||
'\u0483',
|
||||
'\u0484',
|
||||
'\u0485',
|
||||
'\u0486',
|
||||
'\u0487',
|
||||
'\u0592',
|
||||
'\u0593',
|
||||
'\u0594',
|
||||
'\u0595',
|
||||
'\u0597',
|
||||
'\u0598',
|
||||
'\u0599',
|
||||
'\u059C',
|
||||
'\u059D',
|
||||
'\u059E',
|
||||
'\u059F',
|
||||
'\u05A0',
|
||||
'\u05A1',
|
||||
'\u05A8',
|
||||
'\u05A9',
|
||||
'\u05AB',
|
||||
'\u05AC',
|
||||
'\u05AF',
|
||||
'\u05C4',
|
||||
'\u0610',
|
||||
'\u0611',
|
||||
'\u0612',
|
||||
'\u0613',
|
||||
'\u0614',
|
||||
'\u0615',
|
||||
'\u0616',
|
||||
'\u0617',
|
||||
'\u0657',
|
||||
'\u0658',
|
||||
'\u0659',
|
||||
'\u065A',
|
||||
'\u065B',
|
||||
'\u065D',
|
||||
'\u065E',
|
||||
'\u06D6',
|
||||
'\u06D7',
|
||||
'\u06D8',
|
||||
'\u06D9',
|
||||
'\u06DA',
|
||||
'\u06DB',
|
||||
'\u06DC',
|
||||
'\u06DF',
|
||||
'\u06E0',
|
||||
'\u06E1',
|
||||
'\u06E2',
|
||||
'\u06E4',
|
||||
'\u06E7',
|
||||
'\u06E8',
|
||||
'\u06EB',
|
||||
'\u06EC',
|
||||
'\u0730',
|
||||
'\u0732',
|
||||
'\u0733',
|
||||
'\u0735',
|
||||
'\u0736',
|
||||
'\u073A',
|
||||
'\u073D',
|
||||
'\u073F',
|
||||
'\u0740',
|
||||
'\u0741',
|
||||
'\u0743',
|
||||
'\u0745',
|
||||
'\u0747',
|
||||
'\u0749',
|
||||
'\u074A',
|
||||
'\u07EB',
|
||||
'\u07EC',
|
||||
'\u07ED',
|
||||
'\u07EE',
|
||||
'\u07EF',
|
||||
'\u07F0',
|
||||
'\u07F1',
|
||||
'\u07F3',
|
||||
'\u0816',
|
||||
'\u0817',
|
||||
'\u0818',
|
||||
'\u0819',
|
||||
'\u081B',
|
||||
'\u081C',
|
||||
'\u081D',
|
||||
'\u081E',
|
||||
'\u081F',
|
||||
'\u0820',
|
||||
'\u0821',
|
||||
'\u0822',
|
||||
'\u0823',
|
||||
'\u0825',
|
||||
'\u0826',
|
||||
'\u0827',
|
||||
'\u0829',
|
||||
'\u082A',
|
||||
'\u082B',
|
||||
'\u082C',
|
||||
'\u082D',
|
||||
'\u0951',
|
||||
'\u0953',
|
||||
'\u0954',
|
||||
'\u0F82',
|
||||
'\u0F83',
|
||||
'\u0F86',
|
||||
'\u0F87',
|
||||
'\u135D',
|
||||
'\u135E',
|
||||
'\u135F',
|
||||
'\u17DD',
|
||||
'\u193A',
|
||||
'\u1A17',
|
||||
'\u1A75',
|
||||
'\u1A76',
|
||||
'\u1A77',
|
||||
'\u1A78',
|
||||
'\u1A79',
|
||||
'\u1A7A',
|
||||
'\u1A7B',
|
||||
'\u1A7C',
|
||||
'\u1B6B',
|
||||
'\u1B6D',
|
||||
'\u1B6E',
|
||||
'\u1B6F',
|
||||
'\u1B70',
|
||||
'\u1B71',
|
||||
'\u1B72',
|
||||
'\u1B73',
|
||||
'\u1CD0',
|
||||
'\u1CD1',
|
||||
'\u1CD2',
|
||||
'\u1CDA',
|
||||
'\u1CDB',
|
||||
'\u1CE0',
|
||||
'\u1DC0',
|
||||
'\u1DC1',
|
||||
'\u1DC3',
|
||||
'\u1DC4',
|
||||
'\u1DC5',
|
||||
'\u1DC6',
|
||||
'\u1DC7',
|
||||
'\u1DC8',
|
||||
'\u1DC9',
|
||||
'\u1DCB',
|
||||
'\u1DCC',
|
||||
'\u1DD1',
|
||||
'\u1DD2',
|
||||
'\u1DD3',
|
||||
'\u1DD4',
|
||||
'\u1DD5',
|
||||
'\u1DD6',
|
||||
'\u1DD7',
|
||||
'\u1DD8',
|
||||
'\u1DD9',
|
||||
'\u1DDA',
|
||||
'\u1DDB',
|
||||
'\u1DDC',
|
||||
'\u1DDD',
|
||||
'\u1DDE',
|
||||
'\u1DDF',
|
||||
'\u1DE0',
|
||||
'\u1DE1',
|
||||
'\u1DE2',
|
||||
'\u1DE3',
|
||||
'\u1DE4',
|
||||
'\u1DE5',
|
||||
'\u1DE6',
|
||||
'\u1DFE',
|
||||
'\u20D0',
|
||||
'\u20D1',
|
||||
'\u20D4',
|
||||
'\u20D5',
|
||||
'\u20D6',
|
||||
'\u20D7',
|
||||
'\u20DB',
|
||||
'\u20DC',
|
||||
'\u20E1',
|
||||
'\u20E7',
|
||||
'\u20E9',
|
||||
'\u20F0',
|
||||
'\u2CEF',
|
||||
'\u2CF0',
|
||||
'\u2CF1',
|
||||
'\u2DE0',
|
||||
'\u2DE1',
|
||||
'\u2DE2',
|
||||
'\u2DE3',
|
||||
'\u2DE4',
|
||||
'\u2DE5',
|
||||
'\u2DE6',
|
||||
'\u2DE7',
|
||||
'\u2DE8',
|
||||
'\u2DE9',
|
||||
'\u2DEA',
|
||||
'\u2DEB',
|
||||
'\u2DEC',
|
||||
'\u2DED',
|
||||
'\u2DEE',
|
||||
'\u2DEF',
|
||||
'\u2DF0',
|
||||
'\u2DF1',
|
||||
'\u2DF2',
|
||||
'\u2DF3',
|
||||
'\u2DF4',
|
||||
'\u2DF5',
|
||||
'\u2DF6',
|
||||
'\u2DF7',
|
||||
'\u2DF8',
|
||||
'\u2DF9',
|
||||
'\u2DFA',
|
||||
'\u2DFB',
|
||||
'\u2DFC',
|
||||
'\u2DFD',
|
||||
'\u2DFE',
|
||||
'\u2DFF',
|
||||
'\uA66F',
|
||||
'\uA67C',
|
||||
'\uA67D',
|
||||
'\uA6F0',
|
||||
'\uA6F1',
|
||||
'\uA8E0',
|
||||
'\uA8E1',
|
||||
'\uA8E2',
|
||||
'\uA8E3',
|
||||
'\uA8E4',
|
||||
'\uA8E5',
|
||||
'\uA8E6',
|
||||
'\uA8E7',
|
||||
'\uA8E8',
|
||||
'\uA8E9',
|
||||
'\uA8EA',
|
||||
'\uA8EB',
|
||||
'\uA8EC',
|
||||
'\uA8ED',
|
||||
'\uA8EE',
|
||||
'\uA8EF',
|
||||
'\uA8F0',
|
||||
'\uA8F1',
|
||||
'\uAAB0',
|
||||
'\uAAB2',
|
||||
'\uAAB3',
|
||||
'\uAAB7',
|
||||
'\uAAB8',
|
||||
'\uAABE',
|
||||
'\uAABF',
|
||||
'\uAAC1',
|
||||
'\uFE20',
|
||||
'\uFE21',
|
||||
'\uFE22',
|
||||
'\uFE23',
|
||||
'\uFE24',
|
||||
'\uFE25',
|
||||
'\uFE26',
|
||||
'\U00010A0F',
|
||||
'\U00010A38',
|
||||
'\U0001D185',
|
||||
'\U0001D186',
|
||||
'\U0001D187',
|
||||
'\U0001D188',
|
||||
'\U0001D189',
|
||||
'\U0001D1AA',
|
||||
'\U0001D1AB',
|
||||
'\U0001D1AC',
|
||||
'\U0001D1AD',
|
||||
'\U0001D242',
|
||||
'\U0001D243',
|
||||
'\U0001D244',
|
||||
}
|
367
vendor/github.com/charmbracelet/x/ansi/kitty/options.go
generated
vendored
Normal file
367
vendor/github.com/charmbracelet/x/ansi/kitty/options.go
generated
vendored
Normal file
@ -0,0 +1,367 @@
|
||||
package kitty
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
_ encoding.TextMarshaler = Options{}
|
||||
_ encoding.TextUnmarshaler = &Options{}
|
||||
)
|
||||
|
||||
// Options represents a Kitty Graphics Protocol options.
|
||||
type Options struct {
|
||||
// Common options.
|
||||
|
||||
// Action (a=t) is the action to be performed on the image. Can be one of
|
||||
// [Transmit], [TransmitDisplay], [Query], [Put], [Delete], [Frame],
|
||||
// [Animate], [Compose].
|
||||
Action byte
|
||||
|
||||
// Quite mode (q=0) is the quiet mode. Can be either zero, one, or two
|
||||
// where zero is the default, 1 suppresses OK responses, and 2 suppresses
|
||||
// both OK and error responses.
|
||||
Quite byte
|
||||
|
||||
// Transmission options.
|
||||
|
||||
// ID (i=) is the image ID. The ID is a unique identifier for the image.
|
||||
// Must be a positive integer up to [math.MaxUint32].
|
||||
ID int
|
||||
|
||||
// PlacementID (p=) is the placement ID. The placement ID is a unique
|
||||
// identifier for the placement of the image. Must be a positive integer up
|
||||
// to [math.MaxUint32].
|
||||
PlacementID int
|
||||
|
||||
// Number (I=0) is the number of images to be transmitted.
|
||||
Number int
|
||||
|
||||
// Format (f=32) is the image format. One of [RGBA], [RGB], [PNG].
|
||||
Format int
|
||||
|
||||
// ImageWidth (s=0) is the transmitted image width.
|
||||
ImageWidth int
|
||||
|
||||
// ImageHeight (v=0) is the transmitted image height.
|
||||
ImageHeight int
|
||||
|
||||
// Compression (o=) is the image compression type. Can be [Zlib] or zero.
|
||||
Compression byte
|
||||
|
||||
// Transmission (t=d) is the image transmission type. Can be [Direct], [File],
|
||||
// [TempFile], or[SharedMemory].
|
||||
Transmission byte
|
||||
|
||||
// File is the file path to be used when the transmission type is [File].
|
||||
// If [Options.Transmission] is omitted i.e. zero and this is non-empty,
|
||||
// the transmission type is set to [File].
|
||||
File string
|
||||
|
||||
// Size (S=0) is the size to be read from the transmission medium.
|
||||
Size int
|
||||
|
||||
// Offset (O=0) is the offset byte to start reading from the transmission
|
||||
// medium.
|
||||
Offset int
|
||||
|
||||
// Chunk (m=) whether the image is transmitted in chunks. Can be either
|
||||
// zero or one. When true, the image is transmitted in chunks. Each chunk
|
||||
// must be a multiple of 4, and up to [MaxChunkSize] bytes. Each chunk must
|
||||
// have the m=1 option except for the last chunk which must have m=0.
|
||||
Chunk bool
|
||||
|
||||
// Display options.
|
||||
|
||||
// X (x=0) is the pixel X coordinate of the image to start displaying.
|
||||
X int
|
||||
|
||||
// Y (y=0) is the pixel Y coordinate of the image to start displaying.
|
||||
Y int
|
||||
|
||||
// Z (z=0) is the Z coordinate of the image to display.
|
||||
Z int
|
||||
|
||||
// Width (w=0) is the width of the image to display.
|
||||
Width int
|
||||
|
||||
// Height (h=0) is the height of the image to display.
|
||||
Height int
|
||||
|
||||
// OffsetX (X=0) is the OffsetX coordinate of the cursor cell to start
|
||||
// displaying the image. OffsetX=0 is the leftmost cell. This must be
|
||||
// smaller than the terminal cell width.
|
||||
OffsetX int
|
||||
|
||||
// OffsetY (Y=0) is the OffsetY coordinate of the cursor cell to start
|
||||
// displaying the image. OffsetY=0 is the topmost cell. This must be
|
||||
// smaller than the terminal cell height.
|
||||
OffsetY int
|
||||
|
||||
// Columns (c=0) is the number of columns to display the image. The image
|
||||
// will be scaled to fit the number of columns.
|
||||
Columns int
|
||||
|
||||
// Rows (r=0) is the number of rows to display the image. The image will be
|
||||
// scaled to fit the number of rows.
|
||||
Rows int
|
||||
|
||||
// VirtualPlacement (U=0) whether to use virtual placement. This is used
|
||||
// with Unicode [Placeholder] to display images.
|
||||
VirtualPlacement bool
|
||||
|
||||
// DoNotMoveCursor (C=0) whether to move the cursor after displaying the
|
||||
// image.
|
||||
DoNotMoveCursor bool
|
||||
|
||||
// ParentID (P=0) is the parent image ID. The parent ID is the ID of the
|
||||
// image that is the parent of the current image. This is used with Unicode
|
||||
// [Placeholder] to display images relative to the parent image.
|
||||
ParentID int
|
||||
|
||||
// ParentPlacementID (Q=0) is the parent placement ID. The parent placement
|
||||
// ID is the ID of the placement of the parent image. This is used with
|
||||
// Unicode [Placeholder] to display images relative to the parent image.
|
||||
ParentPlacementID int
|
||||
|
||||
// Delete options.
|
||||
|
||||
// Delete (d=a) is the delete action. Can be one of [DeleteAll],
|
||||
// [DeleteID], [DeleteNumber], [DeleteCursor], [DeleteFrames],
|
||||
// [DeleteCell], [DeleteCellZ], [DeleteRange], [DeleteColumn], [DeleteRow],
|
||||
// [DeleteZ].
|
||||
Delete byte
|
||||
|
||||
// DeleteResources indicates whether to delete the resources associated
|
||||
// with the image.
|
||||
DeleteResources bool
|
||||
}
|
||||
|
||||
// Options returns the options as a slice of a key-value pairs.
|
||||
func (o *Options) Options() (opts []string) {
|
||||
opts = []string{}
|
||||
if o.Format == 0 {
|
||||
o.Format = RGBA
|
||||
}
|
||||
|
||||
if o.Action == 0 {
|
||||
o.Action = Transmit
|
||||
}
|
||||
|
||||
if o.Delete == 0 {
|
||||
o.Delete = DeleteAll
|
||||
}
|
||||
|
||||
if o.Transmission == 0 {
|
||||
if len(o.File) > 0 {
|
||||
o.Transmission = File
|
||||
} else {
|
||||
o.Transmission = Direct
|
||||
}
|
||||
}
|
||||
|
||||
if o.Format != RGBA {
|
||||
opts = append(opts, fmt.Sprintf("f=%d", o.Format))
|
||||
}
|
||||
|
||||
if o.Quite > 0 {
|
||||
opts = append(opts, fmt.Sprintf("q=%d", o.Quite))
|
||||
}
|
||||
|
||||
if o.ID > 0 {
|
||||
opts = append(opts, fmt.Sprintf("i=%d", o.ID))
|
||||
}
|
||||
|
||||
if o.PlacementID > 0 {
|
||||
opts = append(opts, fmt.Sprintf("p=%d", o.PlacementID))
|
||||
}
|
||||
|
||||
if o.Number > 0 {
|
||||
opts = append(opts, fmt.Sprintf("I=%d", o.Number))
|
||||
}
|
||||
|
||||
if o.ImageWidth > 0 {
|
||||
opts = append(opts, fmt.Sprintf("s=%d", o.ImageWidth))
|
||||
}
|
||||
|
||||
if o.ImageHeight > 0 {
|
||||
opts = append(opts, fmt.Sprintf("v=%d", o.ImageHeight))
|
||||
}
|
||||
|
||||
if o.Transmission != Direct {
|
||||
opts = append(opts, fmt.Sprintf("t=%c", o.Transmission))
|
||||
}
|
||||
|
||||
if o.Size > 0 {
|
||||
opts = append(opts, fmt.Sprintf("S=%d", o.Size))
|
||||
}
|
||||
|
||||
if o.Offset > 0 {
|
||||
opts = append(opts, fmt.Sprintf("O=%d", o.Offset))
|
||||
}
|
||||
|
||||
if o.Compression == Zlib {
|
||||
opts = append(opts, fmt.Sprintf("o=%c", o.Compression))
|
||||
}
|
||||
|
||||
if o.VirtualPlacement {
|
||||
opts = append(opts, "U=1")
|
||||
}
|
||||
|
||||
if o.DoNotMoveCursor {
|
||||
opts = append(opts, "C=1")
|
||||
}
|
||||
|
||||
if o.ParentID > 0 {
|
||||
opts = append(opts, fmt.Sprintf("P=%d", o.ParentID))
|
||||
}
|
||||
|
||||
if o.ParentPlacementID > 0 {
|
||||
opts = append(opts, fmt.Sprintf("Q=%d", o.ParentPlacementID))
|
||||
}
|
||||
|
||||
if o.X > 0 {
|
||||
opts = append(opts, fmt.Sprintf("x=%d", o.X))
|
||||
}
|
||||
|
||||
if o.Y > 0 {
|
||||
opts = append(opts, fmt.Sprintf("y=%d", o.Y))
|
||||
}
|
||||
|
||||
if o.Z > 0 {
|
||||
opts = append(opts, fmt.Sprintf("z=%d", o.Z))
|
||||
}
|
||||
|
||||
if o.Width > 0 {
|
||||
opts = append(opts, fmt.Sprintf("w=%d", o.Width))
|
||||
}
|
||||
|
||||
if o.Height > 0 {
|
||||
opts = append(opts, fmt.Sprintf("h=%d", o.Height))
|
||||
}
|
||||
|
||||
if o.OffsetX > 0 {
|
||||
opts = append(opts, fmt.Sprintf("X=%d", o.OffsetX))
|
||||
}
|
||||
|
||||
if o.OffsetY > 0 {
|
||||
opts = append(opts, fmt.Sprintf("Y=%d", o.OffsetY))
|
||||
}
|
||||
|
||||
if o.Columns > 0 {
|
||||
opts = append(opts, fmt.Sprintf("c=%d", o.Columns))
|
||||
}
|
||||
|
||||
if o.Rows > 0 {
|
||||
opts = append(opts, fmt.Sprintf("r=%d", o.Rows))
|
||||
}
|
||||
|
||||
if o.Delete != DeleteAll || o.DeleteResources {
|
||||
da := o.Delete
|
||||
if o.DeleteResources {
|
||||
da = da - ' ' // to uppercase
|
||||
}
|
||||
|
||||
opts = append(opts, fmt.Sprintf("d=%c", da))
|
||||
}
|
||||
|
||||
if o.Action != Transmit {
|
||||
opts = append(opts, fmt.Sprintf("a=%c", o.Action))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// String returns the string representation of the options.
|
||||
func (o Options) String() string {
|
||||
return strings.Join(o.Options(), ",")
|
||||
}
|
||||
|
||||
// MarshalText returns the string representation of the options.
|
||||
func (o Options) MarshalText() ([]byte, error) {
|
||||
return []byte(o.String()), nil
|
||||
}
|
||||
|
||||
// UnmarshalText parses the options from the given string.
|
||||
func (o *Options) UnmarshalText(text []byte) error {
|
||||
opts := strings.Split(string(text), ",")
|
||||
for _, opt := range opts {
|
||||
ps := strings.SplitN(opt, "=", 2)
|
||||
if len(ps) != 2 || len(ps[1]) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
switch ps[0] {
|
||||
case "a":
|
||||
o.Action = ps[1][0]
|
||||
case "o":
|
||||
o.Compression = ps[1][0]
|
||||
case "t":
|
||||
o.Transmission = ps[1][0]
|
||||
case "d":
|
||||
d := ps[1][0]
|
||||
if d >= 'A' && d <= 'Z' {
|
||||
o.DeleteResources = true
|
||||
d = d + ' ' // to lowercase
|
||||
}
|
||||
o.Delete = d
|
||||
case "i", "q", "p", "I", "f", "s", "v", "S", "O", "m", "x", "y", "z", "w", "h", "X", "Y", "c", "r", "U", "P", "Q":
|
||||
v, err := strconv.Atoi(ps[1])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
switch ps[0] {
|
||||
case "i":
|
||||
o.ID = v
|
||||
case "q":
|
||||
o.Quite = byte(v)
|
||||
case "p":
|
||||
o.PlacementID = v
|
||||
case "I":
|
||||
o.Number = v
|
||||
case "f":
|
||||
o.Format = v
|
||||
case "s":
|
||||
o.ImageWidth = v
|
||||
case "v":
|
||||
o.ImageHeight = v
|
||||
case "S":
|
||||
o.Size = v
|
||||
case "O":
|
||||
o.Offset = v
|
||||
case "m":
|
||||
o.Chunk = v == 0 || v == 1
|
||||
case "x":
|
||||
o.X = v
|
||||
case "y":
|
||||
o.Y = v
|
||||
case "z":
|
||||
o.Z = v
|
||||
case "w":
|
||||
o.Width = v
|
||||
case "h":
|
||||
o.Height = v
|
||||
case "X":
|
||||
o.OffsetX = v
|
||||
case "Y":
|
||||
o.OffsetY = v
|
||||
case "c":
|
||||
o.Columns = v
|
||||
case "r":
|
||||
o.Rows = v
|
||||
case "U":
|
||||
o.VirtualPlacement = v == 1
|
||||
case "P":
|
||||
o.ParentID = v
|
||||
case "Q":
|
||||
o.ParentPlacementID = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
172
vendor/github.com/charmbracelet/x/ansi/method.go
generated
vendored
Normal file
172
vendor/github.com/charmbracelet/x/ansi/method.go
generated
vendored
Normal file
@ -0,0 +1,172 @@
|
||||
package ansi
|
||||
|
||||
// Method is a type that represents the how the renderer should calculate the
|
||||
// display width of cells.
|
||||
type Method uint8
|
||||
|
||||
// Display width modes.
|
||||
const (
|
||||
WcWidth Method = iota
|
||||
GraphemeWidth
|
||||
)
|
||||
|
||||
// StringWidth returns the width of a string in cells. This is the number of
|
||||
// cells that the string will occupy when printed in a terminal. ANSI escape
|
||||
// codes are ignored and wide characters (such as East Asians and emojis) are
|
||||
// accounted for.
|
||||
func (m Method) StringWidth(s string) int {
|
||||
return stringWidth(m, s)
|
||||
}
|
||||
|
||||
// Truncate truncates a string to a given length, adding a tail to the end if
|
||||
// the string is longer than the given length. This function is aware of ANSI
|
||||
// escape codes and will not break them, and accounts for wide-characters (such
|
||||
// as East-Asian characters and emojis).
|
||||
func (m Method) Truncate(s string, length int, tail string) string {
|
||||
return truncate(m, s, length, tail)
|
||||
}
|
||||
|
||||
// TruncateLeft truncates a string to a given length, adding a prefix to the
|
||||
// beginning if the string is longer than the given length. This function is
|
||||
// aware of ANSI escape codes and will not break them, and accounts for
|
||||
// wide-characters (such as East-Asian characters and emojis).
|
||||
func (m Method) TruncateLeft(s string, length int, prefix string) string {
|
||||
return truncateLeft(m, s, length, prefix)
|
||||
}
|
||||
|
||||
// Cut the string, without adding any prefix or tail strings. This function is
|
||||
// aware of ANSI escape codes and will not break them, and accounts for
|
||||
// wide-characters (such as East-Asian characters and emojis). Note that the
|
||||
// [left] parameter is inclusive, while [right] isn't.
|
||||
func (m Method) Cut(s string, left, right int) string {
|
||||
return cut(m, s, left, right)
|
||||
}
|
||||
|
||||
// Hardwrap wraps a string or a block of text to a given line length, breaking
|
||||
// word boundaries. This will preserve ANSI escape codes and will account for
|
||||
// wide-characters in the string.
|
||||
// When preserveSpace is true, spaces at the beginning of a line will be
|
||||
// preserved.
|
||||
// This treats the text as a sequence of graphemes.
|
||||
func (m Method) Hardwrap(s string, length int, preserveSpace bool) string {
|
||||
return hardwrap(m, s, length, preserveSpace)
|
||||
}
|
||||
|
||||
// Wordwrap wraps a string or a block of text to a given line length, not
|
||||
// breaking word boundaries. This will preserve ANSI escape codes and will
|
||||
// account for wide-characters in the string.
|
||||
// The breakpoints string is a list of characters that are considered
|
||||
// breakpoints for word wrapping. A hyphen (-) is always considered a
|
||||
// breakpoint.
|
||||
//
|
||||
// Note: breakpoints must be a string of 1-cell wide rune characters.
|
||||
func (m Method) Wordwrap(s string, length int, breakpoints string) string {
|
||||
return wordwrap(m, s, length, breakpoints)
|
||||
}
|
||||
|
||||
// Wrap wraps a string or a block of text to a given line length, breaking word
|
||||
// boundaries if necessary. This will preserve ANSI escape codes and will
|
||||
// account for wide-characters in the string. The breakpoints string is a list
|
||||
// of characters that are considered breakpoints for word wrapping. A hyphen
|
||||
// (-) is always considered a breakpoint.
|
||||
//
|
||||
// Note: breakpoints must be a string of 1-cell wide rune characters.
|
||||
func (m Method) Wrap(s string, length int, breakpoints string) string {
|
||||
return wrap(m, s, length, breakpoints)
|
||||
}
|
||||
|
||||
// DecodeSequence decodes the first ANSI escape sequence or a printable
|
||||
// grapheme from the given data. It returns the sequence slice, the number of
|
||||
// bytes read, the cell width for each sequence, and the new state.
|
||||
//
|
||||
// The cell width will always be 0 for control and escape sequences, 1 for
|
||||
// ASCII printable characters, and the number of cells other Unicode characters
|
||||
// occupy. It uses the uniseg package to calculate the width of Unicode
|
||||
// graphemes and characters. This means it will always do grapheme clustering
|
||||
// (mode 2027).
|
||||
//
|
||||
// Passing a non-nil [*Parser] as the last argument will allow the decoder to
|
||||
// collect sequence parameters, data, and commands. The parser cmd will have
|
||||
// the packed command value that contains intermediate and prefix characters.
|
||||
// In the case of a OSC sequence, the cmd will be the OSC command number. Use
|
||||
// [Cmd] and [Param] types to unpack command intermediates and prefixes as well
|
||||
// as parameters.
|
||||
//
|
||||
// Zero [Cmd] means the CSI, DCS, or ESC sequence is invalid. Moreover, checking the
|
||||
// validity of other data sequences, OSC, DCS, etc, will require checking for
|
||||
// the returned sequence terminator bytes such as ST (ESC \\) and BEL).
|
||||
//
|
||||
// We store the command byte in [Cmd] in the most significant byte, the
|
||||
// prefix byte in the next byte, and the intermediate byte in the least
|
||||
// significant byte. This is done to avoid using a struct to store the command
|
||||
// and its intermediates and prefixes. The command byte is always the least
|
||||
// significant byte i.e. [Cmd & 0xff]. Use the [Cmd] type to unpack the
|
||||
// command, intermediate, and prefix bytes. Note that we only collect the last
|
||||
// prefix character and intermediate byte.
|
||||
//
|
||||
// The [p.Params] slice will contain the parameters of the sequence. Any
|
||||
// sub-parameter will have the [parser.HasMoreFlag] set. Use the [Param] type
|
||||
// to unpack the parameters.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// var state byte // the initial state is always zero [NormalState]
|
||||
// p := NewParser(32, 1024) // create a new parser with a 32 params buffer and 1024 data buffer (optional)
|
||||
// input := []byte("\x1b[31mHello, World!\x1b[0m")
|
||||
// for len(input) > 0 {
|
||||
// seq, width, n, newState := DecodeSequence(input, state, p)
|
||||
// log.Printf("seq: %q, width: %d", seq, width)
|
||||
// state = newState
|
||||
// input = input[n:]
|
||||
// }
|
||||
func (m Method) DecodeSequence(data []byte, state byte, p *Parser) (seq []byte, width, n int, newState byte) {
|
||||
return decodeSequence(m, data, state, p)
|
||||
}
|
||||
|
||||
// DecodeSequenceInString decodes the first ANSI escape sequence or a printable
|
||||
// grapheme from the given data. It returns the sequence slice, the number of
|
||||
// bytes read, the cell width for each sequence, and the new state.
|
||||
//
|
||||
// The cell width will always be 0 for control and escape sequences, 1 for
|
||||
// ASCII printable characters, and the number of cells other Unicode characters
|
||||
// occupy. It uses the uniseg package to calculate the width of Unicode
|
||||
// graphemes and characters. This means it will always do grapheme clustering
|
||||
// (mode 2027).
|
||||
//
|
||||
// Passing a non-nil [*Parser] as the last argument will allow the decoder to
|
||||
// collect sequence parameters, data, and commands. The parser cmd will have
|
||||
// the packed command value that contains intermediate and prefix characters.
|
||||
// In the case of a OSC sequence, the cmd will be the OSC command number. Use
|
||||
// [Cmd] and [Param] types to unpack command intermediates and prefixes as well
|
||||
// as parameters.
|
||||
//
|
||||
// Zero [Cmd] means the CSI, DCS, or ESC sequence is invalid. Moreover, checking the
|
||||
// validity of other data sequences, OSC, DCS, etc, will require checking for
|
||||
// the returned sequence terminator bytes such as ST (ESC \\) and BEL).
|
||||
//
|
||||
// We store the command byte in [Cmd] in the most significant byte, the
|
||||
// prefix byte in the next byte, and the intermediate byte in the least
|
||||
// significant byte. This is done to avoid using a struct to store the command
|
||||
// and its intermediates and prefixes. The command byte is always the least
|
||||
// significant byte i.e. [Cmd & 0xff]. Use the [Cmd] type to unpack the
|
||||
// command, intermediate, and prefix bytes. Note that we only collect the last
|
||||
// prefix character and intermediate byte.
|
||||
//
|
||||
// The [p.Params] slice will contain the parameters of the sequence. Any
|
||||
// sub-parameter will have the [parser.HasMoreFlag] set. Use the [Param] type
|
||||
// to unpack the parameters.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// var state byte // the initial state is always zero [NormalState]
|
||||
// p := NewParser(32, 1024) // create a new parser with a 32 params buffer and 1024 data buffer (optional)
|
||||
// input := []byte("\x1b[31mHello, World!\x1b[0m")
|
||||
// for len(input) > 0 {
|
||||
// seq, width, n, newState := DecodeSequenceInString(input, state, p)
|
||||
// log.Printf("seq: %q, width: %d", seq, width)
|
||||
// state = newState
|
||||
// input = input[n:]
|
||||
// }
|
||||
func (m Method) DecodeSequenceInString(data string, state byte, p *Parser) (seq string, width, n int, newState byte) {
|
||||
return decodeSequence(m, data, state, p)
|
||||
}
|
31
vendor/github.com/charmbracelet/x/ansi/mode.go
generated
vendored
31
vendor/github.com/charmbracelet/x/ansi/mode.go
generated
vendored
@ -51,7 +51,8 @@ type Mode interface {
|
||||
// SetMode (SM) returns a sequence to set a mode.
|
||||
// The mode arguments are a list of modes to set.
|
||||
//
|
||||
// If one of the modes is a [DECMode], the sequence will use the DEC format.
|
||||
// If one of the modes is a [DECMode], the function will returns two escape
|
||||
// sequences.
|
||||
//
|
||||
// ANSI format:
|
||||
//
|
||||
@ -74,7 +75,8 @@ func SM(modes ...Mode) string {
|
||||
// ResetMode (RM) returns a sequence to reset a mode.
|
||||
// The mode arguments are a list of modes to reset.
|
||||
//
|
||||
// If one of the modes is a [DECMode], the sequence will use the DEC format.
|
||||
// If one of the modes is a [DECMode], the function will returns two escape
|
||||
// sequences.
|
||||
//
|
||||
// ANSI format:
|
||||
//
|
||||
@ -94,9 +96,9 @@ func RM(modes ...Mode) string {
|
||||
return ResetMode(modes...)
|
||||
}
|
||||
|
||||
func setMode(reset bool, modes ...Mode) string {
|
||||
func setMode(reset bool, modes ...Mode) (s string) {
|
||||
if len(modes) == 0 {
|
||||
return ""
|
||||
return
|
||||
}
|
||||
|
||||
cmd := "h"
|
||||
@ -113,21 +115,24 @@ func setMode(reset bool, modes ...Mode) string {
|
||||
return seq + strconv.Itoa(modes[0].Mode()) + cmd
|
||||
}
|
||||
|
||||
var dec bool
|
||||
list := make([]string, len(modes))
|
||||
for i, m := range modes {
|
||||
list[i] = strconv.Itoa(m.Mode())
|
||||
dec := make([]string, 0, len(modes)/2)
|
||||
ansi := make([]string, 0, len(modes)/2)
|
||||
for _, m := range modes {
|
||||
switch m.(type) {
|
||||
case DECMode:
|
||||
dec = true
|
||||
dec = append(dec, strconv.Itoa(m.Mode()))
|
||||
case ANSIMode:
|
||||
ansi = append(ansi, strconv.Itoa(m.Mode()))
|
||||
}
|
||||
}
|
||||
|
||||
if dec {
|
||||
seq += "?"
|
||||
if len(ansi) > 0 {
|
||||
s += seq + strings.Join(ansi, ";") + cmd
|
||||
}
|
||||
|
||||
return seq + strings.Join(list, ";") + cmd
|
||||
if len(dec) > 0 {
|
||||
s += seq + "?" + strings.Join(dec, ";") + cmd
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// RequestMode (DECRQM) returns a sequence to request a mode from the terminal.
|
||||
|
33
vendor/github.com/charmbracelet/x/ansi/mouse.go
generated
vendored
33
vendor/github.com/charmbracelet/x/ansi/mouse.go
generated
vendored
@ -26,19 +26,28 @@ type MouseButton byte
|
||||
// Other buttons are not supported.
|
||||
const (
|
||||
MouseNone MouseButton = iota
|
||||
MouseLeft
|
||||
MouseMiddle
|
||||
MouseRight
|
||||
MouseWheelUp
|
||||
MouseWheelDown
|
||||
MouseWheelLeft
|
||||
MouseWheelRight
|
||||
MouseBackward
|
||||
MouseForward
|
||||
MouseButton1
|
||||
MouseButton2
|
||||
MouseButton3
|
||||
MouseButton4
|
||||
MouseButton5
|
||||
MouseButton6
|
||||
MouseButton7
|
||||
MouseButton8
|
||||
MouseButton9
|
||||
MouseButton10
|
||||
MouseButton11
|
||||
|
||||
MouseRelease = MouseNone
|
||||
MouseLeft = MouseButton1
|
||||
MouseMiddle = MouseButton2
|
||||
MouseRight = MouseButton3
|
||||
MouseWheelUp = MouseButton4
|
||||
MouseWheelDown = MouseButton5
|
||||
MouseWheelLeft = MouseButton6
|
||||
MouseWheelRight = MouseButton7
|
||||
MouseBackward = MouseButton8
|
||||
MouseForward = MouseButton9
|
||||
MouseRelease = MouseNone
|
||||
)
|
||||
|
||||
var mouseButtons = map[MouseButton]string{
|
||||
@ -61,7 +70,7 @@ func (b MouseButton) String() string {
|
||||
return mouseButtons[b]
|
||||
}
|
||||
|
||||
// Button returns a byte representing a mouse button.
|
||||
// EncodeMouseButton returns a byte representing a mouse button.
|
||||
// The button is a bitmask of the following leftmost values:
|
||||
//
|
||||
// - The first two bits are the button number:
|
||||
@ -85,7 +94,7 @@ func (b MouseButton) String() string {
|
||||
//
|
||||
// If button is [MouseNone], and motion is false, this returns a release event.
|
||||
// If button is undefined, this function returns 0xff.
|
||||
func (b MouseButton) Button(motion, shift, alt, ctrl bool) (m byte) {
|
||||
func EncodeMouseButton(b MouseButton, motion, shift, alt, ctrl bool) (m byte) {
|
||||
// mouse bit shifts
|
||||
const (
|
||||
bitShift = 0b0000_0100
|
||||
|
4
vendor/github.com/charmbracelet/x/ansi/notification.go
generated
vendored
4
vendor/github.com/charmbracelet/x/ansi/notification.go
generated
vendored
@ -2,8 +2,8 @@ package ansi
|
||||
|
||||
// Notify sends a desktop notification using iTerm's OSC 9.
|
||||
//
|
||||
// OSC 9 ; Mc ST
|
||||
// OSC 9 ; Mc BEL
|
||||
// OSC 9 ; Mc ST
|
||||
// OSC 9 ; Mc BEL
|
||||
//
|
||||
// Where Mc is the notification body.
|
||||
//
|
||||
|
70
vendor/github.com/charmbracelet/x/ansi/osc.go
generated
vendored
70
vendor/github.com/charmbracelet/x/ansi/osc.go
generated
vendored
@ -1,70 +0,0 @@
|
||||
package ansi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// OscSequence represents an OSC sequence.
|
||||
//
|
||||
// The sequence starts with a OSC sequence, OSC (0x9D) in a 8-bit environment
|
||||
// or ESC ] (0x1B 0x5D) in a 7-bit environment, followed by positive integer identifier,
|
||||
// then by arbitrary data terminated by a ST (0x9C) in a 8-bit environment,
|
||||
// ESC \ (0x1B 0x5C) in a 7-bit environment, or BEL (0x07) for backwards compatibility.
|
||||
//
|
||||
// OSC Ps ; Pt ST
|
||||
// OSC Ps ; Pt BEL
|
||||
//
|
||||
// See ECMA-48 § 5.7.
|
||||
type OscSequence struct {
|
||||
// Data contains the raw data of the sequence including the identifier
|
||||
// command.
|
||||
Data []byte
|
||||
|
||||
// Cmd contains the raw command of the sequence.
|
||||
Cmd int
|
||||
}
|
||||
|
||||
var _ Sequence = OscSequence{}
|
||||
|
||||
// Clone returns a deep copy of the OSC sequence.
|
||||
func (o OscSequence) Clone() Sequence {
|
||||
return OscSequence{
|
||||
Data: append([]byte(nil), o.Data...),
|
||||
Cmd: o.Cmd,
|
||||
}
|
||||
}
|
||||
|
||||
// Split returns a slice of data split by the semicolon with the first element
|
||||
// being the identifier command.
|
||||
func (o OscSequence) Split() []string {
|
||||
return strings.Split(string(o.Data), ";")
|
||||
}
|
||||
|
||||
// Command returns the OSC command. This is always gonna be a positive integer
|
||||
// that identifies the OSC sequence.
|
||||
func (o OscSequence) Command() int {
|
||||
return o.Cmd
|
||||
}
|
||||
|
||||
// String returns the string representation of the OSC sequence.
|
||||
// To be more compatible with different terminal, this will always return a
|
||||
// 7-bit formatted sequence, terminated by BEL.
|
||||
func (s OscSequence) String() string {
|
||||
return s.buffer().String()
|
||||
}
|
||||
|
||||
// Bytes returns the byte representation of the OSC sequence.
|
||||
// To be more compatible with different terminal, this will always return a
|
||||
// 7-bit formatted sequence, terminated by BEL.
|
||||
func (s OscSequence) Bytes() []byte {
|
||||
return s.buffer().Bytes()
|
||||
}
|
||||
|
||||
func (s OscSequence) buffer() *bytes.Buffer {
|
||||
var b bytes.Buffer
|
||||
b.WriteString("\x1b]")
|
||||
b.Write(s.Data)
|
||||
b.WriteByte(BEL)
|
||||
return &b
|
||||
}
|
45
vendor/github.com/charmbracelet/x/ansi/params.go
generated
vendored
45
vendor/github.com/charmbracelet/x/ansi/params.go
generated
vendored
@ -1,45 +0,0 @@
|
||||
package ansi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
// Params parses and returns a list of control sequence parameters.
|
||||
//
|
||||
// Parameters are positive integers separated by semicolons. Empty parameters
|
||||
// default to zero. Parameters can have sub-parameters separated by colons.
|
||||
//
|
||||
// Any non-parameter bytes are ignored. This includes bytes that are not in the
|
||||
// range of 0x30-0x3B.
|
||||
//
|
||||
// See ECMA-48 § 5.4.1.
|
||||
func Params(p []byte) [][]uint {
|
||||
if len(p) == 0 {
|
||||
return [][]uint{}
|
||||
}
|
||||
|
||||
// Filter out non-parameter bytes i.e. non 0x30-0x3B.
|
||||
p = bytes.TrimFunc(p, func(r rune) bool {
|
||||
return r < 0x30 || r > 0x3B
|
||||
})
|
||||
|
||||
parts := bytes.Split(p, []byte{';'})
|
||||
params := make([][]uint, len(parts))
|
||||
for i, part := range parts {
|
||||
sparts := bytes.Split(part, []byte{':'})
|
||||
params[i] = make([]uint, len(sparts))
|
||||
for j, spart := range sparts {
|
||||
params[i][j] = bytesToUint16(spart)
|
||||
}
|
||||
}
|
||||
|
||||
return params
|
||||
}
|
||||
|
||||
func bytesToUint16(b []byte) uint {
|
||||
var n uint
|
||||
for _, c := range b {
|
||||
n = n*10 + uint(c-'0')
|
||||
}
|
||||
return n
|
||||
}
|
104
vendor/github.com/charmbracelet/x/ansi/parser.go
generated
vendored
104
vendor/github.com/charmbracelet/x/ansi/parser.go
generated
vendored
@ -7,9 +7,6 @@ import (
|
||||
"github.com/charmbracelet/x/ansi/parser"
|
||||
)
|
||||
|
||||
// ParserDispatcher is a function that dispatches a sequence.
|
||||
type ParserDispatcher func(Sequence)
|
||||
|
||||
// Parser represents a DEC ANSI compatible sequence parser.
|
||||
//
|
||||
// It uses a state machine to parse ANSI escape sequences and control
|
||||
@ -20,8 +17,7 @@ type ParserDispatcher func(Sequence)
|
||||
//
|
||||
//go:generate go run ./gen.go
|
||||
type Parser struct {
|
||||
// the dispatch function to call when a sequence is complete
|
||||
dispatcher ParserDispatcher
|
||||
handler Handler
|
||||
|
||||
// params contains the raw parameters of the sequence.
|
||||
// These parameters used when constructing CSI and DCS sequences.
|
||||
@ -43,10 +39,10 @@ type Parser struct {
|
||||
// number of rune bytes collected.
|
||||
paramsLen int
|
||||
|
||||
// cmd contains the raw command along with the private marker and
|
||||
// cmd contains the raw command along with the private prefix and
|
||||
// intermediate bytes of the sequence.
|
||||
// The first lower byte contains the command byte, the next byte contains
|
||||
// the private marker, and the next byte contains the intermediate byte.
|
||||
// the private prefix, and the next byte contains the intermediate byte.
|
||||
//
|
||||
// This is also used when collecting UTF-8 runes treating it as a slice of
|
||||
// 4 bytes.
|
||||
@ -56,24 +52,17 @@ type Parser struct {
|
||||
state byte
|
||||
}
|
||||
|
||||
// NewParser returns a new parser with an optional [ParserDispatcher].
|
||||
// NewParser returns a new parser with the default settings.
|
||||
// The [Parser] uses a default size of 32 for the parameters and 64KB for the
|
||||
// data buffer. Use [Parser.SetParamsSize] and [Parser.SetDataSize] to set the
|
||||
// size of the parameters and data buffer respectively.
|
||||
func NewParser(d ParserDispatcher) *Parser {
|
||||
func NewParser() *Parser {
|
||||
p := new(Parser)
|
||||
p.SetDispatcher(d)
|
||||
p.SetParamsSize(parser.MaxParamsSize)
|
||||
p.SetDataSize(1024 * 64) // 64KB data buffer
|
||||
return p
|
||||
}
|
||||
|
||||
// SetDispatcher sets the dispatcher function to call when a sequence is
|
||||
// complete.
|
||||
func (p *Parser) SetDispatcher(d ParserDispatcher) {
|
||||
p.dispatcher = d
|
||||
}
|
||||
|
||||
// SetParamsSize sets the size of the parameters buffer.
|
||||
// This is used when constructing CSI and DCS sequences.
|
||||
func (p *Parser) SetParamsSize(size int) {
|
||||
@ -93,8 +82,8 @@ func (p *Parser) SetDataSize(size int) {
|
||||
}
|
||||
|
||||
// Params returns the list of parsed packed parameters.
|
||||
func (p *Parser) Params() []Parameter {
|
||||
return unsafe.Slice((*Parameter)(unsafe.Pointer(&p.params[0])), p.paramsLen)
|
||||
func (p *Parser) Params() Params {
|
||||
return unsafe.Slice((*Param)(unsafe.Pointer(&p.params[0])), p.paramsLen)
|
||||
}
|
||||
|
||||
// Param returns the parameter at the given index and falls back to the default
|
||||
@ -104,12 +93,13 @@ func (p *Parser) Param(i, def int) (int, bool) {
|
||||
if i < 0 || i >= p.paramsLen {
|
||||
return def, false
|
||||
}
|
||||
return Parameter(p.params[i]).Param(def), true
|
||||
return Param(p.params[i]).Param(def), true
|
||||
}
|
||||
|
||||
// Cmd returns the packed command of the last dispatched sequence.
|
||||
func (p *Parser) Cmd() Command {
|
||||
return Command(p.cmd)
|
||||
// Command returns the packed command of the last dispatched sequence. Use
|
||||
// [Cmd] to unpack the command.
|
||||
func (p *Parser) Command() int {
|
||||
return p.cmd
|
||||
}
|
||||
|
||||
// Rune returns the last dispatched sequence as a rune.
|
||||
@ -122,6 +112,11 @@ func (p *Parser) Rune() rune {
|
||||
return r
|
||||
}
|
||||
|
||||
// Control returns the last dispatched sequence as a control code.
|
||||
func (p *Parser) Control() byte {
|
||||
return byte(p.cmd & 0xff)
|
||||
}
|
||||
|
||||
// Data returns the raw data of the last dispatched sequence.
|
||||
func (p *Parser) Data() []byte {
|
||||
return p.data[:p.dataLen]
|
||||
@ -183,12 +178,6 @@ func (p *Parser) collectRune(b byte) {
|
||||
p.paramsLen++
|
||||
}
|
||||
|
||||
func (p *Parser) dispatch(s Sequence) {
|
||||
if p.dispatcher != nil {
|
||||
p.dispatcher(s)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Parser) advanceUtf8(b byte) parser.Action {
|
||||
// Collect UTF-8 rune bytes.
|
||||
p.collectRune(b)
|
||||
@ -204,7 +193,9 @@ func (p *Parser) advanceUtf8(b byte) parser.Action {
|
||||
}
|
||||
|
||||
// We have enough bytes to decode the rune using unsafe
|
||||
p.dispatch(Rune(p.Rune()))
|
||||
if p.handler.Print != nil {
|
||||
p.handler.Print(p.Rune())
|
||||
}
|
||||
|
||||
p.state = parser.GroundState
|
||||
p.paramsLen = 0
|
||||
@ -276,16 +267,22 @@ func (p *Parser) performAction(action parser.Action, state parser.State, b byte)
|
||||
p.clear()
|
||||
|
||||
case parser.PrintAction:
|
||||
p.dispatch(Rune(b))
|
||||
p.cmd = int(b)
|
||||
if p.handler.Print != nil {
|
||||
p.handler.Print(rune(b))
|
||||
}
|
||||
|
||||
case parser.ExecuteAction:
|
||||
p.dispatch(ControlCode(b))
|
||||
p.cmd = int(b)
|
||||
if p.handler.Execute != nil {
|
||||
p.handler.Execute(b)
|
||||
}
|
||||
|
||||
case parser.MarkerAction:
|
||||
// Collect private marker
|
||||
// we only store the last marker
|
||||
p.cmd &^= 0xff << parser.MarkerShift
|
||||
p.cmd |= int(b) << parser.MarkerShift
|
||||
case parser.PrefixAction:
|
||||
// Collect private prefix
|
||||
// we only store the last prefix
|
||||
p.cmd &^= 0xff << parser.PrefixShift
|
||||
p.cmd |= int(b) << parser.PrefixShift
|
||||
|
||||
case parser.CollectAction:
|
||||
if state == parser.Utf8State {
|
||||
@ -367,11 +364,6 @@ func (p *Parser) performAction(action parser.Action, state parser.State, b byte)
|
||||
p.parseStringCmd()
|
||||
}
|
||||
|
||||
if p.dispatcher == nil {
|
||||
break
|
||||
}
|
||||
|
||||
var seq Sequence
|
||||
data := p.data
|
||||
if p.dataLen >= 0 {
|
||||
data = data[:p.dataLen]
|
||||
@ -379,23 +371,35 @@ func (p *Parser) performAction(action parser.Action, state parser.State, b byte)
|
||||
switch p.state {
|
||||
case parser.CsiEntryState, parser.CsiParamState, parser.CsiIntermediateState:
|
||||
p.cmd |= int(b)
|
||||
seq = CsiSequence{Cmd: Command(p.cmd), Params: p.Params()}
|
||||
if p.handler.HandleCsi != nil {
|
||||
p.handler.HandleCsi(Cmd(p.cmd), p.Params())
|
||||
}
|
||||
case parser.EscapeState, parser.EscapeIntermediateState:
|
||||
p.cmd |= int(b)
|
||||
seq = EscSequence(p.cmd)
|
||||
if p.handler.HandleEsc != nil {
|
||||
p.handler.HandleEsc(Cmd(p.cmd))
|
||||
}
|
||||
case parser.DcsEntryState, parser.DcsParamState, parser.DcsIntermediateState, parser.DcsStringState:
|
||||
seq = DcsSequence{Cmd: Command(p.cmd), Params: p.Params(), Data: data}
|
||||
if p.handler.HandleDcs != nil {
|
||||
p.handler.HandleDcs(Cmd(p.cmd), p.Params(), data)
|
||||
}
|
||||
case parser.OscStringState:
|
||||
seq = OscSequence{Cmd: p.cmd, Data: data}
|
||||
if p.handler.HandleOsc != nil {
|
||||
p.handler.HandleOsc(p.cmd, data)
|
||||
}
|
||||
case parser.SosStringState:
|
||||
seq = SosSequence{Data: data}
|
||||
if p.handler.HandleSos != nil {
|
||||
p.handler.HandleSos(data)
|
||||
}
|
||||
case parser.PmStringState:
|
||||
seq = PmSequence{Data: data}
|
||||
if p.handler.HandlePm != nil {
|
||||
p.handler.HandlePm(data)
|
||||
}
|
||||
case parser.ApcStringState:
|
||||
seq = ApcSequence{Data: data}
|
||||
if p.handler.HandleApc != nil {
|
||||
p.handler.HandleApc(data)
|
||||
}
|
||||
}
|
||||
|
||||
p.dispatch(seq)
|
||||
}
|
||||
}
|
||||
|
||||
|
4
vendor/github.com/charmbracelet/x/ansi/parser/const.go
generated
vendored
4
vendor/github.com/charmbracelet/x/ansi/parser/const.go
generated
vendored
@ -8,7 +8,7 @@ const (
|
||||
NoneAction Action = iota
|
||||
ClearAction
|
||||
CollectAction
|
||||
MarkerAction
|
||||
PrefixAction
|
||||
DispatchAction
|
||||
ExecuteAction
|
||||
StartAction // Start of a data string
|
||||
@ -24,7 +24,7 @@ var ActionNames = []string{
|
||||
"NoneAction",
|
||||
"ClearAction",
|
||||
"CollectAction",
|
||||
"MarkerAction",
|
||||
"PrefixAction",
|
||||
"DispatchAction",
|
||||
"ExecuteAction",
|
||||
"StartAction",
|
||||
|
16
vendor/github.com/charmbracelet/x/ansi/parser/seq.go
generated
vendored
16
vendor/github.com/charmbracelet/x/ansi/parser/seq.go
generated
vendored
@ -4,9 +4,9 @@ import "math"
|
||||
|
||||
// Shift and masks for sequence parameters and intermediates.
|
||||
const (
|
||||
MarkerShift = 8
|
||||
PrefixShift = 8
|
||||
IntermedShift = 16
|
||||
CommandMask = 0xff
|
||||
FinalMask = 0xff
|
||||
HasMoreFlag = math.MinInt32
|
||||
ParamMask = ^HasMoreFlag
|
||||
MissingParam = ParamMask
|
||||
@ -22,12 +22,12 @@ const (
|
||||
DefaultParamValue = 0
|
||||
)
|
||||
|
||||
// Marker returns the marker byte of the sequence.
|
||||
// Prefix returns the prefix byte of the sequence.
|
||||
// This is always gonna be one of the following '<' '=' '>' '?' and in the
|
||||
// range of 0x3C-0x3F.
|
||||
// Zero is returned if the sequence does not have a marker.
|
||||
func Marker(cmd int) int {
|
||||
return (cmd >> MarkerShift) & CommandMask
|
||||
// Zero is returned if the sequence does not have a prefix.
|
||||
func Prefix(cmd int) int {
|
||||
return (cmd >> PrefixShift) & FinalMask
|
||||
}
|
||||
|
||||
// Intermediate returns the intermediate byte of the sequence.
|
||||
@ -36,12 +36,12 @@ func Marker(cmd int) int {
|
||||
// ',', '-', '.', '/'.
|
||||
// Zero is returned if the sequence does not have an intermediate byte.
|
||||
func Intermediate(cmd int) int {
|
||||
return (cmd >> IntermedShift) & CommandMask
|
||||
return (cmd >> IntermedShift) & FinalMask
|
||||
}
|
||||
|
||||
// Command returns the command byte of the CSI sequence.
|
||||
func Command(cmd int) int {
|
||||
return cmd & CommandMask
|
||||
return cmd & FinalMask
|
||||
}
|
||||
|
||||
// Param returns the parameter at the given index.
|
||||
|
4
vendor/github.com/charmbracelet/x/ansi/parser/transition_table.go
generated
vendored
4
vendor/github.com/charmbracelet/x/ansi/parser/transition_table.go
generated
vendored
@ -178,7 +178,7 @@ func GenerateTransitionTable() TransitionTable {
|
||||
table.AddRange(0x20, 0x2F, DcsEntryState, CollectAction, DcsIntermediateState)
|
||||
// Dcs_entry -> Dcs_param
|
||||
table.AddRange(0x30, 0x3B, DcsEntryState, ParamAction, DcsParamState)
|
||||
table.AddRange(0x3C, 0x3F, DcsEntryState, MarkerAction, DcsParamState)
|
||||
table.AddRange(0x3C, 0x3F, DcsEntryState, PrefixAction, DcsParamState)
|
||||
// Dcs_entry -> Dcs_passthrough
|
||||
table.AddRange(0x08, 0x0D, DcsEntryState, PutAction, DcsStringState) // Follows ECMA-48 § 8.3.27
|
||||
// XXX: allows passing ESC (not a ECMA-48 standard) this to allow for
|
||||
@ -254,7 +254,7 @@ func GenerateTransitionTable() TransitionTable {
|
||||
table.AddRange(0x20, 0x2F, CsiEntryState, CollectAction, CsiIntermediateState)
|
||||
// Csi_entry -> Csi_param
|
||||
table.AddRange(0x30, 0x3B, CsiEntryState, ParamAction, CsiParamState)
|
||||
table.AddRange(0x3C, 0x3F, CsiEntryState, MarkerAction, CsiParamState)
|
||||
table.AddRange(0x3C, 0x3F, CsiEntryState, PrefixAction, CsiParamState)
|
||||
|
||||
// Osc_string
|
||||
table.AddRange(0x00, 0x06, OscStringState, IgnoreAction, OscStringState)
|
||||
|
159
vendor/github.com/charmbracelet/x/ansi/parser_decode.go
generated
vendored
159
vendor/github.com/charmbracelet/x/ansi/parser_decode.go
generated
vendored
@ -4,6 +4,7 @@ import (
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/charmbracelet/x/ansi/parser"
|
||||
"github.com/mattn/go-runewidth"
|
||||
"github.com/rivo/uniseg"
|
||||
)
|
||||
|
||||
@ -14,7 +15,7 @@ type State = byte
|
||||
// ANSI escape sequence states used by [DecodeSequence].
|
||||
const (
|
||||
NormalState State = iota
|
||||
MarkerState
|
||||
PrefixState
|
||||
ParamsState
|
||||
IntermedState
|
||||
EscapeState
|
||||
@ -33,25 +34,25 @@ const (
|
||||
//
|
||||
// Passing a non-nil [*Parser] as the last argument will allow the decoder to
|
||||
// collect sequence parameters, data, and commands. The parser cmd will have
|
||||
// the packed command value that contains intermediate and marker characters.
|
||||
// the packed command value that contains intermediate and prefix characters.
|
||||
// In the case of a OSC sequence, the cmd will be the OSC command number. Use
|
||||
// [Command] and [Parameter] types to unpack command intermediates and markers as well
|
||||
// [Cmd] and [Param] types to unpack command intermediates and prefixes as well
|
||||
// as parameters.
|
||||
//
|
||||
// Zero [Command] means the CSI, DCS, or ESC sequence is invalid. Moreover, checking the
|
||||
// Zero [Cmd] means the CSI, DCS, or ESC sequence is invalid. Moreover, checking the
|
||||
// validity of other data sequences, OSC, DCS, etc, will require checking for
|
||||
// the returned sequence terminator bytes such as ST (ESC \\) and BEL).
|
||||
//
|
||||
// We store the command byte in [Command] in the most significant byte, the
|
||||
// marker byte in the next byte, and the intermediate byte in the least
|
||||
// We store the command byte in [Cmd] in the most significant byte, the
|
||||
// prefix byte in the next byte, and the intermediate byte in the least
|
||||
// significant byte. This is done to avoid using a struct to store the command
|
||||
// and its intermediates and markers. The command byte is always the least
|
||||
// significant byte i.e. [Cmd & 0xff]. Use the [Command] type to unpack the
|
||||
// command, intermediate, and marker bytes. Note that we only collect the last
|
||||
// marker character and intermediate byte.
|
||||
// and its intermediates and prefixes. The command byte is always the least
|
||||
// significant byte i.e. [Cmd & 0xff]. Use the [Cmd] type to unpack the
|
||||
// command, intermediate, and prefix bytes. Note that we only collect the last
|
||||
// prefix character and intermediate byte.
|
||||
//
|
||||
// The [p.Params] slice will contain the parameters of the sequence. Any
|
||||
// sub-parameter will have the [parser.HasMoreFlag] set. Use the [Parameter] type
|
||||
// sub-parameter will have the [parser.HasMoreFlag] set. Use the [Param] type
|
||||
// to unpack the parameters.
|
||||
//
|
||||
// Example:
|
||||
@ -65,7 +66,63 @@ const (
|
||||
// state = newState
|
||||
// input = input[n:]
|
||||
// }
|
||||
//
|
||||
// This function treats the text as a sequence of grapheme clusters.
|
||||
func DecodeSequence[T string | []byte](b T, state byte, p *Parser) (seq T, width int, n int, newState byte) {
|
||||
return decodeSequence(GraphemeWidth, b, state, p)
|
||||
}
|
||||
|
||||
// DecodeSequenceWc decodes the first ANSI escape sequence or a printable
|
||||
// grapheme from the given data. It returns the sequence slice, the number of
|
||||
// bytes read, the cell width for each sequence, and the new state.
|
||||
//
|
||||
// The cell width will always be 0 for control and escape sequences, 1 for
|
||||
// ASCII printable characters, and the number of cells other Unicode characters
|
||||
// occupy. It uses the uniseg package to calculate the width of Unicode
|
||||
// graphemes and characters. This means it will always do grapheme clustering
|
||||
// (mode 2027).
|
||||
//
|
||||
// Passing a non-nil [*Parser] as the last argument will allow the decoder to
|
||||
// collect sequence parameters, data, and commands. The parser cmd will have
|
||||
// the packed command value that contains intermediate and prefix characters.
|
||||
// In the case of a OSC sequence, the cmd will be the OSC command number. Use
|
||||
// [Cmd] and [Param] types to unpack command intermediates and prefixes as well
|
||||
// as parameters.
|
||||
//
|
||||
// Zero [Cmd] means the CSI, DCS, or ESC sequence is invalid. Moreover, checking the
|
||||
// validity of other data sequences, OSC, DCS, etc, will require checking for
|
||||
// the returned sequence terminator bytes such as ST (ESC \\) and BEL).
|
||||
//
|
||||
// We store the command byte in [Cmd] in the most significant byte, the
|
||||
// prefix byte in the next byte, and the intermediate byte in the least
|
||||
// significant byte. This is done to avoid using a struct to store the command
|
||||
// and its intermediates and prefixes. The command byte is always the least
|
||||
// significant byte i.e. [Cmd & 0xff]. Use the [Cmd] type to unpack the
|
||||
// command, intermediate, and prefix bytes. Note that we only collect the last
|
||||
// prefix character and intermediate byte.
|
||||
//
|
||||
// The [p.Params] slice will contain the parameters of the sequence. Any
|
||||
// sub-parameter will have the [parser.HasMoreFlag] set. Use the [Param] type
|
||||
// to unpack the parameters.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// var state byte // the initial state is always zero [NormalState]
|
||||
// p := NewParser(32, 1024) // create a new parser with a 32 params buffer and 1024 data buffer (optional)
|
||||
// input := []byte("\x1b[31mHello, World!\x1b[0m")
|
||||
// for len(input) > 0 {
|
||||
// seq, width, n, newState := DecodeSequenceWc(input, state, p)
|
||||
// log.Printf("seq: %q, width: %d", seq, width)
|
||||
// state = newState
|
||||
// input = input[n:]
|
||||
// }
|
||||
//
|
||||
// This function treats the text as a sequence of wide characters and runes.
|
||||
func DecodeSequenceWc[T string | []byte](b T, state byte, p *Parser) (seq T, width int, n int, newState byte) {
|
||||
return decodeSequence(WcWidth, b, state, p)
|
||||
}
|
||||
|
||||
func decodeSequence[T string | []byte](m Method, b T, state State, p *Parser) (seq T, width int, n int, newState byte) {
|
||||
for i := 0; i < len(b); i++ {
|
||||
c := b[i]
|
||||
|
||||
@ -92,7 +149,7 @@ func DecodeSequence[T string | []byte](b T, state byte, p *Parser) (seq T, width
|
||||
p.paramsLen = 0
|
||||
p.dataLen = 0
|
||||
}
|
||||
state = MarkerState
|
||||
state = PrefixState
|
||||
continue
|
||||
case OSC, APC, SOS, PM:
|
||||
if p != nil {
|
||||
@ -120,18 +177,21 @@ func DecodeSequence[T string | []byte](b T, state byte, p *Parser) (seq T, width
|
||||
|
||||
if utf8.RuneStart(c) {
|
||||
seq, _, width, _ = FirstGraphemeCluster(b, -1)
|
||||
if m == WcWidth {
|
||||
width = runewidth.StringWidth(string(seq))
|
||||
}
|
||||
i += len(seq)
|
||||
return b[:i], width, i, NormalState
|
||||
}
|
||||
|
||||
// Invalid UTF-8 sequence
|
||||
return b[:i], 0, i, NormalState
|
||||
case MarkerState:
|
||||
case PrefixState:
|
||||
if c >= '<' && c <= '?' {
|
||||
if p != nil {
|
||||
// We only collect the last marker character.
|
||||
p.cmd &^= 0xff << parser.MarkerShift
|
||||
p.cmd |= int(c) << parser.MarkerShift
|
||||
// We only collect the last prefix character.
|
||||
p.cmd &^= 0xff << parser.PrefixShift
|
||||
p.cmd |= int(c) << parser.PrefixShift
|
||||
}
|
||||
break
|
||||
}
|
||||
@ -216,7 +276,7 @@ func DecodeSequence[T string | []byte](b T, state byte, p *Parser) (seq T, width
|
||||
p.paramsLen = 0
|
||||
p.cmd = 0
|
||||
}
|
||||
state = MarkerState
|
||||
state = PrefixState
|
||||
continue
|
||||
case ']', 'X', '^', '_':
|
||||
if p != nil {
|
||||
@ -389,17 +449,17 @@ func FirstGraphemeCluster[T string | []byte](b T, state int) (T, T, int, int) {
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
// Command represents a sequence command. This is used to pack/unpack a sequence
|
||||
// command with its intermediate and marker characters. Those are commonly
|
||||
// Cmd represents a sequence command. This is used to pack/unpack a sequence
|
||||
// command with its intermediate and prefix characters. Those are commonly
|
||||
// found in CSI and DCS sequences.
|
||||
type Command int
|
||||
type Cmd int
|
||||
|
||||
// Marker returns the unpacked marker byte of the CSI sequence.
|
||||
// Prefix returns the unpacked prefix byte of the CSI sequence.
|
||||
// This is always gonna be one of the following '<' '=' '>' '?' and in the
|
||||
// range of 0x3C-0x3F.
|
||||
// Zero is returned if the sequence does not have a marker.
|
||||
func (c Command) Marker() int {
|
||||
return parser.Marker(int(c))
|
||||
// Zero is returned if the sequence does not have a prefix.
|
||||
func (c Cmd) Prefix() byte {
|
||||
return byte(parser.Prefix(int(c)))
|
||||
}
|
||||
|
||||
// Intermediate returns the unpacked intermediate byte of the CSI sequence.
|
||||
@ -407,37 +467,40 @@ func (c Command) Marker() int {
|
||||
// characters from ' ', '!', '"', '#', '$', '%', '&', ”', '(', ')', '*', '+',
|
||||
// ',', '-', '.', '/'.
|
||||
// Zero is returned if the sequence does not have an intermediate byte.
|
||||
func (c Command) Intermediate() int {
|
||||
return parser.Intermediate(int(c))
|
||||
func (c Cmd) Intermediate() byte {
|
||||
return byte(parser.Intermediate(int(c)))
|
||||
}
|
||||
|
||||
// Command returns the unpacked command byte of the CSI sequence.
|
||||
func (c Command) Command() int {
|
||||
return parser.Command(int(c))
|
||||
// Final returns the unpacked command byte of the CSI sequence.
|
||||
func (c Cmd) Final() byte {
|
||||
return byte(parser.Command(int(c)))
|
||||
}
|
||||
|
||||
// Cmd returns a packed [Command] with the given command, marker, and
|
||||
// intermediate.
|
||||
// The first byte is the command, the next shift is the marker, and the next
|
||||
// shift is the intermediate.
|
||||
// Command packs a command with the given prefix, intermediate, and final. A
|
||||
// zero byte means the sequence does not have a prefix or intermediate.
|
||||
//
|
||||
// Even though this function takes integers, it only uses the lower 8 bits of
|
||||
// each integer.
|
||||
func Cmd(marker, inter, cmd int) (c Command) {
|
||||
c = Command(cmd & parser.CommandMask)
|
||||
c |= Command(marker&parser.CommandMask) << parser.MarkerShift
|
||||
c |= Command(inter&parser.CommandMask) << parser.IntermedShift
|
||||
// Prefixes are in the range of 0x3C-0x3F that is one of `<=>?`.
|
||||
//
|
||||
// Intermediates are in the range of 0x20-0x2F that is anything in
|
||||
// `!"#$%&'()*+,-./`.
|
||||
//
|
||||
// Final bytes are in the range of 0x40-0x7E that is anything in the range
|
||||
// `@A–Z[\]^_`a–z{|}~`.
|
||||
func Command(prefix, inter, final byte) (c int) {
|
||||
c = int(final)
|
||||
c |= int(prefix) << parser.PrefixShift
|
||||
c |= int(inter) << parser.IntermedShift
|
||||
return
|
||||
}
|
||||
|
||||
// Parameter represents a sequence parameter. Sequence parameters with
|
||||
// Param represents a sequence parameter. Sequence parameters with
|
||||
// sub-parameters are packed with the HasMoreFlag set. This is used to unpack
|
||||
// the parameters from a CSI and DCS sequences.
|
||||
type Parameter int
|
||||
type Param int
|
||||
|
||||
// Param returns the unpacked parameter at the given index.
|
||||
// It returns the default value if the parameter is missing.
|
||||
func (s Parameter) Param(def int) int {
|
||||
func (s Param) Param(def int) int {
|
||||
p := int(s) & parser.ParamMask
|
||||
if p == parser.MissingParam {
|
||||
return def
|
||||
@ -446,16 +509,16 @@ func (s Parameter) Param(def int) int {
|
||||
}
|
||||
|
||||
// HasMore unpacks the HasMoreFlag from the parameter.
|
||||
func (s Parameter) HasMore() bool {
|
||||
func (s Param) HasMore() bool {
|
||||
return s&parser.HasMoreFlag != 0
|
||||
}
|
||||
|
||||
// Param returns a packed [Parameter] with the given parameter and whether this
|
||||
// parameter has following sub-parameters.
|
||||
func Param(p int, hasMore bool) (s Parameter) {
|
||||
s = Parameter(p & parser.ParamMask)
|
||||
// Parameter packs an escape code parameter with the given parameter and
|
||||
// whether this parameter has following sub-parameters.
|
||||
func Parameter(p int, hasMore bool) (s int) {
|
||||
s = p & parser.ParamMask
|
||||
if hasMore {
|
||||
s |= Parameter(parser.HasMoreFlag)
|
||||
s |= parser.HasMoreFlag
|
||||
}
|
||||
return
|
||||
}
|
||||
|
60
vendor/github.com/charmbracelet/x/ansi/parser_handler.go
generated
vendored
Normal file
60
vendor/github.com/charmbracelet/x/ansi/parser_handler.go
generated
vendored
Normal file
@ -0,0 +1,60 @@
|
||||
package ansi
|
||||
|
||||
import "unsafe"
|
||||
|
||||
// Params represents a list of packed parameters.
|
||||
type Params []Param
|
||||
|
||||
// Param returns the parameter at the given index and if it is part of a
|
||||
// sub-parameters. It falls back to the default value if the parameter is
|
||||
// missing. If the index is out of bounds, it returns the default value and
|
||||
// false.
|
||||
func (p Params) Param(i, def int) (int, bool, bool) {
|
||||
if i < 0 || i >= len(p) {
|
||||
return def, false, false
|
||||
}
|
||||
return p[i].Param(def), p[i].HasMore(), true
|
||||
}
|
||||
|
||||
// ForEach iterates over the parameters and calls the given function for each
|
||||
// parameter. If a parameter is part of a sub-parameter, it will be called with
|
||||
// hasMore set to true.
|
||||
// Use def to set a default value for missing parameters.
|
||||
func (p Params) ForEach(def int, f func(i, param int, hasMore bool)) {
|
||||
for i := range p {
|
||||
f(i, p[i].Param(def), p[i].HasMore())
|
||||
}
|
||||
}
|
||||
|
||||
// ToParams converts a list of integers to a list of parameters.
|
||||
func ToParams(params []int) Params {
|
||||
return unsafe.Slice((*Param)(unsafe.Pointer(¶ms[0])), len(params))
|
||||
}
|
||||
|
||||
// Handler handles actions performed by the parser.
|
||||
// It is used to handle ANSI escape sequences, control characters, and runes.
|
||||
type Handler struct {
|
||||
// Print is called when a printable rune is encountered.
|
||||
Print func(r rune)
|
||||
// Execute is called when a control character is encountered.
|
||||
Execute func(b byte)
|
||||
// HandleCsi is called when a CSI sequence is encountered.
|
||||
HandleCsi func(cmd Cmd, params Params)
|
||||
// HandleEsc is called when an ESC sequence is encountered.
|
||||
HandleEsc func(cmd Cmd)
|
||||
// HandleDcs is called when a DCS sequence is encountered.
|
||||
HandleDcs func(cmd Cmd, params Params, data []byte)
|
||||
// HandleOsc is called when an OSC sequence is encountered.
|
||||
HandleOsc func(cmd int, data []byte)
|
||||
// HandlePm is called when a PM sequence is encountered.
|
||||
HandlePm func(data []byte)
|
||||
// HandleApc is called when an APC sequence is encountered.
|
||||
HandleApc func(data []byte)
|
||||
// HandleSos is called when a SOS sequence is encountered.
|
||||
HandleSos func(data []byte)
|
||||
}
|
||||
|
||||
// SetHandler sets the handler for the parser.
|
||||
func (p *Parser) SetHandler(h Handler) {
|
||||
p.handler = h
|
||||
}
|
2
vendor/github.com/charmbracelet/x/ansi/parser_sync.go
generated
vendored
2
vendor/github.com/charmbracelet/x/ansi/parser_sync.go
generated
vendored
@ -8,7 +8,7 @@ import (
|
||||
|
||||
var parserPool = sync.Pool{
|
||||
New: func() any {
|
||||
p := NewParser(nil)
|
||||
p := NewParser()
|
||||
p.SetParamsSize(parser.MaxParamsSize)
|
||||
p.SetDataSize(1024 * 1024 * 4) // 4MB of data buffer
|
||||
return p
|
||||
|
1
vendor/github.com/charmbracelet/x/ansi/screen.go
generated
vendored
1
vendor/github.com/charmbracelet/x/ansi/screen.go
generated
vendored
@ -213,6 +213,7 @@ func DECSLRM(left, right int) string {
|
||||
// CSI <top> ; <bottom> r
|
||||
//
|
||||
// See: https://vt100.net/docs/vt510-rm/DECSTBM.html
|
||||
//
|
||||
// Deprecated: use [SetTopBottomMargins] instead.
|
||||
func SetScrollingRegion(t, b int) string {
|
||||
if t < 0 {
|
||||
|
217
vendor/github.com/charmbracelet/x/ansi/sequence.go
generated
vendored
217
vendor/github.com/charmbracelet/x/ansi/sequence.go
generated
vendored
@ -1,217 +0,0 @@
|
||||
package ansi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/charmbracelet/x/ansi/parser"
|
||||
)
|
||||
|
||||
// Sequence represents an ANSI sequence. This can be a control sequence, escape
|
||||
// sequence, a printable character, etc.
|
||||
// A Sequence can be one of the following types:
|
||||
// - [Rune]
|
||||
// - [ControlCode]
|
||||
// - [Grapheme]
|
||||
// - [EscSequence]
|
||||
// - [CsiSequence]
|
||||
// - [OscSequence]
|
||||
// - [DcsSequence]
|
||||
// - [SosSequence]
|
||||
// - [PmSequence]
|
||||
// - [ApcSequence]
|
||||
type Sequence interface {
|
||||
// Clone returns a deep copy of the sequence.
|
||||
Clone() Sequence
|
||||
}
|
||||
|
||||
// Rune represents a printable character.
|
||||
type Rune rune
|
||||
|
||||
var _ Sequence = Rune(0)
|
||||
|
||||
// Clone returns a deep copy of the rune.
|
||||
func (r Rune) Clone() Sequence {
|
||||
return r
|
||||
}
|
||||
|
||||
// Grapheme represents a grapheme cluster.
|
||||
type Grapheme struct {
|
||||
Cluster string
|
||||
Width int
|
||||
}
|
||||
|
||||
var _ Sequence = Grapheme{}
|
||||
|
||||
// Clone returns a deep copy of the grapheme.
|
||||
func (g Grapheme) Clone() Sequence {
|
||||
return g
|
||||
}
|
||||
|
||||
// ControlCode represents a control code character. This is a character that
|
||||
// is not printable and is used to control the terminal. This would be a
|
||||
// character in the C0 or C1 set in the range of 0x00-0x1F and 0x80-0x9F.
|
||||
type ControlCode byte
|
||||
|
||||
var _ Sequence = ControlCode(0)
|
||||
|
||||
// Bytes implements Sequence.
|
||||
func (c ControlCode) Bytes() []byte {
|
||||
return []byte{byte(c)}
|
||||
}
|
||||
|
||||
// String implements Sequence.
|
||||
func (c ControlCode) String() string {
|
||||
return string(c)
|
||||
}
|
||||
|
||||
// Clone returns a deep copy of the control code.
|
||||
func (c ControlCode) Clone() Sequence {
|
||||
return c
|
||||
}
|
||||
|
||||
// EscSequence represents an escape sequence.
|
||||
type EscSequence Command
|
||||
|
||||
var _ Sequence = EscSequence(0)
|
||||
|
||||
// buffer returns the buffer of the escape sequence.
|
||||
func (e EscSequence) buffer() *bytes.Buffer {
|
||||
var b bytes.Buffer
|
||||
b.WriteByte('\x1b')
|
||||
if i := parser.Intermediate(int(e)); i != 0 {
|
||||
b.WriteByte(byte(i))
|
||||
}
|
||||
if cmd := e.Command(); cmd != 0 {
|
||||
b.WriteByte(byte(cmd))
|
||||
}
|
||||
return &b
|
||||
}
|
||||
|
||||
// Bytes implements Sequence.
|
||||
func (e EscSequence) Bytes() []byte {
|
||||
return e.buffer().Bytes()
|
||||
}
|
||||
|
||||
// String implements Sequence.
|
||||
func (e EscSequence) String() string {
|
||||
return e.buffer().String()
|
||||
}
|
||||
|
||||
// Clone returns a deep copy of the escape sequence.
|
||||
func (e EscSequence) Clone() Sequence {
|
||||
return e
|
||||
}
|
||||
|
||||
// Command returns the command byte of the escape sequence.
|
||||
func (e EscSequence) Command() int {
|
||||
return Command(e).Command()
|
||||
}
|
||||
|
||||
// Intermediate returns the intermediate byte of the escape sequence.
|
||||
func (e EscSequence) Intermediate() int {
|
||||
return Command(e).Intermediate()
|
||||
}
|
||||
|
||||
// SosSequence represents a SOS sequence.
|
||||
type SosSequence struct {
|
||||
// Data contains the raw data of the sequence.
|
||||
Data []byte
|
||||
}
|
||||
|
||||
var _ Sequence = SosSequence{}
|
||||
|
||||
// Bytes implements Sequence.
|
||||
func (s SosSequence) Bytes() []byte {
|
||||
return s.buffer().Bytes()
|
||||
}
|
||||
|
||||
// String implements Sequence.
|
||||
func (s SosSequence) String() string {
|
||||
return s.buffer().String()
|
||||
}
|
||||
|
||||
func (s SosSequence) buffer() *bytes.Buffer {
|
||||
var b bytes.Buffer
|
||||
b.WriteByte('\x1b')
|
||||
b.WriteByte('X')
|
||||
b.Write(s.Data)
|
||||
b.WriteString("\x1b\\")
|
||||
return &b
|
||||
}
|
||||
|
||||
// Clone returns a deep copy of the SOS sequence.
|
||||
func (s SosSequence) Clone() Sequence {
|
||||
return SosSequence{
|
||||
Data: append([]byte(nil), s.Data...),
|
||||
}
|
||||
}
|
||||
|
||||
// PmSequence represents a PM sequence.
|
||||
type PmSequence struct {
|
||||
// Data contains the raw data of the sequence.
|
||||
Data []byte
|
||||
}
|
||||
|
||||
var _ Sequence = PmSequence{}
|
||||
|
||||
// Bytes implements Sequence.
|
||||
func (s PmSequence) Bytes() []byte {
|
||||
return s.buffer().Bytes()
|
||||
}
|
||||
|
||||
// String implements Sequence.
|
||||
func (s PmSequence) String() string {
|
||||
return s.buffer().String()
|
||||
}
|
||||
|
||||
// buffer returns the buffer of the PM sequence.
|
||||
func (s PmSequence) buffer() *bytes.Buffer {
|
||||
var b bytes.Buffer
|
||||
b.WriteByte('\x1b')
|
||||
b.WriteByte('^')
|
||||
b.Write(s.Data)
|
||||
b.WriteString("\x1b\\")
|
||||
return &b
|
||||
}
|
||||
|
||||
// Clone returns a deep copy of the PM sequence.
|
||||
func (p PmSequence) Clone() Sequence {
|
||||
return PmSequence{
|
||||
Data: append([]byte(nil), p.Data...),
|
||||
}
|
||||
}
|
||||
|
||||
// ApcSequence represents an APC sequence.
|
||||
type ApcSequence struct {
|
||||
// Data contains the raw data of the sequence.
|
||||
Data []byte
|
||||
}
|
||||
|
||||
var _ Sequence = ApcSequence{}
|
||||
|
||||
// Clone returns a deep copy of the APC sequence.
|
||||
func (a ApcSequence) Clone() Sequence {
|
||||
return ApcSequence{
|
||||
Data: append([]byte(nil), a.Data...),
|
||||
}
|
||||
}
|
||||
|
||||
// Bytes implements Sequence.
|
||||
func (s ApcSequence) Bytes() []byte {
|
||||
return s.buffer().Bytes()
|
||||
}
|
||||
|
||||
// String implements Sequence.
|
||||
func (s ApcSequence) String() string {
|
||||
return s.buffer().String()
|
||||
}
|
||||
|
||||
// buffer returns the buffer of the APC sequence.
|
||||
func (s ApcSequence) buffer() *bytes.Buffer {
|
||||
var b bytes.Buffer
|
||||
b.WriteByte('\x1b')
|
||||
b.WriteByte('_')
|
||||
b.Write(s.Data)
|
||||
b.WriteString("\x1b\\")
|
||||
return &b
|
||||
}
|
59
vendor/github.com/charmbracelet/x/ansi/status.go
generated
vendored
59
vendor/github.com/charmbracelet/x/ansi/status.go
generated
vendored
@ -5,25 +5,25 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Status represents a terminal status report.
|
||||
type Status interface {
|
||||
// Status returns the status report identifier.
|
||||
Status() int
|
||||
// StatusReport represents a terminal status report.
|
||||
type StatusReport interface {
|
||||
// StatusReport returns the status report identifier.
|
||||
StatusReport() int
|
||||
}
|
||||
|
||||
// ANSIStatus represents an ANSI terminal status report.
|
||||
type ANSIStatus int //nolint:revive
|
||||
// ANSIReport represents an ANSI terminal status report.
|
||||
type ANSIStatusReport int //nolint:revive
|
||||
|
||||
// Status returns the status report identifier.
|
||||
func (s ANSIStatus) Status() int {
|
||||
// Report returns the status report identifier.
|
||||
func (s ANSIStatusReport) StatusReport() int {
|
||||
return int(s)
|
||||
}
|
||||
|
||||
// DECStatus represents a DEC terminal status report.
|
||||
type DECStatus int
|
||||
// DECStatusReport represents a DEC terminal status report.
|
||||
type DECStatusReport int
|
||||
|
||||
// Status returns the status report identifier.
|
||||
func (s DECStatus) Status() int {
|
||||
func (s DECStatusReport) StatusReport() int {
|
||||
return int(s)
|
||||
}
|
||||
|
||||
@ -38,14 +38,14 @@ func (s DECStatus) Status() int {
|
||||
// format.
|
||||
//
|
||||
// See also https://vt100.net/docs/vt510-rm/DSR.html
|
||||
func DeviceStatusReport(statues ...Status) string {
|
||||
func DeviceStatusReport(statues ...StatusReport) string {
|
||||
var dec bool
|
||||
list := make([]string, len(statues))
|
||||
seq := "\x1b["
|
||||
for i, status := range statues {
|
||||
list[i] = strconv.Itoa(status.Status())
|
||||
list[i] = strconv.Itoa(status.StatusReport())
|
||||
switch status.(type) {
|
||||
case DECStatus:
|
||||
case DECStatusReport:
|
||||
dec = true
|
||||
}
|
||||
}
|
||||
@ -56,10 +56,39 @@ func DeviceStatusReport(statues ...Status) string {
|
||||
}
|
||||
|
||||
// DSR is an alias for [DeviceStatusReport].
|
||||
func DSR(status Status) string {
|
||||
func DSR(status StatusReport) string {
|
||||
return DeviceStatusReport(status)
|
||||
}
|
||||
|
||||
// RequestCursorPositionReport is an escape sequence that requests the current
|
||||
// cursor position.
|
||||
//
|
||||
// CSI 6 n
|
||||
//
|
||||
// The terminal will report the cursor position as a CSI sequence in the
|
||||
// following format:
|
||||
//
|
||||
// CSI Pl ; Pc R
|
||||
//
|
||||
// Where Pl is the line number and Pc is the column number.
|
||||
// See: https://vt100.net/docs/vt510-rm/CPR.html
|
||||
const RequestCursorPositionReport = "\x1b[6n"
|
||||
|
||||
// RequestExtendedCursorPositionReport (DECXCPR) is a sequence for requesting
|
||||
// the cursor position report including the current page number.
|
||||
//
|
||||
// CSI ? 6 n
|
||||
//
|
||||
// The terminal will report the cursor position as a CSI sequence in the
|
||||
// following format:
|
||||
//
|
||||
// CSI ? Pl ; Pc ; Pp R
|
||||
//
|
||||
// Where Pl is the line number, Pc is the column number, and Pp is the page
|
||||
// number.
|
||||
// See: https://vt100.net/docs/vt510-rm/DECXCPR.html
|
||||
const RequestExtendedCursorPositionReport = "\x1b[?6n"
|
||||
|
||||
// CursorPositionReport (CPR) is a control sequence that reports the cursor's
|
||||
// position.
|
||||
//
|
||||
|
173
vendor/github.com/charmbracelet/x/ansi/style.go
generated
vendored
173
vendor/github.com/charmbracelet/x/ansi/style.go
generated
vendored
@ -199,7 +199,7 @@ func (s Style) UnderlineColor(c Color) Style {
|
||||
|
||||
// UnderlineStyle represents an ANSI SGR (Select Graphic Rendition) underline
|
||||
// style.
|
||||
type UnderlineStyle = int
|
||||
type UnderlineStyle = byte
|
||||
|
||||
const (
|
||||
doubleUnderlineStyle = "4:2"
|
||||
@ -487,3 +487,174 @@ func underlineColorString(c Color) string {
|
||||
}
|
||||
return defaultUnderlineColorAttr
|
||||
}
|
||||
|
||||
// ReadStyleColor decodes a color from a slice of parameters. It returns the
|
||||
// number of parameters read and the color. This function is used to read SGR
|
||||
// color parameters following the ITU T.416 standard.
|
||||
//
|
||||
// It supports reading the following color types:
|
||||
// - 0: implementation defined
|
||||
// - 1: transparent
|
||||
// - 2: RGB direct color
|
||||
// - 3: CMY direct color
|
||||
// - 4: CMYK direct color
|
||||
// - 5: indexed color
|
||||
// - 6: RGBA direct color (WezTerm extension)
|
||||
//
|
||||
// The parameters can be separated by semicolons (;) or colons (:). Mixing
|
||||
// separators is not allowed.
|
||||
//
|
||||
// The specs supports defining a color space id, a color tolerance value, and a
|
||||
// tolerance color space id. However, these values have no effect on the
|
||||
// returned color and will be ignored.
|
||||
//
|
||||
// This implementation includes a few modifications to the specs:
|
||||
// 1. Support for legacy color values separated by semicolons (;) with respect to RGB, and indexed colors
|
||||
// 2. Support ignoring and omitting the color space id (second parameter) with respect to RGB colors
|
||||
// 3. Support ignoring and omitting the 6th parameter with respect to RGB and CMY colors
|
||||
// 4. Support reading RGBA colors
|
||||
func ReadStyleColor(params Params, co *color.Color) (n int) {
|
||||
if len(params) < 2 { // Need at least SGR type and color type
|
||||
return 0
|
||||
}
|
||||
|
||||
// First parameter indicates one of 38, 48, or 58 (foreground, background, or underline)
|
||||
s := params[0]
|
||||
p := params[1]
|
||||
colorType := p.Param(0)
|
||||
n = 2
|
||||
|
||||
paramsfn := func() (p1, p2, p3, p4 int) {
|
||||
// Where should we start reading the color?
|
||||
switch {
|
||||
case s.HasMore() && p.HasMore() && len(params) > 8 && params[2].HasMore() && params[3].HasMore() && params[4].HasMore() && params[5].HasMore() && params[6].HasMore() && params[7].HasMore():
|
||||
// We have color space id, a 6th parameter, a tolerance value, and a tolerance color space
|
||||
n += 7
|
||||
return params[3].Param(0), params[4].Param(0), params[5].Param(0), params[6].Param(0)
|
||||
case s.HasMore() && p.HasMore() && len(params) > 7 && params[2].HasMore() && params[3].HasMore() && params[4].HasMore() && params[5].HasMore() && params[6].HasMore():
|
||||
// We have color space id, a 6th parameter, and a tolerance value
|
||||
n += 6
|
||||
return params[3].Param(0), params[4].Param(0), params[5].Param(0), params[6].Param(0)
|
||||
case s.HasMore() && p.HasMore() && len(params) > 6 && params[2].HasMore() && params[3].HasMore() && params[4].HasMore() && params[5].HasMore():
|
||||
// We have color space id and a 6th parameter
|
||||
// 48 : 4 : : 1 : 2 : 3 :4
|
||||
n += 5
|
||||
return params[3].Param(0), params[4].Param(0), params[5].Param(0), params[6].Param(0)
|
||||
case s.HasMore() && p.HasMore() && len(params) > 5 && params[2].HasMore() && params[3].HasMore() && params[4].HasMore() && !params[5].HasMore():
|
||||
// We have color space
|
||||
// 48 : 3 : : 1 : 2 : 3
|
||||
n += 4
|
||||
return params[3].Param(0), params[4].Param(0), params[5].Param(0), -1
|
||||
case s.HasMore() && p.HasMore() && p.Param(0) == 2 && params[2].HasMore() && params[3].HasMore() && !params[4].HasMore():
|
||||
// We have color values separated by colons (:)
|
||||
// 48 : 2 : 1 : 2 : 3
|
||||
fallthrough
|
||||
case !s.HasMore() && !p.HasMore() && p.Param(0) == 2 && !params[2].HasMore() && !params[3].HasMore() && !params[4].HasMore():
|
||||
// Support legacy color values separated by semicolons (;)
|
||||
// 48 ; 2 ; 1 ; 2 ; 3
|
||||
n += 3
|
||||
return params[2].Param(0), params[3].Param(0), params[4].Param(0), -1
|
||||
}
|
||||
// Ambiguous SGR color
|
||||
return -1, -1, -1, -1
|
||||
}
|
||||
|
||||
switch colorType {
|
||||
case 0: // implementation defined
|
||||
return 2
|
||||
case 1: // transparent
|
||||
*co = color.Transparent
|
||||
return 2
|
||||
case 2: // RGB direct color
|
||||
if len(params) < 5 {
|
||||
return 0
|
||||
}
|
||||
|
||||
r, g, b, _ := paramsfn()
|
||||
if r == -1 || g == -1 || b == -1 {
|
||||
return 0
|
||||
}
|
||||
|
||||
*co = color.RGBA{
|
||||
R: uint8(r), //nolint:gosec
|
||||
G: uint8(g), //nolint:gosec
|
||||
B: uint8(b), //nolint:gosec
|
||||
A: 0xff,
|
||||
}
|
||||
return
|
||||
|
||||
case 3: // CMY direct color
|
||||
if len(params) < 5 {
|
||||
return 0
|
||||
}
|
||||
|
||||
c, m, y, _ := paramsfn()
|
||||
if c == -1 || m == -1 || y == -1 {
|
||||
return 0
|
||||
}
|
||||
|
||||
*co = color.CMYK{
|
||||
C: uint8(c), //nolint:gosec
|
||||
M: uint8(m), //nolint:gosec
|
||||
Y: uint8(y), //nolint:gosec
|
||||
K: 0,
|
||||
}
|
||||
return
|
||||
|
||||
case 4: // CMYK direct color
|
||||
if len(params) < 6 {
|
||||
return 0
|
||||
}
|
||||
|
||||
c, m, y, k := paramsfn()
|
||||
if c == -1 || m == -1 || y == -1 || k == -1 {
|
||||
return 0
|
||||
}
|
||||
|
||||
*co = color.CMYK{
|
||||
C: uint8(c), //nolint:gosec
|
||||
M: uint8(m), //nolint:gosec
|
||||
Y: uint8(y), //nolint:gosec
|
||||
K: uint8(k), //nolint:gosec
|
||||
}
|
||||
return
|
||||
|
||||
case 5: // indexed color
|
||||
if len(params) < 3 {
|
||||
return 0
|
||||
}
|
||||
switch {
|
||||
case s.HasMore() && p.HasMore() && !params[2].HasMore():
|
||||
// Colon separated indexed color
|
||||
// 38 : 5 : 234
|
||||
case !s.HasMore() && !p.HasMore() && !params[2].HasMore():
|
||||
// Legacy semicolon indexed color
|
||||
// 38 ; 5 ; 234
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
*co = ExtendedColor(params[2].Param(0)) //nolint:gosec
|
||||
return 3
|
||||
|
||||
case 6: // RGBA direct color
|
||||
if len(params) < 6 {
|
||||
return 0
|
||||
}
|
||||
|
||||
r, g, b, a := paramsfn()
|
||||
if r == -1 || g == -1 || b == -1 || a == -1 {
|
||||
return 0
|
||||
}
|
||||
|
||||
*co = color.RGBA{
|
||||
R: uint8(r), //nolint:gosec
|
||||
G: uint8(g), //nolint:gosec
|
||||
B: uint8(b), //nolint:gosec
|
||||
A: uint8(a), //nolint:gosec
|
||||
}
|
||||
return
|
||||
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
125
vendor/github.com/charmbracelet/x/ansi/truncate.go
generated
vendored
125
vendor/github.com/charmbracelet/x/ansi/truncate.go
generated
vendored
@ -4,14 +4,65 @@ import (
|
||||
"bytes"
|
||||
|
||||
"github.com/charmbracelet/x/ansi/parser"
|
||||
"github.com/mattn/go-runewidth"
|
||||
"github.com/rivo/uniseg"
|
||||
)
|
||||
|
||||
// Truncate truncates a string to a given length, adding a tail to the
|
||||
// end if the string is longer than the given length.
|
||||
// This function is aware of ANSI escape codes and will not break them, and
|
||||
// accounts for wide-characters (such as East Asians and emojis).
|
||||
// Cut the string, without adding any prefix or tail strings. This function is
|
||||
// aware of ANSI escape codes and will not break them, and accounts for
|
||||
// wide-characters (such as East-Asian characters and emojis). Note that the
|
||||
// [left] parameter is inclusive, while [right] isn't.
|
||||
// This treats the text as a sequence of graphemes.
|
||||
func Cut(s string, left, right int) string {
|
||||
return cut(GraphemeWidth, s, left, right)
|
||||
}
|
||||
|
||||
// CutWc the string, without adding any prefix or tail strings. This function is
|
||||
// aware of ANSI escape codes and will not break them, and accounts for
|
||||
// wide-characters (such as East-Asian characters and emojis). Note that the
|
||||
// [left] parameter is inclusive, while [right] isn't.
|
||||
// This treats the text as a sequence of wide characters and runes.
|
||||
func CutWc(s string, left, right int) string {
|
||||
return cut(WcWidth, s, left, right)
|
||||
}
|
||||
|
||||
func cut(m Method, s string, left, right int) string {
|
||||
if right <= left {
|
||||
return ""
|
||||
}
|
||||
|
||||
truncate := Truncate
|
||||
truncateLeft := TruncateLeft
|
||||
if m == WcWidth {
|
||||
truncate = TruncateWc
|
||||
truncateLeft = TruncateWc
|
||||
}
|
||||
|
||||
if left == 0 {
|
||||
return truncate(s, right, "")
|
||||
}
|
||||
return truncateLeft(Truncate(s, right, ""), left, "")
|
||||
}
|
||||
|
||||
// Truncate truncates a string to a given length, adding a tail to the end if
|
||||
// the string is longer than the given length. This function is aware of ANSI
|
||||
// escape codes and will not break them, and accounts for wide-characters (such
|
||||
// as East-Asian characters and emojis).
|
||||
// This treats the text as a sequence of graphemes.
|
||||
func Truncate(s string, length int, tail string) string {
|
||||
return truncate(GraphemeWidth, s, length, tail)
|
||||
}
|
||||
|
||||
// TruncateWc truncates a string to a given length, adding a tail to the end if
|
||||
// the string is longer than the given length. This function is aware of ANSI
|
||||
// escape codes and will not break them, and accounts for wide-characters (such
|
||||
// as East-Asian characters and emojis).
|
||||
// This treats the text as a sequence of wide characters and runes.
|
||||
func TruncateWc(s string, length int, tail string) string {
|
||||
return truncate(WcWidth, s, length, tail)
|
||||
}
|
||||
|
||||
func truncate(m Method, s string, length int, tail string) string {
|
||||
if sw := StringWidth(s); sw <= length {
|
||||
return s
|
||||
}
|
||||
@ -33,6 +84,7 @@ func Truncate(s string, length int, tail string) string {
|
||||
// Here we iterate over the bytes of the string and collect printable
|
||||
// characters and runes. We also keep track of the width of the string
|
||||
// in cells.
|
||||
//
|
||||
// Once we reach the given length, we start ignoring characters and only
|
||||
// collect ANSI escape codes until we reach the end of string.
|
||||
for i < len(b) {
|
||||
@ -41,6 +93,9 @@ func Truncate(s string, length int, tail string) string {
|
||||
// This action happens when we transition to the Utf8State.
|
||||
var width int
|
||||
cluster, _, width, _ = uniseg.FirstGraphemeCluster(b[i:], -1)
|
||||
if m == WcWidth {
|
||||
width = runewidth.StringWidth(string(cluster))
|
||||
}
|
||||
|
||||
// increment the index by the length of the cluster
|
||||
i += len(cluster)
|
||||
@ -106,13 +161,27 @@ func Truncate(s string, length int, tail string) string {
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// TruncateLeft truncates a string from the left side to a given length, adding
|
||||
// a prefix to the beginning if the string is longer than the given length.
|
||||
// TruncateLeft truncates a string from the left side by removing n characters,
|
||||
// adding a prefix to the beginning if the string is longer than n.
|
||||
// This function is aware of ANSI escape codes and will not break them, and
|
||||
// accounts for wide-characters (such as East Asians and emojis).
|
||||
func TruncateLeft(s string, length int, prefix string) string {
|
||||
if length == 0 {
|
||||
return ""
|
||||
// accounts for wide-characters (such as East-Asian characters and emojis).
|
||||
// This treats the text as a sequence of graphemes.
|
||||
func TruncateLeft(s string, n int, prefix string) string {
|
||||
return truncateLeft(GraphemeWidth, s, n, prefix)
|
||||
}
|
||||
|
||||
// TruncateLeftWc truncates a string from the left side by removing n characters,
|
||||
// adding a prefix to the beginning if the string is longer than n.
|
||||
// This function is aware of ANSI escape codes and will not break them, and
|
||||
// accounts for wide-characters (such as East-Asian characters and emojis).
|
||||
// This treats the text as a sequence of wide characters and runes.
|
||||
func TruncateLeftWc(s string, n int, prefix string) string {
|
||||
return truncateLeft(WcWidth, s, n, prefix)
|
||||
}
|
||||
|
||||
func truncateLeft(m Method, s string, n int, prefix string) string {
|
||||
if n <= 0 {
|
||||
return s
|
||||
}
|
||||
|
||||
var cluster []byte
|
||||
@ -133,11 +202,14 @@ func TruncateLeft(s string, length int, prefix string) string {
|
||||
if state == parser.Utf8State {
|
||||
var width int
|
||||
cluster, _, width, _ = uniseg.FirstGraphemeCluster(b[i:], -1)
|
||||
if m == WcWidth {
|
||||
width = runewidth.StringWidth(string(cluster))
|
||||
}
|
||||
|
||||
i += len(cluster)
|
||||
curWidth += width
|
||||
|
||||
if curWidth > length && ignoring {
|
||||
if curWidth > n && ignoring {
|
||||
ignoring = false
|
||||
buf.WriteString(prefix)
|
||||
}
|
||||
@ -146,7 +218,7 @@ func TruncateLeft(s string, length int, prefix string) string {
|
||||
continue
|
||||
}
|
||||
|
||||
if curWidth > length {
|
||||
if curWidth > n {
|
||||
buf.Write(cluster)
|
||||
}
|
||||
|
||||
@ -158,7 +230,7 @@ func TruncateLeft(s string, length int, prefix string) string {
|
||||
case parser.PrintAction:
|
||||
curWidth++
|
||||
|
||||
if curWidth > length && ignoring {
|
||||
if curWidth > n && ignoring {
|
||||
ignoring = false
|
||||
buf.WriteString(prefix)
|
||||
}
|
||||
@ -175,7 +247,7 @@ func TruncateLeft(s string, length int, prefix string) string {
|
||||
}
|
||||
|
||||
pstate = state
|
||||
if curWidth > length && ignoring {
|
||||
if curWidth > n && ignoring {
|
||||
ignoring = false
|
||||
buf.WriteString(prefix)
|
||||
}
|
||||
@ -183,3 +255,28 @@ func TruncateLeft(s string, length int, prefix string) string {
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// ByteToGraphemeRange takes start and stop byte positions and converts them to
|
||||
// grapheme-aware char positions.
|
||||
// You can use this with [Truncate], [TruncateLeft], and [Cut].
|
||||
func ByteToGraphemeRange(str string, byteStart, byteStop int) (charStart, charStop int) {
|
||||
bytePos, charPos := 0, 0
|
||||
gr := uniseg.NewGraphemes(str)
|
||||
for byteStart > bytePos {
|
||||
if !gr.Next() {
|
||||
break
|
||||
}
|
||||
bytePos += len(gr.Str())
|
||||
charPos += max(1, gr.Width())
|
||||
}
|
||||
charStart = charPos
|
||||
for byteStop > bytePos {
|
||||
if !gr.Next() {
|
||||
break
|
||||
}
|
||||
bytePos += len(gr.Str())
|
||||
charPos += max(1, gr.Width())
|
||||
}
|
||||
charStop = charPos
|
||||
return
|
||||
}
|
||||
|
14
vendor/github.com/charmbracelet/x/ansi/util.go
generated
vendored
14
vendor/github.com/charmbracelet/x/ansi/util.go
generated
vendored
@ -90,3 +90,17 @@ func XParseColor(s string) color.Color {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type ordered interface {
|
||||
~int | ~int8 | ~int16 | ~int32 | ~int64 |
|
||||
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
|
||||
~float32 | ~float64 |
|
||||
~string
|
||||
}
|
||||
|
||||
func max[T ordered](a, b T) T { //nolint:predeclared
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
18
vendor/github.com/charmbracelet/x/ansi/width.go
generated
vendored
18
vendor/github.com/charmbracelet/x/ansi/width.go
generated
vendored
@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
|
||||
"github.com/charmbracelet/x/ansi/parser"
|
||||
"github.com/mattn/go-runewidth"
|
||||
"github.com/rivo/uniseg"
|
||||
)
|
||||
|
||||
@ -62,7 +63,21 @@ func Strip(s string) string {
|
||||
// cells that the string will occupy when printed in a terminal. ANSI escape
|
||||
// codes are ignored and wide characters (such as East Asians and emojis) are
|
||||
// accounted for.
|
||||
// This treats the text as a sequence of grapheme clusters.
|
||||
func StringWidth(s string) int {
|
||||
return stringWidth(GraphemeWidth, s)
|
||||
}
|
||||
|
||||
// StringWidthWc returns the width of a string in cells. This is the number of
|
||||
// cells that the string will occupy when printed in a terminal. ANSI escape
|
||||
// codes are ignored and wide characters (such as East Asians and emojis) are
|
||||
// accounted for.
|
||||
// This treats the text as a sequence of wide characters and runes.
|
||||
func StringWidthWc(s string) int {
|
||||
return stringWidth(WcWidth, s)
|
||||
}
|
||||
|
||||
func stringWidth(m Method, s string) int {
|
||||
if s == "" {
|
||||
return 0
|
||||
}
|
||||
@ -78,6 +93,9 @@ func StringWidth(s string) int {
|
||||
if state == parser.Utf8State {
|
||||
var w int
|
||||
cluster, _, w, _ = uniseg.FirstGraphemeClusterInString(s[i:], -1)
|
||||
if m == WcWidth {
|
||||
w = runewidth.StringWidth(cluster)
|
||||
}
|
||||
width += w
|
||||
i += len(cluster) - 1
|
||||
pstate = parser.GroundState
|
||||
|
53
vendor/github.com/charmbracelet/x/ansi/winop.go
generated
vendored
Normal file
53
vendor/github.com/charmbracelet/x/ansi/winop.go
generated
vendored
Normal file
@ -0,0 +1,53 @@
|
||||
package ansi
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// ResizeWindowWinOp is a window operation that resizes the terminal
|
||||
// window.
|
||||
ResizeWindowWinOp = 4
|
||||
|
||||
// RequestWindowSizeWinOp is a window operation that requests a report of
|
||||
// the size of the terminal window in pixels. The response is in the form:
|
||||
// CSI 4 ; height ; width t
|
||||
RequestWindowSizeWinOp = 14
|
||||
|
||||
// RequestCellSizeWinOp is a window operation that requests a report of
|
||||
// the size of the terminal cell size in pixels. The response is in the form:
|
||||
// CSI 6 ; height ; width t
|
||||
RequestCellSizeWinOp = 16
|
||||
)
|
||||
|
||||
// WindowOp (XTWINOPS) is a sequence that manipulates the terminal window.
|
||||
//
|
||||
// CSI Ps ; Ps ; Ps t
|
||||
//
|
||||
// Ps is a semicolon-separated list of parameters.
|
||||
// See https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h4-Functions-using-CSI-_-ordered-by-the-final-character-lparen-s-rparen:CSI-Ps;Ps;Ps-t.1EB0
|
||||
func WindowOp(p int, ps ...int) string {
|
||||
if p <= 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
if len(ps) == 0 {
|
||||
return "\x1b[" + strconv.Itoa(p) + "t"
|
||||
}
|
||||
|
||||
params := make([]string, 0, len(ps)+1)
|
||||
params = append(params, strconv.Itoa(p))
|
||||
for _, p := range ps {
|
||||
if p >= 0 {
|
||||
params = append(params, strconv.Itoa(p))
|
||||
}
|
||||
}
|
||||
|
||||
return "\x1b[" + strings.Join(params, ";") + "t"
|
||||
}
|
||||
|
||||
// XTWINOPS is an alias for [WindowOp].
|
||||
func XTWINOPS(p int, ps ...int) string {
|
||||
return WindowOp(p, ps...)
|
||||
}
|
64
vendor/github.com/charmbracelet/x/ansi/wrap.go
generated
vendored
64
vendor/github.com/charmbracelet/x/ansi/wrap.go
generated
vendored
@ -6,6 +6,7 @@ import (
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/charmbracelet/x/ansi/parser"
|
||||
"github.com/mattn/go-runewidth"
|
||||
"github.com/rivo/uniseg"
|
||||
)
|
||||
|
||||
@ -17,7 +18,22 @@ const nbsp = 0xA0
|
||||
// wide-characters in the string.
|
||||
// When preserveSpace is true, spaces at the beginning of a line will be
|
||||
// preserved.
|
||||
// This treats the text as a sequence of graphemes.
|
||||
func Hardwrap(s string, limit int, preserveSpace bool) string {
|
||||
return hardwrap(GraphemeWidth, s, limit, preserveSpace)
|
||||
}
|
||||
|
||||
// HardwrapWc wraps a string or a block of text to a given line length, breaking
|
||||
// word boundaries. This will preserve ANSI escape codes and will account for
|
||||
// wide-characters in the string.
|
||||
// When preserveSpace is true, spaces at the beginning of a line will be
|
||||
// preserved.
|
||||
// This treats the text as a sequence of wide characters and runes.
|
||||
func HardwrapWc(s string, limit int, preserveSpace bool) string {
|
||||
return hardwrap(WcWidth, s, limit, preserveSpace)
|
||||
}
|
||||
|
||||
func hardwrap(m Method, s string, limit int, preserveSpace bool) string {
|
||||
if limit < 1 {
|
||||
return s
|
||||
}
|
||||
@ -42,6 +58,9 @@ func Hardwrap(s string, limit int, preserveSpace bool) string {
|
||||
if state == parser.Utf8State {
|
||||
var width int
|
||||
cluster, _, width, _ = uniseg.FirstGraphemeCluster(b[i:], -1)
|
||||
if m == WcWidth {
|
||||
width = runewidth.StringWidth(string(cluster))
|
||||
}
|
||||
i += len(cluster)
|
||||
|
||||
if curWidth+width > limit {
|
||||
@ -108,7 +127,27 @@ func Hardwrap(s string, limit int, preserveSpace bool) string {
|
||||
// breakpoint.
|
||||
//
|
||||
// Note: breakpoints must be a string of 1-cell wide rune characters.
|
||||
//
|
||||
// This treats the text as a sequence of graphemes.
|
||||
func Wordwrap(s string, limit int, breakpoints string) string {
|
||||
return wordwrap(GraphemeWidth, s, limit, breakpoints)
|
||||
}
|
||||
|
||||
// WordwrapWc wraps a string or a block of text to a given line length, not
|
||||
// breaking word boundaries. This will preserve ANSI escape codes and will
|
||||
// account for wide-characters in the string.
|
||||
// The breakpoints string is a list of characters that are considered
|
||||
// breakpoints for word wrapping. A hyphen (-) is always considered a
|
||||
// breakpoint.
|
||||
//
|
||||
// Note: breakpoints must be a string of 1-cell wide rune characters.
|
||||
//
|
||||
// This treats the text as a sequence of wide characters and runes.
|
||||
func WordwrapWc(s string, limit int, breakpoints string) string {
|
||||
return wordwrap(WcWidth, s, limit, breakpoints)
|
||||
}
|
||||
|
||||
func wordwrap(m Method, s string, limit int, breakpoints string) string {
|
||||
if limit < 1 {
|
||||
return s
|
||||
}
|
||||
@ -154,6 +193,9 @@ func Wordwrap(s string, limit int, breakpoints string) string {
|
||||
if state == parser.Utf8State {
|
||||
var width int
|
||||
cluster, _, width, _ = uniseg.FirstGraphemeCluster(b[i:], -1)
|
||||
if m == WcWidth {
|
||||
width = runewidth.StringWidth(string(cluster))
|
||||
}
|
||||
i += len(cluster)
|
||||
|
||||
r, _ := utf8.DecodeRune(cluster)
|
||||
@ -236,7 +278,26 @@ func Wordwrap(s string, limit int, breakpoints string) string {
|
||||
// (-) is always considered a breakpoint.
|
||||
//
|
||||
// Note: breakpoints must be a string of 1-cell wide rune characters.
|
||||
//
|
||||
// This treats the text as a sequence of graphemes.
|
||||
func Wrap(s string, limit int, breakpoints string) string {
|
||||
return wrap(GraphemeWidth, s, limit, breakpoints)
|
||||
}
|
||||
|
||||
// WrapWc wraps a string or a block of text to a given line length, breaking word
|
||||
// boundaries if necessary. This will preserve ANSI escape codes and will
|
||||
// account for wide-characters in the string. The breakpoints string is a list
|
||||
// of characters that are considered breakpoints for word wrapping. A hyphen
|
||||
// (-) is always considered a breakpoint.
|
||||
//
|
||||
// Note: breakpoints must be a string of 1-cell wide rune characters.
|
||||
//
|
||||
// This treats the text as a sequence of wide characters and runes.
|
||||
func WrapWc(s string, limit int, breakpoints string) string {
|
||||
return wrap(WcWidth, s, limit, breakpoints)
|
||||
}
|
||||
|
||||
func wrap(m Method, s string, limit int, breakpoints string) string {
|
||||
if limit < 1 {
|
||||
return s
|
||||
}
|
||||
@ -282,6 +343,9 @@ func Wrap(s string, limit int, breakpoints string) string {
|
||||
if state == parser.Utf8State {
|
||||
var width int
|
||||
cluster, _, width, _ = uniseg.FirstGraphemeCluster(b[i:], -1)
|
||||
if m == WcWidth {
|
||||
width = runewidth.StringWidth(string(cluster))
|
||||
}
|
||||
i += len(cluster)
|
||||
|
||||
r, _ := utf8.DecodeRune(cluster)
|
||||
|
5
vendor/github.com/charmbracelet/x/ansi/xterm.go
generated
vendored
5
vendor/github.com/charmbracelet/x/ansi/xterm.go
generated
vendored
@ -91,6 +91,7 @@ const (
|
||||
//
|
||||
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s_
|
||||
// See: https://invisible-island.net/xterm/manpage/xterm.html#VT100-Widget-Resources:modifyOtherKeys
|
||||
//
|
||||
// Deprecated: use [SetModifyOtherKeys1] or [SetModifyOtherKeys2] instead.
|
||||
func ModifyOtherKeys(mode int) string {
|
||||
return "\x1b[>4;" + strconv.Itoa(mode) + "m"
|
||||
@ -102,6 +103,7 @@ func ModifyOtherKeys(mode int) string {
|
||||
//
|
||||
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s_
|
||||
// See: https://invisible-island.net/xterm/manpage/xterm.html#VT100-Widget-Resources:modifyOtherKeys
|
||||
//
|
||||
// Deprecated: use [ResetModifyOtherKeys] instead.
|
||||
const DisableModifyOtherKeys = "\x1b[>4;0m"
|
||||
|
||||
@ -111,6 +113,7 @@ const DisableModifyOtherKeys = "\x1b[>4;0m"
|
||||
//
|
||||
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s_
|
||||
// See: https://invisible-island.net/xterm/manpage/xterm.html#VT100-Widget-Resources:modifyOtherKeys
|
||||
//
|
||||
// Deprecated: use [SetModifyOtherKeys1] instead.
|
||||
const EnableModifyOtherKeys1 = "\x1b[>4;1m"
|
||||
|
||||
@ -120,6 +123,7 @@ const EnableModifyOtherKeys1 = "\x1b[>4;1m"
|
||||
//
|
||||
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s_
|
||||
// See: https://invisible-island.net/xterm/manpage/xterm.html#VT100-Widget-Resources:modifyOtherKeys
|
||||
//
|
||||
// Deprecated: use [SetModifyOtherKeys2] instead.
|
||||
const EnableModifyOtherKeys2 = "\x1b[>4;2m"
|
||||
|
||||
@ -129,5 +133,6 @@ const EnableModifyOtherKeys2 = "\x1b[>4;2m"
|
||||
//
|
||||
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s_
|
||||
// See: https://invisible-island.net/xterm/manpage/xterm.html#VT100-Widget-Resources:modifyOtherKeys
|
||||
//
|
||||
// Deprecated: use [QueryModifyOtherKeys] instead.
|
||||
const RequestModifyOtherKeys = "\x1b[?4m"
|
||||
|
Reference in New Issue
Block a user