ERROR 404
ERROR 404

Reputation: 11

How do I erase the last word printed onto the terminal?

I would like to be able to erase the last word printed onto the terminal. The last word should be the last string of text following a space. The following code simulates what I am trying to achieve. I want a function remove_last_word() that will erase str.

import random

words = ['apple', 'banana', 'cherry', 'dog', 'elephant', 'flower', 'guitar', 'house', 'island', 'jungle']
str = 'stop.\n'

for i in range(0, random.randint(0, 100)):
    print(random.choice(words), end=' ')
print(str, end='')

remove_last_word()

Simply printing a carriage return won't work because there might be a newline at the end of the word. Moving the cursor up a line and printing a carriage return won't work because that will overwrite the previous words printed onto the terminal. I need a way to access the contents of the terminal so that I can find the start of the the last word.

Upvotes: 1

Views: 50

Answers (1)

michaelt
michaelt

Reputation: 349

I wasn't able to do this without curses, so here's a solution using curses:

import curses
import random

words = ['apple', 'banana', 'cherry', 'dog', 'elephant',
         'flower', 'guitar', 'house', 'island', 'jungle']

def remove_last_word(stdscr):
    # Clear the screen                                                                                                                                                                        
    stdscr.clear()

    # Randomized words list using the vocabulary provided                                                                                                                                                                      
    picked_words = [random.choice(words) for _ in range(random.randint(1, 100))]

    # Print selected words, separated by spaces                                                                                                                                               
    stdscr.addstr(' '.join(picked_words) + ' stop.\n')
    stdscr.refresh()

    # So we can observe the delete: wait for user input to remove the last word                                                                                                               
    stdscr.addstr("Press Enter to remove the last word...")
    stdscr.refresh()
    stdscr.getch()  # Wait for user to press a key                                                                                                                                            

    # Remove the last printed word using curses                                                                                                                                               
    #-----------------------------------------------------                                                                                                                                    
    # 1.) Clear the screen                                                                                                                                                                    
    stdscr.clear()
    if picked_words:
        # 2.) Remove the last word                                                                                                                                                            
        picked_words.pop()
        # 3.) print it out again with one less word                                                                                                                                               
        stdscr.addstr(' '.join(picked_words) + ' stop.\n')

    stdscr.refresh()
    stdscr.getch()  # Wait for another key press to exit                                                                                                                                      

def main():
    # Clearly, we are using curses to handle our console updates                                                                                                                              
    curses.wrapper(remove_last_word)

if __name__ == "__main__":
    main()

Upvotes: 0

Related Questions