Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- """
- """
- from __future__ import unicode_literals
- from pygments.lexers.html import HtmlLexer
- from prompt_toolkit.application import Application
- from prompt_toolkit.application.current import get_app
- from prompt_toolkit.completion import WordCompleter
- from prompt_toolkit.key_binding import KeyBindings
- from prompt_toolkit.key_binding.bindings.focus import (
- focus_next,
- focus_previous,
- )
- from prompt_toolkit.layout.containers import Float, HSplit, VSplit, Window
- from prompt_toolkit.layout.dimension import D
- from prompt_toolkit.layout.layout import Layout
- from prompt_toolkit.lexers import PygmentsLexer
- from prompt_toolkit.buffer import Buffer
- from prompt_toolkit.layout.controls import BufferControl, FormattedTextControl
- from prompt_toolkit.layout.menus import CompletionsMenu
- from prompt_toolkit.lexers import PygmentsLexer
- from prompt_toolkit.styles import Style
- from prompt_toolkit.shortcuts import yes_no_dialog
- from prompt_toolkit.widgets import (
- Box,
- Button,
- Checkbox,
- Dialog,
- Frame,
- Label,
- MenuContainer,
- MenuItem,
- ProgressBar,
- RadioList,
- TextArea,
- )
- LIPSUM = ' '.join(("""Lorem ipsum dolor sit amet, consectetur adipiscing elit.
- Maecenas quis interdum enim. Nam viverra, mauris et blandit malesuada, ante est
- bibendum mauris, ac digngtgtissim 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. """ * 100).split())
- floats2 = Float(
- Frame(Window(FormattedTextControl(LIPSUM), width=80),
- style='bg:#ff44ff #ffffff'),
- top=1,
- height=50)
- def accept_yes():
- get_app().exit(result=True)
- def accept_no():
- get_app().exit(result=False)
- def do_exit():
- get_app().exit(result=False)
- yes_button = Button(text='Yes', handler=accept_yes)
- no_button = Button(text='No', handler=accept_no)
- textfield = TextArea(lexer=PygmentsLexer(HtmlLexer))
- checkbox1 = Checkbox(text='Checkbox')
- checkbox2 = Checkbox(text='Checkbox')
- radios = RadioList(values=[
- ('Red', 'red'),
- ('Green', 'green'),
- ('Blue', 'blue'),
- ('Orange', 'orange'),
- ('Yellow', 'yellow'),
- ('Purple', 'Purple'),
- ('Brown', 'Brown'),
- ])
- animal_completer = WordCompleter([
- 'alligator', 'ant', 'ape', 'bat', 'bear', 'beaver', 'bee', 'bison',
- 'butterfly', 'cat', 'chicken', 'crocodile', 'dinosaur', 'dog', 'dolphin',
- 'dove', 'duck', 'eagle', 'elephant', 'fish', 'goat', 'gorilla', 'kangaroo',
- 'leopard', 'lion', 'mouse', 'rabbit', 'rat', 'snake', 'spider', 'turkey',
- 'turtle', ], ignore_case=True)
- root_container = HSplit([
- VSplit([
- Frame(body=Label(text='Left framencontent')),
- Dialog(title='The custom window',
- body=Label('hellontest')),
- textfield,
- ], height=D()),
- VSplit([
- Frame(body=ProgressBar(),
- title='Progress bar'),
- Frame(title='Checkbox list',
- body=HSplit([
- checkbox1,
- checkbox2,
- ])),
- Frame(title='Radio list', body=radios),
- ], padding=1),
- Box(
- body=VSplit([
- yes_button,
- no_button,
- ], align='CENTER', padding=3),
- style='class:button-bar',
- height=3,
- ),
- ])
- root_container = MenuContainer(body=root_container, menu_items=[
- MenuItem('File', children=[
- MenuItem('New'),
- MenuItem('Open', children=[
- MenuItem('From file...'),
- MenuItem('From URL...'),
- MenuItem('Something else..', children=[
- MenuItem('A'),
- MenuItem('B'),
- MenuItem('C'),
- MenuItem('D'),
- MenuItem('E'),
- ]),
- ]),
- MenuItem('Save'),
- MenuItem('Save as...'),
- MenuItem('-', disabled=True),
- MenuItem('Exit', handler=do_exit),
- ]),
- MenuItem('Edit', children=[
- MenuItem('Undo'),
- MenuItem('Cut'),
- MenuItem('Copy'),
- MenuItem('Paste'),
- MenuItem('Delete'),
- MenuItem('-', disabled=True),
- MenuItem('Find'),
- MenuItem('Find next'),
- MenuItem('Replace'),
- MenuItem('Go To'),
- MenuItem('Select All'),
- MenuItem('Time/Date'),
- ]),
- MenuItem('View', children=[
- MenuItem('Status Bar'),
- ]),
- MenuItem('Info', children=[
- MenuItem('About'),
- ]),
- ], floats=[
- Float(xcursor=True,
- ycursor=True,
- content=CompletionsMenu(
- max_height=16,
- scroll_offset=1)),
- ])
- # Global key bindings.
- bindings = KeyBindings()
- bindings.add('tab')(focus_next)
- bindings.add('s-tab')(focus_previous)
- style = Style.from_dict({
- 'window.border': '#888888',
- 'shadow': 'bg:#222222',
- 'menu-bar': 'bg:#aaaaaa #888888',
- 'menu-bar.selected-item': 'bg:#ffffff #000000',
- 'menu': 'bg:#888888 #ffffff',
- 'menu.border': '#aaaaaa',
- 'window.border shadow': '#444444',
- 'focused button': 'bg:#880000 #ffffff noinherit',
- # Styling for Dialog widgets.
- 'radiolist focused': 'noreverse',
- 'radiolist focused radio.selected': 'reverse',
- 'button-bar': 'bg:#aaaaff'
- })
- @bindings.add('c-c', eager=True)
- @bindings.add('c-q', eager=True)
- def _(event):
- """
- Pressing Ctrl-Q or Ctrl-C will exit the user interface.
- Setting a return value means: quit the event loop that drives the user
- interface and return this value from the `Application.run()` call.
- Note that Ctrl-Q does not work on all terminals. Sometimes it requires
- executing `stty -ixon`.
- """
- event.app.exit()
- @bindings.add('w')
- def mola(event):
- if not root_container.container.floats:
- root_container.container.floats = [floats2,]
- else:
- root_container.container.floats = []
- @bindings.add('t')
- def ask(event):
- result = yes_no_dialog(
- title='Yes/No dialog example',
- text='Do you want to confirm?').run()
- application = Application(
- layout=Layout(
- root_container,
- focused_element=yes_button,
- ),
- key_bindings=bindings,
- style=style,
- mouse_support=True,
- full_screen=True)
- def run():
- result = application.run()
- print('You said: %r' % result)
- if __name__ == '__main__':
- run()
- def make_yes_no_dialog(yes_handler, no_handler):
- yes_btn = Button(text="YES", handler=yes_handler)
- no_btn = Button(text="NO", handler=no_handler)
- dialog = Dialog(
- title='Yes/No dialog example',
- body=Label(text='Do you want to confirm?', dont_extend_height=True),
- buttons=[yes_btn, no_btn],
- with_background=True
- )
- return dialog
- def yes_handler():
- root_container.container.floats.remove(float_dialog)
- # Que quieres hacer cuando se pulsa YES
- def no_handler():
- root_container.container.floats.remove(float_dialog)
- # Que quieres hacer cuando se pulsa NO
- dialog = make_yes_no_dialog(yes_handler, no_handler)
- float_dialog = Float(dialog)
- @bindings.add('t')
- def ask(event):
- if not float_dialog in root_container.container.floats:
- root_container.container.floats.append(float_dialog)
- event.app.layout.focus(dialog)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement