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

    From Chris Green@21:1/5 to All on Sat Jan 11 14:28:49 2025
    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.


    It might also be possible/useful to use the mouse for this.

    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Chris Green on Sat Jan 11 15:13:57 2025
    Chris Green <cl@isbd.net> wrote or quoted:
    Are there any packages that offer this sort of thing? I'd prefer ones
    from the Debian repositories but that's not absolutely necessary.

    So, in my humble opinion, it's a no-brainer to split this into two
    camps and draw a line between Linux and Windows.

    On Linux, you're going to wanna roll with curses. See [1] below.

    For Windows, whip up your own terminal using tkinter (Yeah,
    tkinter's a GUI library, no doubt! But in this case, it's just
    being used to mimic a terminal.) as your secret sauce. See [2].

    If that's too tied to specific systems for your taste,
    why not cook up a library that smooths out those wrinkles?

    The following drafts still contain bugs, but might convey
    the idea.

    [1]

    import curses

    def main(stdscr):
    # Clear screen and hide cursor
    stdscr.clear()
    curses.curs_set(1)

    # Display prompt
    stdscr.addstr(0, 0, "Color? ")

    # Pre-fill with "red"
    default_text = "red"
    stdscr.addstr(0, 7, default_text)

    curses.echo() # Enable echo of characters
    curses.cbreak() # React to keys instantly without Enter key

    # Initialize cursor position
    cursor_x = 7 + len(default_text)
    current_text = default_text

    while True:
    stdscr.move(0, cursor_x)
    ch = stdscr.getch()

    if ch == ord('\n'): # Enter key
    break
    elif ch in (curses.KEY_BACKSPACE, 127): # Backspace
    if cursor_x > 7:
    cursor_x -= 1
    current_text = current_text[:cursor_x-7] + current_text[cursor_x-6:]
    stdscr.delch(0, cursor_x)
    elif ch == curses.KEY_LEFT:
    if cursor_x > 7:
    cursor_x -= 1
    elif ch == curses.KEY_RIGHT:
    if cursor_x < 7 + len(current_text):
    cursor_x += 1
    elif 32 <= ch <= 126: # Printable characters
    current_text = current_text[:cursor_x-7] + chr(ch) + current_text[cursor_x-7:]
    stdscr.insch(0, cursor_x, ch)
    cursor_x += 1

    stdscr.refresh()

    # Clear screen
    stdscr.clear()

    # Display result
    stdscr.addstr(0, 0, f"You entered: {current_text}")
    stdscr.refresh()
    stdscr.getch()

    if __name__ == "__main__":
    curses.wrapper(main)

    [2]

    import tkinter as tk
    from tkinter import simpledialog

    class ColorQueryApp:
    def __init__(self, master):
    self.master = master
    master.title("Color Query")

    # Create the Text widget
    self.text_field = tk.Text(master, height=10, width=40)
    self.text_field.pack(padx=10, pady=10)

    # Create a button to add new color queries
    self.add_button = tk.Button(master, text="Add Color Query", command=self.add_color_query)
    self.add_button.pack(pady=5)

    # Bind the Return key event
    self.text_field.bind('<Return>', self.show_color_dialog)

    def add_color_query(self):
    # Insert a new line if the Text widget is not empty
    if self.text_field.index('end-1c') != '1.0':
    self.text_field.insert('end', '\n')

    # Insert the new color query line
    query_line = "Color? red"
    self.text_field.insert('end', query_line)

    # Move the cursor to the end of the new line
    self.text_field.mark_set('insert', f'{self.text_field.index("end-1c")}')

    # Bind the key events
    self.text_field.bind('<Key>', self.check_editing)

    def check_editing(self, event):
    # Allow all keystrokes if cursor is in editable area
    if self.text_field.compare('insert', '>=', 'insert linestart+7c'):
    return

    # Prevent editing in the "Color? " area
    if event.keysym in ['BackSpace', 'Delete'] or len(event.char) > 0:
    return 'break'

    def show_color_dialog(self, event):
    # Get the current line
    current_line = self.text_field.get("insert linestart", "insert lineend")

    # Extract the color
    color = current_line[7:].strip()

    # Show a dialog with the entered color
    simpledialog.messagebox.showinfo("Entered Color", f"You entered: {color}")

    # Prevent the default newline behavior
    return 'break'

    root = tk.Tk()
    app = ColorQueryApp(root)
    root.mainloop()

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Stefan Ram on Sat Jan 11 19:31:54 2025
    ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
    If that's too tied to specific systems for your taste,
    why not cook up a library that smooths out those wrinkles?

    Or, if you want to use a library instead of writing one, check out:

    Textual – An async-powered terminal application framework for Python

    Textual was built on top of

    Rich - a terminal application renderer (not using async) and toolkit

    There's also:

    py_cui: library for creating CUI/TUI interfaces with pre-built widgets

    PyTermGUI: terminal UI library

    Python Prompt Toolkit: library for command line applications

    Urwid: a console user interface library for Python

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dn@21:1/5 to Chris Green via Python-list on Sun Jan 12 13:49:03 2025
    On 12/01/25 03:28, Chris Green via Python-list wrote:
    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.


    It might also be possible/useful to use the mouse for this.

    There must be more choices/combinations of packages to do this. Maybe a
    good place to start is the Python Prompt Toolkit (https://python-prompt-toolkit.readthedocs.io/en/master/)


    --
    Regards,
    =dn

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From rustbuckett@nope.com@21:1/5 to All on Sat Jan 11 21:49:39 2025
    This is what I was going to suggest. Rich is super easy to use.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Green@21:1/5 to rustbuckett@nope.com on Sun Jan 12 12:03:37 2025
    rustbuckett@nope.com wrote:

    This is what I was going to suggest. Rich is super easy to use.

    OK, thanks, Rich is on my shortlist then.

    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Green@21:1/5 to Alan Gauld on Tue Jan 14 09: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
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Chris Green on Tue Jan 14 11: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
    . . .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Roel Schroeven@21:1/5 to All on Thu Jan 16 10: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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Mats Wichmann@21:1/5 to Keith Thompson via Python-list on Fri Jan 17 14: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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lele Gaifax@21:1/5 to Chris Green via Python-list on Sat Jan 18 09: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.
    --
    nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
    real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia. lele@metapensiero.it | -- Fortunato Depero, 1929.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)