From 3259e65980343aa3309159cdce8dd1879235f776 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Wed, 12 Oct 2022 17:13:24 -0400 Subject: [PATCH] fix: base64 encode string in Sequence --- osc52.go | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/osc52.go b/osc52.go index 2f4186c..0322b4b 100755 --- a/osc52.go +++ b/osc52.go @@ -124,19 +124,18 @@ func (o *Output) CopyClipboard(str string, c Clipboard) { func (o *Output) osc52Write(str string, c Clipboard) { var seq string term := strings.ToLower(o.envs.Get("TERM")) - b64 := base64.StdEncoding.EncodeToString([]byte(str)) switch { case o.envs.Get("TMUX") != "", strings.HasPrefix(term, "tmux"): - seq = Sequence(b64, "tmux", c) + seq = Sequence(str, "tmux", c) case strings.HasPrefix(term, "screen"): - seq = Sequence(b64, "screen", c) + seq = Sequence(str, "screen", c) case strings.Contains(term, "kitty"): // First, we flush the keyboard before copying, this is required for // Kitty < 0.22.0. o.out.Write([]byte(Clear(term, c))) - seq = Sequence(b64, "kitty", c) + seq = Sequence(str, "kitty", c) default: - seq = Sequence(b64, term, c) + seq = Sequence(str, term, c) } o.out.Write([]byte(seq)) } @@ -171,9 +170,9 @@ func seqEnd(term string) string { return seq.String() } -// Sequence returns the OSC52 sequence for the given string, terminal, and clipboard choice. +// sequence returns the OSC52 sequence for the passed content. // Beware that the string here is not base64 encoded. -func Sequence(contents string, term string, c Clipboard) string { +func sequence(contents string, term string, c Clipboard) string { var seq strings.Builder term = strings.ToLower(term) seq.WriteString(seqStart(term, c)) @@ -194,6 +193,21 @@ func Sequence(contents string, term string, c Clipboard) string { return seq.String() } +// Sequence returns the OSC52 sequence for the given string, terminal, and clipboard choice. +func Sequence(str string, term string, c Clipboard) string { + b64 := base64.StdEncoding.EncodeToString([]byte(str)) + return sequence(b64, term, c) +} + +// Contents returns the contents of the clipboard. +func Contents(term string, c Clipboard) string { + var seq strings.Builder + seq.WriteString(seqStart(term, c)) + seq.WriteString("?") + seq.WriteString(seqEnd(term)) + return seq.String() +} + // Clear returns the OSC52 sequence to clear the clipboard. func Clear(term string, c Clipboard) string { var seq strings.Builder