All Projects → p-ranav → Pprint

p-ranav / Pprint

Licence: mit
Pretty Printer for Modern C++

Programming Languages

cpp17
186 projects

Projects that are alternatives of or similar to Pprint

Unrealeditorpythonscripts
Some of my personal scripts i made to use for my own projects, but free of charge to be used for any project and any purpose as long as it is not violating the Unreal Engine EULA.
Stars: ✭ 282 (-63.98%)
Mutual labels:  mit-license
Bitcoinlib
Bitcoin Core RPC compatible, battle-tested .NET library and RPC wrapper for Bitcoin and Altcoins
Stars: ✭ 350 (-55.3%)
Mutual labels:  mit-license
Wave U Net
Implementation of the Wave-U-Net for audio source separation
Stars: ✭ 506 (-35.38%)
Mutual labels:  mit-license
Little Cms
A free, open source, CMM engine. It provides fast transforms between ICC profiles.
Stars: ✭ 291 (-62.84%)
Mutual labels:  mit-license
Md4c
C Markdown parser. Fast. SAX-like interface. Compliant to CommonMark specification.
Stars: ✭ 322 (-58.88%)
Mutual labels:  mit-license
Amplitudejs
AmplitudeJS: Open Source HTML5 Web Audio Library. Design your web audio player, the way you want. No dependencies required.
Stars: ✭ 3,781 (+382.89%)
Mutual labels:  mit-license
sqlite-okapi-bm25
📑 SQLite extension to add the Okapi BM25 ranking algorithm
Stars: ✭ 30 (-96.17%)
Mutual labels:  mit-license
Argparse
Argument Parser for Modern C++
Stars: ✭ 680 (-13.15%)
Mutual labels:  mit-license
Structopt
Parse command line arguments by defining a struct
Stars: ✭ 323 (-58.75%)
Mutual labels:  mit-license
Glass Isc Dhcp
Glass - ISC DHCP Server Interface
Stars: ✭ 486 (-37.93%)
Mutual labels:  mit-license
Unity Destruction
💥 An open-source script to destroy objects realistically in Unity3D.
Stars: ✭ 291 (-62.84%)
Mutual labels:  mit-license
Librelancer
A re-implementation of Freelancer
Stars: ✭ 303 (-61.3%)
Mutual labels:  mit-license
Picosha2
a header-file-only, SHA256 hash generator in C++
Stars: ✭ 408 (-47.89%)
Mutual labels:  mit-license
Doracms
DoraCMS是基于Nodejs+eggjs+mongodb编写的一套内容管理系统,结构简单,较目前一些开源的cms,doracms易于拓展,特别适合前端开发工程师做二次开发。
Stars: ✭ 3,180 (+306.13%)
Mutual labels:  mit-license
Pathcopycopy
Copy file paths from Windows explorer's contextual menu
Stars: ✭ 511 (-34.74%)
Mutual labels:  mit-license
Covid 19 Statistics Dashboard Angular 9
🦠Corona Virus / Covid 19 Tracker Dashboard With Awesome UI + PWA + NodeJS Scraper
Stars: ✭ 282 (-63.98%)
Mutual labels:  mit-license
Magicavoxel Shaders
Shaders for MagicaVoxel to simplify common and repetitive tasks.
Stars: ✭ 370 (-52.75%)
Mutual labels:  mit-license
Agemojikeyboard
Emoji Keyboard for iOS
Stars: ✭ 686 (-12.39%)
Mutual labels:  mit-license
Godot Make Pro 2d Games
A-RPG demo made with Godot, MIT-licensed, from our Godot course
Stars: ✭ 674 (-13.92%)
Mutual labels:  mit-license
Machineid
Get the unique machine id of any host (without admin privileges)
Stars: ✭ 422 (-46.1%)
Mutual labels:  mit-license

pprint

Highlights

  • Single header file
  • Requires C++17
  • MIT License

Quick Start

Simply include pprint.hpp and you're good to go.

#include <pprint.hpp>

To start printing, create a PrettyPrinter

pprint::PrettyPrinter printer;

You can construct a PrettyPrinter with any stream that inherits from std::ostream , e.g, std::stringstream

std::stringstream stream;
pprint::PrettyPrinter printer(stream);

Fundamental Types

printer.print(5);
printer.print(3.14f);
printer.print(2.718);
printer.print(true);
printer.print('x');
printer.print("Hello, 世界");
printer.print(nullptr);
5
3.14f
2.718
true
x
Hello, 世界
nullptr

Strings

Maybe you want your strings to be quoted? Simply set printer.quotes(true)

printer.quotes(true);
printer.print("A", "B", "C");
"A" "B" "C"

Complex Numbers

using namespace std::complex_literals;
std::complex<double> foo = 1. + 2.5i;
std::complex<double> bar = 9. + 4i;
printer.print(foo, "*", bar, "=", (foo * bar));   // parameter packing
(1 + 2.5i) * (9 + 4i) = (-1 + 26.5i)

Enumeration Types

enum Color { RED = 2, BLUE = 4, GREEN = 8 };
Color color = BLUE;
printer.print(color);
4

If you compile with

  • Clang/LLVM >= 5
  • Visual C++ >= 15.3 / Visual Studio >= 2017
  • Xcode >= 10.2
  • GCC >= 9

then pprint will print the name of the enum for you (thanks to magic_enum)

enum Level { LOW, MEDIUM, HIGH };
Level current_level = MEDIUM;
std::cout << "Current level: ";
printer.print(current_level);
Current level: MEDIUM

STL Sequence Containers

