snips.sh

 1#!/usr/bin/env bash
 2#MENU OPTIONS
 3#suspend
 4suspend() {
 5  loginctl suspend
 6}
 7
 8#quit
 9quit() {
10  awesome-client "awesome.quit()"
11}
12
13#restart awesome
14restart() {
15  awesome-client "awesome.restart()"
16}
17#END MENU OPTIONS
18
19OPTIONS=$(cat $0 | awk "$(cat <<EOF
20/^#MENU OPTIONS/ { declaration=1; next }
21/^#END MENU OPTIONS/ { declaration=0; next}
22/^#/ { desc=\$0; next } 
23/^[a-zA-Z0-9_]+[[:space:]]*\(\)/ { 
24  if (declaration == 1) {
25    gsub("\\\\(\\\\) {","",\$0); 
26    print desc "\t" \$0 
27  }
28}
29EOF
30)")
31
32run_action() {
33  local action="$1"
34  local func=$(echo "$OPTIONS" | awk -F"\t" -v desc="$action" '$1 == "#"desc { print $2 }')
35  if [[ -n "$func" ]]; then
36    "$func"
37  else
38    echo "No function found for description: $action"
39  fi
40}
41
42IFS=$'\n' read -d '' -ra OPTIONS_MAP <<< $(echo "$OPTIONS" | awk -F"\t" '{ print substr($1,2)}')
43# USES DMENU with center patch!
44action=$(printf "%s\n" "${OPTIONS_MAP[@]}" | dmenu -c -l ${#OPTIONS_MAP[@]}) || exit
45run_action "$action"
46