Search code examples
pythonpython-3.xncursescurses

How to implement a "command line" similar to vim in my curses program


i am new to python and wanted to make a small to-do list program using curses for fun.

this is the basic code i have right now:

import curses

mylist = ['item1', 'item2', 'item3']

def main(stdscr):
    y = 1
    x = 1

    for item in mylist:
        stdscr.addstr(y, x, item)
        y += 1

    stdscr.getch()

curses.wrapper(main)

it creates a window and displays each item from "mylist", i wanted to extend this so that a user could enter a command to, say, add a new task to the list and have the screen update to display the new task, how could i go about achieving this?


Solution

  • If you use the curses.echo() and stdscr.getstr(x, y) it then displays whatever you enter.

    import curses
    
    mylist = ['item1', 'item2', 'item3']
    
    def main(stdscr):
        curses.echo()
        y = 1
        x = 1
    
        for item in mylist:
            stdscr.addstr(y, x, item)
            y += 1
        new_item = stdscr.getstr(y, x)
    
    curses.wrapper(main)