~mil/sxmo-utils

ref: f9b96d2d28713714f0837ae988a8fe827a4c7805 sxmo-utils/scripts/core/sxmo_contactmenu.sh -rwxr-xr-x 2.9 KiB
f9b96d2dStacy Harper Update modem status state on errors 9 months ago
                                                                                
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
#!/usr/bin/env sh

# shellcheck source=scripts/core/sxmo_common.sh
. "$(dirname "$0")/sxmo_common.sh"

newcontact() {
	name="$(echo | sxmo_dmenu_with_kb.sh -c -l 2 -p "$icon_usr Name")"
	number="$(echo | sxmo_dmenu_with_kb.sh -c -l 2 -p "$icon_phl Number")"

	PICKED="$number	$name" # now act like if we picked this new contact
	echo "$PICKED" >> "$CONTACTFILE"
}

editcontactname() {
	oldnumber="$(echo "$1" | cut -d"	" -f1)"
	oldname="$(echo "$1" | cut -d"	" -f2)"

	ENTRIES="$(printf %b "Old name: $oldname")"
	PICKED="$(
		echo "$ENTRIES" |
		sxmo_dmenu_with_kb.sh -c -l 3 -p "$icon_edt Edit Contact"
	)"

	if ! echo "$PICKED" | grep -q "^Old name: "; then
		newcontact="$oldnumber	$PICKED"
		sed -i "s/^$1$/$newcontact/" "$CONTACTFILE"
		set -- "$newcontact"
	fi

	editcontact "$1"
}

editcontactnumber() {
	oldnumber="$(echo "$1" | cut -d"	" -f1)"
	oldname="$(echo "$1" | cut -d"	" -f2)"

	ENTRIES="$(printf %b "Old number: $oldnumber")"
	PICKED="$(
		echo "$ENTRIES" |
		sxmo_dmenu_with_kb.sh -c -l 3 -p "$icon_edt Edit Contact"
	)"

	if ! echo "$PICKED" | grep -q "^Old number: "; then
		newcontact="$PICKED	$oldname"
		sed -i "s/^$1$/$newcontact/" "$CONTACTFILE"
		set -- "$newcontact"
	fi

	editcontact "$1"
}

deletecontact() {
	name="$(echo "$1" | cut -d"	" -f2)"

	ENTRIES="$(printf "$icon_cls No\n$icon_chk Yes")"
	PICKED="$(
		echo "$ENTRIES" |
		dmenu -c -l 3 -p "$icon_del Delete $name ?"
	)"

	echo "$PICKED" | grep -q "Yes" && sed -i "/^$1$/d" "$CONTACTFILE"
}

editcontact() {
	number="$(echo "$1" | cut -d"	" -f1)"
	name="$(echo "$1" | cut -d"	" -f2)"
	ENTRIES="$(printf %b "$icon_ret Cancel\n$icon_usr Name: $name\n$icon_phl Number: $number")"

	PICKED="$(
		echo "$ENTRIES" |
		dmenu -c -l 4 -p "$icon_edt Edit Contact"
	)"

	if echo "$PICKED" | grep -q "Name: "; then
		editcontactname "$1"
	elif echo "$PICKED" | grep -q "Number: "; then
		editcontactnumber "$1"
	else
		showcontact "$1"
	fi
}

showcontact() {
	number="$(echo "$1" | cut -d"	" -f1)"
	name="$(echo "$1" | cut -d"	" -f2)"
	ENTRIES="$(printf %b "$icon_ret Cancel\n$icon_msg Send a Message\n$icon_phn Call\n$icon_edt Edit\n$icon_del Delete")"

	PICKED="$(
		echo "$ENTRIES" |
		dmenu -c -l 5 -p "$icon_usr $name"
	)"

	if echo "$PICKED" | grep -q "Send a Message"; then
		sxmo_modemtext.sh sendtextmenu  "$number"
		exit
	elif echo "$PICKED" | grep -q "Call"; then
		sxmo_modemdial.sh "$number"
		exit
	elif echo "$PICKED" | grep -q "Edit"; then
		editcontact "$1"
	elif echo "$PICKED" | grep -q "Delete"; then
		deletecontact "$1" || showcontact "$1"
	fi
}

main() {
	while true; do
		CONTACTS="$(sed 's/\t/: /g' "$CONTACTFILE")"
		ENTRIES="$(echo "$CONTACTS" | xargs -0 printf "$icon_ret Close Menu\n$icon_pls New Contact\n%s")"

		PICKED="$(
			echo "$ENTRIES" |
			sxmo_dmenu_with_kb.sh -i -c -l 10 -p "$icon_lst Contacts"
		)"

		echo "$PICKED" | grep -q "Close Menu" && exit
		echo "$PICKED" | grep -q "New Contact" && newcontact

		showcontact "$(echo "$PICKED" | sed 's/: /\t/g')"
	done
}

main