Skip to content

Instantly share code, notes, and snippets.

@VirtuosoChris
Created August 28, 2020 05:51
Show Gist options
  • Save VirtuosoChris/fb769b589d0f15ca6cfafa0f34b564a4 to your computer and use it in GitHub Desktop.
Save VirtuosoChris/fb769b589d0f15ca6cfafa0f34b564a4 to your computer and use it in GitHub Desktop.
aksjdlakjsd
static int OriginalStrBuf()
{
static int idx = std::ios_base::xalloc();
return idx;
}
std::unordered_map<std::streambuf*, std::shared_ptr<std::streambuf>> replacementStrBufs;
// swap it back
struct ConsoleBuf : public std::streambuf
{
std::ostream* fwd = nullptr;
ConsoleBuf(std::ostream& os) :
fwd(&os)
{
}
int overflow(int c)
{
//assert(fwd);
if (c != EOF && fwd != nullptr)
{
std::ostream& os = (*fwd);
std::streambuf* originalStrb = (std::streambuf*)os.pword(OriginalStrBuf());
assert(originalStrb);
originalStrb->sputc(c);
switch (c)
{
case '\n':
{
const char* rset = "\033[0m";
originalStrb->sputn(rset, strlen(rset));
if (os.pword(OriginalStrBuf()))
{
os.rdbuf(originalStrb);
fwd = nullptr;
//replacementStrBufs.erase(originalStrb);
return c;
}
break;
}
default:
{
break;
}
}
}
return c;
}
virtual ~ConsoleBuf()
{
}
};
std::ostream& error (std::ostream& os)
{
if (!os.pword(OriginalStrBuf()))
{
os.pword(OriginalStrBuf()) = os.rdbuf();
}
std::shared_ptr<std::streambuf> sp(new ConsoleBuf(os));
replacementStrBufs[os.rdbuf()] = sp;
os.rdbuf(sp.get());
return os << "\033[1;35m";
}
int main(void)
{
std::cout << " HELLO \033[1;31mbold red text\033[0m\n" << std::endl;
std::cout << " HELOOOOOO " << error << " ello ello ello \n ello ello ello ello " << std::endl;
std::cout << " HELLO \033[1;31mbold red text\033[0m\n" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment