Skip to content

acd1034/cpp-rich

Repository files navigation

cpp-rich: Text formatting with decoration

Linux build status Github issues Github forks Github stars Github top language Github license

cpp-rich は C++でリッチテキストを出力するためのライブラリです。色付き出力、行数付き出力、枠線、表 (現時点では一列のみ) に対応しています。

Quick example

#define FMT_HEADER_ONLY
#include <rich/rich.hpp>

int divide(int num, int div) {
  if (div == 0)
    throw rich::runtime_error("Division by zero");
  return num / div;
}

int main() {
  try {
    divide(1, 0);
  } catch (rich::exception& e) {
    // location
    auto str = fmt::format("{}:{}:{} in {}",
                           e.where().file_name(), e.where().line(),
                           e.where().column(), e.where().function_name());
    rich::lines<char> location{{std::string_view(str), {}}};

    // numbered_code
    auto contents = rich::get_file_contents(e.where().file_name());
    const std::size_t extra = 3;
    auto partial = rich::extract_partial_contents(std::string_view(contents),
                                                  e.where().line(), extra);
    rich::enumerate numbered_code(rich::syntax_highlight(partial));
    numbered_code.start_line = e.where().line() - extra;
    numbered_code.end_line = e.where().line() + extra;
    numbered_code.highlight_line = e.where().line();
    numbered_code.highlight_spec.width = 2;

    // message
    rich::lines<char> message{{e.what(), {}}};

    // table
    rich::table table(location, numbered_code, message);
    table.title = std::string_view("Traceback (most recent call)");

    fmt::print("{}\n", table);
  }
}

cpp-rich-light

Library Dependencies

This library uses fmt for formatting texts.

About

Text formatting with decoration

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published