Skip to content

Commit

Permalink
Don't pass 'Application' around anymore. Store it in a global instead.
Browse files Browse the repository at this point in the history
The idea is that there can't be more than one active UI anyway. There is
only one stdin and stdout. Passing it around everywhere is way too
cumbersome and this made the code much cleaner in many regards.

For pymux, where we'll need multiple applications, we'll find a
work-around. Maybe by setting the application each time right after
receiving data.
  • Loading branch information
jonathanslenders committed Jun 1, 2017
1 parent eeddec0 commit 6f65bef
Show file tree
Hide file tree
Showing 47 changed files with 762 additions and 917 deletions.
11 changes: 6 additions & 5 deletions examples/full-screen/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
from __future__ import unicode_literals
from prompt_toolkit.application import Application
from prompt_toolkit.application.current import get_app
from prompt_toolkit.key_binding.bindings.focus import focus_next, focus_previous
from prompt_toolkit.key_binding.defaults import load_key_bindings
from prompt_toolkit.key_binding.key_bindings import KeyBindings, merge_key_bindings
Expand All @@ -13,20 +14,20 @@


# Event handlers for all the buttons.
def button1_clicked(app):
def button1_clicked():
text_area.text = 'Button 1 clicked'


def button2_clicked(app):
def button2_clicked():
text_area.text = 'Button 2 clicked'


def button3_clicked(app):
def button3_clicked():
text_area.text = 'Button 3 clicked'


def exit_clicked(app):
app.set_return_value(None)
def exit_clicked():
get_app().set_return_value(None)


# All the widgets for the UI.
Expand Down
2 changes: 1 addition & 1 deletion examples/full-screen/split-screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
# but the user doesn't see any feedback. We will add the search toolbar to the
# bottom by using an HSplit.

def get_titlebar_text(app):
def get_titlebar_text():
return [
('class:title', ' Hello world '),
('class:title', ' (Press [Ctrl-Q] to quit.)'),
Expand Down
75 changes: 38 additions & 37 deletions examples/full-screen/text-editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import unicode_literals

from prompt_toolkit.application import Application
from prompt_toolkit.application.current import get_app
from prompt_toolkit.contrib.completers import PathCompleter
from prompt_toolkit.eventloop import Future, ensure_future, Return
from prompt_toolkit.key_binding.defaults import load_key_bindings
Expand All @@ -28,10 +29,10 @@
show_status_bar = True


def get_statusbar_text(app):
def get_statusbar_text():
return ' Press Ctrl-C to open menu. '

def get_statusbar_right_text(app):
def get_statusbar_right_text():
return ' {}:{} '.format(
text_field.document.cursor_position_row + 1,
text_field.document.cursor_position_col + 1)
Expand All @@ -50,14 +51,14 @@ class TextInputDialog(object):
def __init__(self, title='', label_text='', completer=None):
self.future = Future()

def accept_text(app):
app.layout.focus(ok_button)
def accept_text():
get_app().layout.focus(ok_button)
self.text_area.buffer.complete_state = None

def accept(app):
def accept():
self.future.set_result(self.text_area.text)

def cancel(app):
def cancel():
self.future.set_result(None)

self.text_area = TextArea(
Expand Down Expand Up @@ -90,7 +91,7 @@ def __init__(self, title, text):
def set_done():
self.future.set_result(None)

ok_button = Button(text='OK', handler=(lambda app: set_done()))
ok_button = Button(text='OK', handler=(lambda: set_done()))

self.dialog = Dialog(
title=title,
Expand All @@ -114,7 +115,7 @@ def __pt_container__(self):
Window(FormattedTextControl(get_statusbar_right_text),
style='class:status.right', width=9, align=Align.RIGHT),
], height=1),
filter=Condition(lambda _: show_status_bar)),
filter=Condition(lambda: show_status_bar)),
])

