-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopypastas.sh
executable file
·110 lines (95 loc) · 3.24 KB
/
copypastas.sh
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
#!/bin/sh
# type: string file
# definition:
# "$TMPDIR/copypastas_$$"
# ^- temp dir ^- shell pid
tmpfile="${TMPDIR:-/tmp}/copypastas-sh_$$"
# type: pid file
# definition:
# "$tmpfile.termpid"
# ^ ^- termpid extension
# |- temp file name
export pidfile="${tmpfile}.termpid"
#initialize pidfile
:> "$pidfile"
trap 'rm -f -- $pidfile $tmpfile' EXIT
file_print() {
while read -r line; do
if ! printf '%s\n' "$line" | grep "TITLE=" >/dev/null; then
printf '%s\n' "$line"
fi
done < "$1"
}
find_alt() {
for i; do
command -v "${i%% *}" >/dev/null && {
printf '%s\n' "$i"
return 0
}
done
return 1
}
run_float_term() {
if [ -z "$FLOATING_TERMINAL" ]; then
# find_alt() prints the first parameter recognized by `command -v`
FLOATING_TERMINAL=$(find_alt 'foot -a CopyPaster' 'havoc ' 'alacritty --class=CopyPaster -e' \
'kitty --class=CopyPaster -e' 'konsole -e' 'gnome-terminal -e' 'termite -e' \
'st -c CopyPaster -e' 'uxterm -name CopyPaster -T CopyPaster -e')
fi
$FLOATING_TERMINAL "$@" &
TERM_PID=$!
printf '%s\n' "$TERM_PID" > "$pidfile"
}
#config file
CONFIG="${XDG_CONFIG_HOME:-${HOME}/.config}/copypastas-sh/configrc"
if [ -f "$CONFIG" ]; then
# load config
. "$CONFIG"
else
notify-send "${0##*/}: Error!" "${CONFIG} doesn't exist, example config will be copied"
if [ ! -d "${XDG_CONFIG_HOME:-${HOME}/.config}/copypastas-sh" ]; then
mkdir -p "${XDG_CONFIG_HOME:-${HOME}/.config}/copypastas-sh"
fi
cp examples-placeholder/configrc "${XDG_CONFIG_HOME:-${HOME}/.config}/copypastas-sh/"
# load config
. "$CONFIG"
fi
if [ -z "$PASTAS_DIR" ]; then
PASTAS_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/copypastas-sh"
fi
if [ -d "$PASTAS_DIR" ]; then
if [ $(find "$PASTAS_DIR" | wc -l) -eq 1 ]; then
notify-send "${0##*/}: error" "${PASTAS_DIR} empty, it will be populated"
cp examples-placeholder/gnu+linux "${PASTAS_DIR}/"
fi
cd "$PASTAS_DIR" || { printf '%s\n' "${0##*/}: could not cd into $PASTAS_DIR" >&2; exit 1; }
else
notify-send "${0##*/}: Error!" "${PASTAS_DIR} doesn't exist, it will be created and populated"
mkdir -p "$PASTAS_DIR"
cp examples-placeholder/gnu+linux "${PASTAS_DIR}/"
cd "$PASTAS_DIR" || { printf '%s\n' "${0##*/}: could not cd into $PASTAS_DIR" >&2; exit 1; }
fi
if [ -z "$FZF_PASTA_OPTS" ]; then
FZF_PASTA_OPTS="--layout=reverse --height 100% --header 'Copy Pastas' \
--cycle --preview-window sharp --preview-window 70% \
--prompt='filter: ' \
--bind alt-k:preview-up \
--bind alt-j:preview-down"
fi
export FZF_DEFAULT_OPTS="${FZF_PASTA_OPTS} ${FZF_PASTA_COLORS}"
eval $(run_float_term "fzf --preview '@lib@/pasta_preview {}' > $tmpfile") &
until [ -n "$(cat "$pidfile")" ]; do
sleep .1
done
RUNNING_TERM=$(cat "$pidfile")
while kill -0 "$RUNNING_TERM" 2>/dev/null; do
sleep 0.4
done
SELECTED_PASTA=$(cat "$tmpfile")
if [ -z "$SELECTED_PASTA" ]; then
notify-send "${0##*/}" "no file selected"
else
SELECTED_FILE=$(printf '%s/%s\n' "${PASTAS_DIR}" "${SELECTED_PASTA}")
file_print "$SELECTED_FILE" | xclip -selection clipboard
notify-send "${0##*/}" "${SELECTED_PASTA} copied to clipboard."
fi