clipboard

package
v0.3.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package clipboard is the session's clipboard history (crates/wayle-clipboard): what gets remembered, how it is labelled, what is refused, and the service that keeps it current from the Wayland selection over lys-ui's data-control device.

An entry is bytes plus the mime type they arrived under, not a string. A file manager puts text/uri-list on the clipboard, an image editor image/png, a terminal text/plain;charset=utf-8 — remembering only the last of those would make the history useless for exactly the things that are most annoying to copy twice.

The bytes live off the Go heap and are shared by reference count: the history holds each entry, and so does every Snapshot listing it and every paste serving it. An entry the history drops is freed when the last of those lets go, never while one can still read it.

Index

Constants

View Source
const (
	MinBudgetMiB           = MaxEntryBytes / MiB
	DefaultBudgetMiB       = 64
	DefaultPinnedBudgetMiB = 16
	MaxBudgetMiB           = 1024
)

The history's total byte budget, in MiB. The least is the largest entry the history keeps, so every entry it accepts fits; the most keeps the budget in an int on every platform.

View Source
const DefaultCapacity = 200

DefaultCapacity is how many entries are kept before the oldest is dropped (history.rs DEFAULT_CAPACITY).

View Source
const MaxEntryBytes = 8 * MiB

MaxEntryBytes is the largest selection worth remembering. The history lives in memory for the session, and a copied image or a large file list is unbounded. Past this the entry is refused rather than truncated: half a payload pasted back is worse than not offering it.

View Source
const MiB = 1 << 20

MiB is a mebibyte, the unit the history's byte budget is set in.

View Source
const URIListMime = "text/uri-list"

URIListMime is the mime type a file manager copies files under.

Variables

View Source
var ErrBudgetRange = errors.New("clipboard budget out of range")

ErrBudgetRange reports a byte budget outside MinBudgetMiB-MaxBudgetMiB.

View Source
var ErrCorrupt = errors.New("clipboard: a stored entry does not match its record")

ErrCorrupt is a payload whose bytes are not the ones its record names.

View Source
var ErrEntryGone = errors.New("clipboard: the entry is no longer in the history")

ErrEntryGone is an entry that left the history since a row was drawn for it.

View Source
var ErrNoDataControl = errors.New("clipboard: compositor lacks ext-data-control-v1 and zwlr_data_control_manager_v1")

ErrNoDataControl is the Start failure on a compositor without either data-control protocol: the session simply has no clipboard history, and nothing else depends on it.

View Source
var ErrNoPinStore = errors.New("clipboard: pinned entries have no store")

ErrNoPinStore is a pin with nowhere to keep it: the session has no data directory.

View Source
var ErrPinnedFull = errors.New("clipboard: the pinned entries' budget is full")

ErrPinnedFull is a pin past the pinned entries' byte budget.

View Source
var SensitiveMimes = []string{
	"x-kde-passwordManagerHint",
	"org.kde.passwordManagerHint",
	"password",
	"x-lys-shell-no-history",
}

SensitiveMimes are the mime types lys-shell will not remember, however they are offered. Password managers mark a selection with one of these to ask clipboard managers not to keep it, and honouring that is the difference between a convenience and a credential leak. x-kde-passwordManagerHint carries the value "secret"; the others are presence-only conventions.

View Source
var TextMimes = []string{
	"text/plain;charset=utf-8",
	"text/plain",
	"UTF8_STRING",
	"STRING",
	"TEXT",
}

TextMimes are the mime types a plain-text selection arrives under, best first.

Functions

func BestMime

func BestMime(mimes []string) (string, bool)

BestMime picks the mime worth remembering out of everything the owner offered. Files beat images beat text: a file manager also offers the file's name as text/plain, and remembering that instead would put a bare filename on the clipboard where a file was copied. Within text, the order of TextMimes wins so the entry is UTF-8 wherever the owner can provide it. Anything else is still worth being able to paste back, byte for byte, so the first offered mime is the last resort; false only when nothing was offered.

func HumanSize added in v0.3.14

func HumanSize(n int) string

HumanSize is a byte count short enough for a list row.

func IsSensitive

func IsSensitive(mimes []string) bool

IsSensitive reports whether a selection offering these mimes must not be remembered.

