-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrequent-words.sh
executable file
·78 lines (71 loc) · 1.9 KB
/
frequent-words.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
#!/bin/bash
source ~/Repositories/scripts/essential-functions
[[ -z "$usdd" ]] && return 1
fwf="$usdd/frequent-words"
[[ ! -e "$fwf" ]] && return 1
function output-slot {
IFS=$'\n'
contents=($(cat "$fwf" | sed 's/^$/EMPTYSLOT/g'))
echo {1..9} | grep -q "$1" || return 1
echo ' ' | grep -q "$1" && return 1
# pia = position in array
pia=$(($1-1))
echo "${contents[$pia]}" | sed 's/EMPTYSLOT//g' | tr -d '\n'
unset IFS
}
function copy-slot {
output-slot "$1" | xclip -selection clipboard
}
function mod-slot {
# $1 is slot number {1..9}, $2 is content to write to slot
# leave $2 empty for clearing the slot
# "EMPTYSLOT" is illegal $2 input
IFS=$'\n'
contents=($(cat "$fwf" | sed 's/^$/EMPTYSLOT/g'))
echo {1..9} | grep -q "$1" || return 1
echo ' ' | grep -q "$1" && return 1
[[ "$2" = "EMPTYSLOT" ]] && return 1
newcontent="$(echo "$2" | tr -d '\n')"
# pia = position in array
pia=$(($1-1))
for j in {0..8}
do
[[ "$j" -ne "$pia" ]] && {
[[ "${contents[$j]}" = "EMPTYSLOT" ]] && {
echo >> "$fwf"-temp
} || {
echo "${contents[$j]}" >> "$fwf"-temp
}
}
[[ "$j" -eq "$pia" ]] && {
echo "$newcontent" >> "$fwf"-temp
}
done
mv -f "$fwf"-temp "$fwf"
unset IFS
}
function view-slots {
cat "$fwf" \
| nl -ba -s ' ' \
| sed 's/^ *//g' \
| rofi -normalize-match -dmenu -i \
| cut -c3- \
| tr -d '\n' \
| xclip -selection clipboard
}
function clear-slots {
for j in {1..9}
do
mod-slot "$j"
done
}
case "$1" in
"view") view-slots ;;
"out") output-slot "$2" ;;
"copy") copy-slot "$2" ;;
"clear") clear-slots ;;
"mod") mod-slot "$2" "$3" ;;
'') view-slots ;;
*) echolor red ":: Unknown command"
exit 1 ;;
esac