aboutsummaryrefslogtreecommitdiff
path: root/clip-capture
blob: 5184430a2f2a47a38b4f69bbccbfade6360fa289 (plain) (blame)
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

OUTFILE="${OUTFILE:-$HOME/docs/vimwiki/inbox/clips.md}"
HAS_NOTIFY=$(command -v notify-send >/dev/null 2>&1 && echo 1 || echo 0)

notify() {
  [[ "$HAS_NOTIFY" == 1 ]] && notify-send -u "$1" "$2" "$3"
}

die() {
  notify critical "capture-clip" "$1"
  printf 'ERROR: %s\n' "$1" >&2
  exit 1
}

get_clipboard() {
  local out
  if command -v xclip >/dev/null 2>&1; then
    out="$(xclip -o -selection primary 2>/dev/null)"
    [[ -z "$out" ]] && out="$(xclip -o -selection clipboard 2>/dev/null)"
    printf '%s' "$out"; return
  fi
  if command -v xsel >/dev/null 2>&1; then
    out="$(xsel --primary --output 2>/dev/null)"
    [[ -z "$out" ]] && out="$(xsel --clipboard --output 2>/dev/null)"
    printf '%s' "$out"; return
  fi
  die "need xclip or xsel"
}

clip="$(get_clipboard)"
[[ -n "$clip" ]] || die "clipboard is empty"

ts="$(date '+%Y-%m-%d %H:%M:%S')"
{
  printf '## captured %s\n' "$ts"
  printf '%s\n' "$clip"
  printf -- '\n'
} >> "$OUTFILE"

lines="$(printf '%s\n' "$clip" | wc -l | awk '{print $1}')"
chars="${#clip}"

notify normal "capture-clip" "Saved to vimwiki/inbox/clips.md ($lines lines, $chars chars)"