8

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
wufoo
  • 13,571
  • 12
  • 53
  • 78

2 Answers2

19

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

ziesemer
  • 27,712
  • 8
  • 86
  • 94
MestreLion
  • 12,698
  • 8
  • 66
  • 57
1

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))
                                   ^^^
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Ok, good catch. But if I simplify the addstr() line to screen.addstr (ScreenH - 1, ScreenW - 1, '*', curses.color_pair(1)) why does it still error out? I'm subtracting 1 from the width and the height that way too. It's like I can't print over the lower left corner character. – wufoo Mar 17 '14 at 14:39
  • 3
    @wufoo, Because it will cause the scroll which is disabled unless you call `screen.scrollok(1)`. – falsetru Mar 17 '14 at 14:56