Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # encoding: utf-8
- #
- # Copyright (c) 2008 Doug Hellmann All rights reserved.
- #
- """
- """
- __version__ = "$Id$"
- #end_pymotw_header
- import readline
- import logging
- LOG_FILENAME = '/tmp/completer.log'
- logging.basicConfig(filename=LOG_FILENAME,
- level=logging.DEBUG,
- )
- class BufferAwareCompleter(object):
- def __init__(self, options):
- self.options = options
- self.current_candidates = []
- return
- def complete(self, text, state):
- response = None
- if state == 0:
- # This is the first time for this text,
- # so build a match list.
- origline = readline.get_line_buffer()
- begin = readline.get_begidx()
- end = readline.get_endidx()
- being_completed = origline[begin:end]
- words = origline.split()
- logging.debug('origline=%s', repr(origline))
- logging.debug('begin=%s', begin)
- logging.debug('end=%s', end)
- logging.debug('being_completed=%s', being_completed)
- logging.debug('words=%s', words)
- if not words:
- self.current_candidates = sorted(self.options.keys())
- else:
- try:
- if begin == 0:
- # first word
- candidates = self.options.keys()
- else:
- # later word
- first = words[0]
- candidates = self.options[first]
- if being_completed:
- # match options with portion of input
- # being completed
- self.current_candidates = [
- w for w in candidates
- if w.startswith(being_completed)
- ]
- else:
- # matching empty string so use all candidates
- self.current_candidates = candidates
- logging.debug('candidates=%s',
- self.current_candidates)
- except (KeyError, IndexError), err:
- logging.error('completion error: %s', err)
- self.current_candidates = []
- try:
- response = self.current_candidates[state]
- except IndexError:
- response = None
- logging.debug('complete(%s, %s) => %s',
- repr(text), state, response)
- return response
- def input_loop():
- line = ''
- while line != 'stop':
- line = raw_input('Prompt ("stop" to quit): ')
- print 'Dispatch %s' % line
- # Register our completer function
- readline.set_completer(BufferAwareCompleter(
- {'list':['files', 'directories'],
- 'print':['byname', 'bysize'],
- 'stop':[],
- }).complete)
- # Use the tab key for completion
- readline.parse_and_bind('tab: complete')
- # Prompt the user for text
- input_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement