Skip to content

Commit

Permalink
feat(term): input: support OSC52 request events
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Apr 8, 2024
1 parent c2c5fe8 commit e9202e3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion exp/term/examples/readinput/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ OUT:
execute(ansi.EnableWin32Input)
}
win32Input = !win32Input
case "c" == prev && "c" == curr:
execute(ansi.RequestClipboard('c'))
case prev == "k":
switch curr {
case "0":
Expand Down Expand Up @@ -228,7 +230,7 @@ OUT:
}

for _, e := range events {
log.Printf("%v, %#v\r\n", e, e)
log.Printf("%T %#v\r\n", e, e)
}

// Store last keypress
Expand Down
9 changes: 9 additions & 0 deletions exp/term/input/clipboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package input

// ClipboardEvent is a clipboard read event.
type ClipboardEvent string

// String returns the string representation of the clipboard event.
func (e ClipboardEvent) String() string {
return string(e)
}
13 changes: 13 additions & 0 deletions exp/term/input/parse.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package input

import (
"encoding/base64"
"strings"
"unicode/utf8"

"github.com/charmbracelet/x/exp/term/ansi"
Expand Down Expand Up @@ -595,6 +597,17 @@ func parseOsc(b []byte) (int, Event) {
return i, BackgroundColorEvent{xParseColor(data)}
case 12:
return i, CursorColorEvent{xParseColor(data)}
case 52:
parts := strings.Split(data, ";")
if len(parts) == 0 {
return i, ClipboardEvent("")
}
b64 := parts[len(parts)-1]
bts, err := base64.StdEncoding.DecodeString(b64)
if err != nil {
return i, ClipboardEvent("")
}
return i, ClipboardEvent(bts)
default:
return i, UnknownOscEvent(b[:i])
}
Expand Down
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
Expand Down

0 comments on commit e9202e3

Please sign in to comment.