Skip to content

Commit

Permalink
完善terminal的输入和输出
Browse files Browse the repository at this point in the history
解决了terminal下Backspace和delete操作的输入和显示异常。增加了ins  pgup pgdn的输入控制(不见得有用)
  • Loading branch information
tumuyan committed Aug 27, 2020
1 parent fe590f6 commit 3ec5171
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion SerialTool/include/version.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef __VERSION_H
#define __VERSION_H

#define MAIN_VERSION 1.4.1(T)
#define MAIN_VERSION 1.4.2(T)

#define SOFTWARE_NAME "SerialTool"
#define COPYRIGHT "Copyleft 2017-2018, Wenliang Guan"
Expand Down
34 changes: 34 additions & 0 deletions SerialTool/src/views/terminal/qvterminal/qvterminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,17 @@ void QVTerminal::appendData(const QByteArray &data)

setUpdatesEnabled(false);
QByteArray::const_iterator it = data.cbegin();

qDebug() << "appendData="+data;

while (it != data.cend()) {
QChar c = *it;
switch (_state) {
case QVTerminal::Text:
switch (c.unicode()) {
case '\033':
// \033即\x1B即ASCII 27,ESC
// 匹配ESC[,即CSI ,Control Sequence Introducer
appendString(text);
text.clear();
_state = QVTerminal::Escape;
Expand Down Expand Up @@ -113,6 +118,9 @@ void QVTerminal::appendData(const QByteArray &data)
} else {
_state = QVTerminal::Text;
}
}else if(c=='J') {
reduceString(-1);
_state = QVTerminal::Text;
} else {
formatChar(c);
_state = QVTerminal::Text;
Expand All @@ -127,6 +135,7 @@ void QVTerminal::appendData(const QByteArray &data)
it++;
}
appendString(text);

verticalScrollBar()->setRange(0, _ch * (_layout->lineCount() + 1) - viewport()->size().height());
verticalScrollBar()->setValue(verticalScrollBar()->maximum());
setUpdatesEnabled(true);
Expand Down Expand Up @@ -201,13 +210,24 @@ void QVTerminal::read()

void QVTerminal::appendString(QString str)
{
// qDebug() << "appendString="+str;
foreach (QChar c, str) {
QVTChar termChar(c, _curentFormat);
_layout->lineAt(_cursorPos.y()).append(termChar, _cursorPos.x());
_cursorPos.setX(_cursorPos.x() + 1);
}
}

void QVTerminal::reduceString(int mode)
{
if(mode>0)
_layout->lineAt(_cursorPos.y()).reduce(_cursorPos.x()+1);
else{
_layout->lineAt(_cursorPos.y()).reduce(_cursorPos.x());
// _cursorPos.setX(_cursorPos.x() -1);
}
}

void QVTerminal::toggleCursor()
{
_cvisible = !_cvisible;
Expand Down Expand Up @@ -299,6 +319,20 @@ void QVTerminal::keyPressEvent(QKeyEvent *event)
case Qt::Key_Backspace:
data.append('\b');
break;
// ref.PC-Style Function Keys
// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
case Qt::Key_Delete:
data.append("\033[3~");
break;
case Qt::Key_Insert:
data.append("\033[2~");
break;
case Qt::Key_PageUp:
data.append("\033[5~");
break;
case Qt::Key_PageDown:
data.append("\033[6~");
break;
case Qt::Key_Return:
data.append('\n');
break;
Expand Down
1 change: 1 addition & 0 deletions SerialTool/src/views/terminal/qvterminal/qvterminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public slots:
protected slots:
void read();
void appendString(QString str);
void reduceString(int position);
void toggleCursor();
void clearToEnd();

Expand Down
7 changes: 7 additions & 0 deletions SerialTool/src/views/terminal/qvterminal/qvtline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ void QVTLine::append(const QVTChar &c, int position)
}
}

void QVTLine::reduce(int position)
{
if (position >= 0 && position < _chars.count()) {
_chars.remove(position,1);
}
}

const QVector<QVTChar> &QVTLine::chars() const
{
return _chars;
Expand Down
1 change: 1 addition & 0 deletions SerialTool/src/views/terminal/qvterminal/qvtline.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class QVTLine
QVTLine();

void append(const QVTChar &c, int position = -1);
void reduce(int position);
void reserve(int size);
const QVector<QVTChar> &chars() const;
int size() const;
Expand Down

1 comment on commit 3ec5171

@tumuyan
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CSI n J ,清除屏幕的部分区域。如果n是0(或缺失),则清除从光标位置到屏幕末尾的部分。如果n是1,则清除从光标位置到屏幕开头的部分。如果n是2,则清除整个屏幕(在DOS ANSI.SYS中,光标还会向左上方移动)。如果n是3,则清除整个屏幕,并删除回滚缓存区中的所有行(这个特性是xterm添加的,其他终端应用程序也支持)。
然而我的真机在删除字符1233456中的3时,返回的数据是这样的:"appendData=\b3456\x1B[J\b\b\b\b"
故代码并没按照规则处理x1B[J,而是忽略了数字参数n
这又导致了另一个问题
在clear命令时,返回的数据是这样的"appendData=\x1B[H\x1B[J/ # "
但是无法完成清楚屏幕,为了避免出现更多问题,程序没有添加\x1B[H的解析

Please sign in to comment.