Arduino library to determine the length of print statements.
PrintSize is a minimal library to determine the length of a variable when printed. This includes printing of floats, integers in decimal or HEX notation.
PrintSize can be used e.g. to calculate the spaces needed for right aligning or centring numbers or text for a more readable layout. Right alignment would also enforce alignment of the decimal point of floats and doubles if all printed with the same number of decimals.
Another application is to see if a word would fit on a line (e.g. 80 characters), or need to be printed on the next line / page.
Works for print(), println() and if supported printf() e.g. ESP32.
Since 0.2.0 the library has a total counter to add up the characters "printed" since the last reset() call. See examples. This is useful e.g. to count the position on a line .
- https://github.com/RobTillaart/lineFormatter for tabular formatting.
- https://github.com/RobTillaart/PrintCharArray captures data in a char array buffer.
- https://github.com/RobTillaart/PrintSize counts length of a number of print commands.
- https://github.com/RobTillaart/PrintString captures data in a String.
For printing floats in scientific or engineering format
#include "PrintSize.h"- PrintSize() Constructor
- size_t write(uint8_t c) core function of the Print interface.
- size_t write(uint8_t * str, uint8_t length)
- void reset() reset the total counter.
- uint32_t total() returns the total bytes "printed".
Example PrintSize1.ino shows the right alignment of 10 random numbers.
Example PrintSize_total.ino shows (elementary) line fitting.
Can be used to calculate the needed space characters (bytes),
- to properly do a right alignment e.g. for numbers or variable text.
- do left alignment and overwrite previous output with just enough spaces.
- centring of numbers.
- allocate dynamic memory (for a string to be)
- check if output will fit into a line / display.
- check if a string fits in EEPROM or any other storage medium.
- check if a string fits in a communication buffer.
- add examples
- add a function to handle tab char correctly e.g.
could add more than one char. Interferes with the write(str, length).
- setTabSize(2, 4, 6, 8...);
- getTabSize();
- uint8_t _tabSize = 4;
If you appreciate my libraries, you can support the development and maintenance. Improve the quality of the libraries by providing issues and Pull Requests, or donate through PayPal or GitHub sponsors.
Thank you,