From e13e3e027bd44371bb02854be50d22a38e6d2720 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Sat, 3 Apr 2021 11:26:28 +0900 Subject: [PATCH] include/clip: Add functions for clipboard manipulation This commit adds some convenience functions for reading from, writing to, and swapping X clipboards. --- include/clip.sh | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 include/clip.sh diff --git a/include/clip.sh b/include/clip.sh new file mode 100644 index 0000000..1af36f5 --- /dev/null +++ b/include/clip.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +__init() { + return 0 +} + +clip_get() { + local sel + + sel="$1" + + if ! xclip -selection "$sel" -o 2>/dev/null; then + return 1 + fi + + return 0 +} + +clip_get_any() { + if clip_get "primary"; then + return 0 + fi + + if clip_get "clipboard"; then + return 0 + fi + + if clip_get "secondary"; then + return 0 + fi + + return 1 +} + +clip_set() { + local sel + local data + + sel="$1" + data="$2" + + if (( $# < 2 )); then + data=$(/dev/null; then + return 1 + fi + + return 0 +} + +clip_swap() { + local left + local right + + local left_data + + left="$1" + right="$2" + + left_data=$(clip_get "$left") + clip_get "$right" | clip_set "$left" + clip_set "$right" <<< "$left_data" + + return 0 +} -- 2.47.3