
Xmenu - a small "framework" for building console menus
Good day, dear habralum! It just so happened that, as a result of my service, it was up to me to write a console application (menu) to solve pressing problems related to database migrations (and not only migrations, but also with other, different types of services). I’ll make a reservation right away that I won’t talk about migrations! The development is conducted in the Python language, so the next is an untranslatable pun, using national dialects.
The current workmate of a colleague was plasticinea ball of tightly stuck together and intertwined parts with each other. Therefore, I could not change, supplement, or at worst understand in that code, so I decided to rewrite this balalaika, having previously separated the architectural components. I went on the Net in search of a python framework for building console menus. The search was unsuccessful - it was not just found in the Network of frameworks for building at least a little usable menu. But I found two "almost candidates": cliff does not quite match my requests since this is not a console menu, in fact, but just an interactive session (command line) in which it is possible to manually enter commands and enjoy autocompletion. Not that. console does not correspond at all with desires since this is
Actually, I decided to start by writing a couple of classes for rendering the console menu (the cmd2 module provided me with the help of the rendering module , which, as a result, is required for working with my little xmenu ). Actually, there are no tricks in the implementation. Three classes, Corutin for the storage of history - that’s the whole story. The result is such a mini-framework. I ask you not to start spitting right away, because I urge you to know that this is a pre-alpha version. And you can pick up and see xmenu on the github . And for those who do not want to understand the missing documentation (so far, missing), I will publish an example of client code here:
It will turn out like this:

This is an absolutely chaotic menu - do not judge by the content.
So xmenu provides:
I am not so active in the open software movement, but I promise to move in this direction, I will develop and complement this tool. Thanks to everyone who read up to this proposal, and special thanks to those who tested, expressed their wishes and highlighted my flaws. And Habr sees, maybe I didn’t try in vain, because this is my first article and I’m only learning to be a hara man.
UPD: the version has been updated - write off see on github
The current workmate of a colleague was plasticinea ball of tightly stuck together and intertwined parts with each other. Therefore, I could not change, supplement, or at worst understand in that code, so I decided to rewrite this balalaika, having previously separated the architectural components. I went on the Net in search of a python framework for building console menus. The search was unsuccessful - it was not just found in the Network of frameworks for building at least a little usable menu. But I found two "almost candidates": cliff does not quite match my requests since this is not a console menu, in fact, but just an interactive session (command line) in which it is possible to manually enter commands and enjoy autocompletion. Not that. console does not correspond at all with desires since this is
the same cheese, but with a different sauceMoreover, more than raw. Sorry for the taftalogy.
Actually, I decided to start by writing a couple of classes for rendering the console menu (the cmd2 module provided me with the help of the rendering module , which, as a result, is required for working with my little xmenu ). Actually, there are no tricks in the implementation. Three classes, Corutin for the storage of history - that’s the whole story. The result is such a mini-framework. I ask you not to start spitting right away, because I urge you to know that this is a pre-alpha version. And you can pick up and see xmenu on the github . And for those who do not want to understand the missing documentation (so far, missing), I will publish an example of client code here:
# -*- coding: utf-8 -*-
import sys
from pymenu import Item, Menu, App
class Twitter(Item):
''' This is twitter'''
def __call__(self):
sys.stdout.write('{0}\n'.format(self.name))
class Multiplier(Item):
''' Printing 1. Forever 1.'''
def __call__(self):
sys.stdout.write('Lorem ipsum Labor Excepteur ea ut.')
def func():
''' I am a function. Tasty and fragrand.'''
print 'This is it.'
class MyApp(App):
''' This is my App'''
def __init__(self):
menu = Menu(name='Main menu')
menu.add(Twitter(name='Project run'), color='red')
menu.add(Twitter(name='Other'))
math = Menu(name='Math')
math.add(Multiplier(name='Multi'), color='underline')
inc_menu = Menu(name='Database')
inc_menu.add(math)
inc_menu.add(Twitter(name='Create'))
inc_menu.add(Twitter(name='Drop'))
menu.add(inc_menu)
functinons = Menu(name='Functinons', doc='Useful functions!')
functinons.add(func, name='f')
menu.add(functinons, color='underline')
super(MyApp, self).__init__(menu=menu, stdout=sys.stdout)
def main():
MyApp().run()
if __name__ == '__main__':
main()
It will turn out like this:

This is an absolutely chaotic menu - do not judge by the content.
So xmenu provides:
- Menus of infinite nesting (menus are nested in the menu)
- Coloring menu items
- The ability to walk back in history (maybe in the future I will do walking and forward)
- Any callable objects can be hung on menu items
- Xmenu automatically collects docks from all objects and aggregates them in one menu - help.
I am not so active in the open software movement, but I promise to move in this direction, I will develop and complement this tool. Thanks to everyone who read up to this proposal, and special thanks to those who tested, expressed their wishes and highlighted my flaws. And Habr sees, maybe I didn’t try in vain, because this is my first article and I’m only learning to be a hara man.
UPD: the version has been updated - write off see on github