Skip to content
This repository was archived by the owner on Sep 10, 2026. It is now read-only.

v0.3.0: The Mark(up) of the Snake!

Choose a tag to compare

@bczsalba bczsalba released this 31 Aug 18:32
· 1101 commits to master since this release

Overview

This release focuses on the parser submodule, rebuilding it from the ground up and adding some much needed features & performance in the process.

  • New support for differing line-lengths in Splitter

  • Rewrite pytermgui.parser:

    • The module is now instance based, allowing for isolated macro & tag definitions
    • Much more robust markup parsing
    • Up to 60% speed improvement compared to 0.2.1
    • Better macro syntax: [!align]30:center text -> [!align(30,center)]text
    • Introduce cache system for MarkupLanguage.parse
    • Note: The WindowManager doesn't play too nicely with this system. This will be fixed next patch.
  • Improve terminal size handling:

    • Add new pytermgui.ansi_interface.terminal object
    • This object is used for getting size, width & height of the current terminal
    • Support subscribing to SIGWINCH event to gracefully handle resize events
  • Remove Widget focus system

    • This was introduced specifically for InputField, which no longer uses it
    • Widget.selected_index should be used instead

Details

parser.py & MarkupLanguage

  • The module provides a MarkupLanguage instance under the name markup, which is instantiated at import-time, which should be used for all global language settings & definitions
  • Additional instances may be created to host potentially unsafe macros or non-global aliases
  • Under the hood, there is a much more robust & performant set of tokenizer methods
  • Name changes:
    • define_tag() -> MarkupLanguage.alias()
    • define_macro() -> MarkupLanguage.define()
    • markup() -> MarkupLanguage.get_markup()
    • ansi() -> MarkupLanguage.parse()
    • tokenize_markup() -> MarkupLanguage.tokenize_markup()
    • tokenize_ansi() -> MarkupLanguage.tokenize_ansi()

End result

parser.py

The speed difference can be showcased using a simple script to render a MarkupApplication window:

import time
import pytermgui
from pytermgui.cmd import MarkupApplication

formatter = pytermgui.MarkupFormatter("[60 bold]{item}")

for obj in [pytermgui.Window, pytermgui.Container]:
    obj.set_style("border", formatter)
    obj.set_style("corner", formatter)

manager = pytermgui.WindowManager()
app = MarkupApplication(manager)
window = app.construct_window()

lines = window.get_lines()

for _ in range(2):
    nums = []
    for i in range(100):
        start = time.time()
        window.get_lines()
        took = time.time() - start
        nums.append(took)

    print("avg:", sum(nums) / len(nums))
    print("max:", max(nums))
    print("min:", min(nums))

Version 0.2.1:
Screenshot 2021-08-31 at 20 25 48

Version 0.3.0:
Screenshot 2021-08-31 at 20 26 33

If you do the math, 0.00418 / 0.00970 comes up to 43%, meaning the improvement in this scenario is around 55%. This can be even larger in more complex applications.

Funnily enough, the aforementioned issue with WindowManager is that it cannot handle this speed. It gets greedy with frame-rendering, and overloads what my old MacBook can handle.

ok, bye 🚀