Using Python, I'm trying to write the cursor location to the lower right corner of my curses window using addstr() but I get an error. ScreenH-2
works fine but is printed on the 2nd line up from the bottom of the winddow. ScreenH-1
does not work at all. What am I doing wrong?
import curses
ScreenH = 0
ScreenW = 0
CursorX = 1
CursorY = 1
def repaint(screen):
global ScreenH
global ScreenW
global CursorX
global CursorY
ScreenH, ScreenW = screen.getmaxyx()
cloc = ' ' + str(CursorX) + ':' + str(CursorY) + ' '
cloclen = len (cloc)
screen.addstr (ScreenH - 1, ScreenW - cloclen, cloc, curses.color_pair(1));
def Main(screen):
curses.init_pair (1, curses.COLOR_WHITE, curses.COLOR_BLUE)
repaint (screen)
while True:
ch = screen.getch()
if ch == ord('q'):
break
repaint (screen)
curses.wrapper(Main)
File "test.py", line 17, in repaint
screen.addstr (ScreenH - 1, ScreenW - cloclen, cloc, curses.color_pair(1));
_curses.error: addstr() returned ERR
To clear characters until the end of the line, use clrtoeol(), To clear characters until the end of the window, use clrtobot().
What is curses? ¶ The curses library supplies a terminal-independent screen-painting and keyboard-handling facility for text-based terminals; such terminals include VT100s, the Linux console, and the simulated terminal provided by various programs.
The curses module provides an interface to the curses library, the de-facto standard for portable advanced terminal handling. While curses is most widely used in the Unix environment, versions are available for Windows, DOS, and possibly other systems as well.
You could also use insstr
instead of addstr
:
screen.insstr(ScreenH - 1, ScreenW - 1 - cloclen, cloc, curses.color_pair(1))
That will prevent the scroll, and thus allow you to print up to the very last char in last line
You need to substract 1 from the width as you did for the height. Otherwise the string will exceed the width of the screen.
screen.addstr(ScreenH - 1, ScreenW - 1 - cloclen, cloc, curses.color_pair(1))
^^^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With