summaryrefslogtreecommitdiff
path: root/src/interfaceconsole.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/interfaceconsole.cpp')
-rw-r--r--src/interfaceconsole.cpp222
1 files changed, 222 insertions, 0 deletions
diff --git a/src/interfaceconsole.cpp b/src/interfaceconsole.cpp
index 0b1b43f..05ca56c 100644
--- a/src/interfaceconsole.cpp
+++ b/src/interfaceconsole.cpp
@@ -1,5 +1,16 @@
1#include "interfaceconsole.h" 1#include "interfaceconsole.h"
2 2
3#include "smlnode.h"
4
5#include <bu/sio.h>
6
7#include <bu/plugger.h>
8
9PluginInterface3( plugin_interface_console, console, InterfaceConsole,
10 Interface, "Mike Buland", 1, 0 );
11
12using namespace Bu;
13
3InterfaceConsole::InterfaceConsole() 14InterfaceConsole::InterfaceConsole()
4{ 15{
5} 16}
@@ -8,3 +19,214 @@ InterfaceConsole::~InterfaceConsole()
8{ 19{
9} 20}
10 21
22void InterfaceConsole::appendToken( Bu::String &sCurLine,
23 Bu::String &sNextToken, int &iLineLen, int &iNextLen )
24{
25 if( iLineLen + iNextLen + 1 >= 78 )
26 {
27 sio << sCurLine << sio.nl;
28 iLineLen = 0;
29 sCurLine = sNextToken;
30 }
31 else
32 {
33 sCurLine += sNextToken;
34 }
35 iLineLen += iNextLen + 1;
36 sCurLine += " ";
37 iNextLen = 0;
38 sNextToken.clear();
39}
40
41Bu::String InterfaceConsole::getVt100Style( const StyleStack &sStyle )
42{
43 if( sStyle.isEmpty() )
44 {
45 return "\x1B[0m";
46 }
47
48 int sCurStyle = 0;
49 for( StyleStack::const_iterator i = sStyle.begin(); i; i++ )
50 {
51// sio << "Merging in: " << Fmt::hex() << *i << sio.nl;
52 if( ((sCurStyle&stTypeMask) & ((*i)&stTypeMask)) == 0 )
53 {
54 sCurStyle |= *i;
55// sio << " -> curStyle = " << Fmt::hex() << *i << sio.nl;
56 }
57 }
58
59 Bu::String sRet;
60
61// sio << "Color: " << Fmt::hex() << sCurStyle << sio.nl;
62 switch( sCurStyle&stColor )
63 {
64 case stRed:
65 sRet += "\x1B[1;31m";
66 break;
67
68 case stGreen:
69 sRet += "\x1B[1;32m";
70 break;
71 }
72
73 return sRet;
74}
75
76void InterfaceConsole::display( const SmlNode *pNode )
77{
78 Bu::String sCurLine;
79 Bu::String sNextToken;
80 int iLineLen = 0;
81 int iNextLen = 0;
82 int iState = 0;
83 typedef Bu::List<SmlNode::SmlNodeList::const_iterator> NodeStack;
84 NodeStack sNode;
85
86 StyleStack sStyle;
87
88 sNode.push( pNode->getChildren().begin() );
89
90 for(;;)
91 {
92 if( !sNode.peek() )
93 {
94 sNode.pop();
95 if( sNode.isEmpty() )
96 break;
97 if( sNode.peek() )
98 {
99 // sio << "Pop'd: " << (*sNode.peek())->getText() << sio.nl;
100 Bu::String sTag = (*sNode.peek())->getText();
101 if( sTag == "green" || sTag == "red" )
102 {
103 sStyle.pop();
104 sNextToken += getVt100Style( sStyle );
105 }
106 sNode.peek()++;
107 continue;
108 }
109 }
110 if( sNode.isEmpty() )
111 {
112 break;
113 }
114 const SmlNode *pNode = (*sNode.peek());
115 switch( pNode->getType() )
116 {
117 case SmlNode::typeRoot:
118 throw Bu::ExceptionBase("Invalid root.");
119
120 case SmlNode::typeText:
121 {
122 // sio << "Process text node: " << pNode->getText() <<
123 // sio.nl;
124 Bu::String::const_iterator iBgn = pNode->getText().begin();
125 Bu::String::const_iterator iEnd = iBgn;
126 int iTmpLen = 0;
127 for(;iBgn;)
128 {
129 switch( iState )
130 {
131 case 0: // begining of paragraph
132 if( iBgn && ( *iBgn == ' ' || *iBgn == '\n' ||
133 *iBgn == '\r' || *iBgn == '\t' ) )
134 {
135 iBgn++;
136 }
137 else
138 {
139 // Here is where you would indent paragraphs
140 iEnd = iBgn;
141 iState = 1;
142 }
143 break;
144
145 case 1: // non-whitespace
146 if( !iEnd )
147 {
148 sNextToken.append( iBgn, iEnd );
149 iBgn = iEnd;
150 iNextLen += iTmpLen;
151 iTmpLen = 0;
152 }
153 else if( *iEnd == ' ' || *iEnd == '\n' ||
154 *iEnd == '\r' || *iEnd == '\t' )
155 {
156 sNextToken.append( iBgn, iEnd );
157 iNextLen += iTmpLen;
158 iTmpLen = 0;
159 iState = 2;
160 iBgn = iEnd;
161 }
162 else
163 {
164 iEnd++;
165 iTmpLen++;
166 }
167 break;
168
169 case 2: // Whitespace
170 if( iBgn && (*iBgn == ' ' || *iBgn == '\n' ||
171 *iBgn == '\r' || *iBgn == '\t') )
172 {
173 iBgn++;
174 }
175 else
176 {
177 iEnd = iBgn;
178 iState = 1;
179 appendToken( sCurLine, sNextToken,
180 iLineLen, iNextLen );
181 }
182 break;
183 }
184 }
185 }
186 break;
187
188 case SmlNode::typeTag:
189 if( pNode->getChildren().isEmpty() )
190 {
191 if( pNode->getText() == "break" )
192 {
193 appendToken( sCurLine, sNextToken, iLineLen, iNextLen );
194 if( !sCurLine.isEmpty() )
195 sio << sCurLine << sio.nl;
196 sCurLine.clear();
197 iLineLen = 0;
198 iState = 0;
199 }
200 }
201 else
202 {
203// sio << "Push'd: " << pNode->getText() << sio.nl;
204 Bu::String sTag = pNode->getText();
205 if( sTag == "green" )
206 {
207 sStyle.push( stGreen );
208 }
209 else if( sTag == "red" )
210 {
211 sStyle.push( stRed );
212 }
213 sNextToken += getVt100Style( sStyle );
214 sNode.push( pNode->getChildren().begin() );
215 continue;
216/* for( SmlNode::SmlNodeList::const_iterator i =
217 pNode->getChildren().begin(); i; i++ )
218 {
219 s
220 smlToConsole( *i, sCurLine, sNextToken, iLineLen, iState );
221 }
222*/
223 }
224 break;
225 }
226 sNode.peek()++;
227 }
228 if( !sNextToken.isEmpty() )
229 appendToken( sCurLine, sNextToken, iLineLen, iNextLen );
230 sio << sCurLine << sio.nl;
231}
232