Des menus dans votre terminal - retour accueil
Anonyme, en lecture seule : git clone git://bebou.netlib.re/zenu
Authentifié·e, en écriture : git clone ssh://git@bebou.netlib.re:1459/srv/git/zenu
Log | Files | Refs | README | zip | tar.gz |
| Mode | Name | Size |
| -rw-r--r-- | .gitignore | 2L |
| -rw-r--r-- | README | 259L |
| -rwxr-xr-x | bin/build | 42L |
| -rw-r--r-- | eg/main | 22L |
| -rw-r--r-- | eg/network | 17L |
| -rw-r--r-- | eg/screens | 117L |
| -rw-r--r-- | makefile | 16L |
| -rw-r--r-- | zenu.zsh | 103L |
README (7666B)
1 # A CLI menu engine in ZSH
2
3 Written by Marc Chantreux
4
5 ## How to install
6
7 Zenu requires at least zsh and GNU sed to work. On debian machines they can be
8 installed with :
9
10 apt install zsh coreutils
11
12 Provide menu files in a `menus` folder found at the root of this git
13 project. Create it if it doesn't exist :
14
15 mkdir -p path/to/zenu/menus
16
17 Examples given in the `eg` folder can be copied to `menus` :
18
19 cp path/to/zenu/eg/* menus
20
21 Build the script files
22
23 make
24
25 ### Launch if you use zsh as your login shell
26
27 Source the ZSH functions written in zenu.zsh in your `.zshrc` file. If it
28 doesn't exist you may create it, preferably in your home. In a typical install
29 this would be done with (replace `path/to/zenu/` with the folder in which you
30 found this README file`) :
31
32 echo ". path/to/zenu/zenu.zsh" >> $HOME/.zshrc
33
34 And launch the loop
35
36 zenu.loop in path/to/zenu/menus
37
38 You can make the command `zenu.loop in path/to/zenu/menus` easily accessible
39 in you `.zshrc` file :
40
41 # With an alias
42 alias z="zenu.loop in path/to/zenu/menus"
43 # And a shortcut (ctrl+w)
44 bindkey -s '' 'z'
45
46 ### Launch if you use another login shell
47
48 You may still use zenu if you use another login shell. Launch zsh with a
49 command that sources `zenu.zsh` and calls the `zenu.loop` command :
50
51 zsh -c '. ~/git/zenu/zenu.zsh; zenu.loop in ~/git/zenu/menus'
52
53 Make sure you create a function or an alias in your shell's `rc` file for
54 easier use :)
55
56 ## Usage
57
58 To enter submenus and/or execute commands, use the highlighted key. By default
59 to go back one menu press enter.
60
61 ### Creating / Customizing menus
62
63 Menus are stored in the `menus` folder. The file named `main` is the default
64 starting menu. A menu is defined as
65
66 #### A list of commands and/or submenus with one given key being its activator
67
68 This part of the main menu given as an example looks like :
69
70 _screens
71 _network
72 _update
73
74 by default the character preceding the activator key has to be `_`.
75
76 2. An optional "pre" section
77
78 This section is arbitrary zsh code executed before zenu listens for a key
79 press. It can be used to make the following section easier to write or print
80 information below the menu section. By default this section starts after a `##
81 pre` comment in our menu declaration.
82
83 In the main menu pre section is as follows :
84
85 ## pre
86 cat <<%
87 Highlighted key to open menu
88 q to go back
89 = to edit menu
90 %
91
92 zenu_battery=$( upower -i /org/freedesktop/UPower/devices/DisplayDevice | grep -E 'percentage|time')
93 current_time=$(date +"%Hh%M.")
94 zenu_ssid="ssid:$( nmcli | grep "connected to " | sed 's/.*\( .*$\)/\1/' )"
95 echo "${ssid:-not connected}\n$current_time\n$battery"
96 ip -4 -br addr | tail -n1 | tr -s ' ' | cut -f3 -d" "
97
98
99 Which (at least on our debian machines) prints basic information about the machine network interfaces, battery state & time.
100
101 3. A scripting "react" section
102
103 This section is where you declare what each command should do. It starts with a
104 `## react` command and follows the zsh `case` syntax.
105
106 The react section of the main menu in the eg folder :
107
108 ## react
109 ;; (s) zenu+ screens
110 ;; (n) zenu+ network
111 ;; (u)
112 sudo apt update && sudo apt upgrade
113 read -k1
114
115 The line should always start with `;;`. The letter between brackets should
116 match with the corresponding activator key in the first section. In the first
117 section the "m" key was identified as activating the `mail` command therefore
118 corresponding reaction should be `;; (m) command`.
119
120 What comes after the activation key is arbitrary zsh code. If a submenu is to be
121 opened the `zenu+` function, written in `zenu.zsh` can be used with the name of
122 the submenu passed as an argument. For it to work the given argument has to exist
123 as a menu file in `menus`.
124
125 The entire main menu file with an added small tutorial would be :
126
127 _screens
128 _network
129 _update
130 ## pre
131 cat <<%
132 Highlighted key to open menu
133 q to go back
134 = to edit menu
135 %
136
137 battery=$( upower -i /org/freedesktop/UPower/devices/DisplayDevice | grep -E 'percentage|time')
138 current_time=$(date +"%Hh%M.")
139 ssid="ssid:$( nmcli | grep "connected to " | sed 's/.*\( .*$\)/\1/' )"
140 echo "${ssid:-not connected}\n$current_time\n$battery"
141 ip -4 -br addr | tail -n1 | tr -s ' ' | cut -f3 -d" "
142
143 ## react
144 ;; (s) zenu+ screens
145 ;; (n) zenu+ network
146 ;; (u)
147 sudo apt update && sudo apt upgrade
148 read -k1
149
150 ### Launching a specific submenu
151
152 You might want to directly get access to a specific submenu without having to
153 navigate through the tree, effectively overriding the default start menu
154 `main`. To achieve this use the `from` option :
155
156 zenu.loop in path/to/zenu/menus from network
157
158 `zenu` will be launched as if the `network` was the `main` menu.
159
160 ### Editing an existing menu while inside zenu
161
162 At any moment you can press the default key `=` and edit the current menu.
163 Menus will be recompiled automatically. The `EDITOR` environnment variable will
164 be read to know which text editor to use. If you wish to set it write
165
166 EDITOR=your-favorite-editor
167
168 In the `rc` file of your shell (`.bashrc`, `.zshrc` etc). Alternatively you can
169 specify the variable only for zenu when executing the command :
170
171 EDITOR=your-favorite-editor zenu.loop in path/to/menus
172
173 If EDITOR is not set `vim` is used as the default editor.
174
175 ### Creating a new menu inside zenu
176
177 If you try to open a non existing menu with a `zenu+` command you will be asked
178 wether you want to create said menu or not. If you do a dummy file will be
179 created with the corresponding file name and opened in a text editor. Therefore
180 if you want to create a new menu inside zenu you can edit the current menu with
181 `=`, add a `zenu+ menu-to-create` command, exit the editor and execute said
182 command.
183
184 The `EDITOR` environnment variable will be read to know which text editor to
185 use. If you wish to set it write
186
187 EDITOR=your-favorite-editor
188
189 In the `rc` file of your shell (`.bashrc`, `.zshrc` etc). Alternatively you can
190 specify the variable only for zenu when executing the command :
191
192 EDITOR=your-favorite-editor zenu.loop in path/to/menus
193
194 If EDITOR is not set `vim` is used as the default editor.
195
196 ### Changing defaults
197
198 #### "Go up a menu" key and "edit menu" key
199
200 To modify the default "go up a menu" key from enter to something else edit the
201 case around line 26 of `bin/build`.
202
203 For example, to substitute enter for both backspace and `q`
204
205 ...
206 case "\$zenu_key"
207 in (\$'') zenu--
208 ;; (\$'q') zenu--
209 ...
210
211 The editing key `=` can be changed in a similar way.
212
213 #### Activator key declarator
214
215 To modify which character has to precede the activator key for a command from `_`
216 to say `~` swap the `_` characters in these two lines in `bin/build` :
217
218 ;; (face | -f) sed -En '/^##/q;s/_(.)/[7m\1[m/g;s/^/ /p' "$@"
219 [...]
220 sed -En '/^##/q;s/(.*)_((.).*)/\3 \1\2 /;p' "$@" | awk '
221
222 to
223
224 ;; (face | -f) sed -En '/^##/q;s/~(.)/[7m\1[m/g;s/^/ /p' "$@"
225 [...]
226 sed -En '/^##/q;s/(.*)~((.).*)/\3 \1\2 /;p' "$@" | awk '
227
228 You'll have to modify your menus accordingly. You can achieve this with this
229 command (if you have a `sed` with the `-i` option) :
230
231 sed -iE '1,/## +pre/ s/_/~/' path/to/menus/*
232
233 #### Default starting menu
234
235 To modify the default starting menu from main to another file modify line 36 of
236 zenu.zsh
237
238 zenu_stack=( anotherfile )
239
240 #### Shortcut to the root
241
242 It is possible to take a shortcut back to the root with the command :
243
244 zenu_stack=( main )
245
246 This command could be added to all menus by adding a line to `bin/build` :
247
248 ...
249 case "\$zenu_key"
250 in (\$'q') zenu--
251 in ('~') zenu_stack=( main )
252 ...
253
254 Or to every menu with a sed script :
255
256 sed -Ei '$ a;; ("~") zenu_stack=( main )' path/to/menus/*
257
258 Take note that if you use another root menu than `main` you'll have to adapt
259 the above.