MauiKit Terminal

TerminalCharacterDecoder.cpp
1/*
2 This file is part of Konsole, an X terminal.
3
4 SPDX-FileCopyrightText: 2006-2008 Robert Knight <robertknight@gmail.com>
5 SPDX-License-Identifier: GPL-2.0-or-later
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
21*/
22
23// Own
24#include "TerminalCharacterDecoder.h"
25
26// stdlib
27#include <cwctype>
28
29// Qt
30#include <QTextStream>
31
32// KDE
33// #include <kdebug.h>
34
35// Konsole
36#include "konsole_wcwidth.h"
37
38#include <cwctype>
39
40using namespace Konsole;
41PlainTextDecoder::PlainTextDecoder()
42 : _output(nullptr)
43 , _includeTrailingWhitespace(true)
44 , _recordLinePositions(false)
45{
46}
48{
49 _includeTrailingWhitespace = enable;
50}
52{
53 return _includeTrailingWhitespace;
54}
56{
57 _output = output;
58 if (!_linePositions.isEmpty())
59 _linePositions.clear();
60}
62{
63 _output = nullptr;
64}
65
67{
68 _recordLinePositions = record;
69}
71{
72 return _linePositions;
73}
74
75void PlainTextDecoder::decodeLine(std::span<const Character> characters, LineProperty /*properties*/)
76{
77 Q_ASSERT(_output);
78 int count = characters.size();
79
80 if (_recordLinePositions && _output->string()) {
81 int pos = _output->string()->size();
82 _linePositions << pos;
83 }
84
85 // check the real length
86 for (int i = 0; i < count; i++) {
87 if (characters.subspan(i).empty()) {
88 count = i;
89 break;
90 }
91 }
92
93 // TODO should we ignore or respect the LINE_WRAPPED line property?
94
95 // note: we build up a QString and send it to the text stream rather writing into the text
96 // stream a character at a time because it is more efficient.
97 //(since QTextStream always deals with QStrings internally anyway)
98 QString plainText;
99 plainText.reserve(count);
100
101 int outputCount = count;
102
103 // if inclusion of trailing whitespace is disabled then find the end of the
104 // line
105 if (!_includeTrailingWhitespace) {
106 for (int i = count - 1; i >= 0; i--) {
107 if (characters[i].character != u' ')
108 break;
109 else
110 outputCount--;
111 }
112 }
113
114 for (int i = 0; i < outputCount;) {
115 plainText.push_back(characters[i].character);
116 i += qMax(1, konsole_wcwidth(characters[i].character));
117 }
118 *_output << plainText;
119}
void setTrailingWhitespace(bool enable)
Set whether trailing whitespace at the end of lines should be included in the output.
bool trailingWhitespace() const
Returns whether trailing whitespace at the end of lines is included in the output.
void decodeLine(std::span< const Character > characters, LineProperty properties) override
Converts a line of terminal characters with associated properties into a text string and writes the s...
void setRecordLinePositions(bool record)
Enables recording of character positions at which new lines are added.
void end() override
End decoding.
void begin(QTextStream *output) override
Begin decoding characters.
QList< int > linePositions() const
Returns of character positions in the output stream at which new lines where added.
void clear()
bool isEmpty() const const
void push_back(QChar ch)
void reserve(qsizetype size)
qsizetype size() const const
QString * string() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:10:32 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.