Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.53 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. """
  4. from __future__ import unicode_literals
  5.  
  6. from pygments.lexers.html import HtmlLexer
  7.  
  8. from prompt_toolkit.application import Application
  9. from prompt_toolkit.application.current import get_app
  10. from prompt_toolkit.completion import WordCompleter
  11. from prompt_toolkit.key_binding import KeyBindings
  12. from prompt_toolkit.key_binding.bindings.focus import (
  13. focus_next,
  14. focus_previous,
  15. )
  16. from prompt_toolkit.layout.containers import Float, HSplit, VSplit, Window
  17. from prompt_toolkit.layout.dimension import D
  18. from prompt_toolkit.layout.layout import Layout
  19. from prompt_toolkit.lexers import PygmentsLexer
  20. from prompt_toolkit.buffer import Buffer
  21. from prompt_toolkit.layout.controls import BufferControl, FormattedTextControl
  22. from prompt_toolkit.layout.menus import CompletionsMenu
  23. from prompt_toolkit.lexers import PygmentsLexer
  24. from prompt_toolkit.styles import Style
  25. from prompt_toolkit.shortcuts import yes_no_dialog
  26. from prompt_toolkit.widgets import (
  27. Box,
  28. Button,
  29. Checkbox,
  30. Dialog,
  31. Frame,
  32. Label,
  33. MenuContainer,
  34. MenuItem,
  35. ProgressBar,
  36. RadioList,
  37. TextArea,
  38. )
  39.  
  40. LIPSUM = ' '.join(("""Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  41. Maecenas quis interdum enim. Nam viverra, mauris et blandit malesuada, ante est
  42. bibendum mauris, ac digngtgtissim dui tellus quis ligula. Aenean condimentum leo at
  43. dignissim placerat. In vel dictum ex, vulputate accumsan mi. Donec ut quam
  44. placerat massa tempor elementum. Sed tristique mauris ac suscipit euismod. Ut
  45. tempus vehicula augue non venenatis. Mauris aliquam velit turpis, nec congue
  46. risus aliquam sit amet. Pellentesque blandit scelerisque felis, faucibus
  47. consequat ante. Curabitur tempor tortor a imperdiet tincidunt. Nam sed justo
  48. sit amet odio bibendum congue. Quisque varius ligula nec ligula gravida, sed
  49. convallis augue faucibus. Nunc ornare pharetra bibendum. Praesent blandit ex
  50. quis sodales maximus. """ * 100).split())
  51.  
  52. floats2 = Float(
  53. Frame(Window(FormattedTextControl(LIPSUM), width=80),
  54. style='bg:#ff44ff #ffffff'),
  55. top=1,
  56. height=50)
  57.  
  58.  
  59.  
  60. def accept_yes():
  61. get_app().exit(result=True)
  62.  
  63.  
  64. def accept_no():
  65. get_app().exit(result=False)
  66.  
  67.  
  68. def do_exit():
  69. get_app().exit(result=False)
  70.  
  71.  
  72. yes_button = Button(text='Yes', handler=accept_yes)
  73. no_button = Button(text='No', handler=accept_no)
  74. textfield = TextArea(lexer=PygmentsLexer(HtmlLexer))
  75. checkbox1 = Checkbox(text='Checkbox')
  76. checkbox2 = Checkbox(text='Checkbox')
  77.  
  78. radios = RadioList(values=[
  79. ('Red', 'red'),
  80. ('Green', 'green'),
  81. ('Blue', 'blue'),
  82. ('Orange', 'orange'),
  83. ('Yellow', 'yellow'),
  84. ('Purple', 'Purple'),
  85. ('Brown', 'Brown'),
  86. ])
  87.  
  88. animal_completer = WordCompleter([
  89. 'alligator', 'ant', 'ape', 'bat', 'bear', 'beaver', 'bee', 'bison',
  90. 'butterfly', 'cat', 'chicken', 'crocodile', 'dinosaur', 'dog', 'dolphin',
  91. 'dove', 'duck', 'eagle', 'elephant', 'fish', 'goat', 'gorilla', 'kangaroo',
  92. 'leopard', 'lion', 'mouse', 'rabbit', 'rat', 'snake', 'spider', 'turkey',
  93. 'turtle', ], ignore_case=True)
  94.  
  95. root_container = HSplit([
  96. VSplit([
  97. Frame(body=Label(text='Left framencontent')),
  98. Dialog(title='The custom window',
  99. body=Label('hellontest')),
  100. textfield,
  101. ], height=D()),
  102. VSplit([
  103. Frame(body=ProgressBar(),
  104. title='Progress bar'),
  105. Frame(title='Checkbox list',
  106. body=HSplit([
  107. checkbox1,
  108. checkbox2,
  109. ])),
  110. Frame(title='Radio list', body=radios),
  111. ], padding=1),
  112. Box(
  113. body=VSplit([
  114. yes_button,
  115. no_button,
  116. ], align='CENTER', padding=3),
  117. style='class:button-bar',
  118. height=3,
  119. ),
  120. ])
  121.  
  122. root_container = MenuContainer(body=root_container, menu_items=[
  123. MenuItem('File', children=[
  124. MenuItem('New'),
  125. MenuItem('Open', children=[
  126. MenuItem('From file...'),
  127. MenuItem('From URL...'),
  128. MenuItem('Something else..', children=[
  129. MenuItem('A'),
  130. MenuItem('B'),
  131. MenuItem('C'),
  132. MenuItem('D'),
  133. MenuItem('E'),
  134. ]),
  135. ]),
  136. MenuItem('Save'),
  137. MenuItem('Save as...'),
  138. MenuItem('-', disabled=True),
  139. MenuItem('Exit', handler=do_exit),
  140. ]),
  141. MenuItem('Edit', children=[
  142. MenuItem('Undo'),
  143. MenuItem('Cut'),
  144. MenuItem('Copy'),
  145. MenuItem('Paste'),
  146. MenuItem('Delete'),
  147. MenuItem('-', disabled=True),
  148. MenuItem('Find'),
  149. MenuItem('Find next'),
  150. MenuItem('Replace'),
  151. MenuItem('Go To'),
  152. MenuItem('Select All'),
  153. MenuItem('Time/Date'),
  154. ]),
  155. MenuItem('View', children=[
  156. MenuItem('Status Bar'),
  157. ]),
  158. MenuItem('Info', children=[
  159. MenuItem('About'),
  160. ]),
  161. ], floats=[
  162. Float(xcursor=True,
  163. ycursor=True,
  164. content=CompletionsMenu(
  165. max_height=16,
  166. scroll_offset=1)),
  167. ])
  168.  
  169. # Global key bindings.
  170. bindings = KeyBindings()
  171. bindings.add('tab')(focus_next)
  172. bindings.add('s-tab')(focus_previous)
  173.  
  174.  
  175. style = Style.from_dict({
  176. 'window.border': '#888888',
  177. 'shadow': 'bg:#222222',
  178.  
  179. 'menu-bar': 'bg:#aaaaaa #888888',
  180. 'menu-bar.selected-item': 'bg:#ffffff #000000',
  181. 'menu': 'bg:#888888 #ffffff',
  182. 'menu.border': '#aaaaaa',
  183. 'window.border shadow': '#444444',
  184.  
  185. 'focused button': 'bg:#880000 #ffffff noinherit',
  186.  
  187. # Styling for Dialog widgets.
  188.  
  189. 'radiolist focused': 'noreverse',
  190. 'radiolist focused radio.selected': 'reverse',
  191.  
  192. 'button-bar': 'bg:#aaaaff'
  193. })
  194.  
  195. @bindings.add('c-c', eager=True)
  196. @bindings.add('c-q', eager=True)
  197. def _(event):
  198. """
  199. Pressing Ctrl-Q or Ctrl-C will exit the user interface.
  200.  
  201. Setting a return value means: quit the event loop that drives the user
  202. interface and return this value from the `Application.run()` call.
  203.  
  204. Note that Ctrl-Q does not work on all terminals. Sometimes it requires
  205. executing `stty -ixon`.
  206. """
  207. event.app.exit()
  208.  
  209. @bindings.add('w')
  210. def mola(event):
  211. if not root_container.container.floats:
  212. root_container.container.floats = [floats2,]
  213. else:
  214. root_container.container.floats = []
  215.  
  216. @bindings.add('t')
  217. def ask(event):
  218. result = yes_no_dialog(
  219. title='Yes/No dialog example',
  220. text='Do you want to confirm?').run()
  221.  
  222. application = Application(
  223. layout=Layout(
  224. root_container,
  225. focused_element=yes_button,
  226. ),
  227. key_bindings=bindings,
  228. style=style,
  229. mouse_support=True,
  230. full_screen=True)
  231.  
  232.  
  233. def run():
  234. result = application.run()
  235. print('You said: %r' % result)
  236.  
  237.  
  238. if __name__ == '__main__':
  239. run()
  240.  
  241. def make_yes_no_dialog(yes_handler, no_handler):
  242. yes_btn = Button(text="YES", handler=yes_handler)
  243. no_btn = Button(text="NO", handler=no_handler)
  244. dialog = Dialog(
  245. title='Yes/No dialog example',
  246. body=Label(text='Do you want to confirm?', dont_extend_height=True),
  247. buttons=[yes_btn, no_btn],
  248. with_background=True
  249. )
  250. return dialog
  251.  
  252.  
  253. def yes_handler():
  254. root_container.container.floats.remove(float_dialog)
  255. # Que quieres hacer cuando se pulsa YES
  256.  
  257. def no_handler():
  258. root_container.container.floats.remove(float_dialog)
  259. # Que quieres hacer cuando se pulsa NO
  260.  
  261. dialog = make_yes_no_dialog(yes_handler, no_handler)
  262. float_dialog = Float(dialog)
  263.  
  264. @bindings.add('t')
  265. def ask(event):
  266. if not float_dialog in root_container.container.floats:
  267. root_container.container.floats.append(float_dialog)
  268. event.app.layout.focus(dialog)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement