def initializeContent(self): bufferStore = dict() for name, initialValue, location, updateFunction in self.bufferSpecs: buff = Buffer(name=name) if initialValue is not None: buff.text = initialValue elif updateFunction is not None: buff.text = updateFunction() bufferStore[name] = (buff, location, updateFunction) self.bufferStore = bufferStore
def row(label, default): buffer=Buffer() buffer.text = default return VSplit([ Label(HTML(f'{label}'), width=10), Window(width=2, char=": "), Window(content=BufferControl(buffer=buffer)), ])
def pager(self, html, end="\n"): """ Construct pager using prompt_toolkit """ my_buffer = Buffer() my_window = Window( BufferControl( buffer=my_buffer, focusable=True, preview_search=True, input_processors=[FormatText()], )) my_buffer.text = html my_buffer.read_only = to_filter(True) root_container = HSplit([ my_window, ]) bindings = KeyBindings() @bindings.add("c-c") @bindings.add("q") def _(event): "Quit." event.app.exit() application = Application( layout=Layout( root_container, focused_element=my_window, ), key_bindings=bindings, enable_page_navigation_bindings=True, mouse_support=True, full_screen=True, ) application.run() super().msg(html=html, end=end)
quis interdum enim. Nam viverra, mauris et blandit malesuada, ante est bibendum mauris, ac dignissim dui tellus quis ligula. Aenean condimentum leo at dignissim placerat. In vel dictum ex, vulputate accumsan mi. Donec ut quam placerat massa tempor elementum. Sed tristique mauris ac suscipit euismod. Ut tempus vehicula augue non venenatis. Mauris aliquam velit turpis, nec congue risus aliquam sit amet. Pellentesque blandit scelerisque felis, faucibus consequat ante. Curabitur tempor tortor a imperdiet tincidunt. Nam sed justo sit amet odio bibendum congue. Quisque varius ligula nec ligula gravida, sed convallis augue faucibus. Nunc ornare pharetra bibendum. Praesent blandit ex quis sodales maximus.""" # Create text buffers. Cursorcolumn/cursorline are mostly combined with an # (editable) text buffers, where the user can move the cursor. buff = Buffer() buff.text = LIPSUM # 1. The layout body = HSplit([ Window(FormattedTextControl('Press "q" to quit.'), height=1, style='reverse'), Window(BufferControl(buffer=buff), cursorcolumn=True, cursorline=True), ]) # 2. Key bindings kb = KeyBindings() @kb.add('q') def _(event):
quis interdum enim. Nam viverra, mauris et blandit malesuada, ante est bibendum mauris, ac dignissim dui tellus quis ligula. Aenean condimentum leo at dignissim placerat. In vel dictum ex, vulputate accumsan mi. Donec ut quam placerat massa tempor elementum. Sed tristique mauris ac suscipit euismod. Ut tempus vehicula augue non venenatis. Mauris aliquam velit turpis, nec congue risus aliquam sit amet. Pellentesque blandit scelerisque felis, faucibus consequat ante. Curabitur tempor tortor a imperdiet tincidunt. Nam sed justo sit amet odio bibendum congue. Quisque varius ligula nec ligula gravida, sed convallis augue faucibus. Nunc ornare pharetra bibendum. Praesent blandit ex quis sodales maximus.""" # Create text buffers. Cursorcolumn/cursorline are mostly combined with an # (editable) text buffers, where the user can move the cursor. buff = Buffer() buff.text = LIPSUM # 1. The layout body = HSplit([ Window(FormattedTextControl('Press "q" to quit.'), height=1, style='reverse'), Window(BufferControl(buffer=buff), cursorcolumn=True, cursorline=True), ]) # 2. Key bindings kb = KeyBindings() @kb.add('q') def _(event): " Quit application. "
def text_area(title, text, lexer_name="", height=10, full_screen=False): """ Small implementation of an editor/pager for small pieces of text. :param title: Title of the text_area :type title: str :param text: Editable text :type text: str :param lexer_name: If the editable text should be highlighted with some kind of grammar, examples are ``yaml``, ``python`` ... :type lexer_name: str :param height: Max height of the text area :type height: int :param full_screen: Wether or not the text area should be full screen. :type full_screen: bool """ from prompt_toolkit import Application from prompt_toolkit.enums import EditingMode from prompt_toolkit.buffer import Buffer from prompt_toolkit.layout.containers import HSplit, Window, WindowAlign from prompt_toolkit.layout.controls import (BufferControl, FormattedTextControl) from prompt_toolkit.layout.layout import Layout from prompt_toolkit.layout import Dimension from prompt_toolkit.key_binding import KeyBindings from prompt_toolkit.lexers import PygmentsLexer from pygments.lexers import find_lexer_class_by_name assert (type(title) == str) assert (type(text) == str) assert (type(lexer_name) == str) assert (type(height) == int) assert (type(full_screen) == bool) kb = KeyBindings() buffer1 = Buffer() buffer1.text = text @kb.add('c-q') def exit_(event): event.app.exit(0) @kb.add('c-s') def save_(event): event.app.return_text = buffer1.text class App(Application): return_text = None text_height = Dimension(min=0, max=height) if height is not None else None pygment_lexer = find_lexer_class_by_name(lexer_name) lexer = PygmentsLexer(pygment_lexer) text_window = Window(height=text_height, content=BufferControl(buffer=buffer1, lexer=lexer)) root_container = HSplit([ Window(char='-', align=WindowAlign.CENTER, height=1, content=FormattedTextControl(text=[('fg:ansiblack bg:ansiwhite', title)]), always_hide_cursor=True), text_window, Window(height=1, width=None, align=WindowAlign.CENTER, char='-', content=FormattedTextControl( text=[('fg:ansiblack bg:ansiwhite', "Quit [Ctrl-q] Save [Ctrl-s]")])), ]) layout = Layout(root_container) layout.focus(text_window) app = App(editing_mode=(EditingMode.EMACS if papis.config.get( 'editmode', section='tui') == 'emacs' else EditingMode.VI), layout=layout, key_bindings=kb, full_screen=full_screen) app.run() return app.return_text
def pager(title, text, lexer_name="", height=10, full_screen=False): """Small implementation of an editor/pager for small pieces of text. :param title: Title of the pager :type title: str :param text: Editable text :type text: str :param lexer_name: If the editable text should be highlighted with some kind of grammar, examples are ``yaml``, ``python`` ... :type lexer_name: str :param height: Max height of the text area :type height: int :param full_screen: Wether or not the text area should be full screen. :type full_screen: bool :return: Edited text, if saved, else None :rtype : str """ from prompt_toolkit import Application from prompt_toolkit.enums import EditingMode from prompt_toolkit.buffer import Buffer from prompt_toolkit.layout.containers import HSplit from prompt_toolkit.layout.layout import Layout from prompt_toolkit.key_binding import KeyBindings from prompt_toolkit.widgets import HorizontalLine from .utils.general import ( get_default_window, get_buffer_window ) assert(type(title) == str) assert(type(text) == str) assert(type(lexer_name) == str) assert(type(height) == int) assert(type(full_screen) == bool) buffer = Buffer() buffer.text = text # Define keybindings for pager key_bindings = KeyBindings() # Leave pager @key_bindings.add('c-c') def exit_(event): event.app.exit(0) # Leave pager and save edited text @key_bindings.add('c-a') def save_(event): event.app.return_text = buffer.text event.app.exit(1) class Pager(Application): return_text = None # Generate windows with titles and text, as well as define layout of pager text_window = get_buffer_window(buffer, height=height, lexer_name=lexer_name, style=user._tui_text_style) layout= Layout(HSplit([ get_default_window(text=title, style=user._tui_window_style), HorizontalLine(), text_window , get_default_window(text="Deny [Ctrl-c] Accept [Ctrl-a]", style=user._tui_window_style), ]) ) layout.focus(text_window) # Get object of class Pager, set layout, edit mode as well as key bindings and run the pager pager = Pager( editing_mode=( EditingMode.EMACS if user._tui_edit_mode == 'emacs' else EditingMode.VI ), layout=layout, key_bindings=key_bindings, full_screen=full_screen ) pager.run() # Return the edited text, if saved return pager.return_text
Horizontal split example. """ from __future__ import unicode_literals import json from prompt_toolkit.application import Application from prompt_toolkit.key_binding import KeyBindings from prompt_toolkit.buffer import Buffer from prompt_toolkit.layout.containers import HSplit, Window, Float, FloatContainer from prompt_toolkit.layout.controls import FormattedTextControl, BufferControl from prompt_toolkit.layout.layout import Layout from prompt_toolkit.widgets import Frame edit_buffer = Buffer() edit_buffer.text = '' mid_buffer = Buffer() lower_buffer = Buffer() from pypher import Pypher, builder, create_function, create_statement from pypher.builder import __ builder.CHECK_CUSTOM_CLASHES = False def input(_): try: p = Pypher() cmd = ('{text} \n'
def text_area( title: str, text: str, lexer_name: str = "", height: int = 10, full_screen: bool = False) -> str: """ Small implementation of an editor/pager for small pieces of text. :param title: Title of the text_area :type title: str :param text: Editable text :type text: str :param lexer_name: If the editable text should be highlighted with some kind of grammar, examples are ``yaml``, ``python`` ... :type lexer_name: str :param height: Max height of the text area :type height: int :param full_screen: Wether or not the text area should be full screen. :type full_screen: bool """ from prompt_toolkit import Application from prompt_toolkit.enums import EditingMode from prompt_toolkit.buffer import Buffer from prompt_toolkit.layout.containers import HSplit, Window, WindowAlign from prompt_toolkit.layout.controls import ( BufferControl, FormattedTextControl ) from prompt_toolkit.layout.layout import Layout from prompt_toolkit.utils import Event from prompt_toolkit.layout import Dimension from prompt_toolkit.key_binding import KeyBindings from prompt_toolkit.lexers import PygmentsLexer from pygments.lexers import find_lexer_class_by_name kb = KeyBindings() buffer1 = Buffer() buffer1.text = text @kb.add('c-q') # type: ignore def exit_(event: Event) -> None: event.app.exit(0) @kb.add('c-s') # type: ignore def save_(event: Event) -> None: event.app.return_text = buffer1.text class App(Application): # type: ignore # TODO: add stubs to be able to remove type ignore above return_text = "" # type: str text_height = Dimension(min=0, max=height) if height is not None else None pygment_lexer = find_lexer_class_by_name(lexer_name) lexer = PygmentsLexer(pygment_lexer) text_window = Window( height=text_height, style='bg:black fg:ansiwhite', content=BufferControl(buffer=buffer1, lexer=lexer)) root_container = HSplit([ Window( align=WindowAlign.LEFT, style='bg:ansiwhite', height=1, content=FormattedTextControl( text=[('fg:ansiblack bg:ansiwhite', title)] ), always_hide_cursor=True ), text_window, Window( height=1, width=None, align=WindowAlign.LEFT, style='bg:ansiwhite', content=FormattedTextControl( text=[( 'fg:ansiblack bg:ansiwhite', "Quit [Ctrl-q] Save [Ctrl-s]" )] ) ), ]) layout = Layout(root_container) layout.focus(text_window) app = App( editing_mode=EditingMode.EMACS, layout=layout, key_bindings=kb, full_screen=full_screen) app.run() return app.return_text
body = res.content.decode('utf-8', errors='ignore') if showBody else "") url_argument = parse_args() url = urlparse(url_argument.url) hostvalue = url.hostname if url.port is None else f"{url.hostname}:{url.port}" default = f"""GET {url.path}?{url.query} HTTP/1.1 Host: {hostvalue} User-Agent: sendrequest.py Accept: */* Accept-Encoding: gzip, deflate """ request_buffer = Buffer( completer=http_completer) request_buffer.text = default request_control = BufferControl( request_buffer, lexer=PygmentsLexer(HttpLexer, ), focus_on_click=True) request_window = Window(request_control, wrap_lines=True) response_buffer = Buffer(completer=http_completer) response_control = BufferControl( response_buffer, lexer=PygmentsLexer(HttpLexer), focus_on_click=True) response_window = Window(response_control, wrap_lines=True) request_frame = Frame(request_window, title="Request") response_frame = Frame(response_window, title="Response") root_container = VSplit([ request_frame, response_frame ],)
def _handle_message_entered(self, buffer: Buffer): self.send_message(buffer.text) buffer.text = ""