fix: chinese input issue and optimize input handling#13
Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom Jan 26, 2026
Merged
fix: chinese input issue and optimize input handling#13deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
Conversation
support Chinese input in dtkaivalidator Log: Bug: https://pms.uniontech.com/bug-view-348743.html
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: deepin-mozart The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
deepin pr auto review这份代码修改主要实现了两个功能:一是通过自定义 以下是对这些修改的详细审查意见,涵盖语法逻辑、代码质量、性能和安全等方面: 1. 语法逻辑
2. 代码质量
3. 代码性能
4. 代码安全
改进建议代码示例 (RAII 封装)为了解决异常安全和代码复用问题,建议将终端属性修改封装在一个类中: #include <termios.h>
#include <unistd.h>
#include <stdexcept>
class TerminalModeGuard {
public:
TerminalModeGuard() {
if (tcgetattr(STDIN_FILENO, &old_termios) == -1) {
throw std::runtime_error("Failed to get terminal attributes");
}
new_termios = old_termios;
new_termios.c_lflag &= ~(ICANON | ECHO);
if (tcsetattr(STDIN_FILENO, TCSANOW, &new_termios) == -1) {
throw std::runtime_error("Failed to set terminal attributes");
}
}
~TerminalModeGuard() {
// 无论是否发生异常,析构函数都会执行,确保终端状态恢复
tcsetattr(STDIN_FILENO, TCSANOW, &old_termios);
}
// 禁止拷贝
TerminalModeGuard(const TerminalModeGuard&) = delete;
TerminalModeGuard& operator=(const TerminalModeGuard&) = delete;
private:
struct termios old_termios;
struct termios new_termios;
};
bool getChineseInput(std::string &input, const std::string &prompt = "") {
if (!prompt.empty()) {
std::cout << prompt;
std::cout.flush();
}
// RAII 自动管理终端模式
TerminalModeGuard guard;
input.clear();
char ch;
while (true) {
// 注意:read 可能会被信号中断,返回 EINTR,建议处理
ssize_t n = read(STDIN_FILENO, &ch, 1);
if (n <= 0) break; // 简化处理,实际生产代码应检查 errno
if (ch == '\n' || ch == '\r') {
std::cout << std::endl;
break;
} else if (ch == 127 || ch == 8) { // Backspace
if (!input.empty()) {
// ... (保持原有的 UTF-8 处理逻辑不变) ...
// 建议:引入 wcwidth 处理显示宽度
}
} else if (ch == 3) { // Ctrl+C
// guard 析构函数会自动在这里被调用,恢复终端
// 这里可以选择抛出异常或者返回特定状态,不建议直接 exit
return false;
} else {
input += ch;
std::cout << ch << std::flush;
}
}
return true;
}总结这份代码修改在功能上实现了预期的目标(支持中文输入),但在健壮性(异常安全)和可移植性(Windows 支持)方面存在隐患。建议引入 RAII 机制管理终端资源,并考虑使用 |
Contributor
Author
|
/forcemerge |
|
This pr force merged! (status: unstable) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
support Chinese input in dtkaivalidator
Log:
Bug: https://pms.uniontech.com/bug-view-348743.html