File indexing completed on 2018-06-12 20:16:31

0001 /*
0002     This file is part of Konsole, an X terminal.
0003 
0004     Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
0005 
0006     This program is free software; you can redistribute it and/or modify
0007     it under the terms of the GNU Lesser General Public License as published by
0008     the Free Software Foundation; either version 2 of the License, or
0009     (at your option) any later version.
0010 
0011     This program is distributed in the hope that it will be useful,
0012     but WITHOUT ANY WARRANTY; without even the implied warranty of
0013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014     GNU General Public License for more details.
0015 
0016     You should have received a copy of the GNU Lesser General Public License
0017     along with this program; if not, write to the Free Software
0018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0019     02110-1301  USA.
0020 */
0021 
0022 #ifndef TERMINAL_CHARACTER_DECODER_H
0023 #define TERMINAL_CHARACTER_DECODER_H
0024 
0025 // Qt
0026 #include <QList>
0027 
0028 // Konsole
0029 #include "Character.h"
0030 #include "konsoleprivate_export.h"
0031 
0032 class QTextStream;
0033 
0034 namespace Konsole {
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 colors and other appearance-related properties into text strings.
0040  *
0041  * Derived classes may produce either plain text with no other color or appearance information, or
0042  * they may produce text which incorporates these additional properties.
0043  */
0044 class KONSOLEPRIVATE_EXPORT 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(const Character * const characters, int count,
0065                             LineProperty properties) = 0;
0066 };
0067 
0068 /**
0069  * A terminal character decoder which produces plain text, ignoring colors and other appearance-related
0070  * properties of the original characters.
0071  */
0072 class KONSOLEPRIVATE_EXPORT PlainTextDecoder : public TerminalCharacterDecoder
0073 {
0074 public:
0075     PlainTextDecoder();
0076 
0077     /**
0078      * Set whether leading whitespace at the end of lines should be included
0079      * in the output.
0080      * Defaults to true.
0081      */
0082     void setLeadingWhitespace(bool enable);
0083     /**
0084      * Returns whether leading whitespace at the end of lines is included
0085      * in the output.
0086      */
0087     bool leadingWhitespace() const;
0088     /**
0089      * Set whether trailing whitespace at the end of lines should be included
0090      * in the output.
0091      * Defaults to true.
0092      */
0093     void setTrailingWhitespace(bool enable);
0094     /**
0095      * Returns whether trailing whitespace at the end of lines is included
0096      * in the output.
0097      */
0098     bool trailingWhitespace() const;
0099     /**
0100      * Returns of character positions in the output stream
0101      * at which new lines where added.  Returns an empty if setTrackLinePositions() is false or if
0102      * the output device is not a string.
0103      */
0104     QList<int> linePositions() const;
0105     /** Enables recording of character positions at which new lines are added.  See linePositions() */
0106     void setRecordLinePositions(bool record);
0107 
0108     void begin(QTextStream *output) Q_DECL_OVERRIDE;
0109     void end() Q_DECL_OVERRIDE;
0110 
0111     void decodeLine(const Character * const characters, int count,
0112                     LineProperty properties) Q_DECL_OVERRIDE;
0113 
0114 private:
0115     QTextStream *_output;
0116     bool _includeLeadingWhitespace;
0117     bool _includeTrailingWhitespace;
0118 
0119     bool _recordLinePositions;
0120     QList<int> _linePositions;
0121 };
0122 
0123 /**
0124  * A terminal character decoder which produces pretty HTML markup
0125  */
0126 class KONSOLEPRIVATE_EXPORT HTMLDecoder : public TerminalCharacterDecoder
0127 {
0128 public:
0129     /**
0130      * Constructs an HTML decoder using a default black-on-white color scheme.
0131      */
0132     HTMLDecoder();
0133 
0134     /**
0135      * Sets the color table which the decoder uses to produce the HTML color codes in its
0136      * output
0137      */
0138     void setColorTable(const ColorEntry *table);
0139 
0140     void decodeLine(const Character * const characters, int count,
0141                     LineProperty properties) Q_DECL_OVERRIDE;
0142 
0143     void begin(QTextStream *output) Q_DECL_OVERRIDE;
0144     void end() Q_DECL_OVERRIDE;
0145 
0146 private:
0147     void openSpan(QString &text, const QString &style);
0148     void closeSpan(QString &text);
0149 
0150     QTextStream *_output;
0151     const ColorEntry *_colorTable;
0152     bool _innerSpanOpen;
0153     RenditionFlags _lastRendition;
0154     CharacterColor _lastForeColor;
0155     CharacterColor _lastBackColor;
0156 };
0157 }
0158 
0159 #endif