0

Im trying to create a pad which a bunch of text in it that ill then look to scroll through. However I just get a segmentation fault ?

import curses,sys
from curses import wrapper
import random,string

def main(stdscr):
    pad = curses.newpad(100, 100)
    pad_pos = 0
    exit = False

    a = ''.join(random.choice(string.lowercase) for x in range(5000))

    pad.addstr(0,0,str(a))
    pad.refresh(pad_pos,0, 0,0, 20,75)

    cmd = stdscr.getch()
    while not exit:
         if  cmd == curses.KEY_DOWN:
             pad_pos += 1
             try:
                 pad.refresh(pad_pos,0, 0,0, 20,75)
             except curses.error:
                 pass
         elif cmd == curses.KEY_UP:
             pad_pos -= 1
             try:
                 pad.refresh(pad_pos,0, 0,0, 20,75)
             except curses.error:
                 pass

wrapper(main)

EDIT : My Python version is 2.7.3 on Centos 6. This was install via yum I think.

Any ideas ?

MattDMo
  • 100,794
  • 21
  • 241
  • 231
felix001
  • 15,341
  • 32
  • 94
  • 121
  • What platform are you on (including version)? What Python version do you have, and how did you install Python? (I mean specifically, like "I downloaded and ran the 3.4.1 64-bit Windows installer from python.org" or "I'm using Apple's pre-installed 2.7.5" or "`apt-get install python27` gave me version 2.7.5" or similar.) – abarnert Aug 11 '14 at 22:19
  • Also, I assume `wrapper(main)` is dedented to column 1, otherwise this program won't do anything. So is the rest of your indentation right, or are we going to have to guess before we can even run this? – abarnert Aug 11 '14 at 22:21
  • No need to guess this was a mistake and Ive updated the question. – felix001 Aug 11 '14 at 22:22
  • My first thought is that your program spins forever in that loop once you hit a key, because you never call `getch()` again, and possibly your platform just doesn't like you calling `refresh` over and over again as fast as possible. (Obviously that would be a bug in either libcurses or the Python wrapper, but still, a bug you shouldn't be triggering.) – abarnert Aug 11 '14 at 22:28
  • ok so is there a better way to write the code ? – felix001 Aug 11 '14 at 22:29
  • Anyway, to debug this, you are either going to have to run Python under gdb or lldb, or, more simply, run this script under pdb, or, even more simply, just `write` something to a file at each step along the way, so you can see what which call is actually segfaulting. Without that, nobody can help you (unless they happen to be able to repro it.) – abarnert Aug 11 '14 at 22:30
  • Well, that depends. Do you want it to just read a single key once and then process that same keystroke over and over as fast as possible forever, or do you want it to read a new keystroke each time through the loop? If you want the second, then yes, any way that does what you want is better than a way that doesn't. – abarnert Aug 11 '14 at 22:31
  • 1
    Hmmm. I tried running your code (after moving the `getch` call inside the loop) and it works fine for me. I see the gibberish text fine, and I can scroll it around. I'm running python 2.7.3 on Debian. – Nick ODell Aug 12 '14 at 02:51

1 Answers1

0

Try this:

from __future__ import division  #You don't need this in Python3
import curses
from math import *



screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.start_color()
screen.keypad(1)
curses.init_pair(1,curses.COLOR_BLACK, curses.COLOR_CYAN)
highlightText = curses.color_pair(1)
normalText = curses.A_NORMAL
screen.border(0)
curses.curs_set(0)
box = curses.newwin(12,64,1,1)
box.box()


strings = ["a","b","c","d","e","f","g","h","i","l","m","n"]
row_num = len(strings)
max_row = 10
pages = int(ceil(row_num/max_row))
position = 1
page = 1
for i in range(1,max_row + 1):
    if row_num == 0:
        box.addstr(1,1,"There aren't strings", highlightText)
    else:
        if (i == position):
            box.addstr(i,2,strings[i-1], highlightText)
        else:
            box.addstr(i,2,strings[i-1],normalText)
        if i == row_num:
            break

screen.refresh()


box.refresh()

x = screen.getch()
while x != 27:
    if (x == curses.KEY_DOWN):
        if page == 1:
            if position < i:
                position = position + 1
            else:
                if pages > 1:
                    page = page +1
                    position = 1 + (max_row * (page - 1))
        elif page == pages:
            if position < row_num:
                position = position + 1
        else:
            if position < max_row+(max_row*(page-1)):
                position = position + 1
            else:
                page = page + 1
                position = 1 + (max_row * (page - 1))
    if (x == curses.KEY_UP):
        if page == 1:
            if position > 1:
                position = position - 1
        else:
            if position > (1 + (max_row*(page-1))):
                position = position - 1
            else:
                page = page - 1
                position = max_row + (max_row * (page - 1))
    if (x == curses.KEY_LEFT):
        if page > 1:
            page = page - 1
            position = 1 + (max_row * (page - 1))

    if (x == curses.KEY_RIGHT):
        if page < pages:
            page = page + 1
            position = (1 + (max_row * (page - 1)))


    box.erase()
    screen.border(0)
    box.border(0)

    for i in range(1+(max_row*(page-1)),max_row+1+(max_row*(page-1))):
        if row_num == 0:
            box.addstr(1,1,"There aren't strings", highlightText)
        else:

            if (i+(max_row*(page-1)) == position+(max_row*(page-1))):
                box.addstr(i-(max_row*(page-1)),2,strings[i-1], highlightText)
            else:

                box.addstr(i-(max_row*(page-1)),2,strings[i-1],normalText)
            if i == row_num:
                break



    screen.refresh()
    box.refresh()
    x = screen.getch()

curses.endwin()
exit()
Alessio Ragno
  • 476
  • 1
  • 6
  • 20