clipboard

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 9 Imported by: ? 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.

README

extras/clipboard

Optional OS-native terminal.Clipboard provider for Kite with multi-MIME support (text/plain, text/html, image/png, image/jpeg, image/gif, image/webp, image/tiff, and text/uri-list).

By default, Kite uses terminal OSC 52 via engine.ClipboardProxy, which is SSH-safe but limited to text/plain. Local terminal applications that need rich clipboard formats (such as pasted images or HTML) can use engine.Options{Clipboard: clipboard.New()} when constructing the engine.

Usage

package main

import (
	"github.com/masterkeysrd/kite/element"
	"github.com/masterkeysrd/kite/event"
)

func setup(textarea *element.TextAreaElement, attachImage func(string, []byte)) {
	textarea.AddEventListener(event.EventPaste, func(e event.Event) {
		ce, ok := e.(*event.ClipboardEvent)
		if !ok {
			return
		}

		for mime, data := range ce.Items {
			if mime == "image/png" || mime == "image/jpeg" || mime == "image/gif" || mime == "image/webp" {
				ce.PreventDefault() // cancels textarea's default text insertion
				attachImage(mime, data)
				return
			}
		}
		// No PreventDefault(): textarea inserts ce.Text() normally.
	})
}

How It Works with Kite's Paste Pipeline

  1. KeyDown cancellation: Calling PreventDefault() on a Ctrl+V / Cmd+V KeyDown event aborts EventPaste.
  2. Pre-dispatch multi-MIME loading: Before dispatching EventPaste to the focused element, the engine calls ListFormats() and Read(mime) to populate ce.Items (and enriches RawBracketedPaste with any non-text/plain formats available).
  3. Cancelable default action: <input> and <textarea> insert ce.Text() as a post-dispatch default action only when !ce.DefaultPrevented().

Platform Support

OS Mechanism Supported Formats
macOS pbpaste / pbcopy + osascript (JXA NSPasteboard) text/plain, text/html, image/png (including public.tiff → PNG transcoding), image/jpeg, image/gif, image/webp, image/tiff, text/uri-list
Linux (Wayland) wl-paste / wl-copy All MIME types reported by wl-paste --list-types
Linux (X11) xclip (or xsel for plain text) All MIME types reported by xclip -t TARGETS
Windows PowerShell (System.Windows.Forms.Clipboard) text/plain, image/png (read), text/uri-list (listed)

Other platforms compile cleanly and return clipboard.ErrUnsupported.

Direct API

SystemClipboard implements terminal.Clipboard:

  • ListFormats() *promise.Promise[[]string]
  • Read(mime string) *promise.Promise[[]byte]
  • ReadText() *promise.Promise[string]
  • Write(mime string, data []byte) *promise.Promise[struct{}]
  • WriteText(text string) *promise.Promise[struct{}]

Documentation

Overview

Package clipboard provides a native clipboard for local Kite applications that need MIME formats beyond plain text. Install the provider through engine.Options{Clipboard: clipboard.New()}. Available formats depend on the host operating system and its clipboard utilities.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupported = errors.New("clipboard: unsupported platform or format")

ErrUnsupported is returned when the current platform or clipboard tool does not support the requested operation or MIME type.

Functions

This section is empty.

Types

type SystemClipboard

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

SystemClipboard implements terminal.Clipboard using the host OS clipboard, supporting multiple MIME types (e.g. "text/plain", "text/html", "image/png", "text/uri-list").

func New

func New() *SystemClipboard

New creates a new SystemClipboard backed by the current operating system's native clipboard utilities.

func (*SystemClipboard) ListFormats

func (c *SystemClipboard) ListFormats() *promise.Promise[[]string]

ListFormats returns the deduplicated list of MIME types currently available on the OS clipboard.

func (*SystemClipboard) Read

func (c *SystemClipboard) Read(mime string) *promise.Promise[[]byte]

Read retrieves the payload for the given MIME type from the OS clipboard.

func (*SystemClipboard) ReadText

func (c *SystemClipboard) ReadText() *promise.Promise[string]

ReadText returns the current "text/plain" content from the OS clipboard.

func (*SystemClipboard) Write

func (c *SystemClipboard) Write(mime string, data []byte) *promise.Promise[struct{}]

Write stores data for the given MIME type into the OS clipboard.

func (*SystemClipboard) WriteText

func (c *SystemClipboard) WriteText(text string) *promise.Promise[struct{}]

WriteText stores plain text into the OS clipboard.

Jump to

Keyboard shortcuts

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