|
libxr
Embedded C++ framework for bare-metal, RTOS and Linux
|
基于 RamFS 的命令行终端 / Command-line terminal backed by RamFS. More...
#include <terminal.hpp>
Public Types | |
| enum class | Mode : uint8_t { CRLF = 0 , LF = 1 , CR = 2 } |
| 终端换行模式 / Terminal line ending mode. 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) | |
| 初始化终端的文件系统和读写端口 / Initialize the terminal filesystem and ports. | |
| 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 | ShowTruncatedLineNotice () |
| 输出输入行被截断的提示 Prints the notice that the input line was truncated | |
| 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 the history index, the cursor offset and the truncation mark | |
| void | AddHistory () |
| 将当前输入行添加到历史记录 Adds the current input line to the history | |
| void | GetArgs () |
| 路径解析、命令执行与补全实现 / Path parsing, command execution, and completion. | |
| 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) |
| 输入与控制字符解析实现 / Input and control-character parsing. | |
| 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 command history. | |
| static void | ThreadFun (Terminal *term) |
| 在线程中读取输入并驱动终端 / Read input and drive the terminal in a thread. | |
| static void | TaskFun (Terminal *term) |
| 检查读取结果并推进终端输入 / Check read completion and advance terminal input. | |
Public Attributes | |
| std::atomic< ReadOperation::OperationPollingStatus > | read_status_ |
| 读完成方发布、轮询入口读取的状态 / Read completion status used by the polling entry. | |
| std::atomic< WriteOperation::OperationPollingStatus > | write_status_ |
| 写完成方发布的轮询状态 / Polling status published by write completion. | |
| 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_ |
| 本轮计划读取的字节数 / 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 | |
| bool | line_truncated_ |
| 当前输入行丢弃过超长字符 / Characters beyond MAX_LINE_SIZE were dropped | |
基于 RamFS 的命令行终端 / Command-line terminal backed by RamFS.
| READ_BUFF_SIZE | 读取缓冲区大小 / Read buffer size. |
| MAX_LINE_SIZE | 最大输入行长度(字符数)。超出的字符不回显、不写入当前行, 回车时先输出一行截断提示,再照常执行已保留的部分。 Maximum input line length in characters. Further characters are neither echoed nor stored; on Enter a truncation notice is printed and the kept part is executed as usual. |
| MAX_ARG_NUMBER | 最大参数数量 / Maximum argument count. |
| MAX_HISTORY_NUMBER | 最大历史记录数量 / Maximum history entry count. |
Definition at line 33 of file terminal.hpp.
|
strong |
|
inline |
初始化终端的文件系统和读写端口 / Initialize the terminal filesystem and ports.
| ramfs | 关联的文件系统 / Associated filesystem. |
| current_dir | 当前目录,默认根目录 / Current directory; defaults to the root. |
| read_port | 读端口,默认标准输入 / Read port; defaults to standard input. |
| write_port | 写端口,默认标准输出 / Write port; defaults to standard output. |
| MODE | 换行模式,默认 CRLF / Line ending mode; defaults to CRLF. |
Definition at line 102 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 317 of file terminal.hpp.
|
inline |
实现命令自动补全,匹配目录或文件名 Implements command auto-completion by matching directories or file names
Definition at line 240 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 264 of file terminal.hpp.
|
inline |
清除当前行 Clears the current line
Definition at line 258 of file terminal.hpp.
|
inline |
将历史命令复制到输入行,并重置历史索引、光标偏移和截断标记 Copies the history command to the input line and resets the history index, the cursor offset and the truncation mark
Definition at line 295 of file terminal.hpp.
|
inline |
处理删除字符操作,支持回退删除,并在历史模式下更新显示 Handles the delete character operation, supports backspace deletion, and updates display in history mode
Definition at line 181 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 parsing, command execution, and completion.
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 171 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 command history.
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 |
输入与控制字符解析实现 / Input and control-character parsing.
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 163 of file terminal.hpp.
|
inline |
显示终端提示符,包括当前目录信息 Displays the terminal prompt, including the current directory information
Definition at line 214 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 274 of file terminal.hpp.
|
inline |
输出输入行被截断的提示 Prints the notice that the input line was truncated
MAX_LINE_SIZE,随后换行。 The notice names the kept character count, the template parameter MAX_LINE_SIZE, and ends with a line feed. Definition at line 237 of file terminal.hpp.
|
inlinestatic |
检查读取结果并推进终端输入 / Check read completion and advance terminal input.
| term | 终端实例 / Terminal instance. |
Definition at line 46 of file terminal.hpp.
|
inlinestatic |
在线程中读取输入并驱动终端 / Read input and drive the terminal in a thread.
| term | 终端实例 / Terminal instance. |
Definition at line 15 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 161 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 160 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 156 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 157 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 162 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 163 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 159 of file terminal.hpp.
| bool LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::line_truncated_ |
当前输入行丢弃过超长字符 / Characters beyond MAX_LINE_SIZE were dropped
Definition at line 166 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 165 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 164 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 143 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 158 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 151 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 152 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 145 of file terminal.hpp.
| std::atomic<ReadOperation::OperationPollingStatus> LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::read_status_ |
读完成方发布、轮询入口读取的状态 / Read completion status used by the polling entry.
Definition at line 139 of file terminal.hpp.
| size_t LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::request_read_size_ |
本轮计划读取的字节数 / Byte count requested for the current read attempt.
Definition at line 154 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 149 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 144 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 146 of file terminal.hpp.
| std::atomic<WriteOperation::OperationPollingStatus> LibXR::Terminal< READ_BUFF_SIZE, MAX_LINE_SIZE, MAX_ARG_NUMBER, MAX_HISTORY_NUMBER >::write_status_ |
写完成方发布的轮询状态 / Polling status published by write completion.
Definition at line 141 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 147 of file terminal.hpp.