]> git.corax.cc Git - toolbox/commitdiff
include/clip: Add functions for clipboard manipulation
authorMatthias Kruk <m@m10k.eu>
Sat, 3 Apr 2021 02:26:28 +0000 (11:26 +0900)
committerMatthias Kruk <m@m10k.eu>
Sat, 3 Apr 2021 02:26:28 +0000 (11:26 +0900)
This commit adds some convenience functions for reading from, writing to,
and swapping X clipboards.

include/clip.sh [new file with mode: 0644]

diff --git a/include/clip.sh b/include/clip.sh
new file mode 100644 (file)
index 0000000..1af36f5
--- /dev/null
@@ -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/stdin)
+       fi
+
+       if ! printf "$data" | xclip -selection "$sel" 2>/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
+}