• Re: Tools to help with text mode (i.e. non-GUI) input

    From Alan Gauld@3:633/280.2 to All on Tue Jan 14 19:56:14 2025
    On 14/01/2025 00:20, Grant Edwards via Python-list wrote:
    On 2025-01-13, Alan Gauld via Python-list <python-list@python.org> wrote:

    All of that is possible in curses, you just have to code it.

    All of that is easy with curses in C. Unfortunately, the high level
    "panel" and "menu" curses subystems that make it easy aren't included
    in the Python curses API,

    panel is included. Just import curses.panel.

    menu unfortunately isn't but it's not difficult to roll
    your own by creating a window with a list of options, provided
    you don't try to get too fancy(submenus etc).

    --
    Alan G
    Author of the Learn to Program web site
    http://www.alan-g.me.uk/
    http://www.amazon.com/author/alan_gauld
    Follow my photo-blog on Flickr at:
    http://www.flickr.com/photos/alangauldphotos



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Chris Green@3:633/280.2 to All on Tue Jan 14 20:15:01 2025
    Alan Gauld <learn2program@gmail.com> wrote:
    On 14/01/2025 00:20, Grant Edwards via Python-list wrote:
    On 2025-01-13, Alan Gauld via Python-list <python-list@python.org> wrote:

    All of that is possible in curses, you just have to code it.

    All of that is easy with curses in C. Unfortunately, the high level "panel" and "menu" curses subystems that make it easy aren't included
    in the Python curses API,

    panel is included. Just import curses.panel.

    menu unfortunately isn't but it's not difficult to roll
    your own by creating a window with a list of options, provided
    you don't try to get too fancy(submenus etc).

    Yes, thanks all, maybe just straightforward curses is the way to go.
    Looking at some of the 'cleverer' ones they end up looking remarkably
    like GUI code, in which case I might as well use a GUI. I have written
    a (fairly simple) Gtk based python program, I was just trying to avoid
    all the GUI overheads for a little new project.

    --
    Chris Green
    ú

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Stefan Ram@3:633/280.2 to All on Tue Jan 14 22:22:15 2025
    Chris Green <cl@isbd.net> wrote or quoted:
    Yes, thanks all, maybe just straightforward curses is the way to go.
    Looking at some of the 'cleverer' ones they end up looking remarkably
    like GUI code, in which case I might as well use a GUI. I have written
    a (fairly simple) Gtk based python program, I was just trying to avoid
    all the GUI overheads for a little new project.

    The Cmd class is your go-to for whipping up those bare-bones
    command line interfaces. It's hella useful for cobbling
    together test rigs, admin tools, and rough drafts that'll
    eventually get a facelift with some fancy UI.

    Check out this sample of what Cmd code might look like:

    class TurtleShell( cmd.Cmd ):
    intro = 'Welcome to the turtle shell. Type help or ?.\n'
    prompt = '(turtle) '
    file = None

    def do_forward(self, arg):
    'Move the turtle forward by the specified distance: FORWARD 10'
    forward(*parse(arg))
    . . .
    . . .
    .. . .

    And here's a taste of what a Cmd UI could shape up to be:

    Welcome to the turtle shell. Type help or ? to list commands.

    (turtle) ?

    Documented commands (type help <topic>): ========================================
    bye color goto home playback record right
    circle forward heading left position reset undo

    (turtle) help forward
    Move the turtle forward by the specified distance: FORWARD 10
    (turtle) record spiral.cmd
    (turtle) position
    Current position is 0 0
    .. . .



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From Grant Edwards@3:633/280.2 to All on Wed Jan 15 01:21:41 2025
    On 2025-01-14, Alan Gauld via Python-list <python-list@python.org> wrote:
    On 14/01/2025 00:20, Grant Edwards via Python-list wrote:
    On 2025-01-13, Alan Gauld via Python-list <python-list@python.org> wrote:

    All of that is possible in curses, you just have to code it.

    All of that is easy with curses in C. Unfortunately, the high level
    "panel" and "menu" curses subystems that make it easy aren't included
    in the Python curses API,

    panel is included. Just import curses.panel.

    Ah my bad, I meant "forms" and "menu" support was missing.

    menu unfortunately isn't but it's not difficult to roll your own by
    creating a window with a list of options, provided you don't try to
    get too fancy(submenus etc).

    Yea, it was "menu" and "forms" that I really wanted.

    --
    Grant



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Keith Thompson@3:633/280.2 to All on Wed Jan 15 11:41:51 2025
    Alan Gauld <learn2program@gmail.com> writes:
    On 11/01/2025 14:28, Chris Green via Python-list wrote:
    I'm looking for Python packages that can help with text mode input,

    The standard package for this is curses which comes as part
    of the standard library on *nix distros.

    The thing about curses (which may or may not be a problem) is that, by
    design, it takes over the whole screen. If you want to do simpler text manipulations (showing a dismissible message, showing bold text, etc.)
    without interfering with existing text, curses can't do it, at least not easily.

    If your terminal supports it, your current screen contents can be
    restored after the application finishes (curses enables the "alternate
    screen" and then restores the primary screen on exit).

    There don't seem to be a lot of good solutions for doing curses-like
    text manipulation without taking over the entire screen.

    [...]

    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    void Void(void) { Void(); } /* The recursive call of the void */

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: None to speak of (3:633/280.2@fidonet)
  • From Grant Edwards@3:633/280.2 to All on Wed Jan 15 11:54:09 2025
    On 2025-01-14, Chris Green via Python-list <python-list@python.org> wrote:

    Yes, thanks all, maybe just straightforward curses is the way to go.
    Looking at some of the 'cleverer' ones they end up looking remarkably
    like GUI code, in which case I might as well use a GUI.

    The source code to configure and handle a UI with a certain set of
    input widgets is going to be pretty much the same regardless of the
    low-level screen bashing details involved in rendering the widgets.

    You choose a TUI toolkit like curses panel/menu/forms instead of a GUI
    toolkit like gtk because you need your app to run on a terminal
    instead of on a X11/wayland screen, not because you want your app to
    be simpler than the code for a GUI app (as you've seen, it isn't).

    I have written a (fairly simple) Gtk based python program, I was
    just trying to avoid all the GUI overheads for a little new project.

    If you want to avoid the [TG]UI overhead, then command line options
    are your friend. If that's not sophisticated enough the gnu "readline"
    library with a simple command processor is a common next step.

    Or you can use curses to print some help stuff at the top of the
    terminal window and then do everything based on single-stroke "command
    keys" that print output in the lower part of the terminal window.

    --
    Grant



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Alan Gauld@3:633/280.2 to All on Thu Jan 16 12:06:07 2025
    On 15/01/2025 00:54, Grant Edwards via Python-list wrote:

    are your friend. If that's not sophisticated enough the gnu "readline" library with a simple command processor is a common next step.

    On that front the cmd module in Python is often overlooked
    but is useful for structuring a non-GUI-like text UI.

    It doesn't support mouse or screen mapping or colours etc.
    But if all you want/need is a pdb type interface it works well.

    --
    Alan G
    Author of the Learn to Program web site
    http://www.alan-g.me.uk/
    http://www.amazon.com/author/alan_gauld
    Follow my photo-blog on Flickr at:
    http://www.flickr.com/photos/alangauldphotos



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Roel Schroeven@3:633/280.2 to All on Thu Jan 16 20:09:36 2025
    Op 11/01/2025 om 15:28 schreef Chris Green via Python-list:
    I'm looking for Python packages that can help with text mode input,
    i.e. for use with non-GUI programs that one runs from the command
    prompt in a terminal window running a bash shell or some such.

    What I'm specifically after is a way to provide a default value that
    can be accepted or changed easily and also a way to provide a number
    of different values to choose from.

    I.e. for the default sort of input one might see:-

    Colour? red

    Hitting return would return 'red' to the program but you could also
    backspace over the 'red' and enter something else. Maybe even better
    would be that the 'red' disappears as soon as you hit any key other
    than return.


    For the select a value type of input I want something like the above
    but hitting (say) arrow up and arrow down would change the value
    displayed by the 'Colour?' prompt and hitting return would accept the
    chosen value. In addition I want the ability to narrow down the list
    by entering one or more initial characters, so if you enter 'b' at the Colour? prompt the list of values presented would only include colours starting with 'b' (beige, blue, black, etc.)


    Are there any packages that offer this sort of thing? I'd prefer ones
    from the Debian repositories but that's not absolutely necessary.
    Maybe pythondialog could be useful for this. I've never used it so I
    can't really tell if it will fit your requirements, but at least it
    seems worth a look. It looks like it supports text input with a default
    value (https://pythondialog.sourceforge.io/doc/widgets.html#single-line-input-fields).
    The other thing you describe is, if I understand correctly, an input
    with predefined values but also the ability to enter a custom value. At
    first sight that doesn't seem to be provided.

    PyPI page: https://pypi.org/project/pythondialog/
    Home page: https://pythondialog.sourceforge.io/

    --
    "Let everything happen to you
    Beauty and terror
    Just keep going
    No feeling is final"
    -- Rainer Maria Rilke


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Alan Gauld@3:633/280.2 to All on Sat Jan 18 05:11:47 2025
    On 15/01/2025 00:41, Keith Thompson via Python-list wrote:
    Alan Gauld <learn2program@gmail.com> writes:
    On 11/01/2025 14:28, Chris Green via Python-list wrote:
    I'm looking for Python packages that can help with text mode input,

    The standard package for this is curses which comes as part
    of the standard library on *nix distros.

    The thing about curses (which may or may not be a problem) is that, by design, it takes over the whole screen. If you want to do simpler text manipulations (showing a dismissible message, showing bold text, etc.) without interfering with existing text, curses can't do it, at least not easily.

    It's not that difficult to use the terminfo codes directly. But
    that won't give access to things like lists of default values, mouse
    control etc that the OP wanted. But for simple text characteristics
    and moving the cursor around terminfo works ok even if a bit tedious.

    Here is "hello world" in bold...

    import curses
    curses.setupterm()
    bold = curses.tigetstr('bold').decode('ascii')
    normal = curses.tigetstr('sgr0').decode('ascii')

    print(bold, 'Hello world', normal)

    Whilst you can position the cursor etc it very quickly
    becomes easier to just use full blown curses.

    --
    Alan G
    Author of the Learn to Program web site
    http://www.alan-g.me.uk/
    http://www.amazon.com/author/alan_gauld
    Follow my photo-blog on Flickr at:
    http://www.flickr.com/photos/alangauldphotos



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Keith Thompson@3:633/280.2 to All on Sat Jan 18 06:03:16 2025
    Alan Gauld <learn2program@gmail.com> writes:
    On 15/01/2025 00:41, Keith Thompson via Python-list wrote:
    Alan Gauld <learn2program@gmail.com> writes:
    On 11/01/2025 14:28, Chris Green via Python-list wrote:
    I'm looking for Python packages that can help with text mode input,

    The standard package for this is curses which comes as part
    of the standard library on *nix distros.

    The thing about curses (which may or may not be a problem) is that, by
    design, it takes over the whole screen. If you want to do simpler text
    manipulations (showing a dismissible message, showing bold text, etc.)
    without interfering with existing text, curses can't do it, at least not
    easily.

    It's not that difficult to use the terminfo codes directly. But
    that won't give access to things like lists of default values, mouse
    control etc that the OP wanted. But for simple text characteristics
    and moving the cursor around terminfo works ok even if a bit tedious.

    Here is "hello world" in bold...

    import curses
    curses.setupterm()
    bold = curses.tigetstr('bold').decode('ascii')
    normal = curses.tigetstr('sgr0').decode('ascii')

    print(bold, 'Hello world', normal)

    Cool -- but I think you want:

    print(bold, 'Hello world', normal, sep='')

    Whilst you can position the cursor etc it very quickly
    becomes easier to just use full blown curses.

    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    void Void(void) { Void(); } /* The recursive call of the void */

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: None to speak of (3:633/280.2@fidonet)
  • From Grant Edwards@3:633/280.2 to All on Sat Jan 18 08:09:14 2025
    On 2025-01-17, Alan Gauld via Python-list <python-list@python.org> wrote:
    On 15/01/2025 00:41, Keith Thompson via Python-list wrote:
    Alan Gauld <learn2program@gmail.com> writes:
    On 11/01/2025 14:28, Chris Green via Python-list wrote:
    I'm looking for Python packages that can help with text mode input,

    The standard package for this is curses which comes as part
    of the standard library on *nix distros.

    The thing about curses (which may or may not be a problem) is that, by
    design, it takes over the whole screen. If you want to do simpler text
    manipulations (showing a dismissible message, showing bold text, etc.)
    without interfering with existing text, curses can't do it, at least not
    easily.

    It's not that difficult to use the terminfo codes directly.

    It's easy to do, but it's tricky to do it right.

    https://github.com/GrantEdwards/Python-curses-and-terminfo

    [...]

    Here is "hello world" in bold...

    import curses
    curses.setupterm()
    bold = curses.tigetstr('bold').decode('ascii')
    normal = curses.tigetstr('sgr0').decode('ascii')

    print(bold, 'Hello world', normal)

    Don't forget to use tparm() to parameterize strings for things like
    "move curser".

    Once you've parameterized the string (if needed), you've _might_ need
    to worry about the stuff in the string that's meant to be interpreted
    by tputs/putp:

    Quoting man tparm(3)

    All terminfo strings [including the output of tparm] should be
    printed with tputs or putp.

    That's becuase terminfo strings can contain escape sequences that are
    filterd out and interpreted by tputs/putp. The approach above only
    works if you only care about certain terminals, and you know that none
    of the terminfo strings you're using have those interal terminfo
    escape sequences in them [AFAIK, that's true for the linux console,
    xterm and the like, but not for many serial terminals.]

    --
    Grant



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Mats Wichmann@3:633/280.2 to All on Sat Jan 18 08:11:57 2025
    On 1/17/25 12:03, Keith Thompson via Python-list wrote:
    Alan Gauld <learn2program@gmail.com> writes:
    On 15/01/2025 00:41, Keith Thompson via Python-list wrote:
    Alan Gauld <learn2program@gmail.com> writes:
    On 11/01/2025 14:28, Chris Green via Python-list wrote:
    I'm looking for Python packages that can help with text mode input,
    I haven't followed this whole thread, but rich (low-level) and textual (higher-level) are designed for this - part of the same project family -
    and really worth a look. I know someone else mentioned rich earlier.

    There's a little video on the textual homepage that shows some of what
    it can do in action:

    https://github.com/Textualize/textual


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Lele Gaifax@3:633/280.2 to All on Sat Jan 18 19:08:04 2025
    Chris Green via Python-list <python-list@python.org> writes:

    I'm looking for Python packages that can help with text mode input,
    i.e. for use with non-GUI programs that one runs from the command
    prompt in a terminal window running a bash shell or some such.

    I'd suggest giving a try to https://pypi.org/project/questionary/,
    seems very close to what you are looking for and very simple to drive.

    Another package is https://github.com/petereon/beaupy, that seems a good
    fit as well.

    ciao, lele.
    --=20
    nickname: Lele Gaifax | Quando vivr=C3=B2 di quello che ho pensato ieri
    real: Emanuele Gaifas | comincer=C3=B2 ad aver paura di chi mi copia. lele@metapensiero.it | -- Fortunato Depero, 1929.

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)