NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
TextManager.h
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2018 Erik Haenel et al.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17******************************************************************************/
18
19
20#ifndef TEXTMANAGER__H
21#define TEXTMANAGER__H
22
23#include <vector>
24#include <deque>
25#include <string>
26
27class GenericTerminal;
28
29
38{
39 size_t pos;
40 size_t line;
41 bool isValid;
42
43 LogicalCursor() : pos(0), line(0), isValid(0) {}
44 LogicalCursor(size_t nPos, size_t nLine) : pos(nPos), line(nLine), isValid(true) {}
45 void move(size_t _pos, size_t _line)
46 {
47 pos = _pos;
48 line = _line;
49 }
50 void advance()
51 {
52 pos++;
53 }
54 bool revert()
55 {
56 if (!pos)
57 return false;
58 pos--;
59 return true;
60 }
61 bool operator!() const
62 {
63 return !isValid;
64 }
65 operator bool() const
66 {
67 return isValid;
68 }
70 {
71 this->advance();
72 return *this;
73 }
75 {
76 LogicalCursor cursor(*this);
77 cursor.pos += n;
78 return cursor;
79 }
81 {
82 LogicalCursor cursor(*this);
83 cursor.pos -= n;
84 return cursor;
85 }
86 bool operator--(int)
87 {
88 return this->revert();
89 }
90 bool operator==(const LogicalCursor& cursor) const
91 {
92 return cursor.isValid == isValid && cursor.pos == pos && cursor.line == line;
93 }
94 bool operator<(const LogicalCursor& cursor) const
95 {
96 return (isValid == cursor.isValid) && (line < cursor.line || (line == cursor.line && pos < cursor.pos));
97 }
98 bool operator<=(const LogicalCursor& cursor) const
99 {
100 return operator<(cursor) || operator==(cursor);
101 }
102 bool operator>(const LogicalCursor& cursor) const
103 {
104 return !(operator<=(cursor));
105 }
106 bool operator>=(const LogicalCursor& cursor) const
107 {
108 return !(operator<(cursor));
109 }
110 bool operator!=(const LogicalCursor& cursor) const
111 {
112 return !(operator==(cursor));
113 }
114};
115
116
117
124{
125 size_t x;
126 size_t y;
128
129 ViewCursor() : x(0), y(0), isValid(false) {}
130 ViewCursor(size_t _x, size_t _y) : x(_x), y(_y), isValid(true) {}
131 void move(size_t _x, size_t _y)
132 {
133 x = _x;
134 y = _y;
135 }
136 void advance()
137 {
138 x++;
139 }
140 bool revert()
141 {
142 if (!x)
143 return false;
144 x--;
145 return true;
146 }
147 bool operator!() const
148 {
149 return !isValid;
150 }
151 operator bool() const
152 {
153 return isValid;
154 }
156 {
157 this->advance();
158 return *this;
159 }
160 bool operator--(int)
161 {
162 return this->revert();
163 }
164 bool operator==(const ViewCursor& cursor) const
165 {
166 return cursor.isValid == isValid && cursor.x == x && cursor.y == y;
167 }
168 bool operator<(const ViewCursor& cursor) const
169 {
170 return (isValid == cursor.isValid) && (y < cursor.y || (y == cursor.y && x < cursor.x));
171 }
172 bool operator<=(const ViewCursor& cursor) const
173 {
174 return operator<(cursor) || operator==(cursor);
175 }
176 bool operator>(const ViewCursor& cursor) const
177 {
178 return !(operator<=(cursor));
179 }
180 bool operator>=(const ViewCursor& cursor) const
181 {
182 return !(operator<(cursor));
183 }
184 bool operator!=(const ViewCursor& cursor) const
185 {
186 return !(operator==(cursor));
187 }
188};
189
190
191
197{
198 std::string sLine;
199 std::vector<unsigned short> colors;
200 std::vector<LogicalCursor> coords;
201};
202
203
204
212{
213 char m_char;
215
216 enum
217 {
218 USERTEXT = 0x1000,
219 EDITABLE = 0x2000,
220 SELECTED = 0x4000,
221 COLOR = 0xFFF,
222 FLAGS = 0xF000
223 };
224
226 Character(char c) : m_char(c), m_style(0) {}
227 Character(char&& c) : m_style(0)
228 {
229 m_char = std::move(c);
230 }
233 {
234 m_char = std::move(c.m_char);
235 m_style = std::move(c.m_style);
236 }
237 Character(char c, unsigned char flags) : m_char(c), m_style(flags << 12) {}
238
240 {
241 m_char = c.m_char;
242 m_style = c.m_style;
243 return *this;
244 }
245
247 {
248 m_char = std::move(c.m_char);
249 m_style = std::move(c.m_style);
250 return *this;
251 }
252
254 {
255 m_char = std::move(c);
256 return *this;
257 }
258
259 void setFlags(short flags)
260 {
261 m_style |= (unsigned char)flags << 12;
262 }
263
264 bool userText() const
265 {
266 return m_style & USERTEXT;
267 }
268
269 bool editable() const
270 {
271 return m_style & EDITABLE;
272 }
273
275 {
276 m_style |= USERTEXT;
277 m_style &= ~EDITABLE;
278 }
279
281 {
282 m_style |= EDITABLE;
283 }
284
285 void select()
286 {
287 m_style |= SELECTED;
288 }
289
290 void unselect()
291 {
292 m_style &= ~SELECTED;
293 }
294
295 bool isSelected() const
296 {
297 return m_style & SELECTED;
298 }
299
300 int getColor() const
301 {
302 return m_style & COLOR;
303 }
304
305 void setColor(int color)
306 {
307 m_style = (m_style & FLAGS) | (color & COLOR);
308 }
309};
310
311
312
318class CharacterVector : public std::vector<Character>
319{
320 private:
322
323 void assign(const std::string& sText)
324 {
325 clear();
326 append(sText);
327 }
328
329 void append(const std::string& sText)
330 {
331 for (size_t i = 0; i < sText.length(); i++)
332 emplace_back(sText[i]);
333 }
334
335 public:
336 CharacterVector() : vector<Character>() {}
337
338 CharacterVector(const std::string& sText) : vector<Character>()
339 {
340 append(sText);
341 }
342
343 CharacterVector(const std::string& sText, short flags) : vector<Character>()
344 {
345 append(sText);
346
347 if (flags)
348 {
349 for (size_t i = 0; i < size(); i++)
350 operator[](i).setFlags(flags);
351 }
352 }
353
354 CharacterVector& operator+=(const std::string& sText)
355 {
356 append(sText);
357 return *this;
358 }
359
361 {
362 vector<Character>::insert(end(), vect.begin(), vect.end());
363 return *this;
364 }
365
366 CharacterVector& operator=(const std::string& sText)
367 {
368 assign(sText);
369 return *this;
370 }
371
373 {
374 if (!size())
375 return m_dummy;
376
377 return vector<Character>::operator[](i);
378 }
379
380 const Character& operator[](size_t i) const
381 {
382 if (!size())
383 return m_dummy;
384
385 return vector<Character>::operator[](i);
386 }
387
389 {
390 if (!size())
391 return m_dummy;
392
393 return vector<Character>::front();
394 }
395
396 const Character& front() const
397 {
398 if (!size())
399 return m_dummy;
400
401 return vector<Character>::front();
402 }
403
405 {
406 if (!size())
407 return m_dummy;
408
409 return vector<Character>::back();
410 }
411
412 const Character& back() const
413 {
414 if (!size())
415 return m_dummy;
416
417 return vector<Character>::back();
418 }
419
420 std::string toString() const
421 {
422 std::string sRet;
423
424 for (size_t i = 0; i < size(); i++)
425 sRet.append(1u, operator[](i).m_char);
426
427 return sRet;
428 }
429
430 std::string substr(size_t pos, size_t len = std::string::npos) const
431 {
432 return toString().substr(pos, len);
433 }
434
435 std::vector<unsigned short> subcolors(size_t pos, size_t len = std::string::npos) const
436 {
437 if (pos >= size())
438 return std::vector<unsigned short>();
439
440 if (pos+len >= size())
441 len = size() - pos;
442
443 std::vector<unsigned short> vect;
444
445 for (size_t i = 0; i < len; i++)
446 vect.push_back(operator[](pos+i).getColor());
447
448 return vect;
449
450 }
451
452 size_t length() const
453 {
454 return size();
455 }
456
457 iterator insert(size_t pos, const std::string& sText, short flags = 0)
458 {
459 return insert(pos, CharacterVector(sText, flags));
460 }
461
462 iterator insert(size_t pos, const CharacterVector& vect)
463 {
464 if (pos >= size())
465 return std::vector<Character>::insert(end(), vect.begin(), vect.end());
466
467 return std::vector<Character>::insert(begin() + pos, vect.begin(), vect.end());
468 }
469
470 void append(size_t n, char c)
471 {
472 append(std::string(n, c));
473 }
474};
475
476
477
486{
487 public:
488 TextManager(GenericTerminal* parent = nullptr, int width = 80, int height = 24, int maxWidth = 160, int maxHeight = 300);
489 ~TextManager();
490
491 void printOutput(const std::string& sLine);
492 void insertInput(const std::string& sLine, size_t logicalpos = std::string::npos);
493
494 ViewCursor toViewCursor(const LogicalCursor& logCursor) const;
496 LogicalCursor toLogicalCursor(const ViewCursor& viewCursor) const;
498
499 std::string getRenderedString(size_t viewLine) const;
500 std::vector<unsigned short> getRenderedColors(size_t viewLine) const;
501
502 size_t tab();
503 void newLine();
504 void backspace(const LogicalCursor& logCursor);
505 void eraseLine();
506 bool clearRange(const ViewCursor& cursor1, const ViewCursor& cursor2);
507
508 void selectText(const ViewCursor& viewCursor, bool bSelect = true);
509 void unselectAll();
510 bool isSelected(const ViewCursor& viewCursor) const;
511 bool isSelectedLogical(const LogicalCursor& cursor) const;
512 std::string getSelectedText() const;
513 std::string getCurrentInputLine() const;
514 std::string getPreviousLine() const;
515
516 int GetSize() const;
517 int GetMaxSize() const;
518 int GetHeight() const;
519 int GetNumLinesScrolled() const;
520 int GetLinesReceived() const;
521 std::string GetInputHistory(bool vcursorup = true);
522 std::string GetTextRange(int y, int x0, int x1) const;
523 std::string GetWordAt(int y, int x) const;
524 std::string GetWordStartAt(int y, int x) const;
525 char GetCharAdjusted(int y, int x) const;
526 char GetCharLogical(const LogicalCursor& cursor) const;
527 bool IsUserText(int y, int x) const;
528 bool IsEditable(int y, int x) const;
529 bool IsEditableLogical(const LogicalCursor& logCursor) const;
530 unsigned short GetColor(int y, int x) const;
531 unsigned short GetColorAdjusted(int y, int x) const;
532
533 void ChangeEditableState();
534
535 void SetMaxSize(int newSize);
537 {
538 m_virtualCursor = m_managedText.size() - 1;
539 }
540 void SetColorAdjusted(int y, int x, unsigned short value);
541
542 bool Scroll(int numLines, bool scrollUp);
543 void Resize(int width, int height);
544 void Reset();
545
546 std::string operator[](int index);
547
548 int AdjustIndex(int index) const;
549
550 private:
552
562
565
566 std::deque<CharacterVector> m_managedText;
567 std::deque<RenderedLine> m_renderedBlock;
568
569 int calc_color(int fg, int bg, int flags);
570 void updateColors(bool isErrorLine = false);
571 void renderLayout();
572 void synchronizeRenderedBlock(int linesToDelete);
573 size_t findNextLinebreak(const std::string& currentLine, size_t currentLinebreak) const;
574
575 enum
576 {
580 };
581
582};
583
584
585#endif
586
This class resembles an extended string class, which contains the extended character class.
Definition: TextManager.h:319
std::string toString() const
Definition: TextManager.h:420
const Character & operator[](size_t i) const
Definition: TextManager.h:380
void append(size_t n, char c)
Definition: TextManager.h:470
CharacterVector(const std::string &sText, short flags)
Definition: TextManager.h:343
iterator insert(size_t pos, const std::string &sText, short flags=0)
Definition: TextManager.h:457
std::string substr(size_t pos, size_t len=std::string::npos) const
Definition: TextManager.h:430
const Character & back() const
Definition: TextManager.h:412
void append(const std::string &sText)
Definition: TextManager.h:329
CharacterVector & operator+=(const CharacterVector &vect)
Definition: TextManager.h:360
CharacterVector & operator=(const std::string &sText)
Definition: TextManager.h:366
Character & front()
Definition: TextManager.h:388
CharacterVector(const std::string &sText)
Definition: TextManager.h:338
CharacterVector & operator+=(const std::string &sText)
Definition: TextManager.h:354
Character & operator[](size_t i)
Definition: TextManager.h:372
Character m_dummy
Definition: TextManager.h:321
void assign(const std::string &sText)
Definition: TextManager.h:323
iterator insert(size_t pos, const CharacterVector &vect)
Definition: TextManager.h:462
Character & back()
Definition: TextManager.h:404
size_t length() const
Definition: TextManager.h:452
const Character & front() const
Definition: TextManager.h:396
std::vector< unsigned short > subcolors(size_t pos, size_t len=std::string::npos) const
Definition: TextManager.h:435
An implementation of a generic terminal, which has to be specialized in the child classes.
Definition: gterm.hpp:42
This class manages the actual text by storing it in an internal buffer. During an update the TextMana...
Definition: TextManager.h:486
bool clearRange(const ViewCursor &cursor1, const ViewCursor &cursor2)
Clears the range between two view cursors.
std::string getSelectedText() const
This member function returns the selected text.
void synchronizeRenderedBlock(int linesToDelete)
Removes parts of the already rendered block.
bool IsUserText(int y, int x) const
Determines, whether the character at (x,y) is a user text.
int AdjustIndex(int index) const
void printOutput(const std::string &sLine)
This is the read-only print function.
Definition: TextManager.cpp:76
std::deque< CharacterVector > m_managedText
Definition: TextManager.h:566
int m_bottomLine
Definition: TextManager.h:554
unsigned short GetColorAdjusted(int y, int x) const
std::string getRenderedString(size_t viewLine) const
Return the rendered line for the current viewport setting.
std::string operator[](int index)
void SetColorAdjusted(int y, int x, unsigned short value)
size_t tab()
Insert a tab character at the current position.
size_t m_tabLength
Definition: TextManager.h:563
int GetHeight() const
void renderLayout()
This function renders the layout.
void insertInput(const std::string &sLine, size_t logicalpos=std::string::npos)
This is the user input function.
bool Scroll(int numLines, bool scrollUp)
void newLine()
Adds a new line to the current managed text.
int calc_color(int fg, int bg, int flags)
Helper function for converting the colors into a single int.
int m_viewportHeight
Definition: TextManager.h:557
void backspace(const LogicalCursor &logCursor)
Performs a backspace operation.
ViewCursor toViewCursor(const LogicalCursor &logCursor) const
Convert a logical cursor to a view cursor.
std::string getPreviousLine() const
This member function returns the contents of the line before the current input line.
size_t findNextLinebreak(const std::string &currentLine, size_t currentLinebreak) const
Find the next linebreak position.
TextManager(GenericTerminal *parent=nullptr, int width=80, int height=24, int maxWidth=160, int maxHeight=300)
Definition: TextManager.cpp:46
int m_linesReceived
Definition: TextManager.h:558
void Resize(int width, int height)
std::vector< unsigned short > getRenderedColors(size_t viewLine) const
Return the rendered colors for the selected viewport line.
size_t m_indentDepth
Definition: TextManager.h:564
void updateColors(bool isErrorLine=false)
Update the colors for the current line.
~TextManager()
Destructor will reset the internal buffer.
Definition: TextManager.cpp:60
char GetCharAdjusted(int y, int x) const
bool IsEditableLogical(const LogicalCursor &logCursor) const
Determines, whether the character at the logical position is editable text.
char GetCharLogical(const LogicalCursor &cursor) const
Returns the character at the logical position.
std::string GetInputHistory(bool vcursorup=true)
Get the next history line.
int m_numLinesScrolledUp
Definition: TextManager.h:555
void selectText(const ViewCursor &viewCursor, bool bSelect=true)
Selects the text at the view cursor position.
void SetMaxSize(int newSize)
void eraseLine()
Erase the current line.
ViewCursor getCurrentViewPos() const
Returns the current cursor position as view cursor.
bool IsEditable(int y, int x) const
Determines, whether the character at (x,y) is editable text.
std::string GetTextRange(int y, int x0, int x1) const
Extracts the text between the positions.
void ResetVirtualCursorLine()
Definition: TextManager.h:536
bool isSelected(const ViewCursor &viewCursor) const
Determines, whether the pointed character is selected.
std::string GetWordStartAt(int y, int x) const
Returns the word start at the passed position.
void ChangeEditableState()
Removes the editable flag from the managed text.
LogicalCursor toLogicalCursor(const ViewCursor &viewCursor) const
Convert a view cursor into a logical cursor.
unsigned short GetColor(int y, int x) const
int m_virtualCursor
Definition: TextManager.h:561
LogicalCursor getCurrentLogicalPos() const
Returns the current cursor position as logical cursor.
int GetLinesReceived() const
std::string getCurrentInputLine() const
Returns the contents of the input line.
void unselectAll()
This member function unselects the whole text at once.
int m_viewportWidth
Definition: TextManager.h:556
int GetNumLinesScrolled() const
int GetSize() const
GenericTerminal * m_parent
Definition: TextManager.h:551
std::string GetWordAt(int y, int x) const
Returns the word at the passed position.
std::deque< RenderedLine > m_renderedBlock
Definition: TextManager.h:567
bool isSelectedLogical(const LogicalCursor &cursor) const
Determines, whether the pointed character is selected.
int GetMaxSize() const
Returns the buffer size of the terminal.
This structure combines the character information needed in the terminal: the character itself,...
Definition: TextManager.h:212
Character(Character &&c)
Definition: TextManager.h:232
bool isSelected() const
Definition: TextManager.h:295
void unselect()
Definition: TextManager.h:290
int getColor() const
Definition: TextManager.h:300
void makeUserText()
Definition: TextManager.h:274
char m_char
Definition: TextManager.h:213
Character & operator=(Character &&c)
Definition: TextManager.h:246
void setFlags(short flags)
Definition: TextManager.h:259
void makeEditable()
Definition: TextManager.h:280
bool userText() const
Definition: TextManager.h:264
Character & operator=(const Character &c)
Definition: TextManager.h:239
Character(const Character &c)
Definition: TextManager.h:231
Character(char c, unsigned char flags)
Definition: TextManager.h:237
void select()
Definition: TextManager.h:285
void setColor(int color)
Definition: TextManager.h:305
Character(char c)
Definition: TextManager.h:226
Character(char &&c)
Definition: TextManager.h:227
bool editable() const
Definition: TextManager.h:269
Character & operator=(char &&c)
Definition: TextManager.h:253
Cursor, which is used in the TextManager to identify the actual line and position in the m_text varia...
Definition: TextManager.h:38
bool revert()
Definition: TextManager.h:54
bool operator<=(const LogicalCursor &cursor) const
Definition: TextManager.h:98
void move(size_t _pos, size_t _line)
Definition: TextManager.h:45
bool operator!=(const LogicalCursor &cursor) const
Definition: TextManager.h:110
bool operator>=(const LogicalCursor &cursor) const
Definition: TextManager.h:106
bool operator!() const
Definition: TextManager.h:61
void advance()
Definition: TextManager.h:50
bool operator==(const LogicalCursor &cursor) const
Definition: TextManager.h:90
LogicalCursor(size_t nPos, size_t nLine)
Definition: TextManager.h:44
bool operator<(const LogicalCursor &cursor) const
Definition: TextManager.h:94
bool operator--(int)
Definition: TextManager.h:86
LogicalCursor & operator++(int)
Definition: TextManager.h:69
LogicalCursor operator+(int n)
Definition: TextManager.h:74
LogicalCursor operator-(int n)
Definition: TextManager.h:80
bool operator>(const LogicalCursor &cursor) const
Definition: TextManager.h:102
This structure combines the rendered line with its colors and its coordinates.
Definition: TextManager.h:197
std::vector< unsigned short > colors
Definition: TextManager.h:199
std::string sLine
Definition: TextManager.h:198
std::vector< LogicalCursor > coords
Definition: TextManager.h:200
Cursor, which is used in the terminal. The TextManager is able to convert this cursor into a LogicalC...
Definition: TextManager.h:124
bool operator--(int)
Definition: TextManager.h:160
ViewCursor & operator++(int)
Definition: TextManager.h:155
bool operator>(const ViewCursor &cursor) const
Definition: TextManager.h:176
size_t y
Definition: TextManager.h:126
bool operator==(const ViewCursor &cursor) const
Definition: TextManager.h:164
bool isValid
Definition: TextManager.h:127
bool operator>=(const ViewCursor &cursor) const
Definition: TextManager.h:180
bool operator!=(const ViewCursor &cursor) const
Definition: TextManager.h:184
bool operator<(const ViewCursor &cursor) const
Definition: TextManager.h:168
ViewCursor(size_t _x, size_t _y)
Definition: TextManager.h:130
bool revert()
Definition: TextManager.h:140
size_t x
Definition: TextManager.h:125
bool operator<=(const ViewCursor &cursor) const
Definition: TextManager.h:172
void move(size_t _x, size_t _y)
Definition: TextManager.h:131
bool operator!() const
Definition: TextManager.h:147
void advance()
Definition: TextManager.h:136