Skip to content

Commit 3ec5171

Browse files
committed
完善terminal的输入和输出
解决了terminal下Backspace和delete操作的输入和显示异常。增加了ins pgup pgdn的输入控制(不见得有用)
1 parent fe590f6 commit 3ec5171

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

SerialTool/include/version.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef __VERSION_H
22
#define __VERSION_H
33

4-
#define MAIN_VERSION 1.4.1(T)
4+
#define MAIN_VERSION 1.4.2(T)
55

66
#define SOFTWARE_NAME "SerialTool"
77
#define COPYRIGHT "Copyleft 2017-2018, Wenliang Guan"

SerialTool/src/views/terminal/qvterminal/qvterminal.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,17 @@ void QVTerminal::appendData(const QByteArray &data)
5050

5151
setUpdatesEnabled(false);
5252
QByteArray::const_iterator it = data.cbegin();
53+
54+
qDebug() << "appendData="+data;
55+
5356
while (it != data.cend()) {
5457
QChar c = *it;
5558
switch (_state) {
5659
case QVTerminal::Text:
5760
switch (c.unicode()) {
5861
case '\033':
62+
// \033即\x1B即ASCII 27,ESC
63+
// 匹配ESC[,即CSI ,Control Sequence Introducer
5964
appendString(text);
6065
text.clear();
6166
_state = QVTerminal::Escape;
@@ -113,6 +118,9 @@ void QVTerminal::appendData(const QByteArray &data)
113118
} else {
114119
_state = QVTerminal::Text;
115120
}
121+
}else if(c=='J') {
122+
reduceString(-1);
123+
_state = QVTerminal::Text;
116124
} else {
117125
formatChar(c);
118126
_state = QVTerminal::Text;
@@ -127,6 +135,7 @@ void QVTerminal::appendData(const QByteArray &data)
127135
it++;
128136
}
129137
appendString(text);
138+
130139
verticalScrollBar()->setRange(0, _ch * (_layout->lineCount() + 1) - viewport()->size().height());
131140
verticalScrollBar()->setValue(verticalScrollBar()->maximum());
132141
setUpdatesEnabled(true);
@@ -201,13 +210,24 @@ void QVTerminal::read()
201210

202211
void QVTerminal::appendString(QString str)
203212
{
213+
// qDebug() << "appendString="+str;
204214
foreach (QChar c, str) {
205215
QVTChar termChar(c, _curentFormat);
206216
_layout->lineAt(_cursorPos.y()).append(termChar, _cursorPos.x());
207217
_cursorPos.setX(_cursorPos.x() + 1);
208218
}
209219
}
210220

221+
void QVTerminal::reduceString(int mode)
222+
{
223+
if(mode>0)
224+
_layout->lineAt(_cursorPos.y()).reduce(_cursorPos.x()+1);
225+
else{
226+
_layout->lineAt(_cursorPos.y()).reduce(_cursorPos.x());
227+
// _cursorPos.setX(_cursorPos.x() -1);
228+
}
229+
}
230+
211231
void QVTerminal::toggleCursor()
212232
{
213233
_cvisible = !_cvisible;
@@ -299,6 +319,20 @@ void QVTerminal::keyPressEvent(QKeyEvent *event)
299319
case Qt::Key_Backspace:
300320
data.append('\b');
301321
break;
322+
// ref.PC-Style Function Keys
323+
// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
324+
case Qt::Key_Delete:
325+
data.append("\033[3~");
326+
break;
327+
case Qt::Key_Insert:
328+
data.append("\033[2~");
329+
break;
330+
case Qt::Key_PageUp:
331+
data.append("\033[5~");
332+
break;
333+
case Qt::Key_PageDown:
334+
data.append("\033[6~");
335+
break;
302336
case Qt::Key_Return:
303337
data.append('\n');
304338
break;

SerialTool/src/views/terminal/qvterminal/qvterminal.h

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public slots:
3939
protected slots:
4040
void read();
4141
void appendString(QString str);
42+
void reduceString(int position);
4243
void toggleCursor();
4344
void clearToEnd();
4445

SerialTool/src/views/terminal/qvterminal/qvtline.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ void QVTLine::append(const QVTChar &c, int position)
2020
}
2121
}
2222

23+
void QVTLine::reduce(int position)
24+
{
25+
if (position >= 0 && position < _chars.count()) {
26+
_chars.remove(position,1);
27+
}
28+
}
29+
2330
const QVector<QVTChar> &QVTLine::chars() const
2431
{
2532
return _chars;

SerialTool/src/views/terminal/qvterminal/qvtline.h

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class QVTLine
1010
QVTLine();
1111

1212
void append(const QVTChar &c, int position = -1);
13+
void reduce(int position);
1314
void reserve(int size);
1415
const QVector<QVTChar> &chars() const;
1516
int size() const;

0 commit comments

Comments
 (0)