1#!/usr/bin/env python 2""" 3Demonstration of a custom completer class and the possibility of styling 4completions independently. 5""" 6from __future__ import unicode_literals 7 8from prompt_toolkit.completion import Completer, Completion 9from prompt_toolkit.output.color_depth import ColorDepth 10from prompt_toolkit.shortcuts import CompleteStyle, prompt 11 12colors = ['red', 'blue', 'green', 'orange', 'purple', 'yellow', 'cyan', 13 'magenta', 'pink'] 14 15 16class ColorCompleter(Completer): 17 def get_completions(self, document, complete_event): 18 word = document.get_word_before_cursor() 19 for color in colors: 20 if color.startswith(word): 21 yield Completion( 22 color, 23 start_position=-len(word), 24 style='fg:' + color, 25 selected_style='fg:white bg:' + color) 26 27 28def main(): 29 # Simple completion menu. 30 print('(The completion menu displays colors.)') 31 prompt('Type a color: ', completer=ColorCompleter()) 32 33 # Multi-column menu. 34 prompt('Type a color: ', completer=ColorCompleter(), 35 complete_style=CompleteStyle.MULTI_COLUMN) 36 37 # Readline-like 38 prompt('Type a color: ', completer=ColorCompleter(), 39 complete_style=CompleteStyle.READLINE_LIKE) 40 41 # Prompt with true color output. 42 message = [('#cc2244', 'T'), ('#bb4444', 'r'), ('#996644', 'u'), ('#cc8844', 'e '), 43 ('#ccaa44', 'C'), ('#bbaa44', 'o'), ('#99aa44', 'l'), 44 ('#778844', 'o'), ('#55aa44', 'r '), 45 ('#33aa44', 'p'), ('#11aa44', 'r'), ('#11aa66', 'o'), 46 ('#11aa88', 'm'), ('#11aaaa', 'p'), ('#11aacc', 't'), 47 ('#11aaee', ': ')] 48 prompt(message, completer=ColorCompleter(), color_depth=ColorDepth.TRUE_COLOR) 49 50 51if __name__ == '__main__': 52 main() 53