func IsTextMime

func IsTextMime(mime string) bool

IsTextMime reports whether a mime type names plain text.

func SecretContent

func SecretContent(text string) transfer.Content

SecretContent is text offered as a password manager offers it: the text under every text mime, marked with x-kde-passwordManagerHint so a clipboard history (this one included) does not keep it.

Types

type Budget added in v0.2.0

type Budget struct {
	// contains filtered or unexported fields
}

Budget is the most the history holds in all, validated: never less than one entry of MaxEntryBytes.

func DefaultBudget added in v0.2.0

func DefaultBudget() Budget

DefaultBudget is DefaultBudgetMiB.

func DefaultPinnedBudget added in v0.3.15

func DefaultPinnedBudget() Budget

DefaultPinnedBudget is DefaultPinnedBudgetMiB.

func NewBudget added in v0.2.0

func NewBudget(mib int64) (Budget, error)

NewBudget validates a budget of mib mebibytes.

func (Budget) Bytes added in v0.2.0

func (b Budget) Bytes() int

Bytes is the budget in bytes.

func (Budget) MiB added in v0.2.0

func (b Budget) MiB() int64

MiB is the budget in mebibytes.

type Clipboard

type Clipboard struct {
	// contains filtered or unexported fields
}

Clipboard is the session's clipboard history, kept current: it watches the regular selection, remembers what lands on it, and puts an entry back when asked (service.rs). The primary selection is not remembered, as in the Rust service.

Entries, Forget, Clear and SetBudget are safe from any goroutine. Copy hops onto the loop goroutine itself, since claiming the selection is Wayland work.

func Start

func Start(a *app.Application, history *History, stores Stores) (*Clipboard, error)

Start begins watching the session's selection into history, after restoring what stores hold. Call it once at shell startup rather than when the launcher first opens, so the history covers the session instead of starting empty at the moment somebody goes looking for it. Fails with ErrNoDataControl when the compositor offers no data-control protocol.

func (*Clipboard) CanPin added in v0.3.15

func (c *Clipboard) CanPin() bool

CanPin reports whether entries can be pinned at all.

func (*Clipboard) ClearUnpinned added in v0.3.15

func (c *Clipboard) ClearUnpinned()

ClearUnpinned forgets every entry that is not pinned.

func (*Clipboard) Close added in v0.2.0

func (c *Clipboard) Close()

Close gives up a restored selection and frees the history; the clipboard records nothing afterwards. Pastes still being written free their entry when they finish; reads still in flight are freed now, and their transfers end on the next write. A persisted history is saved one last time first. Loop goroutine, or once the loop ended.

func (*Clipboard) Copy

func (c *Clipboard) Copy(id uint64) bool

Copy puts a remembered entry back on the clipboard, under the mime it arrived with (and every text alias, for text), and reports whether the entry was still in the history. The entry keeps its place: a restore is not a new copy.

func (*Clipboard) Entries

func (c *Clipboard) Entries() *Snapshot

Entries returns a snapshot of the history, pinned entries first, for the caller to release once it has read it. Entries restored from disk are read in first.

func (*Clipboard) Forget

func (c *Clipboard) Forget(id uint64) bool

Forget drops one entry, pinned or not, reporting whether it was there.

func (*Clipboard) Persisting added in v0.3.15

func (c *Clipboard) Persisting() bool

Persisting reports whether the unpinned entries are kept on disk.

func (*Clipboard) Pin added in v0.3.15

func (c *Clipboard) Pin(id uint64) error

Pin keeps an entry for good: listed first, never evicted, and written to the pinned store before Pin returns. Refused past the pinned budget, with ErrPinnedFull.

func (*Clipboard) Put added in v0.2.0

func (c *Clipboard) Put(content transfer.Content) (retract func(), err error)

Put claims the regular selection with content through data-control, which needs neither keyboard focus nor an input event, serves it to every receiver that asks until another client takes the selection, and remembers it like any other copy. Retract, from any goroutine, takes the copy back: it forgets the history entry and gives the selection up if it still holds it. Loop goroutine only.

func (*Clipboard) SetBudget added in v0.2.0

func (c *Clipboard) SetBudget(budget Budget) error

SetBudget bounds the unpinned entries to budget from now on, dropping the oldest past it.

func (*Clipboard) SetPersist added in v0.3.15

func (c *Clipboard) SetPersist(store *Store) error

SetPersist starts keeping the unpinned entries in store, or, with nil, stops and removes what was kept: the entries restored from it and not read since go with it, the rest stay in memory.

func (*Clipboard) SetPinnedBudget added in v0.3.15

func (c *Clipboard) SetPinnedBudget(budget Budget) error

SetPinnedBudget bounds what pins may hold from now on.

func (*Clipboard) Unpin added in v0.3.15

func (c *Clipboard) Unpin(id uint64) error

Unpin returns an entry to the rest of the history and removes it from the pinned store; the budget may then evict it like any other.

type Digest added in v0.3.15

type Digest [sha256.Size]byte

Digest is the SHA-256 of an entry's bytes: the name its payload file is stored under, so the same bytes are written once however many records name them.

func DigestOf added in v0.3.15

func DigestOf(data []byte) Digest

DigestOf is data's digest.

func (Digest) String added in v0.3.15

func (d Digest) String() string

String is the digest in lowercase hex, its file name.

type Entry

type Entry struct {
	// ID is stable for the life of the entry, so a row the launcher
	// drew still names the same selection once something new arrives
	// at the front.
	ID uint64
	// Mime is the type the bytes are in, and the one they are offered
	// back under.
	Mime string
	// Pinned entries are listed first, survive restarts and are never
	// evicted.
	Pinned bool
	// contains filtered or unexported fields
}

Entry is one remembered selection, as a Snapshot lists it.

func (Entry) Bytes

func (e Entry) Bytes() []byte

Bytes copies the entry's bytes out, for a reader that outlives its snapshot.

func (Entry) Kind

func (e Entry) Kind() Kind

Kind reports what the entry is, from its mime type.

func (Entry) Paths

func (e Entry) Paths() []string

Paths returns the decoded paths in a text/uri-list entry.

func (Entry) Preview

func (e Entry) Preview(maxChars int) string

Preview is a single line for a list row, at most maxChars characters.

func (Entry) Size added in v0.3.14

func (e Entry) Size() int

Size is how many bytes the entry holds.

type History

type History struct {
	// contains filtered or unexported fields
}

History is the clipboard history: pinned entries first, then the rest, each most recent first. The unpinned entries are bounded by entry count and by total bytes, the oldest making way for a new one; pinned entries are never evicted, and their own byte budget refuses a pin rather than dropping one. An entry restored from a Store holds no bytes until it is filled. It is plain bookkeeping and not safe for concurrent use; the Clipboard service guards it.

func DefaultHistory

func DefaultHistory(budget, pinned Budget) (*History, error)

DefaultHistory is the history the shell runs with: DefaultCapacity entries of up to MaxEntryBytes, budget in all, and pinned entries of pinned in all.

func NewHistory

func NewHistory(capacity, maxEntryBytes, budget, pinnedBudget int) (*History, error)

NewHistory builds an empty history keeping at most capacity unpinned entries of at most maxEntryBytes each, budget bytes in all, and pinned entries of pinnedBudget bytes in all. A zero capacity or entry size remembers nothing. A negative bound is an error, and so is a budget smaller than the largest entry: that entry would be accepted only to evict everything, itself included.

func (*History) Clear

func (h *History) Clear()

Clear forgets everything, pinned entries included.

func (*History) ClearUnpinned added in v0.3.15

func (h *History) ClearUnpinned()

ClearUnpinned forgets every entry that is not pinned.

func (*History) Close added in v0.2.0

func (h *History) Close()

Close forgets everything for good: a later Push is an error.

func (*History) Entries

func (h *History) Entries() *Snapshot

Entries returns every entry whose bytes are in memory, pinned first, each most recent first, each held until the snapshot is released.

func (*History) Len

func (h *History) Len() int

Len reports how many entries are held.

func (*History) Pin added in v0.3.15

func (h *History) Pin(id uint64) error

Pin keeps the entry with this id for good: listed first, never evicted, and refused past the pinned budget.

func (*History) Push

func (h *History) Push(mime string, data []byte) (uint64, bool, error)

