|
libxr
1.0
Want to be the best embedded framework
|
终端类,实现一个基于 RamFS 的基本命令行接口 Terminal class implementing a basic command-line interface based on RamFS More...
#include <terminal.hpp>
Public Types | |
| enum class | Mode : uint8_t { CRLF = 0 , LF = 1 , CR = 2 } |
| 终端换行模式 Line feed modes for the terminal More... | |
Public Member Functions | |
| Terminal (LibXR::RamFS &ramfs, RamFS::Dir *current_dir=nullptr, ReadPort *read_port=STDIO::read_, WritePort *write_port=STDIO::write_, Mode MODE=Mode::CRLF) | |
| 终端构造函数,初始化文件系统、I/O 端口和当前目录 Constructor to initialize the terminal with file system, I/O ports, and current directory | |
| const HistoryLine & | HistoryFromNewest (int index_from_newest) |
| 按“从最新到最旧”的顺序取历史记录 Fetches one history entry counting from newest to oldest | |
| void | LineFeed () |
| 执行换行操作 Performs a line feed operation | |
| void | UpdateDisplayPosition () |
| 更新光标位置 Updates cursor position | |
| bool | CanDisplayChar () |
| 检查是否可以显示字符 Checks if a character can be displayed | |
| bool | CanDeleteChar () |
| 检查是否可以删除字符 Checks if a character can be deleted | |
| void | AddCharToInputLine (char data) |
| 向输入行中添加字符,支持在光标位置插入 Adds a character to the input line, supports insertion at the cursor position | |
| void | DisplayChar (char data) |
| 在终端上显示字符,并根据历史记录模式进行相应操作 Displays a character on the terminal and updates accordingly if history mode is active | |
| void | RemoveCharFromInputLine () |
| 从输入行中删除字符,支持在光标位置删除 Removes a character from the input line, supports deletion at the cursor position | |
| void | DeleteChar () |
| 处理删除字符操作,支持回退删除,并在历史模式下更新显示 Handles the delete character operation, supports backspace deletion, and updates display in history mode | |
| void | ShowHeader () |
| 显示终端提示符,包括当前目录信息 Displays the terminal prompt, including the current directory information | |
| void | ClearLine () |
| 清除当前行 Clears the current line | |
| void | Clear () |
| 清除整个终端屏幕 Clears the entire terminal screen | |
| void | ShowHistory () |
| 显示历史记录中的输入行,更新终端显示 Displays the input line from history and updates the terminal display | |
| void | CopyHistoryToInputLine () |
| 将历史命令复制到输入行,并重置历史索引和光标偏移 Copies the history command to the input line and resets history index and cursor offset | |
| void | AddHistory () |
| 将当前输入行添加到历史记录 Adds the current input line to the history | |
| void | GetArgs () |
| 路径解析、命令执行与自动补全片段 Path-resolution, command-execution, and auto-completion fragment | |
| RamFS::Dir * | Path2Dir (char *path) |
| 将路径字符串解析为目录对象 Converts a path string into a directory object | |
| RamFS::File * | Path2File (char *path) |
| 将路径字符串解析为文件对象 Converts a path string into a file object | |
| void | ExecuteCommand () |
| 解析并执行输入的命令 Parses and executes the entered command | |
| void | AutoComplete () |
| 实现命令自动补全,匹配目录或文件名 Implements command auto-completion by matching directories or file names | |
| void | Parse (RawData &raw_data) |
| 输入字节解析与 ANSI / 控制字符分发片段 Input-byte parsing plus ANSI / control-character dispatch fragment | |
| void | HandleAnsiCharacter (char data) |
| 处理 ANSI 序列中的后续字符 Handles the follow-up characters of an ANSI sequence | |
| void | HandleControlCharacter (char data) |
| 处理控制字符,包括换行、删除、制表符等 Handles control characters such as newline, delete, and tab | |
| void | HandleCharacter (char data) |
| 处理输入字符,根据类型调用相应的处理函数 Handles input characters, dispatching them to the appropriate handler | |
Static Public Member Functions | |
| static size_t | HistoryLineSize (const HistoryLine &line) |
| 行编辑、显示与历史记录片段 Line-editing, display, and history-management fragment | |
| static void | ThreadFun (Terminal *term) |
| 线程 / 轮询驱动入口片段 Threaded / polling driver-entry fragment | |
| static void | TaskFun (Terminal *term) |
| 终端任务函数,以定时器任务方式驱动终端 Terminal task function, drives the terminal using a scheduled task | |
Data Fields | |
| ReadOperation::OperationPollingStatus | read_status_ |
| WriteOperation::OperationPollingStatus | write_status_ |
| 当前读/写轮询状态 / Current polling status of the read / write side. | |
| const Mode | MODE |
| 终端换行模式 Terminal line feed mode | |
| WriteOperation | write_op_ |
| 终端写操作 Terminal write operation | |
| ReadPort * | read_port_ |
| 读取端口 Read port | |
| WritePort * | write_port_ |
| 写入端口 Write port | |
| WritePort::Stream | write_stream_ |
| 写入流 Write stream | |
| LibXR::Mutex * | write_mutex_ = nullptr |
| 写入端口互斥锁 Write port mutex | |
| RamFS & | ramfs_ |
| 关联的文件系统 Associated file system | |
| char | read_buff_ [READ_BUFF_SIZE] |
| 读取缓冲区 Read buffer | |
| size_t | request_read_size_ = 0 |
| 本轮计划读取的字节数 / Byte count requested for the current read attempt. | |
| RamFS::Dir * | current_dir_ |
| 当前目录 Current directory | |
| uint8_t | flag_ansi_ = 0 |
| ANSI 控制字符状态 ANSI control character state. | |
| int | offset_ = 0 |
| 光标偏移 Cursor offset | |
| Stack< char > | input_line_ |
| 输入行缓冲区 Input line buffer | |
| char * | arg_tab_ [MAX_ARG_NUMBER] |
| 命令参数列表 Command argument list | |
| size_t | arg_number_ = 0 |
| 参数数量 Number of arguments | |
| Queue< HistoryLine > | history_ |
| 历史命令 History of commands | |
| int | history_index_ = -1 |
| 当前历史索引 Current history index | |
| bool | linefeed_flag_ = false |
| CRLF 抑制标志 CRLF suppression flag. | |
| char | linefeed_char_ = '\0' |
| 上一个换行字符 Previous line feed character | |
Private Types | |
| using | HistoryLine = std::array<char, MAX_LINE_SIZE + 1> |
| 一条历史命令的固定存储单元 Fixed storage slot for one history command line | |
Private Member Functions | |
| char * | StrchrRev (char *str, char c) |
| 反向查找字符串中的特定字符 Finds a specific character in a string from the end | |
Static Private Attributes | |
| static constexpr char | CLEAR_ALL [] |
| 终端控制序列常量 Terminal control-sequence constants | |
| static constexpr char | CLEAR_LINE [] |
| 清除当前行命令 Clear current line command | |
| static constexpr char | CLEAR_BEHIND [] |
| 清除光标后内容命令 Clear content after cursor command | |
| static constexpr char | KEY_RIGHT [] = "\033[C" |
| 右箭头键 Right arrow key | |
| static constexpr char | KEY_LEFT [] = "\033[D" |
| 左箭头键 Left arrow key | |
| static constexpr char | KEY_SAVE [] = "\033[s" |
| 保存光标位置 Save cursor position | |
| static constexpr char | KEY_LOAD [] = "\033[u" |
| 恢复光标位置 Restore cursor position | |
| static constexpr char | DELETE_CHAR [] |
| 退格删除字符 Backspace delete character | |
终端类,实现一个基于 RamFS 的基本命令行接口 Terminal class implementing a basic command-line interface based on RamFS
| READ_BUFF_SIZE | 读取缓冲区大小 Read buffer size |
| MAX_LINE_SIZE | 最大输入行长度 Maximum input line size |
| MAX_ARG_NUMBER | 最大参数数量 Maximum number of arguments |
| MAX_HISTORY_NUMBER | 最大历史记录数量 Maximum number of command history records |
Definition at line 30 of file terminal.hpp.
|
private |
一条历史命令的固定存储单元 Fixed storage slot for one history command line
Definition at line 37 of file terminal.hpp.
|
strong |
终端换行模式 Line feed modes for the terminal
| Enumerator | |
|---|---|
| CRLF | 回车换行 Carriage Return + Line Feed (\r |
| LF | 仅换行 Line Feed ( |
| CR | 仅回车 Carriage Return (\r) |
Definition at line 87 of file terminal.hpp.
|
inline |
终端构造函数,初始化文件系统、I/O 端口和当前目录 Constructor to initialize the terminal with file system, I/O ports, and current directory
| ramfs | 关联的 RamFS 文件系统 Associated RamFS file system |
| current_dir | 当前目录(默认为根目录)Current directory (default: root) |
| read_port | 读取端口(默认使用标准输入)Read port (default: standard input) |
| write_port | 写入端口(默认使用标准输出)Write port (default: standard output) |
| MODE | 终端换行模式(默认 CRLF)Terminal line feed mode (default: CRLF) |
Definition at line 107 of file terminal.hpp.
|
inline |
向输入行中添加字符,支持在光标位置插入 Adds a character to the input line, supports insertion at the cursor position
| data | 要添加的字符 The character to add |
Definition at line 107 of file terminal.hpp.
|
inline |
将当前输入行添加到历史记录 Adds the current input line to the history
MAX_HISTORY_NUMBER 时,当前实现会丢掉最旧的一条,再压入最新输入。 When MAX_HISTORY_NUMBER is exceeded, the current implementation drops the oldest entry before pushing the newest input line. Definition at line 287 of file terminal.hpp.
|
inline |
实现命令自动补全,匹配目录或文件名 Implements command auto-completion by matching directories or file names
Definition at line 239 of file terminal.hpp.
|
inline |
检查是否可以删除字符 Checks if a character can be deleted
Definition at line 100 of file terminal.hpp.
|
inline |
检查是否可以显示字符 Checks if a character can be displayed
Definition at line 93 of file terminal.hpp.
|
inline |
清除整个终端屏幕 Clears the entire terminal screen
Definition at line 235 of file terminal.hpp.
|
inline |
清除当前行 Clears the current line
Definition at line 229 of file terminal.hpp.
|
inline |
将历史命令复制到输入行,并重置历史索引和光标偏移 Copies the history command to the input line and resets history index and cursor offset
Definition at line 266 of file terminal.hpp.
|
inline |
处理删除字符操作,支持回退删除,并在历史模式下更新显示 Handles the delete character operation, supports backspace deletion, and updates display in history mode
Definition at line 177 of file terminal.hpp.
|
inline |
在终端上显示字符,并根据历史记录模式进行相应操作 Displays a character on the terminal and updates accordingly if history mode is active
| data | 要显示的字符 The character to display |
Definition at line 126 of file terminal.hpp.
|
inline |
解析并执行输入的命令 Parses and executes the entered command
cd 和 ls;其它命令都按“把第一个参数当 RamFS 可执行文件 路径”来处理。 The current implementation provides only the built-in cd and ls; every other command is handled by treating the first argument as the path of an executable RamFS file. Definition at line 152 of file terminal.hpp.
|
inline |
路径解析、命令执行与自动补全片段 Path-resolution, command-execution, and auto-completion fragment
Terminal 的命令解析片段 Command-parsing fragment of Terminal
解析输入行,将其拆分为参数数组 Parses the input line and splits it into argument array
\0,并把每个参数起始位置填进 arg_tab_。 This process overwrites spaces in-place with \0 and stores each argument start pointer into arg_tab_. Definition at line 21 of file terminal.hpp.
|
inline |
处理 ANSI 序列中的后续字符 Handles the follow-up characters of an ANSI sequence
| data | 输入字符 The input character |
Definition at line 37 of file terminal.hpp.
|
inline |
处理输入字符,根据类型调用相应的处理函数 Handles input characters, dispatching them to the appropriate handler
| data | 输入的字符 The input character |
Definition at line 166 of file terminal.hpp.
|
inline |
处理控制字符,包括换行、删除、制表符等 Handles control characters such as newline, delete, and tab
| data | 输入的控制字符 The input control character |
\r\n / \n\r 双换行中的重复第二字节,避免一次回车被 当成两次命令提交。 This path also suppresses the duplicated second byte of one \r\n / \n\r pair so a single Enter key does not submit the command twice. Definition at line 111 of file terminal.hpp.
|
inline |
按“从最新到最旧”的顺序取历史记录 Fetches one history entry counting from newest to oldest
| index_from_newest | 从最新一条开始的偏移;0 表示最新一条 Offset counted from the newest entry; 0 means the newest one |
Queue::operator[] 支持负索引从尾部反向访问,但这里把这个细节包起来, 避免调用点直接暴露 history_[-index - 1] 这种难读写法。 Queue::operator[] supports negative indices to walk backward from the tail, but this helper hides that detail so call sites do not have to expose the hard-to-read history_[-index - 1] form directly. Definition at line 42 of file terminal.hpp.
|
inlinestatic |
行编辑、显示与历史记录片段 Line-editing, display, and history-management fragment
Terminal 的显示与历史记录片段 Display and history fragment of Terminal
计算一条历史命令的实际文本长度 Calculates the actual text length of one stored history line
| line | 历史命令缓存 / Stored history line |
Definition at line 18 of file terminal.hpp.
|
inline |
执行换行操作 Performs a line feed operation
\r\n、\n 还是 \r 由构造时选定的 MODE 决定。 The actual emitted line ending is selected by the MODE chosen at construction time. Definition at line 57 of file terminal.hpp.
|
inline |
输入字节解析与 ANSI / 控制字符分发片段 Input-byte parsing plus ANSI / control-character dispatch fragment
Terminal 的输入解析片段 Input-parsing fragment of Terminal
解析输入数据流,将其转换为字符并处理 Parses the input data stream, converting it into characters and processing them
| raw_data | 输入的原始数据 Input raw data |
Definition at line 19 of file terminal.hpp.
|
inline |
将路径字符串解析为目录对象 Converts a path string into a directory object
| path | 目录路径字符串 The directory path string |
/ 改写成 \0 再恢复,所以调用方必须传入可写 缓冲区,而不是字符串常量。 The resolver temporarily rewrites / to \0 and restores it later, so callers must provide a writable buffer rather than a string literal. Definition at line 52 of file terminal.hpp.
|
inline |
将路径字符串解析为文件对象 Converts a path string into a file object
| path | 文件路径字符串 The file path string |
path 可写。 This resolver also temporarily splits the path string and therefore requires path to be writable. Definition at line 110 of file terminal.hpp.
|
inline |
从输入行中删除字符,支持在光标位置删除 Removes a character from the input line, supports deletion at the cursor position
Definition at line 159 of file terminal.hpp.
|
inline |
显示终端提示符,包括当前目录信息 Displays the terminal prompt, including the current directory information
Definition at line 210 of file terminal.hpp.
|
inline |
显示历史记录中的输入行,更新终端显示 Displays the input line from history and updates the terminal display
history_index_ < 0 时,这里回显的是当前尚未提交的输入行,而不是历史项。 When history_index_ < 0, this echoes the current in-progress input line rather than one history entry. Definition at line 245 of file terminal.hpp.
|
inlineprivate |
反向查找字符串中的特定字符 Finds a specific character in a string from the end
| str | 输入字符串 Input string |
| c | 要查找的字符 Character to find |
Definition at line 68 of file terminal.hpp.
|
inlinestatic |
终端任务函数,以定时器任务方式驱动终端 Terminal task function, drives the terminal using a scheduled task
该函数用于以定时任务(或轮询方式)驱动终端,适用于资源受限的系统。 它不会持续运行,而是在定时器触发或系统任务调度时运行,执行一次数据读取和解析后返回。
This function drives the terminal using a scheduled task (or polling mode), making it suitable for resource-constrained systems. Unlike the thread-based approach, it only runs when scheduled (e.g., triggered by a timer) and processes available input data before returning.
TaskFun() 每次只推进有限一步状态机:发起读取、等待完成、消费完成结果, 然后立即返回给调度方。 TaskFun() advances the state machine by only a bounded step each time: start one read, wait for completion, consume one completed result, then return to the scheduler immediately. Definition at line 77 of file terminal.hpp.
|
inlinestatic |
线程 / 轮询驱动入口片段 Threaded / polling driver-entry fragment
Terminal 的驱动入口片段 Driver-entry fragment of Terminal
终端线程函数,以独立线程方式持续驱动终端 Terminal thread function, continuously drives the terminal as an independent thread
该函数用于以独立线程的方式驱动终端,持续从输入流读取数据并解析,适用于高实时性应用。 它会在循环中不断检查输入流的大小,并在数据可用时进行解析。
This function runs as a separate thread to continuously drive the terminal, reading and parsing input data in a loop. It is suitable for high real-time applications. It continuously checks the input stream size and processes data when available.
write_mutex_ 保护下完成, 以免和外部写口共享同一个输出流时交错。 Once one input batch is actually obtained, parsing and output commit are both completed under write_mutex_ so they do not interleave with other writers sharing the same output stream. Definition at line 34 of file terminal.hpp.
|
inline |
更新光标位置 Updates cursor position
Definition at line 81 of file terminal.hpp.
| size_t LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::arg_number_ = 0 |
参数数量 Number of arguments
Definition at line 164 of file terminal.hpp.
| char* LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::arg_tab_[MAX_ARG_NUMBER] |
命令参数列表 Command argument list
Definition at line 163 of file terminal.hpp.
|
staticconstexprprivate |
终端控制序列常量 Terminal control-sequence constants
These literals are emitted directly into the write stream to implement clear-screen, clear-line, cursor-save/restore, and simple cursor motion. 这些字面量会被直接写进终端输出流,用来实现清屏、清行、保存/恢复光标和基础光标移动。 清屏命令 Clear screen command
Definition at line 47 of file terminal.hpp.
|
staticconstexprprivate |
清除光标后内容命令 Clear content after cursor command
Definition at line 51 of file terminal.hpp.
|
staticconstexprprivate |
清除当前行命令 Clear current line command
Definition at line 49 of file terminal.hpp.
| RamFS::Dir* LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::current_dir_ |
当前目录 Current directory
Definition at line 159 of file terminal.hpp.
|
staticconstexprprivate |
退格删除字符 Backspace delete character
Definition at line 57 of file terminal.hpp.
| uint8_t LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::flag_ansi_ = 0 |
ANSI 控制字符状态 ANSI control character state.
Definition at line 160 of file terminal.hpp.
| Queue<HistoryLine> LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::history_ |
历史命令 History of commands
Definition at line 165 of file terminal.hpp.
| int LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::history_index_ = -1 |
当前历史索引 Current history index
Definition at line 166 of file terminal.hpp.
| Stack<char> LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::input_line_ |
输入行缓冲区 Input line buffer
Definition at line 162 of file terminal.hpp.
|
staticconstexprprivate |
左箭头键 Left arrow key
Definition at line 54 of file terminal.hpp.
|
staticconstexprprivate |
恢复光标位置 Restore cursor position
Definition at line 56 of file terminal.hpp.
|
staticconstexprprivate |
右箭头键 Right arrow key
Definition at line 53 of file terminal.hpp.
|
staticconstexprprivate |
保存光标位置 Save cursor position
Definition at line 55 of file terminal.hpp.
| char LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::linefeed_char_ = '\0' |
上一个换行字符 Previous line feed character
Definition at line 168 of file terminal.hpp.
| bool LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::linefeed_flag_ = false |
CRLF 抑制标志 CRLF suppression flag.
Definition at line 167 of file terminal.hpp.
| const Mode LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::MODE |
终端换行模式 Terminal line feed mode
Definition at line 147 of file terminal.hpp.
| int LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::offset_ = 0 |
光标偏移 Cursor offset
Definition at line 161 of file terminal.hpp.
| RamFS& LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::ramfs_ |
关联的文件系统 Associated file system
Definition at line 155 of file terminal.hpp.
| char LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::read_buff_[READ_BUFF_SIZE] |
读取缓冲区 Read buffer
Definition at line 156 of file terminal.hpp.
| ReadPort* LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::read_port_ |
读取端口 Read port
Definition at line 149 of file terminal.hpp.
| ReadOperation::OperationPollingStatus LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::read_status_ |
Definition at line 143 of file terminal.hpp.
| size_t LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::request_read_size_ = 0 |
本轮计划读取的字节数 / Byte count requested for the current read attempt.
Definition at line 158 of file terminal.hpp.
| LibXR::Mutex* LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::write_mutex_ = nullptr |
写入端口互斥锁 Write port mutex
Definition at line 153 of file terminal.hpp.
| WriteOperation LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::write_op_ |
终端写操作 Terminal write operation
Definition at line 148 of file terminal.hpp.
| WritePort* LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::write_port_ |
写入端口 Write port
Definition at line 150 of file terminal.hpp.
| WriteOperation::OperationPollingStatus LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::write_status_ |
当前读/写轮询状态 / Current polling status of the read / write side.
Definition at line 145 of file terminal.hpp.
| WritePort::Stream LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::write_stream_ |
写入流 Write stream
Definition at line 151 of file terminal.hpp.