• Stars
    star
    574
  • Rank 77,739 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 4 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ“‹ cross-platform clipboard package that supports accessing text and image in Go (macOS/Linux/Windows/Android/iOS)

clipboard PkgGoDev clipboard

Cross platform (macOS/Linux/Windows/Android/iOS) clipboard package in Go

import "golang.design/x/clipboard"

Features

  • Cross platform supports: macOS, Linux (X11), Windows, iOS, and Android
  • Copy/paste UTF-8 text
  • Copy/paste PNG encoded images (Desktop-only)
  • Command gclip as a demo application
  • Mobile app gclip-gui as a demo application

API Usage

Package clipboard provides cross platform clipboard access and supports macOS/Linux/Windows/Android/iOS platform. Before interacting with the clipboard, one must call Init to assert if it is possible to use this package:

// Init returns an error if the package is not ready for use.
err := clipboard.Init()
if err != nil {
      panic(err)
}

The most common operations are Read and Write. To use them:

// write/read text format data of the clipboard, and
// the byte buffer regarding the text are UTF8 encoded.
clipboard.Write(clipboard.FmtText, []byte("text data"))
clipboard.Read(clipboard.FmtText)

// write/read image format data of the clipboard, and
// the byte buffer regarding the image are PNG encoded.
clipboard.Write(clipboard.FmtImage, []byte("image data"))
clipboard.Read(clipboard.FmtImage)

Note that read/write regarding image format assumes that the bytes are PNG encoded since it serves the alpha blending purpose that might be used in other graphical software.

In addition, clipboard.Write returns a channel that can receive an empty struct as a signal, which indicates the corresponding write call to the clipboard is outdated, meaning the clipboard has been overwritten by others and the previously written data is lost. For instance:

changed := clipboard.Write(clipboard.FmtText, []byte("text data"))

select {
case <-changed:
      println(`"text data" is no longer available from clipboard.`)
}

You can ignore the returning channel if you don't need this type of notification. Furthermore, when you need more than just knowing whether clipboard data is changed, use the watcher API:

ch := clipboard.Watch(context.TODO(), clipboard.FmtText)
for data := range ch {
      // print out clipboard data whenever it is changed
      println(string(data))
}

Demos

  • A command line tool gclip for command line clipboard accesses, see document here.
  • A GUI application gclip-gui for functionality verifications on mobile systems, see a document here.

Command Usage

gclip command offers the ability to interact with the system clipboard from the shell. To install:

$ go install golang.design/x/clipboard/cmd/gclip@latest
$ gclip
gclip is a command that provides clipboard interaction.

usage: gclip [-copy|-paste] [-f <file>]

options:
  -copy
        copy data to clipboard
  -f string
        source or destination to a given file path
  -paste
        paste data from clipboard

examples:
gclip -paste                    paste from clipboard and prints the content
gclip -paste -f x.txt           paste from clipboard and save as text to x.txt
gclip -paste -f x.png           paste from clipboard and save as image to x.png

cat x.txt | gclip -copy         copy content from x.txt to clipboard
gclip -copy -f x.txt            copy content from x.txt to clipboard
gclip -copy -f x.png            copy x.png as image data to clipboard

If -copy is used, the command will exit when the data is no longer available from the clipboard. You can always send the command to the background using a shell & operator, for example:

$ cat x.txt | gclip -copy &

Platform Specific Details

This package spent efforts to provide cross platform abstraction regarding accessing system clipboards, but here are a few details you might need to know.

Dependency

  • macOS: require Cgo, no dependency
  • Linux: require X11 dev package. For instance, install libx11-dev or xorg-dev or libX11-devel to access X window system.
  • Windows: no Cgo, no dependency
  • iOS/Android: collaborate with gomobile

Screenshot

In general, when you need test your implementation regarding images, There are system level shortcuts to put screenshot image into your system clipboard:

  • On macOS, use Ctrl+Shift+Cmd+4
  • On Linux/Ubuntu, use Ctrl+Shift+PrintScreen
  • On Windows, use Shift+Win+s

As described in the API documentation, the package supports read/write UTF8 encoded plain text or PNG encoded image data. Thus, the other types of data are not supported yet, i.e. undefined behavior.

Who is using this package?

The main purpose of building this package is to support the midgard project, which offers clipboard-based features like universal clipboard service that syncs clipboard content across multiple systems, allocating public accessible for clipboard content, etc.

To know more projects, check our wiki page.

License

MIT | ยฉ 2021 The golang.design Initiative Authors, written by Changkun Ou.

More Repositories

1

go-questions

๐Ÿ“– Go ็จ‹ๅบๅ‘˜้ข่ฏ•็ฌ”่ฏ•ๅฎๅ…ธ | ไปŽ้—ฎ้ข˜ๅˆ‡ๅ…ฅ๏ผŒไธฒ่ฟž Go ่ฏญ่จ€็›ธๅ…ณ็š„ๆ‰€ๆœ‰็Ÿฅ่ฏ†๏ผŒ่žไผš่ดฏ้€šใ€‚ https://golang.design/go-questions
Go
5,813
star
2

under-the-hood

๐Ÿ“š Go: Under The Hood | Go ่ฏญ่จ€ๅŽŸๆœฌ | https://golang.design/under-the-hood
Go
4,301
star
3

history

๐Ÿ“ Go: A Documentary | https://golang.design/history
Go
1,059
star
4

hotkey

โŒจ๏ธ cross-platform hotkey package
Go
202
star
5

go2generics

๐Ÿงช A chunk of experiments and demos about Go 2 generics design (type parameter & type set)
Go
150
star
6

lockfree

โšก๏ธ lock-free utilities in Go
Go
144
star
7

tgstore

An encrypted object storage system with unlimited space backed by Telegram.
Go
96
star
8

code2img

๐Ÿ–จ๏ธ A carbon-now API wrapper and iOS Shortcut
Go
79
star
9

ssaplayground

๐Ÿƒ The Go SSA Playground: Exploring SSA IR | https://golang.design/gossa
Go
76
star
10

bench

โฑ๏ธ Reliable performance measurement for Go programs. All in one design.
Go
38
star
11

chann

a unified channel package for buffered, unbuffered, and unbounded channels
Go
37
star
12

mainthread

๐Ÿ”€ schedule functions on the main thread
Go
33
star
13

redir

๐Ÿงญ a request redirector
Go
28
star
14

mkill

โ˜ ๏ธ mkill limits the number of threads in a Go program, without crashing it.
Go
23
star
15

thread

๐Ÿงต thead facilities in Go
Go
15
star
16

research

๐Ÿ—ž๏ธ (Possibly) Naรฏve thoughts regarding Go.
Go
12
star
17

reflect

Package reflect implements the proposal https://go.dev/issue/51520.
Go
5
star
18

verbose

๐Ÿคช verbose repository for testing purpose | go get golang.design/x/verbose
Go
4
star
19

.github

Community health files for the @golang-design organization
3
star
20

iter

Iterator utils.
Go
2
star
21

website

๐ŸŒ website | https://golang.design
HTML
1
star
22

io

a replacement to the standard io package.
Go
1
star