Browse Source

Add new 'menu' sub-command

New command allows to define flexible menus in INI files.

Single INI root now contains all menu definitons.  Each menu consists of
item generator, selector and consumer, which are checked and chained
together.

Alternatively to specifying a generator, items can be listed right in
the INI section.
Alois Mahdal 9 years ago
parent
commit
d4b9abeaaf
2 changed files with 139 additions and 0 deletions
  1. 1
    0
      mkit/config.ini
  2. 138
    0
      src/libexec/saturnin-menu

+ 1
- 0
mkit/config.ini View File

@@ -52,6 +52,7 @@
52 52
     src/libexec/saturnin-iam                = saturnin-iam
53 53
     src/libexec/saturnin-kb                 = saturnin-kb
54 54
     src/libexec/saturnin-ln                 = saturnin-ln
55
+    src/libexec/saturnin-menu               = saturnin-menu
55 56
     src/libexec/saturnin-www                = saturnin-www
56 57
 
57 58
 [files:share]

+ 138
- 0
src/libexec/saturnin-menu View File

@@ -0,0 +1,138 @@
1
+#!/bin/bash
2
+
3
+. $(ffoom path)
4
+
5
+ffoo import inigrep
6
+ffoo import pretty
7
+
8
+usage() {
9
+    mkusage "[name]"
10
+}
11
+
12
+SATURNIN_MENU_MNAME=""
13
+SATURNIN_MENU_GENERATOR=""
14
+SATURNIN_MENU_SELECTOR=""
15
+SATURNIN_MENU_SELECT_ARGS=""
16
+SATURNIN_MENU_CONSUMER=""
17
+
18
+load_parts() {
19
+    #
20
+    # Load mandatory parts
21
+    #
22
+    local mname
23
+    mname=$(get_mname) || usage
24
+    inigrep -s menu.$mname | grep -q . \
25
+     || die "no such menu: $mname"
26
+    SATURNIN_MENU_GENERATOR="$(get_generator)"  \
27
+     || die "cannot find generator for menu: $mname"
28
+    SATURNIN_MENU_SELECTOR="$(get_selector)" \
29
+     || die "cannot find selector for menu: $mname"
30
+    SATURNIN_MENU_SELECT_ARGS="$(get_select_args)"
31
+    SATURNIN_MENU_CONSUMER="$(get_consumer)" \
32
+     || die "cannot find consumer for menu: $mname"
33
+}
34
+
35
+get_mname() {
36
+    #
37
+    # If not set, take from global INI default
38
+    #
39
+    test -n "$SATURNIN_MENU_MNAME" && {
40
+        printf '%s' "$SATURNIN_MENU_MNAME"
41
+        return 0
42
+    }
43
+    inigrep -1 -p menu._default_.name | grep .
44
+}
45
+
46
+get_generator() {
47
+    #
48
+    # Obtain and echo generator for menu $mname
49
+    #
50
+    # First, look for 'generator' key in menu definition,
51
+    # then for items as 'item' keys at the same place,
52
+    # finally default 'generator'.
53
+    #
54
+    inigrep -1 -p menu.$mname.generator | grep . \
55
+     && return 0
56
+    inigrep -p menu.$mname.item | grep -m 1 -q . \
57
+     && printf "inigrep -p menu.$mname.item" \
58
+     && return 0
59
+    inigrep -1 -p menu._default_.generator | grep . \
60
+     && return 0
61
+    return 1
62
+}
63
+
64
+get_selector() {
65
+    #
66
+    # Obtain and echo selector for menu $mname
67
+    #
68
+    # First, look for 'selector' key in menu definition,
69
+    # then a default 'selector'.
70
+    #
71
+    inigrep -1 -p menu.$mname.selector | grep . \
72
+     && return 0
73
+    inigrep -1 -p menu._default_.selector | grep .
74
+}
75
+
76
+get_select_args() {
77
+    #
78
+    # Obtain and echo selector args for menu $mname
79
+    #
80
+    # First, look for 'select_args' key in menu definition,
81
+    # then a default 'select_args'.
82
+    #
83
+    inigrep -1 -p menu.$mname.select_args | grep . \
84
+     && return 0
85
+    inigrep -1 -p menu._default_.select_args
86
+}
87
+
88
+get_consumer() {
89
+    #
90
+    # Obtain and echo consumer args for menu $mname
91
+    #
92
+    # First, look for 'consumer' key in menu definition,
93
+    # then a default 'consumer'.
94
+    #
95
+    inigrep -1 -p menu.$mname.consumer | grep . \
96
+     && return 0
97
+    inigrep -1 -p menu._default_.consumer | grep .
98
+}
99
+
100
+do_generate() {
101
+    #
102
+    # Generate menu items
103
+    #
104
+    bash -n <<<"$SATURNIN_MENU_GENERATOR" \
105
+     || die "syntax errors in generator: $SATURNIN_MENU_GENERATOR"
106
+    eval "$SATURNIN_MENU_GENERATOR"
107
+}
108
+
109
+do_select() {
110
+    #
111
+    # Bring up selection UI
112
+    #
113
+    bash -n <<<"$SATURNIN_MENU_SELECTOR" \
114
+     || die "syntax errors in selector: $SATURNIN_MENU_SELECTOR"
115
+    eval "$SATURNIN_MENU_SELECTOR $SATURNIN_MENU_SELECT_ARGS"
116
+}
117
+
118
+
119
+do_consume() {
120
+    #
121
+    # Process the selected item(s)
122
+    #
123
+    bash -n <<<"$SATURNIN_MENU_CONSUMER" \
124
+     || die "syntax errors in consumer: $SATURNIN_MENU_CONSUMER"
125
+    eval "$SATURNIN_MENU_CONSUMER"
126
+}
127
+
128
+SATURNIN_MENU_MNAME="$1"
129
+
130
+load_parts
131
+
132
+debug -v SATURNIN_MENU_MNAME \
133
+         SATURNIN_MENU_GENERATOR \
134
+         SATURNIN_MENU_SELECTOR \
135
+         SATURNIN_MENU_SELECT_ARGS \
136
+         SATURNIN_MENU_CONSUMER
137
+
138
+do_generate | do_select | do_consume