-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an interactive Python command console #145
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
* Copyright (c) 2022, Yung-Yu Chen <yyc@solvcon.net> | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* - Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* - Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* - Neither the name of the copyright holder nor the names of its contributors | ||
* may be used to endorse or promote products derived from this software | ||
* without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#include <modmesh/view/RPythonConsoleDockWidget.hpp> | ||
#include <QVBoxLayout> | ||
#include <QKeyEvent> | ||
|
||
namespace modmesh | ||
{ | ||
|
||
void RPythonConsoleDockWidget::appendPastCommand(const std::string & code) | ||
{ | ||
if (code.size() > 0) | ||
{ | ||
m_past_command_strings.push_back(code); | ||
if (m_past_command_strings.size() > m_past_limit) | ||
{ | ||
m_past_command_strings.pop_front(); | ||
} | ||
} | ||
} | ||
|
||
void RPythonCommandTextEdit::keyPressEvent(QKeyEvent * event) | ||
{ | ||
if (Qt::Key_Return == event->key()) | ||
{ | ||
execute(); | ||
} | ||
else if (Qt::Key_Up == event->key()) | ||
{ | ||
navigate(/* offset */ -1); | ||
} | ||
else if (Qt::Key_Down == event->key()) | ||
{ | ||
navigate(/* offset */ 1); | ||
} | ||
else | ||
{ | ||
QTextEdit::keyPressEvent(event); | ||
} | ||
} | ||
|
||
RPythonConsoleDockWidget::RPythonConsoleDockWidget(const QString & title, QWidget * parent, Qt::WindowFlags flags) | ||
: QDockWidget(title, parent, flags) | ||
, m_history_edit(new QTextEdit) | ||
, m_command_edit(new RPythonCommandTextEdit) | ||
{ | ||
setWidget(new QWidget); | ||
widget()->setLayout(new QVBoxLayout); | ||
|
||
m_history_edit->setFont(QFont("Courier New")); | ||
m_history_edit->setPlainText(QString("")); | ||
m_history_edit->setReadOnly(true); | ||
widget()->layout()->addWidget(m_history_edit); | ||
|
||
m_command_edit->setFont(QFont("Courier New")); | ||
m_command_edit->setPlainText(QString("")); | ||
m_command_edit->setFixedHeight(40); | ||
widget()->layout()->addWidget(m_command_edit); | ||
|
||
connect(m_command_edit, &RPythonCommandTextEdit::execute, this, &RPythonConsoleDockWidget::executeCommand); | ||
connect(m_command_edit, &RPythonCommandTextEdit::navigate, this, &RPythonConsoleDockWidget::navigateCommand); | ||
} | ||
|
||
void RPythonConsoleDockWidget::executeCommand() | ||
{ | ||
std::string const code = m_command_edit->toPlainText().toStdString(); | ||
appendPastCommand(code); | ||
m_history_edit->insertPlainText(m_command_edit->toPlainText()); | ||
m_history_edit->insertPlainText("\n"); | ||
m_command_edit->setPlainText(""); | ||
m_command_string = ""; | ||
m_current_command_index = static_cast<int>(m_past_command_strings.size()); | ||
auto & interp = modmesh::python::Interpreter::instance(); | ||
interp.exec_code(code); | ||
} | ||
|
||
void RPythonConsoleDockWidget::navigateCommand(int offset) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Navigate through the history. Slight complexity. |
||
{ | ||
int const cmdsize = static_cast<int>(m_past_command_strings.size()); // make msc happy. | ||
if (cmdsize == m_current_command_index) | ||
{ | ||
m_command_string = m_command_edit->toPlainText().toStdString(); | ||
} | ||
|
||
int new_index = m_current_command_index + offset; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Limit the range of |
||
if (new_index > cmdsize) | ||
{ | ||
new_index = cmdsize; | ||
} | ||
else if (new_index < 0) | ||
{ | ||
new_index = 0; | ||
} | ||
|
||
if ((0 == m_past_command_strings.size()) || (new_index == m_current_command_index)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there's nothing in the history or the new index does not change, do nothing. |
||
{ | ||
// do nothing | ||
} | ||
else | ||
{ | ||
if (new_index > m_current_command_index) | ||
{ | ||
if (new_index >= static_cast<int>(m_past_command_strings.size())) | ||
{ | ||
m_command_edit->setPlainText(QString::fromStdString(m_command_string)); | ||
} | ||
else | ||
{ | ||
m_command_edit->setPlainText(QString::fromStdString(m_past_command_strings[new_index])); | ||
} | ||
m_current_command_index = new_index; | ||
} | ||
else // new_index < m_current_command_index | ||
{ | ||
m_command_edit->setPlainText(QString::fromStdString(m_past_command_strings[new_index])); | ||
m_current_command_index = new_index; | ||
} | ||
// Move cursor to the end of line. | ||
{ | ||
QTextCursor cursor = m_command_edit->textCursor(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
cursor.movePosition(QTextCursor::EndOfLine); | ||
m_command_edit->setTextCursor(cursor); | ||
} | ||
} | ||
} | ||
|
||
} /* end namespace modmesh */ | ||
|
||
// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4: |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#pragma once | ||
|
||
/* | ||
* Copyright (c) 2022, Yung-Yu Chen <yyc@solvcon.net> | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* - Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* - Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* - Neither the name of the copyright holder nor the names of its contributors | ||
* may be used to endorse or promote products derived from this software | ||
* without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#include <modmesh/python/common.hpp> // must be first. | ||
|
||
#include <string> | ||
#include <deque> | ||
|
||
#include <Qt> | ||
#include <QDockWidget> | ||
#include <QTextEdit> | ||
|
||
namespace modmesh | ||
{ | ||
|
||
class RPythonCommandTextEdit | ||
: public QTextEdit | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
|
||
void keyPressEvent(QKeyEvent * event) override; | ||
|
||
signals: | ||
|
||
void execute(); | ||
void navigate(int offset); | ||
|
||
}; /* end class RPythonCommandTextEdit */ | ||
|
||
class RPythonConsoleDockWidget | ||
: public QDockWidget | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
|
||
explicit RPythonConsoleDockWidget( | ||
QString const & title = "Console", | ||
QWidget * parent = nullptr, | ||
Qt::WindowFlags flags = Qt::WindowFlags()); | ||
|
||
public slots: | ||
|
||
void executeCommand(); | ||
void navigateCommand(int offset); | ||
|
||
private: | ||
|
||
void appendPastCommand(std::string const & code); | ||
|
||
QTextEdit * m_history_edit = nullptr; | ||
RPythonCommandTextEdit * m_command_edit = nullptr; | ||
std::string m_command_string; | ||
std::deque<std::string> m_past_command_strings; | ||
int m_current_command_index = 0; | ||
size_t m_past_limit = 1024; | ||
|
||
}; /* end class RPythonConsoleDockWidget */ | ||
|
||
} /* end namespace modmesh */ | ||
|
||
// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4: |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,16 +52,18 @@ class AppEnvironment: | |
""" | ||
Collects the environment for an application. | ||
|
||
:ivar globals: | ||
The global namespace of the application. | ||
:ivar locals: | ||
The local namespace of the application. | ||
""" | ||
def __init__(self, name): | ||
self.globals = {} | ||
self.locals = { | ||
self.globals = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to have the symbols to appear in the "module global". |
||
# Give the application an alias of the top package. | ||
'mm': importlib.import_module('modmesh'), | ||
'appenv': self, | ||
} | ||
self.locals = {} | ||
self.name = name | ||
# Each run of the application appends a new environment. | ||
environ[name] = self | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,14 +34,17 @@ | |
import sys | ||
import os | ||
import argparse | ||
import traceback | ||
|
||
import modmesh | ||
from . import view | ||
from . import apputil | ||
|
||
|
||
__all__ = [ | ||
'setup_process', | ||
'enter_main', | ||
'exec_code', | ||
] | ||
|
||
|
||
|
@@ -111,4 +114,14 @@ def enter_main(argv): | |
sys.stderr.write('mode "{}" is not supported'.format(args.mode)) | ||
return ret | ||
|
||
|
||
def exec_code(code): | ||
try: | ||
apputil.run_code(code) | ||
except Exception as e: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this doesn't work well I think. |
||
sys.stdout.write("code:\n{}\n".format(code)) | ||
sys.stdout.write("{}: {}\n".format(type(e).__name__, str(e))) | ||
sys.stdout.write("traceback:\n") | ||
traceback.print_stack() | ||
|
||
# vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Allow the console to be at the bottom or the top. Having it in the left or right could make it too narrow.