summaryrefslogtreecommitdiff
path: root/polybar/scripts/clipboard-menu.sh
blob: 10fe648634cf236acba4dfb74da29444e0618360 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env bash
set -euo pipefail

state_dir="${XDG_CACHE_HOME:-$HOME/.cache}"
mkdir -p "$state_dir" 2>/dev/null || true
if [ ! -w "$state_dir" ]; then
  state_dir="/tmp"
fi
history_file="$state_dir/polybar-clipboard-history"

export DISPLAY="${DISPLAY:-:0}"
if [ -z "${XAUTHORITY:-}" ] && [ -r "$HOME/.Xauthority" ]; then
  export XAUTHORITY="$HOME/.Xauthority"
fi

current=""
if command -v xclip >/dev/null 2>&1; then
  current="$(xclip -selection clipboard -o 2>/dev/null || true)"
elif command -v xsel >/dev/null 2>&1; then
  current="$(xsel --clipboard --output 2>/dev/null || true)"
fi

if [ -n "$current" ]; then
  one_line="$(printf '%s' "$current" | tr '\n' ' ' | cut -c 1-180)"
  if ! grep -Fxq "$one_line" "$history_file" 2>/dev/null; then
    tmp="${history_file}.tmp"
    { printf '%s\n' "$one_line"; cat "$history_file" 2>/dev/null; } | awk 'NF && !seen[$0]++' | head -n 50 >"$tmp"
    mv "$tmp" "$history_file"
  fi
fi

choice="$(
  dmenu -i -p clipboard \
    -fn "FiraCode Nerd Font-14" \
    -nb "#282828" -nf "#ebdbb2" \
    -sb "#d79921" -sf "#282828" <"$history_file"
)"

[ -n "$choice" ] || exit 0

if command -v xclip >/dev/null 2>&1; then
  printf '%s' "$choice" | xclip -selection clipboard
elif command -v xsel >/dev/null 2>&1; then
  printf '%s' "$choice" | xsel --clipboard --input
fi