feat(clipboard): keep an in-memory history of copied text

This commit is contained in:
BlackAngelTV committed 2026-11-11 21:05:30 +01:00
1 parent 02f43b2ee9
commit 10981dcf00
3 files changed
+162 -4

No files matched your search

+39
View File
@@ -40,6 +40,7 @@ from .clipboard import (
set_clipboard_string,
set_primary_selection,
)
from .clipboard_history import ClipboardHistory
from .colors import ColorSchemes, theme_colors
from .conf.utils import BadLine, KeyAction, to_cmdline
from .config import common_opts_as_dict, prepare_config_file_for_editing, store_effective_config
@@ -419,6 +420,7 @@ class Boss:
self.atexit = Atexit()
set_layout_options(opts)
self.clipboard = Clipboard()
self.clipboard_history = ClipboardHistory()
self.window_for_dispatch: Window | None = None
self.primary_selection = Clipboard(ClipboardType.primary_selection)
self.update_check_started = False
@@ -3159,6 +3161,43 @@ class Boss:
if text:
w.paste_with_actions(text)
def _clipboard_history_preview(self, text: str) -> str:
# The picker shows one line per entry, so flatten newlines and keep the line short
q = ' '.join(text.split())
return q if len(q) <= 120 else q[:117] + '…'
def _choose_from_clipboard_history(self, entries: tuple[str, ...]) -> None:
if not entries:
self.ring_bell_if_allowed()
return
previews = [(text, self._clipboard_history_preview(text) or _('(blank)')) for text in entries]
def chosen(text: str | None) -> None:
if text:
self.paste_to_active_window(text)
self.choose_entry(_('Clipboard history'), previews, chosen)
@ac('cp', 'Paste an entry from the clipboard history. See :ref:`clipboard_history`.')
def paste_from_clipboard_history(self) -> None:
self._choose_from_clipboard_history(self.clipboard_history.entries())
@ac('cp', 'Search the clipboard history and paste the selected entry. See :ref:`clipboard_history`.')
def search_clipboard_history(self) -> None:
if not self.clipboard_history:
self.ring_bell_if_allowed()
return
def query_entered(query: str) -> None:
self._choose_from_clipboard_history(self.clipboard_history.search(query))
self.get_line(
_('Search the clipboard history. Leave empty to list everything.'),
query_entered,
prompt=_('Search: '),
window_title=_('Search clipboard history'),
)
def current_primary_selection(self) -> str:
return get_primary_selection() if supports_primary_selection else ''