8 #include "../apps_container.h" 16 static const char * sStandardPromptText =
">>> ";
27 m_rowHeight(
KDText::charSize(k_fontSize).height()),
28 m_importScriptsWhenViewAppears(
false),
29 m_selectableTableView(this, this, this, this),
30 m_editCell(this, this),
31 m_pythonHeap(nullptr),
32 m_scriptStore(scriptStore),
33 m_sandboxController(this),
34 m_inputRunLoopActive(
false)
36 , m_locked(lockOnConsole)
41 m_editCell.
setPrompt(sStandardPromptText);
42 for (
int i = 0; i < k_numberOfLineCells; i++) {
55 emptyOutputAccumulationBuffer();
56 m_pythonHeap = (
char *)
malloc(k_pythonHeapSize);
57 if (m_pythonHeap ==
nullptr) {
64 m_importScriptsWhenViewAppears = autoImportScripts;
73 m_pythonHeap =
nullptr;
78 return (m_pythonHeap !=
nullptr);
89 assert(m_outputAccumulationBuffer[0] ==
'\0');
92 flushOutputAccumulationBufferToStore();
98 m_inputRunLoopActive =
true;
108 return c->inputRunLoopActive();
111 flushOutputAccumulationBufferToStore();
113 m_editCell.
setPrompt(sStandardPromptText);
115 return m_editCell.
text();
121 if (m_importScriptsWhenViewAppears) {
122 m_importScriptsWhenViewAppears =
false;
137 askInputRunLoopTermination();
157 return copyCurrentLineToClipboard();
160 m_consoleStore.
clear();
199 if (type == LineCellType) {
200 assert(index < k_numberOfLineCells);
201 return m_cells+index;
203 assert(type == EditCellType);
210 if (type == LineCellType) {
211 return k_numberOfLineCells;
241 if (previousSelectedCellY > -1 && previousSelectedCellY < m_consoleStore.
numberOfLines()) {
263 return static_cast<App *
>(textField->
app())->textInputDidReceiveEvent(textField, event);
267 if (inputRunLoopActive()) {
268 askInputRunLoopTermination();
283 if (inputRunLoopActive()) {
284 askInputRunLoopTermination();
289 stackViewController()->
pop();
309 stackViewController()->
push(&m_sandboxController);
315 size_t textCutIndex = firstNewLineCharIndex(text, length);
318 if (textCutIndex >= length) {
319 appendTextToOutputAccumulationBuffer(text, length);
324 if (textCutIndex < length - 1) {
326 printText(&text[textCutIndex+1], length - (textCutIndex + 1));
331 if (textCutIndex == length - 1) {
332 appendTextToOutputAccumulationBuffer(text, length-1);
333 flushOutputAccumulationBufferToStore();
340 char command[k_maxImportCommandSize];
341 size_t currentChar =
strlcpy(command, k_importCommand1,
strlen(k_importCommand1)+1);
342 const char * scriptName = script.
name();
343 currentChar +=
strlcpy(command+currentChar, scriptName,
strlen(scriptName)+1);
346 currentChar +=
strlcpy(command+currentChar, k_importCommand2,
strlen(k_importCommand2)+1);
357 void ConsoleController::flushOutputAccumulationBufferToStore() {
358 m_consoleStore.
pushResult(m_outputAccumulationBuffer,
strlen(m_outputAccumulationBuffer));
359 emptyOutputAccumulationBuffer();
362 void ConsoleController::appendTextToOutputAccumulationBuffer(
const char * text,
size_t length) {
363 int endOfAccumulatedText =
strlen(m_outputAccumulationBuffer);
364 int spaceLeft = k_outputAccumulationBufferSize - endOfAccumulatedText;
365 if (spaceLeft > (
int)length) {
366 memcpy(&m_outputAccumulationBuffer[endOfAccumulatedText], text, length);
369 memcpy(&m_outputAccumulationBuffer[endOfAccumulatedText], text, spaceLeft-1);
370 flushOutputAccumulationBufferToStore();
371 appendTextToOutputAccumulationBuffer(&text[spaceLeft-1], length - (spaceLeft - 1));
374 void ConsoleController::emptyOutputAccumulationBuffer() {
375 for (
int i = 0; i < k_outputAccumulationBufferSize; i++) {
376 m_outputAccumulationBuffer[i] = 0;
380 size_t ConsoleController::firstNewLineCharIndex(
const char * text,
size_t length) {
382 while (index < length) {
383 if (text[index] ==
'\n') {
395 bool ConsoleController::copyCurrentLineToClipboard() {
void tableViewDidChangeSelection(SelectableTableView *t, int previousSelectedCellX, int previousSelectedCellY) override
void setText(const char *text)
int deleteCommandAndResultsAtIndex(int index)
void willDisplayCellAtLocation(HighlightCell *cell, int i, int j) override
void displaySandbox() override
bool textFieldDidReceiveEvent(TextField *textField, Ion::Events::Event event) override
void runCode(const char *)
int numberOfLines() const
HighlightCell * reusableCell(int index, int type) override
bool insertText(const char *text)
int typeAtLocation(int i, int j) override
bool pythonEnvironmentIsLoaded()
void init(void *heapStart, void *heapEnd)
HighlightCell * selectedCell()
KDCoordinate cumulatedHeightFromIndex(int j) override
void setText(const char *text)
static constexpr KDCoordinate CommonRightMargin
void setPrompt(const char *prompt)
void push(ViewController *vc, KDColor textColor=Palette::SubTab, KDColor backgroundColor=KDColorWhite, KDColor separatorColor=Palette::GreyBright)
Responder * parentResponder() const
void autoImportScript(Script script, bool force=false)
size_t strlcpy(char *dst, const char *src, size_t len)
bool textFieldDidAbortEditing(TextField *textField, const char *text) override
bool selectCellAtLocation(int i, int j, bool setFirstResponder=true)
LIBA_BEGIN_DECLS void free(void *ptr)
const char * inputText(const char *prompt) override
HighlightCell * cellAtLocation(int i, int j)
Toolbox * toolboxForTextInput(TextInput *textInput) override
virtual void setEditing(bool isEditing, bool reinitDraftBuffer=true)
bool textFieldDidFinishEditing(TextField *textField, const char *text, Ion::Events::Event event) override
void printText(const char *text, size_t length) override
void store(const char *storedText)
void reloadData(bool setFirstResponder=true)
constexpr KDColor KDColorWhite
size_t strlen(const char *s)
const char * text() const
PythonToolbox * pythonToolbox()
void setParentResponder(Responder *responder)
Script scriptAtIndex(int index)
void registerScriptProvider(ScriptProvider *s)
void deleteLastLineIfEmpty()
bool handleEvent(Ion::Events::Event event) override
ConsoleController(Responder *parentResponder, ScriptStore *scriptStore)
const char * name() const
int numberOfRows() override
void * malloc(size_t size)
void runWhile(bool(*callback)(void *ctx), void *ctx)
bool textFieldShouldFinishEditing(TextField *textField, Ion::Events::Event event) override
bool importationStatus() const
const Container * container() const
size_t draftTextLength() const
KDCoordinate rowHeight(int j) override
void setFirstResponder(Responder *responder)
void runAndPrintForCommand(const char *command)
void pushCommand(const char *text, size_t length)
void setEditing(bool isEditing, bool reinitDraftBuffer=false)
void pushResult(const char *text, size_t length)
static Clipboard * sharedClipboard()
void unloadPythonEnvironment()
bool loadPythonEnvironment(bool autoImportScripts=true)
bool m_sandboxIsDisplayed
void reloadCell() override
static constexpr KDCoordinate TitleBarExternHorizontalMargin
ConsoleLine lineAtIndex(int i) const
const char * text() const
void * memcpy(void *dst, const void *src, size_t n)
static constexpr char k_scriptExtension[]
int reusableCellCount(int type) override
void viewWillAppear() override
constexpr Event Backspace
void didBecomeFirstResponder() override
int indexFromCumulatedHeight(KDCoordinate offsetY) override