0.1.3 LineFormatter

This commit is contained in:
rob tillaart 2021-11-06 17:49:23 +01:00
parent fe0438cae1
commit 09c126cec2
13 changed files with 180 additions and 62 deletions

View File

@ -2,9 +2,13 @@ compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
- leonardo
- due
- zero
# - due
# - zero
# - leonardo
- m4
- esp32
# - esp8266
# - mega2560
libraries:
- "Ethernet"
- "SD"
- "SD"

View File

@ -4,10 +4,14 @@ name: Arduino CI
on: [push, pull_request]
jobs:
arduino_ci:
runTest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: Arduino-CI/action@master
# Arduino-CI/action@v0.1.1
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
- run: |
gem install arduino_ci
arduino_ci.rb

View File

@ -1,8 +1,8 @@
//
// FILE: LineFormatter.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// PURPOSE: Simpe positioning wrapper class for Serial
// VERSION: 0.1.3
// PURPOSE: Simple positioning wrapper class for Serial
// DATE: 2020-05-14
// URL: https://github.com/RobTillaart/LineFormatter
//
@ -10,21 +10,30 @@
// 0.1.0 2020-05-14 initial version
// 0.1.1 2020-06-19 fix library.json
// 0.1.2 2020-12-30 Arduino-ci + unit tests
// 0.1.3 2021-11-06 update Arduino-CI, badges
// update readme.md, add reset();
//
#include "LineFormatter.h"
LineFormatter::LineFormatter(Print* stream)
{
_stream = stream;
// reset
reset();
};
void LineFormatter::reset()
{
_pos = 0;
_maxPos = 0;
_lineCount = 0;
_anl = 0;
_autoNewLine = 0;
_tabCount = 0;
};
}
///////////////////////////////////////////
@ -75,6 +84,7 @@ size_t LineFormatter::write(uint8_t c)
return 1;
}
///////////////////////////////////////////
//
// REPEAT
@ -85,12 +95,14 @@ void LineFormatter::repeat(uint8_t n, char c, uint8_t newLine)
while (newLine--) write('\n');
}
void LineFormatter::repeat(uint8_t n, const char* str, uint8_t newLine)
{
while (n--) print(str);
while (newLine--) write('\n');
}
///////////////////////////////////////////
//
// AUTONEWLINE
@ -101,6 +113,7 @@ void LineFormatter::setAutoNewLine(uint8_t n)
_anl = 0;
};
///////////////////////////////////////////
//
// TAB
@ -114,6 +127,7 @@ void LineFormatter::clearTabs()
}
};
bool LineFormatter::addTab(uint8_t n)
{
if (_tabCount >= MAX_TAB_STOPS) return false;
@ -122,6 +136,7 @@ bool LineFormatter::addTab(uint8_t n)
return true;
}
bool LineFormatter::addRelTab(uint8_t n)
{
if (_tabCount >= MAX_TAB_STOPS) return false;
@ -131,6 +146,7 @@ bool LineFormatter::addRelTab(uint8_t n)
return true;
}
///////////////////////////////////////////
//
// DEBUGGING
@ -156,4 +172,6 @@ void LineFormatter::printRuler(uint8_t n)
write('\n');
}
// -- END OF FILE --

View File

@ -2,8 +2,8 @@
//
// FILE: LineFormatter.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// PURPOSE: Simpe positioning wrapper class for Serial
// VERSION: 0.1.3
// PURPOSE: Simple positioning wrapper class for Serial / Stream
// DATE: 2020-05-14
// URL: https://github.com/RobTillaart/LineFormatter
//
@ -15,29 +15,39 @@
#define MAX_TAB_STOPS 12
#endif
#define LINEFORMATTER_LIB_VERSION (F("0.1.3"))
class LineFormatter: public Print
{
public:
LineFormatter(Print* stream = &Serial);
void reset();
size_t write(uint8_t c);
// set the maximum line length - bold cut off
void setMaxLength(uint8_t maxPos) { _maxPos = maxPos; };
uint8_t getMaxLength() { return _maxPos; };
// if position is smaller than n, move to the right
uint8_t gotoPos(uint8_t n) { while (_pos < n) write(' '); return _pos; };
// repeat a char or a "string" n times
// followed by 0 or more newlines.
void repeat(uint8_t n, char c, uint8_t newLine = 0);
void repeat(uint8_t n, const char* str, uint8_t newLine = 0);
// n = 0 switches autoNewLine off
void setAutoNewLine(uint8_t n);
uint8_t getAutoNewLine() { return _autoNewLine; };
// Add a tab at (absolute/relative) position
// returns true on success
bool addTab(uint8_t n);
@ -55,8 +65,10 @@ public:
uint8_t getTabStop(uint8_t n) { return _tabStop[n]; };
void printRuler(uint8_t n);
private:
Print * _stream;
uint8_t _pos = 0;
uint8_t _maxPos = 0;
uint16_t _lineCount = 0;
@ -67,4 +79,6 @@ private:
uint8_t _tabCount = 0;
};
// -- END OF FILE --

