Improve contact menu script

- Make it stopeabable with gesture or Esc
- Prefer printf over echo
master
Stacy Harper 3 years ago
parent af856d5bfa
commit f41bf2aee6
No known key found for this signature in database
GPG Key ID: EABA44759877E02A
  1. 107
      scripts/core/sxmo_contactmenu.sh

@ -3,34 +3,36 @@
# shellcheck source=scripts/core/sxmo_common.sh # shellcheck source=scripts/core/sxmo_common.sh
. "$(dirname "$0")/sxmo_common.sh" . "$(dirname "$0")/sxmo_common.sh"
set -e
newcontact() { newcontact() {
name="$(echo | sxmo_dmenu_with_kb.sh -p "$icon_usr Name")" name="$(printf "" | sxmo_dmenu_with_kb.sh -p "$icon_usr Name")"
number="$1" number="$1"
if [ -n "$number" ]; then if [ -n "$number" ]; then
number="$(valid_number "$number")" number="$(sxmo_validnumber.sh "$number")"
fi fi
while [ -z "$number" ]; do while [ -z "$number" ]; do
number="$(sxmo_contacts.sh --unknown | sxmo_dmenu_with_kb.sh -p "$icon_phl Number")" number="$(sxmo_contacts.sh --unknown | sxmo_dmenu_with_kb.sh -p "$icon_phl Number")"
number="$(valid_number "$number")" number="$(sxmo_validnumber.sh "$number")"
done done
PICKED="$number $name" # now act like if we picked this new contact PICKED="$number $name" # now act like if we picked this new contact
echo "$PICKED" >> "$CONTACTFILE" printf %s "$PICKED" >> "$CONTACTFILE"
} }
editcontactname() { editcontactname() {
oldnumber="$(echo "$1" | cut -d" " -f1)" oldnumber="$(printf %s "$1" | cut -d" " -f1)"
oldname="$(echo "$1" | cut -d" " -f2)" oldname="$(printf %s "$1" | cut -d" " -f2)"
ENTRIES="$(printf %b "Old name: $oldname")" ENTRIES="$(printf %b "Old name: $oldname")"
PICKED="$( PICKED="$(
echo "$ENTRIES" | printf %b "$ENTRIES" |
sxmo_dmenu_with_kb.sh -p "$icon_edt Edit Contact" sxmo_dmenu_with_kb.sh -p "$icon_edt Edit Contact"
)" )"
if ! echo "$PICKED" | grep -q "^Old name: "; then if ! printf %s "$PICKED" | grep -q "^Old name: "; then
newcontact="$oldnumber $PICKED" newcontact="$oldnumber $PICKED"
sed -i "s/^$1$/$newcontact/" "$CONTACTFILE" sed -i "s/^$1$/$newcontact/" "$CONTACTFILE"
set -- "$newcontact" set -- "$newcontact"
@ -40,17 +42,17 @@ editcontactname() {
} }
editcontactnumber() { editcontactnumber() {
oldnumber="$(echo "$1" | cut -d" " -f1)" oldnumber="$(printf %s "$1" | cut -d" " -f1)"
oldname="$(echo "$1" | cut -d" " -f2)" oldname="$(printf %s "$1" | cut -d" " -f2)"
ENTRIES="$(sxmo_contacts.sh --unknown | xargs -0 printf "%b (Old number)\n%b" "$oldnumber")" ENTRIES="$(sxmo_contacts.sh --unknown | xargs -0 printf "%b (Old number)\n%b" "$oldnumber")"
PICKED= # already used var name PICKED= # already used var name
while [ -z "$PICKED" ]; do while [ -z "$PICKED" ]; do
PICKED="$( PICKED="$(
echo "$ENTRIES" | printf %b "$ENTRIES" |
sxmo_dmenu_with_kb.sh -p "$icon_edt Edit Contact" sxmo_dmenu_with_kb.sh -p "$icon_edt Edit Contact"
)" )"
if echo "$PICKED" | grep -q "(Old number)$"; then if printf %s "$PICKED" | grep -q "(Old number)$"; then
editcontact "$1" editcontact "$1"
return return
fi fi
@ -63,77 +65,88 @@ editcontactnumber() {
} }
deletecontact() { deletecontact() {
name="$(echo "$1" | cut -d" " -f2)" name="$(printf %s "$1" | cut -d" " -f2)"
# shellcheck disable=SC2059 # shellcheck disable=SC2059
ENTRIES="$(printf "$icon_cls No\n$icon_chk Yes")" ENTRIES="$(printf "$icon_cls No\n$icon_chk Yes")"
PICKED="$( PICKED="$(
echo "$ENTRIES" | printf %b "$ENTRIES" |
dmenu -p "$icon_del Delete $name ?" dmenu -p "$icon_del Delete $name ?"
)" )"
echo "$PICKED" | grep -q "Yes" && sed -i "/^$1$/d" "$CONTACTFILE" printf %s "$PICKED" | grep -q "Yes" && sed -i "/^$1$/d" "$CONTACTFILE"
} }
editcontact() { editcontact() {
number="$(echo "$1" | cut -d" " -f1)" number="$(printf %s "$1" | cut -d" " -f1)"
name="$(echo "$1" | cut -d" " -f2)" name="$(printf %s "$1" | cut -d" " -f2)"
ENTRIES="$(printf %b "$icon_ret Cancel\n$icon_usr Name: $name\n$icon_phl Number: $number")" ENTRIES="$(printf %b "$icon_ret Cancel\n$icon_usr Name: $name\n$icon_phl Number: $number")"
PICKED="$( PICKED="$(
echo "$ENTRIES" | printf %b "$ENTRIES" |
dmenu -p "$icon_edt Edit Contact" dmenu -p "$icon_edt Edit Contact"
)" )"
if echo "$PICKED" | grep -q "Name: "; then case "$PICKED" in
editcontactname "$1" *"Name: "*)
elif echo "$PICKED" | grep -q "Number: "; then editcontactname "$1"
editcontactnumber "$1" ;;
else *"Number: "*)
showcontact "$1" editcontactnumber "$1"
fi ;;
*)
showcontact "$1"
;;
esac
} }
showcontact() { showcontact() {
number="$(echo "$1" | cut -d" " -f1)" number="$(printf %s "$1" | cut -d" " -f1)"
name="$(echo "$1" | cut -d" " -f2)" name="$(printf %s "$1" | cut -d" " -f2)"
ENTRIES="$(printf %b "$icon_ret Cancel\n$icon_lst List Messages\n$icon_msg Send a Message\n$icon_phn Call\n$icon_edt Edit\n$icon_del Delete")" ENTRIES="$(printf %b "$icon_ret Cancel\n$icon_lst List Messages\n$icon_msg Send a Message\n$icon_phn Call\n$icon_edt Edit\n$icon_del Delete")"
PICKED="$( PICKED="$(
echo "$ENTRIES" | printf %b "$ENTRIES" |
dmenu -p "$icon_usr $name" dmenu -p "$icon_usr $name"
)" )"
if echo "$PICKED" | grep -q "List Messages"; then case "$PICKED" in
sxmo_modemtext.sh tailtextlog "$number" *"List Messages")
exit sxmo_modemtext.sh tailtextlog "$number"
elif echo "$PICKED" | grep -q "Send a Message"; then exit
sxmo_modemtext.sh sendtextmenu "$number" ;;
exit *"Send a Message")
elif echo "$PICKED" | grep -q "Call"; then sxmo_modemtext.sh sendtextmenu "$number"
sxmo_modemdial.sh "$number" exit
exit ;;
elif echo "$PICKED" | grep -q "Edit"; then *"Call")
editcontact "$1" sxmo_modemdial.sh "$number"
elif echo "$PICKED" | grep -q "Delete"; then exit
deletecontact "$1" || showcontact "$1" ;;
fi *"Edit")
editcontact "$1"
;;
*"Delete")
deletecontact "$1" || showcontact "$1"
;;
esac
} }
main() { main() {
while true; do while true; do
CONTACTS="$(sxmo_contacts.sh --all)" CONTACTS="$(sxmo_contacts.sh --all)"
ENTRIES="$(echo "$CONTACTS" | xargs -0 printf "$icon_ret Close Menu\n$icon_pls New Contact\n%s")" ENTRIES="$(printf %b "$CONTACTS" | xargs -0 printf "$icon_ret Close Menu\n$icon_pls New Contact\n%s")"
PICKED="$( PICKED="$(
echo "$ENTRIES" | printf %b "$ENTRIES" |
sxmo_dmenu_with_kb.sh -i -p "$icon_lst Contacts" sxmo_dmenu_with_kb.sh -i -p "$icon_lst Contacts"
)" )"
echo "$PICKED" | grep -q "Close Menu" && exit printf %s "$PICKED" | grep -q "Close Menu" && exit
echo "$PICKED" | grep -q "New Contact" && newcontact printf %s "$PICKED" | grep -q "New Contact" && newcontact
showcontact "$(echo "$PICKED" | sed 's/: /\t/g')" showcontact "$(printf %s "$PICKED" | sed 's/: /\t/g')"
done done
} }

Loading…
Cancel
Save