PyRofi wraps Rofi and helps you to build the hierarchical menus with neat navigation. You can also use Wofi as the "backend".
Make sure that you have Rofi or the Wofi installed. Then just python3 -m pip install --update --user pyrofi
(requires Python ^3.6
).
Every menu is just a dictionary: the keys are menu item captions, values are actions or submenus.
run_menu({
'command': ['man', 'ls'],
'submenu': {
'command': [...],
...
},
})
Submenus are just dictionaries with the same structure.
Action can be:
- the "command" — a list with command plus arguments
'google': ['firefox', 'https://google.com']`
- some
Callable[List[str], ]
that receives a path to the item and returnsNone
if all the work is done and you need to stop the interaction- a command (see above)
def show_path(path): return ['echo'] + path ... 'where am I': show_path
- a submenu to "dive" into (you can build the submenus dynamically)
def games_menu(path): return { ... } 'games': games_menu
Also, the run_menu
function can receive a callback: Callable[List[Any], bool]
argument: that's how you can override the command execution. Any callback function should take a path as a sole argument and return either True
if no more interactions are needed or False
if you want to keep the menu active.
#!/usr/bin/env python3
from pyrofi import run_menu
def hello_world(_):
print('Hello World!')
def dice():
import random
return ['echo', random.choice('123456')]
run_menu({
'Calculator': ['xcalc'],
'Games': {
'Rogue': ['rogue'],
'Angband': ['angband']
},
'Calendar': ['ncal', '2019'],
'Hello World': hello_world,
'Dice': dice,
})
If you want to use Wofi, you will need to add menu_cmd=pyrofi.WOFI_CMD
(or just menu_cmd='wofi
) to the run_menu
call.
More complex example you can see here and run it with python3 -m pyrofi
.