summaryrefslogtreecommitdiff
path: root/conf.d/99-pastebin.zsh
blob: 42a72cdd85f58d220938d8cbdc31dd336f618f1e (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
: ${PB_INSTANCE:=ptpb.pw}
: ${PB_CURL_ARGS:=""}

PASTE_LOG_DIR="$HOME/.local/pb/$PB_INSTANCE"
local LIVE="$PASTE_LOG_DIR/live"
local DEAD="$PASTE_LOG_DIR/dead"

mkdir -p "$LIVE" "$DEAD"

function pbupload() {
  local res

  if [ "$#" -eq 0 ]; then
    # paste from stdin
    res=$(curl $PB_CURL_ARGS -sF c=@- "https://$PB_INSTANCE$PB_URL_PATH")
  else
    res=$(curl $PB_CURL_ARGS -sF "c=@$1" "https://$PB_INSTANCE$PB_URL_PATH")
  fi

  local full_name=$(awk '/url/ { print $2 }' <<<"$res" | tr -d '\n' | sed 's@.*://.*/\(.*\)@\1@')
  local name="${full_name%%.*}"
  if [ -z "$name" ]; then
    echo "$res" 1>&2
    exit 1
  fi

  if [ ! -e "$LIVE/$name" ]; then
    echo "$res" > "$LIVE/$name"
  fi
  echo "$name"
}

local function pbexists() {
  [ -e "$LIVE/$1" ]
}

local function pbget() {
  local short="$1"
  local attr="$2"
  awk "/$attr/ { print \$2 }" < "$LIVE/$short" | tr -d '\n'
}

local function pbclip() {
  local name="$1"
  local url=$(sed 's@.*://\(.*\)@\1@' <<<"$2")
  if [[ -v DISPLAY ]]; then
    xclip -i <<<"$url"
    notify-send "$name" "Copied to clipboard: $url"
  fi
}

# pb upload
function pbu() {
  local short=$(pbupload "$@")

  local url=$(pbget "$short" "url")
  pbclip "pbu" "$url"
}

# pb change
function pbc() {
  local short=$1
  local file=$2
  local uuid=$(pbget "$short" "uuid")

  if [ "$#" -eq 1 ]; then
    # update from stdin
    curl $PB_CURL_ARGS -X PUT -F c=@- "https://$PB_INSTANCE/$uuid"
  else
    curl $PB_CURL_ARGS -X PUT -F "c=@$file" "https://$PB_INSTANCE/$uuid"
  fi
}

# pb list
function pbl() {
  find "$LIVE" -type f -exec basename {} \;
}

# pb delete, takes short id
# someone please fix this
function pbd() {
  local uuid=$(pbget "$1" "uuid")
  curl $PB_CURL_ARGS -X DELETE "https://$PB_INSTANCE/$uuid"
  mv "$LIVE/$1" "$DEAD/"
}

# pb delete all
function pbdall() {
  for p in $(pbl); pbd $p
}

# pb shorten
function pbs() {
  local name=$(PB_URL_PATH="/u" pbupload)
  local url=$(pbget "$name" "url")
  pbclip "pbs" "$url"
}

# pb vanity
function pbv() {
  local name="$1"
  shift

  pbexists "~$1" && pbd "~$1"
  local short=$(PB_URL_PATH="/~$1" pbupload "$@")
  local url=$(pbget "$short" "url")
  pbclip "pbv" "$url"
}

# pb image
function pbi() {
  local tmp=$(mktemp '/tmp/XXXXXXXX')
  scrot --select --quality 0 $tmp.png
  pngcrush -ow $tmp.png

  # Remove .png suffix, for shorter URL
  # Relies on sending the correct mimetype
  local short=$(pbupload $tmp.png)
  local url=$(pbget "$short" "url")
  pbclip "pbi" "$(echo $url | sed 's@\(.*\)\.png@\1@')"
}

# pb record
function pbr() {
  local tmp=$(mktemp '/tmp/XXXXXXXX')
  asciinema rec $tmp
  
  local short=$(pbupload $tmp)
  pbclip "pbr" "$PB_INSTANCE/t/$short"
}

function termbin() {
  nc termbin.com 9999
}