pythonwindowsterminalcurses

Windows Curses Adding Weird Spacing


I have some python code that uses curses to continuously print "Test Bot." And allowing me to send messages at the same time now its printing "Test Bot." fine but when I try to enter "Test User." there is some weird spacing before it Here's the code:

import curses, threading, time
def print_hello(chat_win):
    while True:
        chat_win.addstr("Test Bot.\n")
        chat_win.refresh()
        time.sleep(0.5)
def chat_app():
    stdscr = curses.initscr()
    curses.noecho()
    curses.cbreak()
    stdscr.keypad(True)
    curses.curs_set(0)
    chat_win = curses.newwin(curses.LINES - 2, curses.COLS, 0, 0)
    chat_win.scrollok(True)
    input_win = curses.newwin(1, curses.COLS, curses.LINES - 1, 0)
    input_win.addstr("Enter Message: ")
    input_win.refresh()
    hello_thread = threading.Thread(target=print_hello, args=(chat_win,))
    hello_thread.daemon = True
    hello_thread.start()
    while True:
        key = input_win.getch()
        if key == key == 10:
            message = input_win.instr(0, 14).decode()
            chat_win.addstr(message + "\n")
            input_win.clear()
            input_win.addstr("Enter Message: ")
        else:
            input_win.addch(key)
        input_win.refresh()
        chat_win.refresh()
chat_app()

now here is what happens when I run it and try to input "Test User.":

Test Bot.                                                                                                                                                         Test Bot.                                                                                                                                                         Test Bot.                                                                                                                                                         Test Bot.                                                                                                                                                         Test Bot.                                                                                                                                                         Test Bot.                                                                                                                                                         
Test Bot.                                                                                                                                                         
Test Bot.                                                                                                                                                                                                                                                                                                                Test Bot. 
 Test User.                                                                                                                                                        
Test Bot.                                                                                                                                                         
Test Bot.                                                                                                                                                         
Test Bot.                                                                                                                                                         
Test Bot.                                                                                                                                                         
Test Bot.                                                                                                                                                         
Test Bot.                                                                                                                                                         
Test Bot.                                                                                                                                                         
Test Bot.





                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 Enter Message:      

Now I do not understand at all why this is happening.


Solution

  • This is the last space of the "Enter Message: " string. You should shift the x coordinate of the instr method by one more character:

    message = input_win.instr(0, 15).decode()