Debian Codegraph @

demos/clipboard/clipboard.go

raw symbols SCIP
1 // Copyright 2026 The TCell Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package main
16
17 import (
18 "fmt"
19 "os"
20 "unicode/utf8"
21
22 "github.com/gdamore/tcell/v3"
23 "github.com/gdamore/tcell/v3/color"
24 )
25
26 var clipboard []byte
27
28 func displayHelloWorld(s tcell.Screen) {
29 w, h := s.Size()
30 s.Clear()
31 style := tcell.StyleDefault.Foreground(color.CadetBlue.TrueColor()).Background(color.White)
32 has := "does NOT claim"
33 if s.HasClipboard() {
34 has = "claims to"
35 }
36 term, version := s.Terminal()
37 termId := "(unidentified)"
38 if term != "" {
39 termId = fmt.Sprintf("(%s %s)", term, version)
40 }
41 line := fmt.Sprintf("Your terminal %s %s to allow clipboard access.", termId, has)
42
43 s.PutStrStyled(w/2-14, h/2, "Press 1 to set clipboard", style)
44 s.PutStrStyled(w/2-14, h/2+1, "Press 2 to get clipboard", style)
45 s.PutStr((w-len(line))/2, h/2+9, line)
46
47 msg := ""
48 if utf8.Valid(clipboard) {
49 cp := string(clipboard)
50 if len(cp) >= w-25 {
51 cp = cp[:21] + " ..."
52 }
53 msg = fmt.Sprintf("Clipboard (%d bytes): %s", len(clipboard), cp)
54 } else if clipboard != nil {
55 msg = fmt.Sprintf("Clipboard (%d bytes) Not Valid UTF-8", len(clipboard))
56 } else {
57 msg = "No clipboard data"
58 }
59 s.PutStr((w-len(msg))/2, h/2+3, msg)
60 line = "Press Control-Q to exit."
61 s.PutStr((w-len(line))/2, h/2+5, line)
62 s.Show()
63 }
64
65 // This program demonstrates access to the clipboard.
66 // Not all terminals support, and many only support write access.
67 // (Read access to the clipboard is often restricted for security reasons.)
68 func main() {
69
70 s, e := tcell.NewScreen()
71 if e != nil {
72 fmt.Fprintf(os.Stderr, "%v\n", e)
73 os.Exit(1)
74 }
75 if e := s.Init(); e != nil {
76 fmt.Fprintf(os.Stderr, "%v\n", e)
77 os.Exit(1)
78 }
79 defer s.Fini()
80
81 defStyle := tcell.StyleDefault.
82 Background(color.Black).
83 Foreground(color.White)
84 s.SetStyle(defStyle)
85
86 displayHelloWorld(s)
87
88 for {
89 ev1 := <-s.EventQ()
90 switch ev := ev1.(type) {
91 case *tcell.EventResize:
92 s.Sync()
93 displayHelloWorld(s)
94 case *tcell.EventKey:
95 switch ev.Key() {
96 case tcell.KeyRune:
97 switch ev.Str() {
98 case "1":
99 s.SetClipboard([]byte("Enjoy your new clipboard content!"))
100 case "2":
101 s.GetClipboard()
102 }
103 case tcell.KeyEscape, tcell.KeyCtrlQ:
104 return
105 }
106 case *tcell.EventClipboard:
107 clipboard = ev.Data()
108 displayHelloWorld(s)
109 }
110 }
111 }