Skip to content

RobTillaart/PrintSize

Repository files navigation

Arduino CI Arduino-lint JSON check GitHub issues

License: MIT GitHub release PlatformIO Registry

PrintSize

Arduino library to determine the length of print statements.

Description

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 .

Related

For printing floats in scientific or engineering format

https://github.com/RobTillaart/printHelpers

Interface

#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".

Operational

Example PrintSize1.ino shows the right alignment of 10 random numbers.

Example PrintSize_total.ino shows (elementary) line fitting.

Applications

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.

Future

Must

Should

  • 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;

Could

Wont

Support

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,

About

Arduino library to determine the length of print statements

Topics

Resources

License

Stars

2 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors

Languages