File: example.py

package info (click to toggle)
python-urwid-readline 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 136 kB
  • sloc: python: 1,062; makefile: 3
file content (33 lines) | stat: -rw-r--r-- 752 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import urwid

import urwid_readline


def unhandled_input(txt, key):
    if key in ("ctrl q", "ctrl Q"):
        raise urwid.ExitMainLoop()
    txt.set_edit_text("unknown key: " + repr(key))
    txt.set_edit_pos(len(txt.edit_text))


def compl(text, state):
    cmd = ("start", "stop", "next")
    tmp = [c for c in cmd if c and c.startswith(text)] if text else cmd
    try:
        return tmp[state]
    except (IndexError, TypeError):
        return None


def main():
    txt = urwid_readline.ReadlineEdit(multiline=True)
    txt.enable_autocomplete(compl)
    fill = urwid.Filler(txt, "top")
    loop = urwid.MainLoop(
        fill, unhandled_input=lambda key: unhandled_input(txt, key)
    )
    loop.run()


if __name__ == "__main__":
    main()