
import sys
import errors
import types
import window

def viewer (win,
            text,
            y,
            x,
            dispheight,
            dispwidth,
            extracommands):

    y = int (y)
    x = int (x)
    dispwidth -= 1

    # We store the lines separately
    lines = text.split ('\n')

    # Find maximum useful value for column.
    # Note that we have to allow for one more character
    # than you might think because if the line is longer
    # than dispwidth we display dispwidth-1 chars and a
    # trailing '$'
    maxcol = 0
    for line in lines:
        col = len (line) - dispwidth + 1
        if col > maxcol: maxcol = col
        pass

    # Now display it.  Assume win is full screen for now...

    topline = 0
    leftcol = 0
    rows = len (lines)    
    while 1:
        win.clear ()      # clear window
        for lineidx in range (topline, topline + dispheight):
            if lineidx < rows:

                # Determine string to display
                dispstr = str (lines[lineidx])
                flag = 0
                if len (dispstr) - leftcol >= dispwidth:
                    dispstr = dispstr[leftcol:leftcol+dispwidth-1]
                    flag = 1
                    pass
                else:
                    dispstr = dispstr[leftcol:]
                    pass
                    
                win.addstr (y+lineidx-topline, x, dispstr)

                if flag:
                    win.addstr (y+lineidx-topline, x+dispwidth-1, "$",
                                window.A_BOLD)
                    pass
                
                pass
            pass
        win.refresh ()

        ch = win.getch ()
        if ch == window.KEY_DOWN:
            topline += 1
            pass
        if ch == window.KEY_UP:
            topline -= 1
            pass
        if ch == window.KEY_LEFT:
            leftcol -= 1
            pass
        if ch == window.KEY_RIGHT:
            leftcol += 1
            pass
        if ch == window.KEY_HOME:
            leftcol = 0
            pass
        if ch == window.KEY_END:
            leftcol += dispwidth # moves a screenful at a time
            pass
        if ch in (window.KEY_NPAGE, ord(' ')):
            topline += dispheight
            pass
        if ch == window.KEY_PPAGE:
            topline -= dispheight
            pass
        if ch == ord ('q'):
            return

        # Check if it matches any of the 'extracommands'; if so,
        # return that
        try:
            if extracommands: return extracommands[ch]
        except KeyError:
            pass
            

        if topline > rows - dispheight: topline = rows - dispheight
        if topline < 0: topline = 0
        if leftcol < 0: leftcol = 0
        if leftcol > maxcol: leftcol = maxcol
        pass
        
    return

def test (win):
    text = open (sys.argv[1]).read ()
    viewer (win, text, 0, 0, win.height, win.width, None)
    pass
        
if __name__ == "__main__":
    window.start (test)
    pass
