-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shell): adds improvements in repl session
This commit introduces `shell` sub-command and deprecates the `repl` sub-command. Following improvements were made: * Current Project Prompt * Reads Prompt Toolkit options from Config * Adds support for suspending the session
- Loading branch information
1 parent
b38c7a0
commit b7a481e
Showing
11 changed files
with
180 additions
and
57 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
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
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
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,68 @@ | ||
# Copyright 2022 Rapyuta Robotics | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License 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. | ||
import os | ||
|
||
import click | ||
from click_help_colors import HelpColorsCommand | ||
from click_repl import repl | ||
from prompt_toolkit.history import FileHistory, ThreadedHistory | ||
|
||
from riocli.config import Configuration | ||
from riocli.shell.prompt import prompt_callback | ||
|
||
|
||
@click.command( | ||
cls=HelpColorsCommand, | ||
help_headers_color='yellow', | ||
help_options_color='green', | ||
) | ||
@click.pass_context | ||
def shell(ctx: click.Context): | ||
""" | ||
Interactive Shell for Rapyuta.io | ||
""" | ||
start_shell(ctx) | ||
|
||
|
||
@click.command( | ||
'repl', | ||
cls=HelpColorsCommand, | ||
help_headers_color='yellow', | ||
help_options_color='green', | ||
hidden=True | ||
) | ||
@click.pass_context | ||
def deprecated_repl(ctx: click.Context): | ||
""" | ||
[Deprecated] Use "rio shell" instead | ||
""" | ||
start_shell(ctx) | ||
|
||
|
||
def start_shell(ctx: click.Context): | ||
prompt_config = _parse_config(ctx.obj) | ||
repl(click.get_current_context(), prompt_kwargs=prompt_config) | ||
|
||
|
||
def _parse_config(config: Configuration) -> dict: | ||
history_path = os.path.join(click.get_app_dir(config.APP_NAME), "history") | ||
default_prompt_kwargs = { | ||
'history': ThreadedHistory(FileHistory(history_path)), | ||
'message': prompt_callback, | ||
'enable_suspend': True | ||
} | ||
|
||
shell_config = config.data.get('shell', {}) | ||
|
||
return {**default_prompt_kwargs, **shell_config} |
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,19 @@ | ||
# Copyright 2022 Rapyuta Robotics | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License 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. | ||
import click | ||
|
||
|
||
@click.pass_context | ||
def prompt_callback(ctx: click.Context) -> str: | ||
return '{} > '.format(ctx.obj.data['project_name']) |
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,27 @@ | ||
# Copyright 2022 Rapyuta Robotics | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License 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 click import Context | ||
|
||
|
||
def get_root_context(ctx: Context) -> Context: | ||
""" | ||
get_root_context figures out the top-level Context from the given context by walking down the linked-list. | ||
https://click.palletsprojects.com/en/8.0.x/complex/#contexts | ||
""" | ||
while True: | ||
if ctx.parent is None: | ||
return ctx | ||
|
||
ctx = ctx.parent |