View File

@ -1,12 +1,16 @@
[![Arduino CI](https://github.com/RobTillaart/LineFormatter/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/LineFormatter/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/LineFormatter/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/LineFormatter/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/LineFormatter/actions/workflows/jsoncheck.yml)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/LineFormatter/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/LineFormatter.svg?maxAge=3600)](https://github.com/RobTillaart/LineFormatter/releases)
# LineFormatter
Arduino library to enhance the layout of tabular data on serial output,
# Description
LineFormatter is a wrapper class for Serial (and other streams) to enhance
@ -40,15 +44,60 @@ of positions to the right e.g. when copying a file from disk to Serial.
Setting the value to 0 results in no maximum line length.
Note: the maximum value is 255.
## Interface
- **LineFormatter(Print\* stream = &Serial)** constructor
- **reset()** reset internal variables to start over again.
#### Printing
- **size_t write(uint8_t c)** implements print.
- **uint8_t gotoPos(uint8_t n)** if position is smaller than n, move to the right
- **void repeat(uint8_t n, char c, uint8_t newLine = 0)** repeat a char n times
- **void repeat(uint8_t n, const char\* str, uint8_t newLine = 0)** repeat a "string" n times
#### Tab configuration
- **bool addTab(uint8_t n)** Add a tab at an absolute position. returns true on success
- **bool addRelTab(uint8_t n)** Add a tab at a relative position. returns true on success
- **void clearTabs()** remove all the tabs.
- **void tab(uint8_t n = 1)** print zero or more tabs, similar as e.g. "\t\t\t"
#### Line configuration
- **void setMaxLength(uint8_t maxPos)** set the maximum line length - bold cut off
- **uint8_t getMaxLength()** return max line length set.
- **void setAutoNewLine(uint8_t n)** n = 0 switches autoNewLine off
- **uint8_t getAutoNewLine()** returns number of newlines.
### Debugging
For debugging purposes there are the following functions:
- **getPos()** - returns current position
- **resetLineCount()** - sets internal lineCounter to zero
- **getLineCount()** - returns current line number (since last reset)
- **getTabCount()** - get the number of tab positions added.
- **printRuler()** - prints a dotted line with 5 and 10 markers, and # for tab positions.
- **uint8_t getPos()** returns current position
- **void resetLineCount()** sets internal lineCounter to zero
- **uint16_t getLineCount()** returns current line number (since last reset)
- **uint8_t getTabCount()** get the number of tab positions added.
- **uint8_t getTabStop(uint8_t n)**
- **void printRuler()** prints a dotted line with 5 and 10 markers, and # for tab positions.
# Operational
See examples
## Future
- improve documentation
- add examples
- add reset();
- check if print interface is completely covered.
- set defaults for functions
- add **rmTab(position)**
-

View File

@ -0,0 +1,14 @@
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
# - due
# - zero
# - leonardo
- m4
# - esp32 # has other server code
# - esp8266
# - mega2560
libraries:
- "Ethernet"
- "SD"

View File

@ -7,6 +7,7 @@
// URL: https://github.com/RobTillaart/LineFormatter
//
#include <LineFormatter.h>
#include <SPI.h>
@ -22,6 +23,7 @@ EthernetServer server(80); // change to your config
char httpRequest[40];
uint8_t reqCnt;
////////////////////////////////////////////////////////////////////
//
// HTTP HELPER CODE
@ -172,6 +174,7 @@ void loop()
}
}
void test_table_1(LineFormatter L)
{
L.println();
@ -231,6 +234,7 @@ void test_table_1(LineFormatter L)
L.repeat(3, '\n');
}
void test_table_2(LineFormatter L)
{
L.println();
@ -291,4 +295,6 @@ void test_table_2(LineFormatter L)
L.repeat(3, '\n');
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -7,6 +7,7 @@
// URL: https://github.com/RobTillaart/LineFormatter
//
#include <SPI.h>
#include <SD.h>
// SPI PINS
@ -18,6 +19,7 @@
#include <LineFormatter.h>
void setup()
{
Serial.begin(115200);
@ -54,7 +56,6 @@ void loop()
}
void test_table(LineFormatter L)
{
L.println();
@ -117,4 +118,6 @@ void test_table(LineFormatter L)
L.repeat(3, '\n');
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -9,10 +9,12 @@
// 0.1.0 2020-05-14 initial version
//
#include "LineFormatter.h"
LineFormatter L;
void setup()
{
Serial.begin(115200);
@ -34,10 +36,12 @@ void setup()
L.println("Done...");
}
void loop()
{
}
void test_ruler()
{
L.println();
@ -62,6 +66,7 @@ void test_ruler()
L.repeat(3, '\n');
}
void test_table_1()
{
L.println();
@ -119,6 +124,7 @@ void test_table_1()
L.repeat(3, '\n');
}
void test_table_2()
{
L.println();
@ -178,4 +184,6 @@ void test_table_2()
L.repeat(3, '\n');
}
// -- END OF FILE --

View File

@ -1,25 +1,31 @@
# Syntax Coloring Map For LineFormatter
# Syntax Colouring Map For LineFormatter
# Datatypes (KEYWORD1)
# Data types (KEYWORD1)
LineFormatter KEYWORD1
# Methods and Functions (KEYWORD2)
reset KEYWORD2
write KEYWORD2
setMaxLength KEYWORD2
gotoPos KEYWORD2
getPos KEYWORD2
repeat KEYWORD2
resetLineCount KEYWORD2
getLineCount KEYWORD2
setAutoNewLine KEYWORD2
addTab KEYWORD2
addRelTab KEYWORD2
clearTabs KEYWORD2
getTabCount KEYWORD2
tab KEYWORD2
printRuler KEYWORD2
# Constants (LITERAL1)
MAX_TAB_STOPS LITERAL1
LINEFORMATTER_LIB_VERSION LITERAL1

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/LineFormatter.git"
},
"version": "0.1.2",
"version": "0.1.3",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*"

View File

@ -1,5 +1,5 @@
name=LineFormatter
version=0.1.2
version=0.1.3
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Wrapper class for Serial to enhance layout of tabular data.

View File

@ -29,6 +29,7 @@
// assertNAN(arg); // isnan(a)
// assertNotNAN(arg); // !isnan(a)
#include <ArduinoUnitTests.h>
@ -44,69 +45,60 @@ unittest_teardown()
{
}
/*
unittest(test_new_operator)
{
assertEqualINF(exp(800));
assertEqualINF(0.0/0.0);
assertEqualINF(42);
assertEqualNAN(INFINITY - INFINITY);
assertEqualNAN(0.0/0.0);
assertEqualNAN(42);
}
*/
unittest(test_constructor)
{
LineFormatter L;
fprintf(stderr, "LINEFORMATTER_LIB_VERSION: %s\n", (char*) LINEFORMATTER_LIB_VERSION);
assertEqual(0, L.getMaxLength());
L.setMaxLength(80);
assertEqual(80, L.getMaxLength());
LineFormatter Line;
assertEqual(0, L.getAutoNewLine());
L.setAutoNewLine(5);
assertEqual(5, L.getAutoNewLine());
assertEqual(0, Line.getMaxLength());
Line.setMaxLength(80);
assertEqual(80, Line.getMaxLength());
assertEqual(0, Line.getAutoNewLine());
Line.setAutoNewLine(5);
assertEqual(5, Line.getAutoNewLine());
}
unittest(test_position)
{
LineFormatter L;
LineFormatter Line;
assertEqual(0, (int)L.getPos());
assertEqual(20, (int)L.gotoPos(20));
assertEqual(20, (int)L.gotoPos(15));
assertEqual(0, (int)Line.getPos());
assertEqual(20, (int)Line.gotoPos(20));
assertEqual(20, (int)Line.gotoPos(15));
L.repeat(10, '*');
assertEqual(30, (int)L.getPos());
L.repeat(10, "--");
assertEqual(50, (int)L.getPos());
Line.repeat(10, '*');
assertEqual(30, (int)Line.getPos());
Line.repeat(10, "--");
assertEqual(50, (int)Line.getPos());
}
unittest(test_tab)
{
LineFormatter L;
LineFormatter Line;
fprintf(stderr, "tab setting\n");
for (int i = 8; i <= 80; i += 8)
{
L.addTab(i);
Line.addTab(i);
}
assertEqual(10, L.getTabCount());
for (int i = 0; i < L.getTabCount(); i++)
assertEqual(10, Line.getTabCount());
for (int i = 0; i < Line.getTabCount(); i++)
{
fprintf(stderr, "%d\t", 8 + i*8);
assertEqual(8 + i*8, L.getTabStop(i));
assertEqual(8 + i*8, Line.getTabStop(i));
}
fprintf(stderr, "tab test - !! cur pos is one before tab pos\n");
for (int i = 0; i < L.getTabCount(); i++)
fprintf(stderr, "tab test - !! cur position is one before tab position\n");
for (int i = 0; i < Line.getTabCount(); i++)
{
fprintf(stderr, "%d\t", 8 + i*8); // tabpos
L.write('\t');
assertEqual(8 + i*8, (int)L.getPos() + 1 ); // current pos is just before
fprintf(stderr, "%d\t", 8 + i*8); // tab positions
Line.write('\t');
assertEqual(8 + i*8, (int)Line.getPos() + 1 ); // current position is just before tab
}
}