Push records a selection and returns the id it is stored under. Re-copying something already remembered moves it to the front and keeps its id (and its pin) rather than adding a second copy: the clipboard is a stack of what you have, and the same bytes twice is one thing. The same bytes under another mime are another entry — pasting a PNG back as text/plain would hand the receiver bytes it cannot use. The oldest unpinned entries make way until the history is within its capacity and budget again.

False when the selection is not worth remembering: empty, oversized, whitespace-only text, or a zero-capacity history. An error when the history is closed or the bytes cannot be held.

func (*History) Remove

func (h *History) Remove(id uint64) bool

Remove forgets one entry, pinned or not, reporting whether it was there.

func (*History) SetBudget added in v0.2.0

func (h *History) SetBudget(budget int) error

SetBudget bounds the unpinned entries to budget bytes from now on, evicting the oldest past it.

func (*History) SetPinnedBudget added in v0.3.15

func (h *History) SetPinnedBudget(budget int) error

SetPinnedBudget bounds the pinned entries to budget bytes for pins from now on. Entries already pinned stay: a pin is never undone behind the user's back.

func (*History) Unpin added in v0.3.15

func (h *History) Unpin(id uint64) bool

Unpin returns the entry with this id to the rest of the history, where the budget may evict it; false when it was not pinned.

type Kind

type Kind uint8

Kind is what a remembered selection is, for labelling and for choosing an icon.

const (
	// KindText is plain text.
	KindText Kind = iota
	// KindFiles is one or more file:// URIs — what copying in a file
	// manager puts down.
	KindFiles
	// KindImage is an image, in whatever encoding the mime names.
	KindImage
	// KindOther is anything else, kept verbatim and offered back
	// under its own mime.
	KindOther
)

func (Kind) String

func (k Kind) String() string

String names the kind.

type Record added in v0.3.15

type Record struct {
	Digest Digest
	Mime   string
	Size   int
}

Record is one entry a Store holds: which bytes, under which mime.

type Snapshot added in v0.2.0

type Snapshot struct {
	// contains filtered or unexported fields
}

Snapshot is the history as it stood, each entry held until Release so the history may drop it meanwhile without the listing losing its bytes. Release it exactly once, as soon as it has been read: reading an entry afterwards panics.

func (*Snapshot) Entries added in v0.2.0

func (s *Snapshot) Entries() []Entry

Entries are the snapshot's entries, most recent first.

func (*Snapshot) Release added in v0.2.0

func (s *Snapshot) Release()

Release lets go of every entry. A second Release panics.

type Store added in v0.3.15

type Store struct {
	// contains filtered or unexported fields
}

Store keeps clipboard entries in a directory: one payload file per distinct content, named by its digest, and an index listing the records in order. Opening one reads the index only; a payload is read when Read asks for it. Every file is replaced atomically, so a write interrupted at any point leaves the store as it was before it or as it is after it. Safe for concurrent use.

func OpenHistoryStore added in v0.3.15

func OpenHistoryStore(dataDir string) (*Store, error)

OpenHistoryStore opens the persisted history's store under dataDir.

func OpenPinnedStore added in v0.3.15

func OpenPinnedStore(dataDir string) (*Store, error)

OpenPinnedStore opens the pinned entries' store under dataDir, the shell's data directory.

func OpenStore added in v0.3.15

func OpenStore(dir string) (*Store, error)

OpenStore opens the store in dir, creating it, and reads its index. Files the index does not name are what an interrupted write left behind, and are removed.

func (*Store) Read added in v0.3.15

func (s *Store) Read(r Record, w io.Writer) error

Read copies r's payload into w, checking it against the record.

func (*Store) Records added in v0.3.15

func (s *Store) Records() []Record

Records are the store's records, most recent first.

func (*Store) Sync added in v0.3.15

func (s *Store) Sync(items []storeItem) error

Sync makes the store hold exactly items, in their order: payloads it lacks are written first, then the index naming them, and only then are the payloads no record names any more removed. An item without data must already be in the store.

type Stores added in v0.3.15

type Stores struct {
	// Pinned holds the pinned entries; nil offers no pinning.
	Pinned *Store
	// History holds the unpinned entries as well; nil keeps them in
	// memory only.
	History *Store
}

Stores are where the history keeps entries across restarts.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL