![]() ![]() ![]() That concludes our journey through the creation of a powerful REPL that has all the features of a common shell, such as history, key bindings, and user-friendly features such as auto-completion, fuzzy finding, pager support, editor support, and syntax highlighting. Now all user input is treated as SQL statements and colored appropriately. We pick SqlLexer supplied by Pygments and pass it into the prompt API from prompt-toolkit. Prompt Toolkit works well with the Pygments library. Let's use Pygments to add color to our SQL REPL:įrom pygments. Adding syntax highlighting makes an application colorful, which helps users spot mistakes-such as typos, unmatched quotes, or brackets-in their SQL before executing it.įirst install Pygments: pip install pygments ![]() Pygments is a syntax highlighting library with built-in support for more than 300 languages. We are building a SQL REPL, and having colorful SQL statements will be nice. Now let's add syntax highlighting to the user input. Yield Completion (m, start_position =- len (word_before_cursor ) )Ĭlick. ![]() Matches = fuzzyfinder (word_before_cursor, SQLKeywords ) SQLKeywords = ĭef get_completions ( self, document, complete_event ): click.echo_via_pager() will try to use decent defaults for the pager to be able to show color codes if necessary:įrom prompt_toolkit. It is platform-agnostic, so it will work in Unix or Windows. This will take care of sending the output to stdout via a pager. Instead of using the default print() statement, we can use click.echo_via_pager(). Displaying the output of a command via a pager is not just friendly design, but also the decent thing to do. Examples of pagers are less, more, most, etc. Pagers are Unix utilities that display long output one page at a time. Installing click is simple: pip install click Pager This section does not talk about how to use Click as an arguments parser instead, I'm going to look at some utilities that ship with Click. ClickĬlick is a command-line creation toolkit that makes it easy to parse command-line options arguments and parameters for the program. All of that in less than 10 lines of actual code. We now have a REPL that can do auto-completion, fish-style suggestions from history, and up/down traversal of history. Once again, we simply can use a built-in completion routine of prompt-toolkit called WordCompleter, which matches the user input with the dictionary of possible suggestions and offers up a list. SQLCompleter = WordCompleter ( ,Īuto_suggest =AutoSuggestFromHistory ( ) , auto_suggest import AutoSuggestFromHistoryįrom prompt_toolkit. history import FileHistoryįrom prompt_toolkit. Welcome to the communityįrom prompt_toolkit. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2022
Categories |