mercurial - Tab Completion in Python Command Line Interface - how to catch Tab events -
i'm writing little cli in python (as extension mercurial) , support tab-completion. specifically, catch tabs in prompt , show list of matching options (just bash).
example: enter section name:
 ext*tab*    extensions    extras   the problem i'm not sure how catch tab events. i'm using ui.prompt() api of mercurial, calling raw_input() under hood. 
as far know, raw_input() returns on 'enter' , if user enters tab, string returned includes "\t". 
for use readline module.
simplest code can think:
import readline commands = ['extra', 'extension', 'stuff', 'errors',             'email', 'foobar', 'foo']  def complete(text, state):     cmd in commands:         if cmd.startswith(text):             if not state:                 return cmd             else:                 state -= 1  readline.parse_and_bind("tab: complete") readline.set_completer(complete) raw_input('enter section name: ')   example usage:
enter section name: <tab> email      errors     extension       foo        foobar    stuff enter section name: e<tab> email      errors     extension        enter section name: ext<tab> extension          besides completion, readline provides with:
- line editing
 - keybinding configuration (emacs , vi modes included)
 - history (up arrow recall previous values)
 - history searching, saving , loading
 
Comments
Post a Comment