summaryrefslogtreecommitdiffstats
path: root/client/browser/qtermwidget/src/TerminalCharacterDecoder.h
blob: 5d97fc3780da39706c566e47b75eacac68b3b907 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
    This file is part of Konsole, an X terminal.
    
    Copyright (C) 2006-7 by Robert Knight <robertknight@gmail.com>
    
    Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA.
*/

#ifndef TERMINAL_CHARACTER_DECODER_H
#define TERMINAL_CHARACTER_DECODER_H

#include "Character.h"

class QTextStream;

namespace Konsole
{

/**
 * Base class for terminal character decoders
 *
 * The decoder converts lines of terminal characters which consist of a unicode character, foreground
 * and background colours and other appearance-related properties into text strings.
 *
 * Derived classes may produce either plain text with no other colour or appearance information, or
 * they may produce text which incorporates these additional properties. 
 */
class TerminalCharacterDecoder
{
public:
	virtual ~TerminalCharacterDecoder() {}

    /** Begin decoding characters.  The resulting text is appended to @p output. */
    virtual void begin(QTextStream* output) = 0;
    /** End decoding. */
    virtual void end() = 0;

	/**
	 * Converts a line of terminal characters with associated properties into a text string
	 * and writes the string into an output QTextStream.
	 *
	 * @param characters An array of characters of length @p count.
	 * @param properties Additional properties which affect all characters in the line
	 * @param output The output stream which receives the decoded text
	 */
	virtual void decodeLine(const Character* const characters, 
							int count,
							LineProperty properties) = 0; 
};

/**
 * A terminal character decoder which produces plain text, ignoring colours and other appearance-related
 * properties of the original characters.
 */
class PlainTextDecoder : public TerminalCharacterDecoder
{
public:
	PlainTextDecoder(); 

    /** 
     * Set whether trailing whitespace at the end of lines should be included 
     * in the output.
     * Defaults to true.
     */
    void setTrailingWhitespace(bool enable);
    /**
     * Returns whether trailing whitespace at the end of lines is included
     * in the output.
     */
    bool trailingWhitespace() const;

    virtual void begin(QTextStream* output);
    virtual void end();

	virtual void decodeLine(const Character* const characters,
							int count,
							LineProperty properties);	

    
private:
    QTextStream* _output;
    bool _includeTrailingWhitespace;
};

/**
 * A terminal character decoder which produces pretty HTML markup
 */
class HTMLDecoder : public TerminalCharacterDecoder
{
public:
	/** 
	 * Constructs an HTML decoder using a default black-on-white color scheme.
	 */
	HTMLDecoder();

	/**
	 * Sets the colour table which the decoder uses to produce the HTML colour codes in its
	 * output
	 */
	void setColorTable( const ColorEntry* table );
		
	virtual void decodeLine(const Character* const characters,
							int count,
							LineProperty properties);

    virtual void begin(QTextStream* output);
    virtual void end();

private:
	void openSpan(QString& text , const QString& style);
	void closeSpan(QString& text);

    QTextStream* _output;
	const ColorEntry* _colorTable;
    bool _innerSpanOpen; 
	quint8 _lastRendition;
	CharacterColor _lastForeColor;
	CharacterColor _lastBackColor;

};

}

#endif