# Global key bindings.
Expand All @@ -130,74 +131,74 @@ def _(event):
# Handlers for menu items.
#

def do_open_file(app):
def do_open_file():
def coroutine():
open_dialog = TextInputDialog(
title='Open file',
label_text='Enter the path of a file:',
completer=PathCompleter())

path = yield ensure_future(show_dialog_as_float(app, open_dialog))
path = yield ensure_future(show_dialog_as_float(open_dialog))

if path is not None:
try:
with open(path, 'rb') as f:
text_field.text = f.read().decode('utf-8', errors='ignore')
except IOError as e:
show_message(app, 'Error', '{}'.format(e))
show_message('Error', '{}'.format(e))

ensure_future(coroutine())


def do_about(app):
show_message(app, 'About', 'Text editor demo.\nCreated by Jonathan Slenders.')
def do_about():
show_message('About', 'Text editor demo.\nCreated by Jonathan Slenders.')


def show_message(app, title, text):
def show_message(title, text):
def coroutine():
dialog = MessageDialog(title, text)
yield ensure_future(show_dialog_as_float(app, dialog))
yield ensure_future(show_dialog_as_float(dialog))

ensure_future(coroutine())


def show_dialog_as_float(app, dialog):
def show_dialog_as_float(dialog):
" Coroutine. "
float_ = Float(content=dialog)
root_container.floats.insert(0, float_)

app = get_app()

focussed_before = app.layout.current_window
app.layout.focus(dialog)
result = yield dialog.future
app.layout.focus(focussed_before)


if float_ in root_container.floats:
root_container.floats.remove(float_)

raise Return(result)


def do_new_file(app):
def do_new_file():
text_field.text = ''


def do_exit(app):
app.set_return_value(None)
def do_exit():
get_app().set_return_value(None)

def do_time_date(app):
def do_time_date():
text = datetime.datetime.now().isoformat()
text_field.buffer.insert_text(text)


def do_go_to(app):
def do_go_to():
def coroutine():
dialog = TextInputDialog(
title='Go to line',
label_text='Line number:')

line_number = yield ensure_future(show_dialog_as_float(app, dialog))
line_number = yield ensure_future(show_dialog_as_float(dialog))

try:
line_number = int(line_number)
Expand All @@ -209,47 +210,47 @@ def coroutine():

ensure_future(coroutine())

def do_undo(app):
def do_undo():
text_field.buffer.undo()


def do_cut(app):
def do_cut():
data = text_field.buffer.cut_selection()
app.clipboard.set_data(data)
get_app().clipboard.set_data(data)


def do_copy(app):
def do_copy():
data = text_field.buffer.copy_selection()
app.clipboard.set_data(data)
get_app().clipboard.set_data(data)


def do_delete(app):
def do_delete():
text_field.buffer.cut_selection()


def do_find(app):
app.layout.focus(search_field)
def do_find():
get_app().layout.focus(search_field)


def do_find_next(app):
search_state = app.current_search_state
def do_find_next():
search_state = get_app().current_search_state

cursor_position = text_field.buffer.get_search_position(
search_state, include_current_position=False)
text_field.buffer.cursor_position = cursor_position


def do_paste(app):
text_field.buffer.paste_clipboard_data(app.clipboard.get_data())
def do_paste():
text_field.buffer.paste_clipboard_data(get_app().clipboard.get_data())


def do_select_all(app):
def do_select_all():
text_field.buffer.cursor_position = 0
text_field.buffer.start_selection()
text_field.buffer.cursor_position = len(text_field.buffer.text)


def do_status_bar(app):
def do_status_bar():
global show_status_bar
show_status_bar = not show_status_bar

Expand Down
15 changes: 8 additions & 7 deletions examples/full-screen/yes-no-dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@
from __future__ import unicode_literals

from prompt_toolkit.application import Application
from prompt_toolkit.application.current import get_app
from prompt_toolkit.contrib.completers import WordCompleter
from prompt_toolkit.key_binding.bindings.focus import focus_next, focus_previous
from prompt_toolkit.key_binding.defaults import load_key_bindings
from prompt_toolkit.key_binding.key_bindings import KeyBindings, merge_key_bindings
from prompt_toolkit.layout.containers import VSplit, HSplit, Float
from prompt_toolkit.layout.layout import Layout
from prompt_toolkit.layout.dimension import D
from prompt_toolkit.layout.layout import Layout
from prompt_toolkit.layout.lexers import PygmentsLexer
from prompt_toolkit.layout.menus import CompletionsMenu
from prompt_toolkit.layout.widgets import TextArea, Label, Frame, Box, Checkbox, Dialog, Button, RadioList, MenuContainer, MenuItem, ProgressBar
from prompt_toolkit.styles import Style, merge_styles, default_style
from pygments.lexers import HtmlLexer


def accept_yes(app):
app.set_return_value(True)
def accept_yes():
get_app().set_return_value(True)

def accept_no(app):
app.set_return_value(False)
def accept_no():
get_app().set_return_value(False)

def do_exit(app):
app.set_return_value(False)
def do_exit():
get_app().set_return_value(False)


yes_button = Button(text='Yes', handler=accept_yes)
Expand Down
4 changes: 2 additions & 2 deletions examples/prompts/bottom-toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def main():
print('You said: %s' % text)

# Example 2: fixed text from a callable:
def get_toolbar(app):
def get_toolbar():
return 'Bottom toolbar: time=%r' % time.time()

text = prompt('Say something: ', bottom_toolbar=get_toolbar,
Expand All @@ -46,7 +46,7 @@ def get_toolbar(app):
print('You said: %s' % text)

# Example 5: Using a list of tokens.
def get_bottom_toolbar(app):
def get_bottom_toolbar():
return [
('', ' '),
('bg:#ff0000 fg:#000000', 'This'),
Expand Down
2 changes: 1 addition & 1 deletion examples/prompts/custom-lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class RainbowLexer(Lexer):
def lex_document(self, app, document):
def lex_document(self, document):
colors = list(sorted(NAMED_COLORS, key=NAMED_COLORS.get))

def get_line(lineno):
Expand Down
2 changes: 1 addition & 1 deletion examples/prompts/finalterm-shell-integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
AFTER_OUTPUT = '\033]133;D;{command_status}\a' # command_status is the command status, 0-255


def get_prompt_text(app):
def get_prompt_text():
# Generate the text fragments for the prompt.
# Important: use the `ZeroWidthEscape` fragment only if you are sure that
# writing this as raw text to the output will not introduce any
Expand Down
2 changes: 1 addition & 1 deletion examples/prompts/get-multiline-input.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from prompt_toolkit import prompt


def prompt_continuation(app, width):
def prompt_continuation(width):
" The continuation: display dots before all the following lines. "

# (make sure that the width of the continuation does not exceed the given
Expand Down
2 changes: 1 addition & 1 deletion examples/prompts/rprompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
})


def get_rprompt_text(app):
def get_rprompt_text():
return [
('', ' '),
('underline', '<rprompt>'),
Expand Down
5 changes: 3 additions & 2 deletions examples/prompts/switch-between-vi-emacs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
from prompt_toolkit import prompt
from prompt_toolkit.application.current import get_app
from prompt_toolkit.enums import EditingMode
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.styles import Style
Expand All @@ -22,9 +23,9 @@ def _(event):
else:
event.app.editing_mode = EditingMode.VI

def bottom_toolbar(app):
def bottom_toolbar():
" Display the current input mode. "
if app.editing_mode == EditingMode.VI:
if get_app().editing_mode == EditingMode.VI:
return ' [F4] Vi '
else:
return ' [F4] Emacs '
Expand Down
Loading

0 comments on commit 6f65bef

Please sign in to comment.