[python] "input" now displays the provided prompt

This commit is contained in:
Romain Goyet
2018-01-30 14:12:54 +01:00
committed by EmilieNumworks
parent 314fde955a
commit fd7516f8ac
8 changed files with 40 additions and 26 deletions

View File

@@ -14,6 +14,8 @@ extern "C" {
namespace Code {
static const char * sStandardPromptText = ">>> ";
ConsoleController::ConsoleController(Responder * parentResponder, ScriptStore * scriptStore) :
ViewController(parentResponder),
SelectableTableViewDataSource(),
@@ -27,6 +29,7 @@ ConsoleController::ConsoleController(Responder * parentResponder, ScriptStore *
m_scriptStore(scriptStore),
m_sandboxController(this)
{
m_editCell.setPrompt(sStandardPromptText);
for (int i = 0; i < k_numberOfLineCells; i++) {
m_cells[i].setParentResponder(&m_selectableTableView);
}
@@ -75,18 +78,20 @@ void ConsoleController::autoImport() {
void ConsoleController::runAndPrintForCommand(const char * command) {
m_consoleStore.pushCommand(command, strlen(command));
assert(m_outputAccumulationBuffer[0] == '\0');
runCode(command);
flushOutputAccumulationBufferToStore();
m_consoleStore.deleteLastLineIfEmpty();
}
const char * ConsoleController::input() {
const char * ConsoleController::inputText(const char * prompt) {
AppsContainer * a = (AppsContainer *)(app()->container());
m_inputRunLoopActive = true;
m_selectableTableView.reloadData();
m_selectableTableView.selectCellAtLocation(0, m_consoleStore.numberOfLines());
m_editCell.setText("???");
m_editCell.setPrompt(prompt);
m_editCell.setText("");
a->redrawWindow();
a->runWhile([](void * a){
@@ -96,6 +101,7 @@ const char * ConsoleController::input() {
flushOutputAccumulationBufferToStore();
m_consoleStore.deleteLastLineIfEmpty();
m_editCell.setPrompt(sStandardPromptText);
return m_editCell.text();
}

View File

@@ -62,7 +62,7 @@ public:
// MicroPython::ExecutionEnvironment
void displaySandbox() override;
void printText(const char * text, size_t length) override;
const char * input() override;
const char * inputText(const char * prompt) override;
private:
bool inputRunLoopActive() { return m_inputRunLoopActive; }

View File

@@ -10,7 +10,7 @@ ConsoleEditCell::ConsoleEditCell(Responder * parentResponder, TextFieldDelegate
HighlightCell(),
Responder(parentResponder),
m_textBuffer{0},
m_promptView(ConsoleController::k_fontSize, I18n::Message::ConsolePrompt, 0, 0.5),
m_promptView(ConsoleController::k_fontSize, nullptr, 0, 0.5),
m_textField(this, m_textBuffer, m_textBuffer, TextField::maxBufferSize(), delegate, false, ConsoleController::k_fontSize)
{
}
@@ -29,9 +29,9 @@ View * ConsoleEditCell::subviewAtIndex(int index) {
}
void ConsoleEditCell::layoutSubviews() {
KDSize chevronsSize = KDText::stringSize(I18n::translate(I18n::Message::ConsolePrompt), ConsoleController::k_fontSize);
m_promptView.setFrame(KDRect(KDPointZero, chevronsSize.width(), bounds().height()));
m_textField.setFrame(KDRect(KDPoint(chevronsSize.width(), KDCoordinate(0)), bounds().width() - chevronsSize.width(), bounds().height()));
KDSize promptSize = m_promptView.minimalSizeForOptimalDisplay();
m_promptView.setFrame(KDRect(KDPointZero, promptSize.width(), bounds().height()));
m_textField.setFrame(KDRect(KDPoint(promptSize.width(), KDCoordinate(0)), bounds().width() - promptSize.width(), bounds().height()));
}
void ConsoleEditCell::didBecomeFirstResponder() {
@@ -46,6 +46,11 @@ void ConsoleEditCell::setText(const char * text) {
m_textField.setText(text);
}
void ConsoleEditCell::setPrompt(const char * prompt) {
m_promptView.setText(prompt);
layoutSubviews();
}
bool ConsoleEditCell::insertText(const char * text) {
bool didCopy = m_textField.insertTextAtLocation(text, m_textField.cursorLocation());
if (didCopy) {

View File

@@ -5,7 +5,7 @@
#include <escher/highlight_cell.h>
#include <escher/text_field.h>
#include <escher/text_field_delegate.h>
#include <escher/message_text_view.h>
#include <escher/pointer_text_view.h>
namespace Code {
@@ -31,10 +31,10 @@ public:
const char * text() const { return m_textField.text(); }
void setText(const char * text);
bool insertText(const char * text);
void setPrompt(const char * prompt);
private:
char m_textBuffer[TextField::maxBufferSize()];
MessageTextView m_promptView;
PointerTextView m_promptView;
TextField m_textField;
};

View File

@@ -9,20 +9,23 @@ mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs)
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
mp_obj_t mp_builtin_input(size_t n_args, const mp_obj_t *args) {
// 1 - Retrieve the prompt if any
const char * prompt = NULL;
if (n_args == 1) {
prompt = mp_obj_str_get_str(args[0]);
}
// 2 - Perform the HAL input command
const char * result = mp_hal_input(prompt);
// 3 - Log the prompt, result and flush a new line
mp_obj_t resultStr = mp_obj_new_str(result, strlen(result), false);
if (n_args == 1) {
mp_obj_print(args[0], PRINT_STR);
}
/*vstr_t line;
vstr_init(&line, 16);
int ret = mp_hal_readline(&line, "");
if (ret == CHAR_CTRL_C) {
nlr_raise(mp_obj_new_exception(&mp_type_KeyboardInterrupt));
}
if (line.len == 0 && ret == CHAR_CTRL_D) {
nlr_raise(mp_obj_new_exception(&mp_type_EOFError));
}
*/
const char * input = mp_hal_input();
return mp_obj_new_str(input, strlen(input), false);
mp_obj_print(resultStr, PRINT_STR);
mp_print_str(MP_PYTHON_PRINTER, "\n");
return resultStr;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj, 0, 1, mp_builtin_input);

View File

@@ -4,6 +4,6 @@
extern int mp_interrupt_char;
void mp_hal_set_interrupt_char(int c);
void mp_keyboard_interrupt(void);
const char * mp_hal_input(void);
const char * mp_hal_input(const char * prompt);
#endif

View File

@@ -171,7 +171,7 @@ void mp_hal_stdout_tx_strn_cooked(const char * str, size_t len) {
sCurrentExecutionEnvironment->printText(str, len);
}
const char * mp_hal_input() {
const char * mp_hal_input(const char * prompt) {
assert(sCurrentExecutionEnvironment != nullptr);
return sCurrentExecutionEnvironment->input();
return sCurrentExecutionEnvironment->inputText(prompt);
}

View File

@@ -13,7 +13,7 @@ public:
ExecutionEnvironment();
static ExecutionEnvironment * currentExecutionEnvironment();
void runCode(const char * );
virtual const char * input() {
virtual const char * inputText(const char * prompt) {
return nullptr;
}
virtual void displaySandbox() {