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
- import os
- LOG_FILENAME = '/tmp/completer.log'
- HISTORY_FILENAME = '/tmp/completer.hist'
- logging.basicConfig(filename=LOG_FILENAME,
- level=logging.DEBUG,
- )
- def get_history_items():
- num_items = readline.get_current_history_length() + 1
- return [ readline.get_history_item(i)
- for i in xrange(1, num_items)
- ]
- class HistoryCompleter(object):
- def __init__(self):
- self.matches = []
- return
- def complete(self, text, state):
- response = None
- if state == 0:
- history_values = get_history_items()
- logging.debug('history: %s', history_values)
- if text:
- self.matches = sorted(h
- for h in history_values
- if h and h.startswith(text))
- else:
- self.matches = []
- logging.debug('matches: %s', self.matches)
- try:
- response = self.matches[state]
- except IndexError:
- response = None
- logging.debug('complete(%s, %s) => %s',
- repr(text), state, repr(response))
- return response
- def input_loop():
- if os.path.exists(HISTORY_FILENAME):
- readline.read_history_file(HISTORY_FILENAME)
- print 'Max history file length:', readline.get_history_length()
- print 'Startup history:', get_history_items()
- try:
- while True:
- line = raw_input('Prompt ("stop" to quit): ')
- if line == 'stop':
- break
- if line:
- print 'Adding "%s" to the history' % line
- finally:
- print 'Final history:', get_history_items()
- readline.write_history_file(HISTORY_FILENAME)
- # Register our completer function
- readline.set_completer(HistoryCompleter().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