File indexing completed on 2018-12-11 17:32:21

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 "Profile.h"
0031 #include "konsoleprivate_export.h"
0032 
0033 class QTextStream;
0034 
0035 namespace Konsole {
0036 /**
0037  * Base class for terminal character decoders
0038  *
0039  * The decoder converts lines of terminal characters which consist of a unicode character, foreground
0040  * and background colors and other appearance-related properties into text strings.
0041  *
0042  * Derived classes may produce either plain text with no other color or appearance information, or
0043  * they may produce text which incorporates these additional properties.
0044  */
0045 class KONSOLEPRIVATE_EXPORT TerminalCharacterDecoder
0046 {
0047 public:
0048     virtual ~TerminalCharacterDecoder()
0049     {
0050     }
0051 
0052     /** Begin decoding characters.  The resulting text is appended to @p output. */
0053     virtual void begin(QTextStream *output) = 0;
0054     /** End decoding. */
0055     virtual void end() = 0;
0056 
0057     /**
0058      * Converts a line of terminal characters with associated properties into a text string
0059      * and writes the string into an output QTextStream.
0060      *
0061      * @param characters An array of characters of length @p count.
0062      * @param count The number of characters
0063      * @param properties Additional properties which affect all characters in the line
0064      */
0065     virtual void decodeLine(const Character * const characters, int count,
0066                             LineProperty properties) = 0;
0067 };
0068 
0069 /**
0070  * A terminal character decoder which produces plain text, ignoring colors and other appearance-related
0071  * properties of the original characters.
0072  */
0073 class KONSOLEPRIVATE_EXPORT PlainTextDecoder : public TerminalCharacterDecoder
0074 {
0075 public:
0076     PlainTextDecoder();
0077 
0078     /**
0079      * Set whether leading whitespace at the end of lines should be included
0080      * in the output.
0081      * Defaults to true.
0082      */
0083     void setLeadingWhitespace(bool enable);
0084     /**
0085      * Returns whether leading whitespace at the end of lines is included
0086      * in the output.
0087      */
0088     bool leadingWhitespace() const;
0089     /**
0090      * Set whether trailing whitespace at the end of lines should be included
0091      * in the output.
0092      * Defaults to true.
0093      */
0094     void setTrailingWhitespace(bool enable);
0095     /**
0096      * Returns whether trailing whitespace at the end of lines is included
0097      * in the output.
0098      */
0099     bool trailingWhitespace() const;
0100     /**
0101      * Returns of character positions in the output stream
0102      * at which new lines where added.  Returns an empty if setTrackLinePositions() is false or if
0103      * the output device is not a string.
0104      */
0105     QList<int> linePositions() const;
0106     /** Enables recording of character positions at which new lines are added.  See linePositions() */
0107     void setRecordLinePositions(bool record);
0108 
0109     void begin(QTextStream *output) Q_DECL_OVERRIDE;
0110     void end() Q_DECL_OVERRIDE;
0111 
0112     void decodeLine(const Character * const characters, int count,
0113                     LineProperty properties) Q_DECL_OVERRIDE;
0114 
0115 private:
0116     QTextStream *_output;
0117     bool _includeLeadingWhitespace;
0118     bool _includeTrailingWhitespace;
0119 
0120     bool _recordLinePositions;
0121     QList<int> _linePositions;
0122 };
0123 
0124 /**
0125  * A terminal character decoder which produces pretty HTML markup
0126  */
0127 class KONSOLEPRIVATE_EXPORT HTMLDecoder : public TerminalCharacterDecoder
0128 {
0129 public:
0130     /**
0131      * Constructs an HTML decoder using a default black-on-white color scheme.
0132      */
0133     explicit HTMLDecoder(const Profile::Ptr &profile = Profile::Ptr());
0134 
0135     void decodeLine(const Character * const characters, int count,
0136                     LineProperty properties) Q_DECL_OVERRIDE;
0137 
0138     void begin(QTextStream *output) Q_DECL_OVERRIDE;
0139     void end() Q_DECL_OVERRIDE;
0140 
0141 private:
0142     void openSpan(QString &text, const QString &style);
0143     void closeSpan(QString &text);
0144 
0145     QTextStream *_output;
0146     Profile::Ptr _profile;
0147     ColorEntry _colorTable[TABLE_COLORS];
0148     bool _innerSpanOpen;
0149     RenditionFlags _lastRendition;
0150     CharacterColor _lastForeColor;
0151     CharacterColor _lastBackColor;
0152 };
0153 }
0154 
0155 #endif