Example #1
0
def getinput():
    'a generator function that returns a line or two from an interactive session.'

    previousline, line = '', ''
    isloop = False
    while True:

        if previousline: prompt = 'more> '
        else:
            if po.color: prompt = ps_lib.eval_str(po.colorprompt)
            else: prompt = ps_lib.eval_str(po.prompt)
            ansi.xtermTitle(ps_lib.eval_str(po.title))
        try:
            line = raw_input(prompt)  # get input from user
        except EOFError:  # sys.exit(1) or ^Z, ^D
            raise StopIteration
            break
        except KeyboardInterrupt:  # ^C ( now caught earlier)
            print
            continue

        if line.isspace(): line = ''  # make sure blank line is false
        if not line and not previousline:
            continue  # just an enter hit, I think

        # check if we need more input
        if line:
            if isloop:
                #previousline = previousline + '    ' + line + '\n'
                previousline = previousline + line + '\n'
                continue
            if line.split()[0] in pi.branch_words:  #ps_lib.
                isloop = True
                previousline = previousline + line + '\n'
                continue
            if line[-1] == '\\':
                # get more
                previousline = previousline + line[:-1]
                continue

        else:  # line is nada '', and a previous line exists, end extended line
            isloop = False
            if previousline:
                line = previousline + ' '  # keeep from triggering EOF downstream
                previousline = ''

        if po.opts.debug: ps_lib.msg('debug', ` line `)
        if '\n' in line:
            for item in line.split('\n'):
                #print 'yielding:', `item`
                yield item
            #yield ''
        else:
            yield line
Example #2
0
def getinput():
    'a generator function that returns a line or two from an interactive session.'

    previousline, line = '', ''
    isloop = False
    while True:

        if previousline:  prompt = 'more> '
        else:
            if po.color:    prompt = ps_lib.eval_str(po.colorprompt)
            else:           prompt = ps_lib.eval_str(po.prompt)
            ansi.xtermTitle(ps_lib.eval_str(po.title))
        try:
            line = raw_input(prompt)    # get input from user
        except EOFError:                # sys.exit(1) or ^Z, ^D
            raise StopIteration
            break
        except KeyboardInterrupt:       # ^C ( now caught earlier)
            print;  continue

        if line.isspace():  line = ''   # make sure blank line is false
        if not line and not previousline: continue  # just an enter hit, I think

        # check if we need more input
        if line:
            if isloop:
                #previousline = previousline + '    ' + line + '\n'
                previousline = previousline + line + '\n'
                continue
            if line.split()[0] in pi.branch_words:  #ps_lib.
                isloop = True
                previousline = previousline + line + '\n'
                continue
            if line[-1] == '\\':
                # get more
                previousline = previousline + line[:-1]
                continue

        else: # line is nada '', and a previous line exists, end extended line
            isloop = False
            if previousline:
                line = previousline + ' ' # keeep from triggering EOF downstream
                previousline = ''

        if po.opts.debug: ps_lib.msg('debug', `line`)
        if '\n' in line:
            for item in line.split('\n'):
                #print 'yielding:', `item`
                yield item
            #yield ''
        else:
            yield line
Example #3
0
def getinputline(infile):
    'return a line or two from the input file - is this obsolete?'

    previousline, line = '', ''
    isloop = False
    while True:
       if po.runmode == 'file':
          line = infile.readline()
          if line.isspace():  continue
          if line == '':    break # EOF
       elif po.runmode == 'interactive':
          if previousline:    prompt = 'more> '
          else:
                #prompt = process_prompt()
                prompt = exec_str(po.prompt)
                ansi.xtermTitle(exec_str(po.title))
          try:           line = raw_input(prompt)
          except EOFError:    break #sys.exit(1) # ^Z
          except KeyboardInterrupt:
             print;       continue
          if line.isspace():  line = ''  # make sure line is true
          if line == '' and not previousline: continue
       else:  return infile # string passed from -c " "

       # check if we need more input
       if   line and line[-1] == '\\':                # get more
             previousline = previousline + line[:-1]
             continue
       elif line and line.split()[0] in pi.branch_words:    # get more
             previousline = previousline + line
             isloop = True
             continue
       elif line and isloop:                       # spaces for auto indent
             previousline = previousline + '\n' + line
             continue
       else: # line is nada '', and a previous line exists
          isloop = False
          if previousline:
             line = previousline + line
             previousline = ''

       if po.opts.debug:
          if po.runmode == 'file':   msg('debug', 'file:%s> %s' % (infile.name, line) )
          else:                     msg('debug', '%s> %s' % (po.runmode, line) )

       return line
    return None
Example #4
0
if sys.platform == 'win32':
    #os.system('cmd /c title %s' % ps_lib.eval_str(po.title))
    # set title (doesnt work - reset on exit of cmd)
    pass
    readline = False

elif os.name == 'posix':

    #import curses                                      # term routines
    import readline  # line editor
    import rlcompleter  # tab completion
    #print 'delims', readline.get_completer_delims()
    readline.parse_and_bind('tab: complete')
    readline.set_completer_delims('\r\n`~!@#$%^&*()-=+[{]}\|;:\'",<>? ')
    readline.set_completer(ps_interactive.ps_completer(po.namespace).complete)
    ansi.xtermTitle(ps_lib.eval_str(po.title))  # set title, bug!

    # Register the signal handlers
    def termsize_change(signum, frame):
        x, y = ansi.getTermSize()
        if x: os.environ['COLUMNS'] = str(x)
        if y: os.environ['LINES'] = str(y)

    termsize_change(None, None)  # once to set environment

    def sighandler(signum, frame):
        '''Clean and tidy shutdown when killed.'''
        if po.opts.debug: ps_lib.msg('debug', 'Signal %s received.' % signum)
        #print 'removing:', readline.remove_history_item(readline.get_current_history_length()-1)
        # how to clear the line buffer?  there doesn't seem to be a function to do it.
        print
Example #5
0
    # set title (doesnt work - reset on exit of cmd)
    pass
    readline = False
    
elif os.name == 'posix':

    #import curses                                      # term routines
    import readline                                     # line editor
    import rlcompleter                                  # tab completion
    #print 'delims', readline.get_completer_delims()
    readline.parse_and_bind('tab: complete')
    readline.set_completer_delims('\r\n`~!@#$%^&*()-=+[{]}\|;:\'",<>? ')
    readline.set_completer(
            ps_interactive.ps_completer(po.namespace).complete
        )
    ansi.xtermTitle(ps_lib.eval_str(po.title))          # set title, bug!

    # Register the signal handlers
    def termsize_change(signum, frame):
        x, y = ansi.getTermSize()
        if x:  os.environ['COLUMNS'] = str(x)
        if y:  os.environ['LINES']   = str(y)
    termsize_change(None, None) # once to set environment

    def sighandler(signum, frame):
        '''Clean and tidy shutdown when killed.'''
        if po.opts.debug: ps_lib.msg('debug', 'Signal %s received.' % signum)
        #print 'removing:', readline.remove_history_item(readline.get_current_history_length()-1)
        # how to clear the line buffer?  there doesn't seem to be a function to do it.
        print
        readline.redisplay()