Refactoring...

This commit is contained in:
Benoit Blanchon
2014-08-26 09:53:32 +02:00
parent e48ea94789
commit b5002265cf
2 changed files with 13 additions and 19 deletions

View File

@ -2,12 +2,14 @@
void IndentedPrintDecorator::indent() void IndentedPrintDecorator::indent()
{ {
currentTabCount++; if (level<127)
level++;
} }
void IndentedPrintDecorator::unindent() void IndentedPrintDecorator::unindent()
{ {
currentTabCount--; if (level>0)
level--;
} }
size_t IndentedPrintDecorator::write(uint8_t c) size_t IndentedPrintDecorator::write(uint8_t c)
@ -15,7 +17,7 @@ size_t IndentedPrintDecorator::write(uint8_t c)
size_t n = 0; size_t n = 0;
if (isNewLine) if (isNewLine)
n += writeCurrentTabs(); n += writeTabs();
n += sink.write(c); n += sink.write(c);
@ -24,16 +26,11 @@ size_t IndentedPrintDecorator::write(uint8_t c)
return n; return n;
} }
size_t IndentedPrintDecorator::writeCurrentTabs() size_t IndentedPrintDecorator::writeTabs()
{
return writeTabs(currentTabCount);
}
size_t IndentedPrintDecorator::writeTabs(int count)
{ {
size_t n = 0; size_t n = 0;
for (int i = 0; i<count; i++) for (int i = 0; i<level; i++)
n += sink.write(' '); n += sink.write(' ');
return n; return n;

View File

@ -14,23 +14,20 @@ public:
IndentedPrintDecorator(Print& p) IndentedPrintDecorator(Print& p)
: sink(p) : sink(p)
{ {
pendingTabCount = 0; level = 0;
currentTabCount = 0;
isNewLine = true; isNewLine = true;
} }
virtual size_t write(uint8_t);
void indent(); void indent();
void unindent(); void unindent();
bool isNewLine;
virtual size_t write(uint8_t);
private: private:
Print& sink; Print& sink;
int currentTabCount; uint8_t level : 7;
int pendingTabCount; bool isNewLine : 1;
size_t writeCurrentTabs(); size_t writeTabs();
size_t writeTabs(int count);
}; };