Skip to content

Commit

Permalink
Add screen.SetClipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Consolatis committed Sep 26, 2022
1 parent 96bb70f commit 420210e
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 1 deletion.
88 changes: 88 additions & 0 deletions _demos/set_clipboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// +build ignore

// Copyright 2020 The TCell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"fmt"
"os"

"github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/encoding"

"github.com/mattn/go-runewidth"
)

func emitStr(s tcell.Screen, x, y int, style tcell.Style, str string) {
for _, c := range str {
var comb []rune
w := runewidth.RuneWidth(c)
if w == 0 {
comb = []rune{c}
c = ' '
w = 1
}
s.SetContent(x, y, c, comb, style)
x += w
}
}

func displayHelloWorld(s tcell.Screen) {
w, h := s.Size()
s.Clear()
style := tcell.StyleDefault.Foreground(tcell.ColorCadetBlue.TrueColor()).Background(tcell.ColorWhite)
emitStr(s, w/2-14, h/2, style, "Press Enter to set clipboard")
emitStr(s, w/2-9, h/2+1, tcell.StyleDefault, "Press ESC to exit.")
s.Show()
}

// This program just prints "Hello, World!". Press ESC to exit.
func main() {
encoding.Register()

s, e := tcell.NewScreen()
if e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
os.Exit(1)
}
if e := s.Init(); e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
os.Exit(1)
}

defStyle := tcell.StyleDefault.
Background(tcell.ColorBlack).
Foreground(tcell.ColorWhite)
s.SetStyle(defStyle)

displayHelloWorld(s)

for {
switch ev := s.PollEvent().(type) {
case *tcell.EventResize:
s.Sync()
displayHelloWorld(s)
case *tcell.EventKey:
switch ev.Key() {
case tcell.KeyEnter:
s.SetClipboard("enjoy your new clipboard content")
case tcell.KeyEscape:
s.Fini()
os.Exit(0)
}
}
}
}
4 changes: 4 additions & 0 deletions console_win.go
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,10 @@ func (s *cScreen) SetSize(w, h int) {
s.resize()
}

func (s *cScreen) SetClipboard(content string) bool {
return false
}

func (s *cScreen) resize() {
info := consoleInfo{}
s.getConsoleInfo(&info)
Expand Down
2 changes: 2 additions & 0 deletions screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ type Screen interface {
// does not support application-initiated resizing, whereas the legacy terminal does.
// Also, some emulators can support this but may have it disabled by default.
SetSize(int, int)

SetClipboard(string) bool
}

// NewScreen returns a default Screen suitable for the user's terminal
Expand Down
4 changes: 4 additions & 0 deletions simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,10 @@ func (s *simscreen) SetSize(w, h int) {
s.Unlock()
}

func (s *simscreen) SetClipboard(content string) bool {
return false
}

func (s *simscreen) GetContents() ([]SimCell, int, int) {
s.Lock()
cells, w, h := s.front, s.physw, s.physh
Expand Down
3 changes: 2 additions & 1 deletion terminfo/dynamic/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func unescape(s string) string {
}

func (tc *termcap) setupterm(name string) error {
cmd := exec.Command("infocmp", "-1", name)
cmd := exec.Command("infocmp", "-x", "-1", name)
output := &bytes.Buffer{}
cmd.Stdout = output

Expand Down Expand Up @@ -219,6 +219,7 @@ func LoadTerminfo(name string) (*terminfo.Terminfo, string, error) {
t.SetCursor = tc.getstr("cup")
t.CursorBack1 = tc.getstr("cub1")
t.CursorUp1 = tc.getstr("cuu1")
t.SetClipboard = tc.getstr("Ms")
t.KeyF1 = tc.getstr("kf1")
t.KeyF2 = tc.getstr("kf2")
t.KeyF3 = tc.getstr("kf3")
Expand Down
1 change: 1 addition & 0 deletions terminfo/terminfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ type Terminfo struct {
EnterUrl string
ExitUrl string
SetWindowSize string
SetClipboard string
}

const (
Expand Down
11 changes: 11 additions & 0 deletions tscreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package tcell

import (
"bytes"
"encoding/base64"
"errors"
"io"
"os"
Expand Down Expand Up @@ -1777,6 +1778,16 @@ func (t *tScreen) SetSize(w, h int) {
t.resize()
}

func (t *tScreen) SetClipboard(content string) bool {
ti := t.ti
if ti.SetClipboard == "" {
return false
}
encoded := base64.StdEncoding.EncodeToString([]byte(content))
t.TPuts(ti.TParm(ti.SetClipboard, "c", encoded))
return true
}

func (t *tScreen) Resize(int, int, int, int) {}

func (t *tScreen) Suspend() error {
Expand Down

0 comments on commit 420210e

Please sign in to comment.