From: Matthias Kruk Date: Sat, 3 Apr 2021 02:26:28 +0000 (+0900) Subject: include/clip: Add functions for clipboard manipulation X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=e13e3e027bd44371bb02854be50d22a38e6d2720;p=toolbox include/clip: Add functions for clipboard manipulation This commit adds some convenience functions for reading from, writing to, and swapping X clipboards. --- 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 +}