File indexing completed on 2024-05-19 05:28:19

0001 /*
0002     This file is part of Konsole, an X terminal.
0003 
0004     SPDX-FileCopyrightText: 2006-2008 Robert Knight <robertknight@gmail.com>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 
0007     This program is free software; you can redistribute it and/or modify
0008     it under the terms of the GNU Lesser General Public License as published by
0009     the Free Software Foundation; either version 2 of the License, or
0010     (at your option) any later version.
0011 
0012     This program is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015     GNU General Public License for more details.
0016 
0017     You should have received a copy of the GNU Lesser General Public License
0018     along with this program; if not, write to the Free Software
0019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0020     02110-1301  USA.
0021 */
0022 
0023 #ifndef TERMINAL_CHARACTER_DECODER_H
0024 #define TERMINAL_CHARACTER_DECODER_H
0025 
0026 #include "Character.h"
0027 
0028 #include <QList>
0029 
0030 class QTextStream;
0031 
0032 namespace Konsole
0033 {
0034 
0035 /**
0036  * Base class for terminal character decoders
0037  *
0038  * The decoder converts lines of terminal characters which consist of a unicode character, foreground
0039  * and background colours and other appearance-related properties into text strings.
0040  *
0041  * Derived classes may produce either plain text with no other colour or appearance information, or
0042  * they may produce text which incorporates these additional properties.
0043  */
0044 class TerminalCharacterDecoder
0045 {
0046 public:
0047     virtual ~TerminalCharacterDecoder()
0048     {
0049     }
0050 
0051     /** Begin decoding characters.  The resulting text is appended to @p output. */
0052     virtual void begin(QTextStream *output) = 0;
0053     /** End decoding. */
0054     virtual void end() = 0;
0055 
0056     /**
0057      * Converts a line of terminal characters with associated properties into a text string
0058      * and writes the string into an output QTextStream.
0059      *
0060      * @param characters An array of characters of length @p count.
0061      * @param count The number of characters
0062      * @param properties Additional properties which affect all characters in the line
0063      */
0064     virtual void decodeLine(std::span<const Character> characters, LineProperty properties) = 0;
0065 };
0066 
0067 /**
0068  * A terminal character decoder which produces plain text, ignoring colours and other appearance-related
0069  * properties of the original characters.
0070  */
0071 class PlainTextDecoder : public TerminalCharacterDecoder
0072 {
0073 public:
0074     PlainTextDecoder();
0075 
0076     /**
0077      * Set whether trailing whitespace at the end of lines should be included
0078      * in the output.
0079      * Defaults to true.
0080      */
0081     void setTrailingWhitespace(bool enable);
0082     /**
0083      * Returns whether trailing whitespace at the end of lines is included
0084      * in the output.
0085      */
0086     bool trailingWhitespace() const;
0087     /**
0088      * Returns of character positions in the output stream
0089      * at which new lines where added.  Returns an empty if setTrackLinePositions() is false or if
0090      * the output device is not a string.
0091      */
0092     QList<int> linePositions() const;
0093     /** Enables recording of character positions at which new lines are added.  See linePositions() */
0094     void setRecordLinePositions(bool record);
0095 
0096     void begin(QTextStream *output) override;
0097     void end() override;
0098 
0099     void decodeLine(std::span<const Character> characters, LineProperty properties) override;
0100 
0101 private:
0102     QTextStream *_output;
0103     bool _includeTrailingWhitespace;
0104 
0105     bool _recordLinePositions;
0106     QList<int> _linePositions;
0107 };
0108 
0109 }
0110 
0111 #endif