pprint supports a variety of STL sequence containers including std::vector, std::list, std::deque, and std::array.

Here's an example pretty print of a simple 3x3 matrix:

typedef std::array<std::array<int, 3>, 3> Mat3x3;
Mat3x3 matrix;
matrix[0] = {1, 2, 3};
matrix[1] = {4, 5, 6};
matrix[2] = {7, 8, 9};
printer.print("Matrix =", matrix);
Matrix = [
  [1, 2, 3], 
  [4, 5, 6], 
  [7, 8, 9]
]

Compact Printing

pprint also supports compact printing of containers. Simply call printer.compact(true) to enable this:

std::vector<std::map<std::string, int>> foo {{{"a", 1}, {"b", 2}}, {{"c", 3}, {"d", 4}}};
printer.compact(true);
printer.print("Foo =", foo);
Foo = [{a : 1, b : 2}, {c : 3, d : 4}]

STL Associative Containers

Support for associative containers includes pretty printing of std::map, std::multimap, std::unordered_map, std::unordered_multimap, std::set, std::multiset, std::unordered_set and , std::unordered_multiset

printer.print(std::map<std::string, std::set<int>>{ 
    {"foo", {1, 2, 3, 3, 2, 1}}, {"bar", {7, 6, 5, 4}}});
{
  "bar" : {4, 5, 6, 7}, 
  "foo" : {1, 2, 3}
}

STL Container Adaptors

pprint can print container adaptors including std::queue, std::priority_queue and std::stack. Here's an example print of a priority queue:

std::priority_queue<int> queue;
for(int n : {1,8,5,6,3,4,0,9,7,2}) queue.push(n);
printer.print(queue);
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Fixed-size Heterogeneous Tuples

auto get_student = [](int id) {
  if (id == 0) return std::make_tuple(3.8, 'A', "Lisa Simpson");
  if (id == 1) return std::make_tuple(2.9, 'C', "Milhouse Van Houten");
  if (id == 2) return std::make_tuple(1.7, 'D', "Ralph Wiggum");
  throw std::invalid_argument("id");
};
printer.print({ get_student(0), get_student(1), get_student(2) });
{(1.7, 'D', "Ralph Wiggum"), (2.9, 'C', "Milhouse Van Houten"), (3.8, 'A', "Lisa Simpson")}

Type-safe Unions

// Construct a vector of values
std::vector<std::variant<bool, int, int *, float, std::string, std::vector<int>,		      
       std::map<std::string, std::map<std::string, int>>, 
       std::pair<double, double>>> var;
var.push_back(5);
var.push_back(nullptr);
var.push_back(3.14f);
var.push_back(std::string{"Hello World"});
var.push_back(std::vector<int>{1, 2, 3, 4});
var.push_back(std::map<std::string, std::map<std::string, int>>{{"a",{{"b",1}}}, {"c",{{"d",2}, {"e",3}}}});
var.push_back(true);
var.push_back(std::pair<double, double>{1.1, 2.2});

// Print the vector
pprint::PrettyPrinter printer;
printer.indent(2);
printer.quotes(true);
printer.print(var);
[
  5, 
  nullptr,
  3.14f, 
  "Hello World", 
  [1, 2, 3, 4], 
  {"a" : {"b" : 1}, "c" : {"d" : 2, "e" : 3}}, 
  true, 
  (1.1, 2.2)
]

Optional Values

std::optional<int> opt = 5;
std::optional<int> opt2;

printer.print(opt);
printer.print(opt2);
5
nullopt

Class Objects

pprint print class objects with or without an overloaded << operator

class Foo {};
Foo foo;
printer.print(foo);
<Object main::Foo>

If an << operator is available, pprint will use it to print your object:

class Date {
  unsigned int month, day, year;
public:
  Date(unsigned int m, unsigned int d, unsigned int y) : month(m), day(d), year(y) {}
  friend std::ostream& operator<<(std::ostream& os, const Date& dt);
};

    
std::ostream& operator<<(std::ostream& os, const Date& dt) {
  os << dt.month << '/' << dt.day << '/' << dt.year;
  return os;
}
Date date(04, 07, 2019);
printer.print("Today's date is", date);
Today's date is 4/7/2019

User-defined types

Here's an example to print user-defined types. Let's say you want to print Mesh objects

struct Vector3 {
  float x, y, z;
};

struct Mesh {
  std::vector<Vector3> vertices;
};

First, overload the << operator for these structs:

std::ostream& operator<<(std::ostream& os, const Vector3& v) {
  pprint::PrettyPrinter printer(os);
  printer.print_inline(std::make_tuple(v.x, v.y, v.z));
  return os;
}

std::ostream& operator<<(std::ostream& os, const Mesh& mesh) {
  pprint::PrettyPrinter printer(os);
  printer.print("Mesh {");
  printer.indent(2);
  printer.print_inline("vertices:", mesh.vertices);
  printer.print("}");
  return os;
}

then simply call printer.print(Mesh)

Mesh quads = {{
  {0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 0, 0}, {1, 1, 0}, {0, 1, 0},
  {0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 0, 1}, {1, 1, 1}, {0, 1, 1},
  }};

pprint::PrettyPrinter printer;
printer.print(quads);
Mesh {
  vertices: [
      (0, 0, 0), 
      (1, 0, 0), 
      (1, 1, 0), 
      (0, 0, 0), 
      (1, 1, 0), 
      (0, 1, 0), 
      (0, 0, 1), 
      (1, 0, 1), 
      (1, 1, 1), 
      (0, 0, 1), 
      (1, 1, 1), 
      (0, 1, 1)
  ]
}

License

The project is available under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].