Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce style output #9

Open
fl4p opened this issue Oct 29, 2024 · 0 comments
Open

Reduce style output #9

fl4p opened this issue Oct 29, 2024 · 0 comments

Comments

@fl4p
Copy link

fl4p commented Oct 29, 2024

Hi,

I am using this asciichart on a ESP32 micro controller. Resources are limited and the current implementation generates a lot of unnessary styling set & unset sequences.

We can omit styling when two consecutive text elements share the same style. C++ has a stream state API, that we can use to store information about the current style.

This works for me
Here's my implementation: (Note that you need to un-set the style at each line-end: std::cout << ascii::Decoration::From(ascii::Decoration::RESET) << "\n";):

text.h:

  friend std::ostream &operator<<(std::ostream &os, const Text &val) {

      std::stringstream ss;
      ss << val.style_;
      std::hash<std::string> hasher;
      auto textStyleHash = (long)hasher(ss.str());

      auto &streamStyleHash = os.iword(get_style_stream_iword());

      if(textStyleHash == streamStyleHash)
        os << val.text_;
      else {
          os << val.style_ << val.text_; // << Decoration::From(Decoration::RESET);
          streamStyleHash = textStyleHash;
      }
    return os;
  }
inline int get_style_stream_iword() {
    static int i = std::ios_base::xalloc();
    return i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant