-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5728 from kyleknap/improved-wizard-init
[v2] Initial groundwork for wizard improvements
- Loading branch information
Showing
12 changed files
with
674 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
# ANY KIND, either express or implied. See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
from prompt_toolkit.application import Application | ||
from prompt_toolkit.key_binding import KeyBindings | ||
|
||
from awscli.customizations.wizard.ui.keybindings import get_default_keybindings | ||
from awscli.customizations.wizard.ui.layout import WizardLayoutFactory | ||
from awscli.customizations.wizard.ui.style import get_default_style | ||
|
||
|
||
def create_wizard_app(definition): | ||
layout_factory = WizardLayoutFactory() | ||
app = Application( | ||
key_bindings=get_default_keybindings(), | ||
style=get_default_style(), | ||
layout=layout_factory.create_wizard_layout(definition), | ||
full_screen=True, | ||
) | ||
app.values = {} | ||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
# ANY KIND, either express or implied. See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
from prompt_toolkit.key_binding import KeyBindings | ||
|
||
|
||
def get_default_keybindings(): | ||
kb = KeyBindings() | ||
|
||
@kb.add('c-c') | ||
def exit_(event): | ||
event.app.exit() | ||
|
||
def submit_current_answer(event): | ||
current_buffer = event.app.current_buffer | ||
event.app.values[current_buffer.name] = current_buffer.text | ||
|
||
@kb.add('tab') | ||
@kb.add('enter') | ||
def next_prompt(event): | ||
submit_current_answer(event) | ||
event.app.layout.focus_next() | ||
|
||
@kb.add('s-tab') | ||
def last_prompt(event): | ||
submit_current_answer(event) | ||
event.app.layout.focus_last() | ||
|
||
return kb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
# ANY KIND, either express or implied. See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
from prompt_toolkit.layout import Layout | ||
from prompt_toolkit.layout.containers import WindowAlign, HSplit | ||
from prompt_toolkit.widgets import Label | ||
|
||
from awscli.customizations.wizard.ui.prompt import WizardPrompt | ||
|
||
|
||
class WizardLayoutFactory: | ||
def create_wizard_layout(self, defintion): | ||
return Layout( | ||
container=HSplit( | ||
[ | ||
self._create_title(defintion), | ||
self._create_all_prompts(defintion) | ||
], | ||
padding=1, | ||
) | ||
) | ||
|
||
def _create_title(self, definition): | ||
title = Label(f'{definition["title"]}', style='class:wizard.title') | ||
title.window.align = WindowAlign.CENTER | ||
return title | ||
|
||
def _create_all_prompts(self, definition): | ||
prompts = [] | ||
for step_definition in definition['plan'].values(): | ||
prompts.extend( | ||
self._create_prompts_from_step_definition(step_definition) | ||
) | ||
return HSplit(prompts, padding=0) | ||
|
||
def _create_prompts_from_step_definition(self, step_definition): | ||
prompts = [] | ||
for value_name, value_definition in step_definition['values'].items(): | ||
if value_definition['type'] == 'prompt': | ||
prompts.append(WizardPrompt(value_name, value_definition)) | ||
return prompts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
# ANY KIND, either express or implied. See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
from prompt_toolkit.application import get_app | ||
from prompt_toolkit.buffer import Buffer | ||
from prompt_toolkit.document import Document | ||
from prompt_toolkit.layout.containers import Window, VSplit, Dimension | ||
from prompt_toolkit.layout.controls import BufferControl | ||
|
||
|
||
class WizardPrompt: | ||
def __init__(self, value_name, value_definition): | ||
self._value_name = value_name | ||
self._value_definition = value_definition | ||
self.container = self._get_container() | ||
|
||
def _get_container(self): | ||
return VSplit( | ||
[ | ||
WizardPromptDescription( | ||
self._value_name, | ||
self._value_definition['description'] | ||
), | ||
WizardPromptAnswer(self._value_name) | ||
] | ||
) | ||
|
||
def __pt_container__(self): | ||
return self.container | ||
|
||
|
||
class WizardPromptDescription: | ||
def __init__(self, value_name, value_description): | ||
self._value_name = value_name | ||
self._value_description = value_description | ||
self.container = self._get_container() | ||
|
||
def _get_container(self): | ||
content = f'{self._value_description}:' | ||
buffer = Buffer( | ||
document=Document(content), | ||
read_only=True | ||
) | ||
return Window( | ||
content=BufferControl( | ||
buffer=buffer, focusable=False | ||
), | ||
style=self._get_style, | ||
width=Dimension.exact(len(content) + 1), | ||
dont_extend_height=True, | ||
) | ||
|
||
def _get_style(self): | ||
if get_app().layout.has_focus(self._value_name): | ||
return 'class:wizard.prompt.description.current' | ||
else: | ||
return 'class:wizard.prompt.description' | ||
|
||
def __pt_container__(self): | ||
return self.container | ||
|
||
|
||
class WizardPromptAnswer: | ||
def __init__(self, value_name): | ||
self._value_name = value_name | ||
self.container = self._get_answer_container() | ||
|
||
def _get_answer_container(self): | ||
return Window( | ||
content=BufferControl( | ||
buffer=Buffer(name=self._value_name) | ||
), | ||
style=self._get_style, | ||
dont_extend_height=True, | ||
) | ||
|
||
def _get_style(self): | ||
if get_app().layout.has_focus(self._value_name): | ||
return 'class:wizard.prompt.answer.current' | ||
else: | ||
return 'class:wizard.prompt.answer' | ||
|
||
def __pt_container__(self): | ||
return self.container |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
# ANY KIND, either express or implied. See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
from prompt_toolkit.styles import Style | ||
|
||
|
||
def get_default_style(): | ||
return Style( | ||
[ | ||
('wizard', ''), | ||
('wizard.title', 'underline bold'), | ||
('wizard.prompt.description', 'bold'), | ||
('wizard.prompt.description.current', 'white'), | ||
('wizard.prompt.answer', ''), | ||
('wizard.prompt.answer.current', 'white'), | ||
] | ||
) |
Oops, something went wrong.