Skip to content

Instantly share code, notes, and snippets.

@jkonecny12
Last active January 17, 2018 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkonecny12/fb9e8aeccbf7ba175aec0553eebfefa5 to your computer and use it in GitHub Desktop.
Save jkonecny12/fb9e8aeccbf7ba175aec0553eebfefa5 to your computer and use it in GitHub Desktop.
Simpleline - new way how to write your

Simpleline is a text UI framework. Originally a part of the Anaconda installer project. It is designed to be used with line-based machines and tools (e.g. serial console) so that every new line it appended to the bottom of the screen. Printed lines are never rewritten! It is written completely in Python 3 with a possibility to have [non-python event loops]{.std .std-ref}{.reference .internal}. By default you can use GLib event loop or Simpleline's event loop. With the exception of optional event loops, Simpleline has almost no dependencies on external libraries. I know there is the **ncurses library **which is widely used for text user interface but we (Anaconda developers) required something which will be easy to show in line-based devices and services like serial console. Thanks to this library you can even create nice and easy UI which can be printed by fax machine if you want to :).

How to use

The best learning sources can be found in the examples directory{.reference .external} in the GitHub repository{.reference .external} and you can read the [Guide to Simpleline]{.std .std-ref}{.reference .internal} section of this documentation. However, some basic usage of Simpleline will be shown here too, to get an idea of how Simpleline works:

from simpleline import App
from simpleline.render.screen import UIScreen
from simpleline.render.screen_handler import ScreenHandler
from simpleline.render.widgets import TextWidget


# UIScreen is the main building item for Simpleline. Every screen
# which will user see should be inherited from UIScreen.
class HelloWorld(UIScreen):

    def __init__(self):
        # Set title of the screen.
        super().__init__(title=u"Hello World")

    def refresh(self, args=None):
        # Fill the self.window attribute by the WindowContainer and set screen title as header.
        super().refresh()
        widget = TextWidget("Body text")
        self.window.add_with_separator(widget)


if __name__ == "__main__":
    # Initialize application (create scheduler and event loop).
    App.initialize()

    # Create our screen.
    screen = HelloWorld()

    # Schedule screen to the screen scheduler.
    # This can be called only after App.initialize().
    ScreenHandler.schedule_screen(screen)

    # Run the application. You must have some screen scheduled
    # otherwise it will end in an infinite loop.
    App.run()

The output from the simple Hello World example above:

$ ./run_example.sh 00_basic
================================================================================
================================================================================
Hello World

Body text

Please make a selection from the above ['c' to continue, 'q' to quit, 'r' to
refresh]:

If a user presses r and then enter to refresh, the same screen is printed again. This will be printed to a monitor:

$ ./run_example.sh 00_basic
================================================================================
================================================================================
Hello World

Body text

Please make a selection from the above ['c' to continue, 'q' to quit, 'r' to
refresh]: r
================================================================================
================================================================================
Hello World

Body text

Please make a selection from the above ['c' to continue, 'q' to quit, 'r' to
refresh]:

As you can see the whole screen is not rewritten – only printed again on the bottom. This is the expected behavior so the actual screen is always at the bottom but you can see the whole history. This behavior makes working with line based machines and tools much easier.

Improve Simpleline

This library is still in its beginning. It is mature enough for use but the goal is to polish a few more things before releasing version 1.0 version. If you have any interesting ideas, or if you want to help with development, please go to the GitHub page and create an issue or make a pull request. I will gladly discuss your ideas on the channel #anaconda on the IRC Freenode server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment