-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyank_nou
executable file
·53 lines (44 loc) · 1.83 KB
/
yank_nou
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
#!/bin/bash
#
# SPDX-FileCopyrightText: 2020 Amerlyq <[email protected]> and contributors.
#
# SPDX-License-Identifier: MIT
#
#%SUMMARY: copy current title/url or use selected text as title
#%USAGE: :bind yv spawn --userscript yank_nou [prefix] [block]
#%OR(with-ia-args): :bind yV set-cmd-text -s :spawn --userscript yank_nou
#%DEBUG: $ QUTE_COUNT=2 QUTE_TITLE=aaa QUTE_URL=bbb ./yank_nou
set -o errexit -o noclobber -o noglob -o nounset -o pipefail
# NOTE: cache last indent count
cache=${TMPDIR:-${XDG_RUNTIME_DIR:?}}/qute.cfg
[[ ${QUTE_COUNT-} ]] || [[ ! -f $cache ]] || QUTE_COUNT=$(sed -n '/^[0-9]$/s//\0/p;q' "$cache")
# HACK: use count=10 to use no indent
count=$(( ${QUTE_COUNT:-0} % 10 ))
echo "$count" >| "$cache"
blk=${1-}
pfx=${2-}
# HACK: timestamp working as xref
# [_] TRY:FIXME: use link-open time instead of copy-time
# i.e. I could open link, read article, and same completed task with two timestamps
# [_] BET: parse qutebrowser history "last open time"
xtref=$(just xts new xts pin | tr -d $' \n')
## BAD: no easy keyboard-only way to remove accidental selections on page
# title=${QUTE_SELECTED_TEXT:-$QUTE_TITLE}
title=$QUTE_TITLE
url=$QUTE_URL
# USAGE: :hint --rapid links spawn --userscript yank_nou -- '{hint-url}'
if [[ $blk == -- ]]; then
url=$pfx
# https://unix.stackexchange.com/questions/103252/how-do-i-get-a-websites-title-using-command-line
# FAIL: don't work with PDF
title=$(curl -s -- "$url" | grep -o '<title>[^<]*' | tail -c+8)
blk=
pfx=
fi
title=$(printf '%s' "$title" | tr $'\n' $' ' | sed 's/^\s*//;s/\s*$//')
# SRC: https://stackoverflow.com/questions/296536/how-to-urlencode-data-for-curl-command
printf -v pretty_url '%b' "${url//%/\\x}"
exec xclip -selection clipboard <<EOT
${blk}$(printf "%$((count * 2))s")${pfx}${title}${xtref:+ $xtref}
${blk}$(printf "%$(((count + 1) * 2))s")$pretty_url
EOT