2020-05-17 04:37:41 -04:00
|
|
|
//
|
|
|
|
// FILE: LineFormatter.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2023-01-31 07:39:06 -05:00
|
|
|
// VERSION: 0.2.0
|
2021-11-06 12:49:23 -04:00
|
|
|
// PURPOSE: Simple positioning wrapper class for Serial
|
2020-05-17 04:37:41 -04:00
|
|
|
// DATE: 2020-05-14
|
|
|
|
// URL: https://github.com/RobTillaart/LineFormatter
|
|
|
|
|
|
|
|
|
|
|
|
#include "LineFormatter.h"
|
|
|
|
|
2021-11-06 12:49:23 -04:00
|
|
|
|
2020-05-17 04:37:41 -04:00
|
|
|
LineFormatter::LineFormatter(Print* stream)
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
_stream = stream;
|
2021-11-06 12:49:23 -04:00
|
|
|
reset();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void LineFormatter::reset()
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
_pos = 0;
|
|
|
|
_maxPos = 0;
|
|
|
|
_lineCount = 0;
|
|
|
|
_anl = 0;
|
2020-05-17 04:37:41 -04:00
|
|
|
_autoNewLine = 0;
|
2021-01-29 06:31:58 -05:00
|
|
|
_tabCount = 0;
|
2021-11-06 12:49:23 -04:00
|
|
|
}
|
2020-05-17 04:37:41 -04:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////
|
|
|
|
//
|
2022-11-14 14:50:36 -05:00
|
|
|
// WRITE - the core
|
2020-05-17 04:37:41 -04:00
|
|
|
//
|
|
|
|
size_t LineFormatter::write(uint8_t c)
|
|
|
|
{
|
2022-11-14 14:50:36 -05:00
|
|
|
// handle tabs.
|
2023-01-31 07:39:06 -05:00
|
|
|
if (_tabCount && (c == '\t'))
|
2020-05-17 04:37:41 -04:00
|
|
|
{
|
|
|
|
write(' ');
|
|
|
|
for (int i = 0; i < _tabCount; i++)
|
|
|
|
{
|
2022-11-14 14:50:36 -05:00
|
|
|
if (_tabStop[i] > _pos + 1) // assume sorted
|
2020-05-17 04:37:41 -04:00
|
|
|
{
|
|
|
|
gotoPos(_tabStop[i] - 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_stream->write(c);
|
|
|
|
_pos++;
|
|
|
|
}
|
|
|
|
|
2022-11-14 14:50:36 -05:00
|
|
|
// handle return
|
2020-05-17 04:37:41 -04:00
|
|
|
if (c == '\n')
|
|
|
|
{
|
|
|
|
_pos = 0;
|
|
|
|
_lineCount++;
|
|
|
|
_anl++;
|
|
|
|
}
|
|
|
|
|
2023-01-31 07:39:06 -05:00
|
|
|
// handle _maxPos if enabled (_maxPos > 0)
|
|
|
|
if ((_maxPos > 0) && (_pos == _maxPos))
|
2020-05-17 04:37:41 -04:00
|
|
|
{
|
2023-01-31 07:39:06 -05:00
|
|
|
write('\n'); // recursive call!
|
2020-05-17 04:37:41 -04:00
|
|
|
}
|
|
|
|
|
2022-11-14 14:50:36 -05:00
|
|
|
// handle autoNewLine
|
2020-05-17 04:37:41 -04:00
|
|
|
if (_autoNewLine && (_anl == _autoNewLine))
|
|
|
|
{
|
2023-01-31 07:39:06 -05:00
|
|
|
write('\n'); // recursive call!
|
2020-05-17 04:37:41 -04:00
|
|
|
_anl = 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-11-06 12:49:23 -04:00
|
|
|
|
2023-01-31 07:39:06 -05:00
|
|
|
void LineFormatter::setMaxLength(uint8_t maxPos)
|
|
|
|
{
|
|
|
|
_maxPos = maxPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t LineFormatter::getMaxLength()
|
|
|
|
{
|
|
|
|
return _maxPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t LineFormatter::gotoPos(uint8_t pos)
|
|
|
|
{
|
|
|
|
while (_pos < pos) write(' ');
|
|
|
|
return _pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-17 04:37:41 -04:00
|
|
|
///////////////////////////////////////////
|
|
|
|
//
|
2022-11-14 14:50:36 -05:00
|
|
|
// REPEAT
|
2020-05-17 04:37:41 -04:00
|
|
|
//
|
|
|
|
void LineFormatter::repeat(uint8_t n, char c, uint8_t newLine)
|
|
|
|
{
|
|
|
|
while (n--) print(c);
|
|
|
|
while (newLine--) write('\n');
|
|
|
|
}
|
|
|
|
|
2021-11-06 12:49:23 -04:00
|
|
|
|
2020-05-17 04:37:41 -04:00
|
|
|
void LineFormatter::repeat(uint8_t n, const char* str, uint8_t newLine)
|
|
|
|
{
|
|
|
|
while (n--) print(str);
|
|
|
|
while (newLine--) write('\n');
|
|
|
|
}
|
|
|
|
|
2021-11-06 12:49:23 -04:00
|
|
|
|
2020-05-17 04:37:41 -04:00
|
|
|
///////////////////////////////////////////
|
|
|
|
//
|
2022-11-14 14:50:36 -05:00
|
|
|
// AUTONEWLINE
|
2020-05-17 04:37:41 -04:00
|
|
|
//
|
|
|
|
void LineFormatter::setAutoNewLine(uint8_t n)
|
|
|
|
{
|
|
|
|
_autoNewLine = n;
|
|
|
|
_anl = 0;
|
|
|
|
};
|
|
|
|
|
2021-11-06 12:49:23 -04:00
|
|
|
|
2023-01-31 07:39:06 -05:00
|
|
|
uint8_t LineFormatter::getAutoNewLine()
|
|
|
|
{
|
|
|
|
return _autoNewLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-17 04:37:41 -04:00
|
|
|
///////////////////////////////////////////
|
|
|
|
//
|
2022-11-14 14:50:36 -05:00
|
|
|
// TAB
|
2020-05-17 04:37:41 -04:00
|
|
|
//
|
2023-01-31 07:39:06 -05:00
|
|
|
bool LineFormatter::addTab(uint8_t pos)
|
|
|
|
{
|
|
|
|
if (pos == 0) return false;
|
|
|
|
// full ?
|
|
|
|
if (_tabCount >= MAX_TAB_STOPS) return false;
|
|
|
|
// prevent doubles.
|
|
|
|
if (existTab(pos)) return false;
|
|
|
|
|
|
|
|
_tabStop[_tabCount] = pos;
|
|
|
|
_tabCount++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LineFormatter::addRelTab(uint8_t n)
|
|
|
|
{
|
|
|
|
if (_tabCount >= MAX_TAB_STOPS) return false;
|
|
|
|
uint8_t newPos = n;
|
|
|
|
if (_tabCount != 0) newPos += _tabStop[_tabCount - 1];
|
|
|
|
addTab(newPos);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-17 04:37:41 -04:00
|
|
|
void LineFormatter::clearTabs()
|
|
|
|
{
|
|
|
|
_tabCount = 0;
|
|
|
|
for (uint8_t i = 0; i < MAX_TAB_STOPS; i++)
|
|
|
|
{
|
|
|
|
_tabStop[i] = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-06 12:49:23 -04:00
|
|
|
|
2023-01-31 07:39:06 -05:00
|
|
|
bool LineFormatter::removeTab(uint8_t pos)
|
2020-05-17 04:37:41 -04:00
|
|
|
{
|
2023-01-31 07:39:06 -05:00
|
|
|
for (uint8_t i = 0; i < _tabCount; i++)
|
|
|
|
{
|
|
|
|
if (_tabStop[i] == pos)
|
|
|
|
{
|
|
|
|
_tabCount--;
|
|
|
|
while(i < _tabCount)
|
|
|
|
{
|
|
|
|
_tabStop[i] = _tabStop[i + 1];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2020-05-17 04:37:41 -04:00
|
|
|
}
|
|
|
|
|
2021-11-06 12:49:23 -04:00
|
|
|
|
2023-01-31 07:39:06 -05:00
|
|
|
bool LineFormatter::existTab(uint8_t pos)
|
2020-05-17 04:37:41 -04:00
|
|
|
{
|
2023-01-31 07:39:06 -05:00
|
|
|
for (uint8_t i = 0; i < _tabCount; i++)
|
|
|
|
{
|
|
|
|
if (_tabStop[i] == pos) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LineFormatter::tab(uint8_t n)
|
|
|
|
{
|
|
|
|
while (n--) write('\t');
|
2020-05-17 04:37:41 -04:00
|
|
|
}
|
|
|
|
|
2021-11-06 12:49:23 -04:00
|
|
|
|
2020-05-17 04:37:41 -04:00
|
|
|
///////////////////////////////////////////
|
|
|
|
//
|
2023-01-31 07:39:06 -05:00
|
|
|
// MISCELLANEOUS
|
2020-05-17 04:37:41 -04:00
|
|
|
//
|
2023-01-31 07:39:06 -05:00
|
|
|
uint8_t LineFormatter::getPos()
|
|
|
|
{
|
|
|
|
return _pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LineFormatter::resetLineCount()
|
|
|
|
{
|
|
|
|
_lineCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t LineFormatter::getLineCount()
|
|
|
|
{
|
|
|
|
return _lineCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t LineFormatter::getTabCount()
|
|
|
|
{
|
|
|
|
return _tabCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t LineFormatter::getTabStop(uint8_t n)
|
|
|
|
{
|
|
|
|
return _tabStop[n];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LineFormatter::printRuler(uint8_t length)
|
2020-05-17 04:37:41 -04:00
|
|
|
{
|
2023-01-31 07:39:06 -05:00
|
|
|
// DEBUG
|
|
|
|
// for (uint8_t i = 0; i < _tabCount; i++) _stream->println(_tabStop[i]);
|
2020-05-17 04:37:41 -04:00
|
|
|
|
|
|
|
uint8_t t = 0;
|
2023-01-31 07:39:06 -05:00
|
|
|
for (uint8_t i = 1; i <= length; i++)
|
2020-05-17 04:37:41 -04:00
|
|
|
{
|
|
|
|
char c = '.';
|
2023-01-31 07:39:06 -05:00
|
|
|
if (i == _tabStop[t])
|
2020-05-17 04:37:41 -04:00
|
|
|
{
|
|
|
|
c = '#';
|
|
|
|
t++;
|
|
|
|
}
|
2023-01-31 07:39:06 -05:00
|
|
|
else if ((i % 10) == 0) c = '0';
|
|
|
|
else if ((i % 5) == 0) c = '5';
|
2020-05-17 04:37:41 -04:00
|
|
|
write(c);
|
|
|
|
}
|
|
|
|
write('\n');
|
|
|
|
}
|
|
|
|
|
2021-11-06 12:49:23 -04:00
|
|
|
|
2023-01-31 07:39:06 -05:00
|
|
|
// -- END OF FILE --
|
2021-11-06 12:49:23 -04:00
|
|
|
|