Simple ANSI Library for C++ Posted in Programming on 08 of Nov, 2016

For some time I have had the idea that when I have written enough lowlevel mini-tutorials about how to do the most basic things in every language I have ever worked with on Polyglot Bits I would begin doing small projects.

Among these projects has been to write a basic (terminal based) text editor.

One of the things I know I will need is to be able to write ansi sequences to the terminal.

So I wrote a small header only library which is just convenience functions for emitting ansi sequences as std::strings in C++.

A very simple usage would be:

#include "ansicpp.hpp"
#include <string>
#include <iostream>

using namespace ansicpp;

int main(int ac, char* av[])
  std::cout << Ansi::clear() << Ansi::cursor_abs_pos(1,1) << "Testing" << Ansi::show_cursor() << std::flush;
  std::cout << Ansi::set_color(FG::black, BG::red);
  std::cout << Ansi::cursor_forward(10) << "moar testing" << std::flush;
  std::cout << Ansi::set_color(FG::default_color, BG::default_color);
  std::cout << Ansi::cursor_next_line();
  std::cout << "." << std::flush;
  return 0;

Which would result in the following (screenshot):

Get the ansicpp library on github