|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +An example of a BufferControl in a full screen layout that offers auto |
| 4 | +completion. |
| 5 | +
|
| 6 | +Important is to make sure that there is a `CompletionsMenu` in the layout, |
| 7 | +otherwise the completions won't be visible. |
| 8 | +""" |
| 9 | + |
| 10 | +from prompt_toolkit.application import Application |
| 11 | +from prompt_toolkit.buffer import Buffer |
| 12 | +from prompt_toolkit.filters import Condition |
| 13 | +from prompt_toolkit.formatted_text import to_formatted_text, HTML |
| 14 | +from prompt_toolkit.key_binding import KeyBindings |
| 15 | +from prompt_toolkit.layout.containers import Float, FloatContainer, HSplit, Window |
| 16 | +from prompt_toolkit.layout.controls import BufferControl, FormattedTextControl |
| 17 | +from prompt_toolkit.layout.layout import Layout |
| 18 | +from prompt_toolkit.layout.menus import CompletionsMenu |
| 19 | + |
| 20 | +LIPSUM = ' '.join("""\ |
| 21 | +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas |
| 22 | +quis interdum enim. Nam viverra, mauris et blandit malesuada, ante est bibendum |
| 23 | +mauris, ac dignissim dui tellus quis ligula. Aenean condimentum leo at |
| 24 | +dignissim placerat. In vel dictum ex, vulputate accumsan mi. Donec ut quam |
| 25 | +placerat massa tempor elementum. Sed tristique mauris ac suscipit euismod. Ut |
| 26 | +tempus vehicula augue non venenatis. Mauris aliquam velit turpis, nec congue |
| 27 | +risus aliquam sit amet. Pellentesque blandit scelerisque felis, faucibus |
| 28 | +consequat ante. Curabitur tempor tortor a imperdiet tincidunt. Nam sed justo |
| 29 | +sit amet odio bibendum congue. Quisque varius ligula nec ligula gravida, sed |
| 30 | +convallis augue faucibus. Nunc ornare pharetra bibendum. Praesent blandit ex |
| 31 | +quis sodales maximus.""".split('\n')) |
| 32 | + |
| 33 | + |
| 34 | +def get_line_prefix(lineno, wrap_count): |
| 35 | + if wrap_count == 0: |
| 36 | + return HTML('[%s] <style bg="orange" fg="black">--></style> ') % lineno |
| 37 | + |
| 38 | + text = str(lineno) + "-" + "*" * (lineno // 2) + ": " |
| 39 | + return HTML('[%s.%s] <style bg="ansigreen" fg="ansiblack">%s</style>') % ( |
| 40 | + lineno, |
| 41 | + wrap_count, |
| 42 | + text, |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +# Global wrap lines flag. |
| 47 | +wrap_lines = True |
| 48 | + |
| 49 | + |
| 50 | +# The layout |
| 51 | +buff = Buffer(complete_while_typing=True) |
| 52 | +buff.text = LIPSUM |
| 53 | + |
| 54 | + |
| 55 | +body = FloatContainer( |
| 56 | + content=HSplit( |
| 57 | + [ |
| 58 | + Window( |
| 59 | + FormattedTextControl( |
| 60 | + 'Press "q" to quit. Press "w" to enable/disable wrapping.' |
| 61 | + ), |
| 62 | + height=1, |
| 63 | + style="reverse", |
| 64 | + ), |
| 65 | + # Break words |
| 66 | + Window( |
| 67 | + BufferControl(buffer=buff), |
| 68 | + wrap_lines=Condition(lambda: wrap_lines), |
| 69 | + word_wrap=False, |
| 70 | + ), |
| 71 | + # Default word wrapping |
| 72 | + Window( |
| 73 | + BufferControl(buffer=buff), |
| 74 | + wrap_lines=Condition(lambda: wrap_lines), |
| 75 | + word_wrap=True, |
| 76 | + ), |
| 77 | + # Add a marker to signify continuation |
| 78 | + Window( |
| 79 | + BufferControl(buffer=buff), |
| 80 | + wrap_lines=True, |
| 81 | + wrap_finder=Window._whitespace_wrap_finder( |
| 82 | + lambda n: [] if n else to_formatted_text(LIPSUM), |
| 83 | + continuation = to_formatted_text(' ⮠'), |
| 84 | + ), |
| 85 | + get_line_prefix=lambda lineno, wrap_count: to_formatted_text(' ⭢ ') if wrap_count else [] |
| 86 | + ), |
| 87 | + # Truncating (only wrap the first timle around) |
| 88 | + Window( |
| 89 | + BufferControl(buffer=buff), |
| 90 | + wrap_lines=True, |
| 91 | + wrap_finder=lambda lineno, wrap_count, start, end, fallback=Window._whitespace_wrap_finder( |
| 92 | + lambda n: [] if n else to_formatted_text(LIPSUM), |
| 93 | + ): (end - 1, -1, '…') if wrap_count > 0 else fallback(lineno, wrap_count, start, end) |
| 94 | + ), |
| 95 | + ] |
| 96 | + ), |
| 97 | + floats=[ |
| 98 | + Float( |
| 99 | + xcursor=True, |
| 100 | + ycursor=True, |
| 101 | + content=CompletionsMenu(max_height=16, scroll_offset=1), |
| 102 | + ) |
| 103 | + ], |
| 104 | +) |
| 105 | + |
| 106 | + |
| 107 | +# Key bindings |
| 108 | +kb = KeyBindings() |
| 109 | + |
| 110 | + |
| 111 | +@kb.add("q") |
| 112 | +@kb.add("c-c") |
| 113 | +def _(event): |
| 114 | + "Quit application." |
| 115 | + event.app.exit() |
| 116 | + |
| 117 | + |
| 118 | +@kb.add("w") |
| 119 | +def _(event): |
| 120 | + "Disable/enable wrapping." |
| 121 | + global wrap_lines |
| 122 | + wrap_lines = not wrap_lines |
| 123 | + |
| 124 | + |
| 125 | +# The `Application` |
| 126 | +application = Application( |
| 127 | + layout=Layout(body), key_bindings=kb, full_screen=True, mouse_support=True |
| 128 | +) |
| 129 | + |
| 130 | + |
| 131 | +def run(): |
| 132 | + application.run() |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + run() |
0 commit comments