ReplView
Header: examples/UI/REPL/ReplView.h
Namespace: global
ReplView is a terminal-style REPL (Read-Eval-Print Loop) UI framework built on M5GFX LGFX_Sprite (canvas). It provides a keyboard-driven interface with a prompt, input buffer, cursor blinking, and command callbacks. Useful for building interactive shells, debug consoles, or text-based UIs on the M5Cardputer.
Callbacks
onRenderTips
Called during init() after clearing the screen. Use this to render header text, usage hints, or ASCII art at the top of the screen.
repl.onRenderTips = []() {
M5Cardputer.Display.println("=== My App v1.0 ===");
M5Cardputer.Display.println("Type 'help' for commands");
};
onCommand
Called when the user presses Enter with a non-empty input buffer. Receives the raw command string.
repl.onCommand = [](const std::string& cmd) {
if (cmd == "help") {
M5Cardputer.Display.println("Available: help, clear, exit");
} else if (cmd == "clear") {
// ...
}
};
Fields
autoClearPrompt
When true (default), the input buffer is automatically cleared after onCommand is called. Set to false if you want to retain the input buffer after command execution.
Methods
init(canvas)
Initialize the REPL on a given canvas. Calls render_interface() which:
1. Clears the screen to black
2. Sets default font (efontCN_16) and text scroll mode
3. Calls onRenderTips if set
4. Renders the initial prompt
| Parameter | Type | Description |
|---|---|---|
canvas |
LGFX_Sprite* |
Pointer to an LGFX_Sprite (usually &M5Cardputer.Display) |
update()
Call every loop() iteration. Handles:
- Keyboard input via M5Cardputer.Keyboard (calls isChange() + keysState())
- Blinking cursor every 500ms
- Enter key → calls onCommand callback
- Backspace key → removes last character from input buffer
- Printable characters → appended to input buffer
clearScreen()
Clears the entire screen (fills black, resets cursor to top-left, pushes sprite).
showMessage(message, color)
Prints a line of text to the canvas and pushes it to the display. Useful for outputting results after commands.
| Parameter | Type | Default | Description |
|---|---|---|---|
message |
const std::string& |
— | Text to display |
color |
uint16_t |
TFT_WHITE |
Text color |
repl.showMessage("Command executed.", TFT_GREEN);
repl.showMessage("Error: invalid input", TFT_RED);
showPrompt(prompt_text)
Changes the prompt text displayed before the input cursor and re-renders the current line.
| Parameter | Type | Default | Description |
|---|---|---|---|
prompt_text |
const std::string& |
">>> " |
New prompt string |
setPromptText(prompt)
Changes the prompt text without re-rendering. Use showPrompt() if you want immediate visual update; use setPromptText() if you want the change to appear on the next render cycle.
setInputBuffer(text)
Sets the current input buffer content and re-renders the prompt line. Used for programmatic input injection.
getInputBuffer()
Returns the current input buffer content.
clearInputBuffer()
Clears the input buffer and re-renders the prompt.
Complete Example
#include <M5Cardputer.h>
#include "examples/UI/REPL/ReplView.h"
ReplView repl;
void setup() {
auto cfg = M5.config();
M5Cardputer.begin(cfg, true);
repl.onRenderTips = []() {
M5Cardputer.Display.println("=== REPL Demo ===");
M5Cardputer.Display.println("Commands: help, hello, clear");
};
repl.onCommand = [](const std::string& cmd) {
if (cmd == "help") {
repl.showMessage("help - show this message", TFT_CYAN);
repl.showMessage("hello - greet", TFT_CYAN);
repl.showMessage("clear - clear screen", TFT_CYAN);
} else if (cmd == "hello") {
repl.showMessage("Hello, M5Cardputer!", TFT_GREEN);
} else if (cmd == "clear") {
repl.clearScreen();
repl.onRenderTips();
repl.showPrompt();
} else {
repl.showMessage("Unknown: " + cmd, TFT_RED);
}
};
repl.init(&M5Cardputer.Display);
}
void loop() {
M5Cardputer.update();
repl.update();
}
Note
ReplView is distributed as an example class, not part of the src/ library. To use it, copy ReplView.h and ReplView.cpp into your project or add the examples directory to your include path.