Question

For example, I have a character string "Hello world" at the first line. How can I move it to the second line?

ps: I know I can use code like this:

import curses

stdscr = initscr()
stdscr.adstr(x,y,"Hello World")
y += 1
stdscr.erase()
stdscr.adstr(x,y,"Hello World")
stdscr.getch()

but I have lots of context under this "Hello World", I'd like to find a way which will move or erase "Hello World" only. How can I do it?

Was it helpful?

Solution

If the text takes only one line you can use the clrtoeol function to erase the whole line starting from the cursor:

curses.setsyx(y, 0)   # beginning of the correct line
# stdscr.move(y, 0)   # alternative
stdscr.clrtoeol()    # clears the current line

Alternatively you can use the hline function to insert a whole line of whitespace:

stdscr.hline(y, 0, ord(' '), 80)   # where 80 is the width of the screen

OTHER TIPS

Using curses.panel.Panel.move:

import curses
import curses.panel

x = 3
y = 3
stdscr = curses.initscr()


w = curses.newwin(1, 20, y, x)
p = curses.panel.new_panel(w)
w.addstr(0, 0, "Hello World")
w.getch()

p.move(y+1, x)
curses.panel.update_panels()
curses.doupdate()
w.getch()

curses.endwin()

BTW, parameter to addstr is y, x, str, not x, y, str.

See Demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top