aboutsummaryrefslogtreecommitdiff
path: root/operators/callback_short.zsh
blob: a48a27cebdfc52472a49787b376054f743469d13 (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
# Copyright (C) 2024-2026 Maria Lisina
# Copyright (C) 2024-2026 Danil Lisin
# SPDX-License-Identifier: Apache-2.0

until mkdir "${user_config}_shorts.lock"
do
    sleep 1
done

if [[ -n "${1}" ]]
then
    short_query="${@}"
    shift ${#}
else
    notification_text="You must specify query"

    rmdir "${user_config}_shorts.lock"
    return
fi

shorts_config="${user_config}/shorts"

if ! mkdir -p "${shorts_config}"
then
    notification_text="Failed to create user config"

    rmdir "${user_config}_shorts.lock"
    return
fi

short_hash="$(sha1sum <<< "${short_query}" | cut -d ' ' -f 1)"
short="${shorts_config}/${short_hash}"
shorts=($(ls -1 "${shorts_config}"))

if [[ -f "${short}" ]]
then
    rm -f "${short}"
    notification_text="Removed shortcut"
elif [[ ${#shorts} -le ${shorts_limit} ]]
then
    printf "%s\n" "${short_query}" > "${short}"
    notification_text="Saved shortcut"
else
    notification_text="Too many shortcuts"
fi

shorts=($(ls -1 "${shorts_config}"))

if [[ ${#shorts} -eq 0 ]]
then
    rmdir "${shorts_config}"
fi

rmdir "${user_config}_shorts.lock"