IndentedPrint.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #pragma once
  6. #include "../Arduino/Print.h"
  7. namespace ArduinoJson
  8. {
  9. namespace Generator
  10. {
  11. // Decorator on top of Print to allow indented output.
  12. // This class is used by JsonPrintable::prettyPrintTo() but can also be used
  13. // for your own purpose, like logging.
  14. class IndentedPrint : public Print
  15. {
  16. public:
  17. IndentedPrint(Print& p)
  18. : sink(p)
  19. {
  20. level = 0;
  21. tabSize = 2;
  22. isNewLine = true;
  23. }
  24. virtual size_t write(uint8_t);
  25. // Adds one level of indentation
  26. void indent();
  27. // Removes one level of indentation
  28. void unindent();
  29. // Set the number of space printed for each level of indentation
  30. void setTabSize(uint8_t n);
  31. private:
  32. Print& sink;
  33. uint8_t level : 4;
  34. uint8_t tabSize : 3;
  35. bool isNewLine : 1;
  36. size_t writeTabs();
  37. static const int MAX_LEVEL = 15; // because it's only 4 bits
  38. static const int MAX_TAB_SIZE = 7; // because it's only 3 bits
  39. };
  40. }
  41. }