-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(term): support XTGETTCAP sequence
- Loading branch information
1 parent
8f64c9d
commit ad8dadc
Showing
5 changed files
with
273 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package ansi | ||
|
||
import ( | ||
"encoding/hex" | ||
"strings" | ||
) | ||
|
||
// RequestTermcap (XTGETTCAP) requests Termcap/Terminfo strings. | ||
// | ||
// DCS + q <Pt> ST | ||
// | ||
// Where <Pt> is a list of Termcap/Terminfo capabilities, encoded in 2-digit | ||
// hexadecimals, separated by semicolons. | ||
// | ||
// See: https://man7.org/linux/man-pages/man5/terminfo.5.html | ||
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands | ||
func RequestTermcap(caps ...string) string { | ||
if len(caps) == 0 { | ||
return "" | ||
} | ||
|
||
s := "\x1bP+q" | ||
for i, c := range caps { | ||
if i > 0 { | ||
s += ";" | ||
} | ||
s += strings.ToUpper(hex.EncodeToString([]byte(c))) | ||
} | ||
|
||
return s + "\x1b\\" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
parser "github.com/charmbracelet/x/exp/term/vtparser" | ||
) | ||
|
||
type dispatcher struct{} | ||
|
||
func (p *dispatcher) Print(r rune) { | ||
fmt.Printf("[Print] %c\n", r) | ||
} | ||
|
||
func (p *dispatcher) Execute(code byte) { | ||
fmt.Printf("[Execute] 0x%02x\n", code) | ||
} | ||
|
||
func (p *dispatcher) DcsPut(code byte) { | ||
fmt.Printf("[DcsPut] %02x\n", code) | ||
} | ||
|
||
func (p *dispatcher) DcsUnhook() { | ||
fmt.Printf("[DcsUnhook]\n") | ||
} | ||
|
||
func (p *dispatcher) DcsHook(prefix string, params [][]uint16, intermediates []byte, r rune, ignore bool) { | ||
fmt.Print("[DcsHook]") | ||
if prefix != "" { | ||
fmt.Printf(" prefix=%s", prefix) | ||
} | ||
fmt.Printf(" params=%v, intermediates=%v, final=%c, ignore=%v\n", params, intermediates, r, ignore) | ||
} | ||
|
||
func (p *dispatcher) OscDispatch(params [][]byte, bellTerminated bool) { | ||
fmt.Printf("[OscDispatch]") | ||
for _, param := range params { | ||
fmt.Printf(" param=%q", param) | ||
} | ||
fmt.Printf(" bellTerminated=%v\n", bellTerminated) | ||
} | ||
|
||
func (p *dispatcher) CsiDispatch(prefix string, params [][]uint16, intermediates []byte, r rune, ignore bool) { | ||
fmt.Print("[CsiDispatch]") | ||
if prefix != "" { | ||
fmt.Printf(" prefix=%s", prefix) | ||
} | ||
fmt.Printf(" params=%v, intermediates=%v, final=%c, ignore=%v\n", params, intermediates, r, ignore) | ||
} | ||
|
||
func (p *dispatcher) EscDispatch(intermediates []byte, r rune, ignore bool) { | ||
fmt.Printf("[EscDispatch] intermediates=%v, final=%c, ignore=%v\n", intermediates, r, ignore) | ||
} | ||
|
||
func main() { | ||
dispatcher := &dispatcher{} | ||
if err := parser.New(dispatcher).Parse(os.Stdin); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package input | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// TermcapEvent represents a Termcap response event. Termcap responses are | ||
// generated by the terminal in response to RequestTermcap (XTGETTCAP) | ||
// requests. | ||
// | ||
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands | ||
type TermcapEvent struct { | ||
Values map[string]string | ||
IsValid bool | ||
} | ||
|
||
// String implements fmt.Stringer. | ||
func (t TermcapEvent) String() string { | ||
var s strings.Builder | ||
var i int | ||
if !t.IsValid { | ||
s.WriteString("!") | ||
if len(t.Values) > 0 { | ||
s.WriteString(" ") | ||
} | ||
} | ||
for k, v := range t.Values { | ||
if i > 0 { | ||
s.WriteString(",") | ||
} | ||
s.WriteString(k) | ||
if v != "" { | ||
s.WriteString("=") | ||
s.WriteString(fmt.Sprintf("%q", v)) | ||
} | ||
i++ | ||
} | ||
return s.String() | ||
} | ||
|
||
func parseTermcap(data []byte) TermcapEvent { | ||
// XTGETTCAP | ||
if len(data) == 0 { | ||
return TermcapEvent{} | ||
} | ||
|
||
tc := TermcapEvent{Values: make(map[string]string)} | ||
split := bytes.Split(data, []byte{';'}) | ||
for _, s := range split { | ||
parts := bytes.SplitN(s, []byte{'='}, 2) | ||
if len(parts) == 0 { | ||
return TermcapEvent{} | ||
} | ||
|
||
name, err := hex.DecodeString(string(parts[0])) | ||
if err != nil || len(name) == 0 { | ||
continue | ||
} | ||
|
||
var value []byte | ||
if len(parts) > 1 { | ||
value, err = hex.DecodeString(string(parts[1])) | ||
if err != nil { | ||
continue | ||
} | ||
} | ||
|
||
tc.Values[string(name)] = string(value) | ||
} | ||
|
||
return tc | ||
} |