Blame view

build4/epsilon-master/apps/code/console_controller.h 4.67 KB
6663b6c9   adorian   projet complet av...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  #ifndef CODE_CONSOLE_CONTROLLER_H
  #define CODE_CONSOLE_CONTROLLER_H
  
  #include <escher.h>
  #include <python/port/port.h>
  
  #include "console_edit_cell.h"
  #include "console_line_cell.h"
  #include "console_store.h"
  #include "sandbox_controller.h"
  #include "script_store.h"
  
  namespace Code {
  
  class ConsoleController : public ViewController, public ListViewDataSource, public SelectableTableViewDataSource, public SelectableTableViewDelegate, public TextFieldDelegate, public MicroPython::ExecutionEnvironment {
  public:
    static constexpr KDText::FontSize k_fontSize = KDText::FontSize::Large;
  
    ConsoleController(Responder * parentResponder, ScriptStore * scriptStore
  #if EPSILON_GETOPT
        , bool m_lockOnConsole
  #endif
        );
    ~ConsoleController();
    ConsoleController(const ConsoleController& other) = delete;
    ConsoleController(ConsoleController&& other) = delete;
    ConsoleController operator=(const ConsoleController& other) = delete;
    ConsoleController& operator=(ConsoleController&& other) = delete;
  
    bool loadPythonEnvironment(bool autoImportScripts = true);
    void unloadPythonEnvironment();
    bool pythonEnvironmentIsLoaded();
  
    void autoImport();
    void autoImportScript(Script script, bool force = false);
    void runAndPrintForCommand(const char * command);
    bool inputRunLoopActive() { return m_inputRunLoopActive; }
    void askInputRunLoopTermination() { m_inputRunLoopActive = false; }
  
    // ViewController
    View * view() override { return &m_selectableTableView; }
    void viewWillAppear() override;
    void didBecomeFirstResponder() override;
    bool handleEvent(Ion::Events::Event event) override;
    ViewController::DisplayParameter displayParameter() override { return ViewController::DisplayParameter::WantsMaximumSpace; }
  
    // ListViewDataSource
    int numberOfRows() override;
    KDCoordinate rowHeight(int j) override;
    KDCoordinate cumulatedHeightFromIndex(int j) override;
    int indexFromCumulatedHeight(KDCoordinate offsetY) override;
    HighlightCell * reusableCell(int index, int type) override;
    int reusableCellCount(int type) override;
    int typeAtLocation(int i, int j) override;
    void willDisplayCellAtLocation(HighlightCell * cell, int i, int j) override;
  
    // SelectableTableViewDelegate
    void tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY) override;
  
    // TextFieldDelegate
    bool textFieldShouldFinishEditing(TextField * textField, Ion::Events::Event event) override;
    bool textFieldDidReceiveEvent(TextField * textField, Ion::Events::Event event) override;
    bool textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) override;
    bool textFieldDidAbortEditing(TextField * textField) override;
    Toolbox * toolboxForTextInput(TextInput * textInput) override;
  
    // MicroPython::ExecutionEnvironment
    void displaySandbox() override;
    void printText(const char * text, size_t length) override;
    const char * inputText(const char * prompt) override;
  
  #if EPSILON_GETOPT
    bool locked() const {
      return m_locked;
    }
  #endif
  private:
    static constexpr const char * k_importCommand1 = "from ";
    static constexpr const char * k_importCommand2 = " import *";
    static constexpr size_t k_maxImportCommandSize = 5 + 9 + TextField::maxBufferSize(); // strlen(k_importCommand1) + strlen(k_importCommand2) + TextField::maxBufferSize()
    static constexpr int LineCellType = 0;
    static constexpr int EditCellType = 1;
    static constexpr int k_numberOfLineCells = 15; // May change depending on the screen height
    static constexpr int k_pythonHeapSize = 16384;
    static constexpr int k_outputAccumulationBufferSize = 100;
    void flushOutputAccumulationBufferToStore();
    void appendTextToOutputAccumulationBuffer(const char * text, size_t length);
    void emptyOutputAccumulationBuffer();
    size_t firstNewLineCharIndex(const char * text, size_t length);
    StackViewController * stackViewController();
    int m_rowHeight;
    bool m_importScriptsWhenViewAppears;
    ConsoleStore m_consoleStore;
    SelectableTableView m_selectableTableView;
    ConsoleLineCell m_cells[k_numberOfLineCells];
    ConsoleEditCell m_editCell;
    char * m_pythonHeap;
    char m_outputAccumulationBuffer[k_outputAccumulationBufferSize];
    /* The Python machine might call printText several times to print a single
     * string. We thus use m_outputAccumulationBuffer to store and concatenate the
     * different strings until a new line char appears in the text. When this
     * happens, or when m_outputAccumulationBuffer is full, we create a new
     * ConsoleLine in the ConsoleStore and empty m_outputAccumulationBuffer. */
    ScriptStore * m_scriptStore;
    SandboxController m_sandboxController;
    bool m_inputRunLoopActive;
  #if EPSILON_GETOPT
    bool m_locked;
  #endif
  };
  }
  
  #endif