Преглед изворни кода

Rewrite charmenu.sh (docstring and style improvements)

Alois Mahdal пре 6 година
родитељ
комит
033dba5083
1 измењених фајлова са 74 додато и 18 уклоњено
  1. 74
    18
      src/include-bash/charmenu.sh

+ 74
- 18
src/include-bash/charmenu.sh Прегледај датотеку

@@ -8,31 +8,88 @@ charmenu() {
8 8
     #
9 9
     # Present "1-char-menu" (but actually N-char) or its parts
10 10
     #
11
-    # Read file in CHARMENU_FILE or given as -f parameter,
12
-    # and do what either `full` menu, `prompt`, or just terse
13
-    # list of `items` (like "y,n,q,?").
11
+    # Usage:
12
+    #     CHARMENU_FILE=/path/to/file
13
+    #     charmenu [options] full [ARG]...
14
+    #     charmenu [options] items
15
+    #     charmenu [options] prompt
16
+    #     charmenu [options] has ITEM
17
+    #
18
+    # Read file in $CHARMENU_FILE and display either `full`
19
+    # menu, `items` menu (e.g. "y,n,q,?") or just `prompt`.
20
+    #
21
+    # Options:
22
+    #
23
+    #     -f FILE, --file FILE   Read contents of menu freom FILE
24
+    #         instead of variable `$CHARMENU_FILE`.
25
+    #
26
+    #     -p PROMPT, --prompt PROMPT   Set prompt string to PROMPT.
27
+    #         If PROMPT contains '%s', it will be replaced by 'items'
28
+    #         menu (eg. "y,n,q").  Note that you most probably want
29
+    #         to include space as last character.  Default: "What
30
+    #         next [%s]? ".
31
+    #
32
+    #     -i, --ignore-case   Ignore case of user reply.  Default:
33
+    #         case is respected.
34
+    #
35
+    #     -d DLM, --delimiter DLM   Use delimiter DLM when printing
36
+    #         'items' menu.  Default: "," (comma).
14 37
     #
15 38
     # Format of menu file is one item per line, where item is
16 39
     # delimited from its description by single colon. Leading
17 40
     # and trailing spaces and spaces around the colon are
18 41
     # stripped.
19 42
     #
43
+    # Simple example menu file:
44
+    #
45
+    #     a: abort
46
+    #     r: retry
47
+    #     f: fail
48
+    #
20 49
     # You can use printf symbols like `%s` in menu and then
21 50
     # call `charmenu full arg1 arg2...` to have your arguments
22
-    # expanded in the full menu.
51
+    # expanded in the full menu.  More fleshed out example:
52
+    #
53
+    #     . "$(shellfu-get path)" || exit 3
54
+    #     shellfu import charmenu
55
+    #     shellfu import pretty
23 56
     #
24
-    # Similarly, you can specify single `%s` in the prompt
25
-    # parameter like `charmenu -p "choose from %s" and it will
26
-    # be replaced by list of items read from menu file,
27
-    # delimited either by comma or character provided as `-d`
28
-    # parameter.
57
+    #     cat <<EOM >menu
58
+    #     r: re-run test %s
59
+    #     c: clean screen
60
+    #     d: download results to %s
61
+    #     q: quit
62
+    #     EOM
29 63
     #
30
-    # `has` checks if given string is a valid menu item.
64
+    #     CHARMENU_FILE=menu
65
+    #     running=true
66
+    #     while $running; do
67
+    #         charmenu full "mytest" "some storage"
68
+    #         read -r response
69
+    #         case $response in
70
+    #             r)  rerun_test ;;
71
+    #             c)  reset ;;
72
+    #             d)  download_results ;;
73
+    #             q)  running=false ;;
74
+    #             *)  warn "invalid response: $response"
75
+    #         esac
76
+    #     done
77
+    #     clean_up
31 78
     #
32
-    local mfile="$CHARMENU_FILE"
33
-    local prompt="What to do next [%s]? "
34
-    local delim=","
35
-    local cmd cases
79
+    # `charmenu has ITEM` exits with zero if ITEM is a valid
80
+    # menu item.  With abort/retry/fail example above, calling
81
+    # `if charmenu has a; echo OK; fi` would print "OK"..
82
+    #
83
+    local mfile     # path to menu file
84
+    local prompt    # prompt text
85
+    local delim     # delimiter (in 'items' menu)
86
+    local cmd       # command do execute
87
+    local cases     # "-i" in case-insensitive mode
88
+    local menu      # loaded menu text
89
+    local alist     # loaded list of items
90
+    mfile=$CHARMENU_FILE
91
+    prompt="What next [%s]? "
92
+    delim=,
36 93
     while true; do case $1 in
37 94
         -d|--delimiter)         delim="$2";     shift 2 || return 2 ;;
38 95
         -f|--file)              mfile="$2";     shift 2 || return 2 ;;
@@ -44,11 +101,10 @@ charmenu() {
44 101
                     "prompt [PRINTF_FMT]"  \
45 102
                     "has ITEM"                                      ;;
46 103
     esac done
47
-
48
-    local menu="$(cat "$mfile")"
49
-    local alist="$(echo "$menu" | cut -d: -f1 | sed -re 's/^ +//; s/ +$//')"
104
+    menu="$(cat "$mfile")"
105
+    alist="$(echo "$menu" | cut -d: -f1 | sed -re 's/^ +//; s/ +$//')"
50 106
     debug -v alist
51
-
107
+    #shellcheck disable=SC2059
52 108
     case $cmd in
53 109
         full)   # print the full menu, with any printf notation expanded
54 110
             printf "$menu\n" "$@"