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

Row cells not lining up #5

Open
mgravitt opened this issue Jun 25, 2018 · 3 comments
Open

Row cells not lining up #5

mgravitt opened this issue Jun 25, 2018 · 3 comments

Comments

@mgravitt
Copy link

This is a great library, thank you. I am having a problem where the rows do not add up to the column header. Here is my code:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <chrono>
#include <cstdio>
#include <thread>
#include "nlohmann/json.hpp"
#include "bprinter/table_printer.h"

#if defined(USE_BOOST_KARMA)
#include <boost/spirit/include/karma.hpp>
namespace karma = boost::spirit::karma;
#endif

using bprinter::TablePrinter;

// #include <BitcarbonConstants.hpp>

using namespace std;
using json = nlohmann::json;

std::string exec(const char* cmd) {
  std::array<char, 128> buffer;
  std::string result;
  std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
  if (!pipe) throw std::runtime_error("popen() failed!");
  while (!feof(pipe.get())) {
      if (fgets(buffer.data(), 128, pipe.get()) != nullptr)
          result += buffer.data();
  }
  return result;
}

int main () {

  const std::string CLEOS_OPTS = "";//"-u http://dev.cryptolions.io:38888";
  const std::string ACCT_NAME = "eva";

  double duration;
  auto start = std::chrono::system_clock::now();
  srand (time(NULL));

  string command = "cleos " + CLEOS_OPTS + " get table -l 1000 " + ACCT_NAME
                            + " " + ACCT_NAME + " rides";
  cout << command << endl;

  string result = exec (command.c_str());
  json j = json::parse(result);

  json row = j["rows"];
  TablePrinter tp(&std::cout);
  tp.AddColumn("RideId", 6);
  tp.AddColumn("Driver", 10);
  tp.AddColumn("Rider", 10);
  tp.AddColumn("Dist Chg", 10);
  tp.AddColumn("Time Chg", 10);
  tp.AddColumn("Tip", 10);
  tp.AddColumn("Comm", 10);
  tp.AddColumn("World", 10);
  tp.AddColumn("Total", 10);

  tp.PrintHeader();

  int counter = 0;
  for (auto el : row) {
    // string rideId = ;
    // string driver = el["driver"];
    // string rider = el["rider"];
    
    tp   << el["rideId"]        << el["driver"]  << el["rider"] << el["dist_charge"]
         << el["time_charge"]   << el["tip_charge"]    
         << el["comm_charge"]   << el["world_charge"]  
         << el["total_charge"];
    counter++;
  }
  tp.PrintFooter();

  auto end = std::chrono::system_clock::now();
  std::chrono::duration<double> elapsed_seconds = end-start;

  //duration = ( clock() - start ) / (double) CLOCKS_PER_SEC;
  cout << "Receipts: " << counter;
  cout << "::: Elapsed time: " << elapsed_seconds.count() << "s" << endl;
  cout << "; RPS: " << counter / elapsed_seconds.count() << "\n";

  return 0;

}

And here is the output. The header is perfect but the rows below do not match up. Any ideas?
thank you

+----------------------------------------------------------------------------------------------+
|RideId|    Driver|     Rider|  Dist Chg|  Time Chg|       Tip|      Comm|     World|     Total|
+----------------------------------------------------------------------------------------------+
|0|"driver1"|"rider2"|25500|0|28055|1275|1275|5|
|1|"driver3"|"rider1"|21964|2650|34128|4507|4507|500|
|2|"driver1"|"rider1"|25500|0|28055|1275|1275|5|
|3|"driver2"|"rider1"|58392|0|18356|12750|12750|0|
|4|"driver1"|"rider2"|25500|0|28055|1275|1275|5|
|5|"driver2"|"rider1"|25500|53|28112|1277|1277|5|
|6|"driver2"|"rider1"|25500|53|28112|1277|1277|5|
|7|"driver2"|"rider1"|25500|213|28288|1285|1285|5|
|8|"driver2"|"rider1"|25500|160|28231|1283|1283|5|
|9|"driver2"|"rider1"|25500|106|28171|1280|1280|5|
+----------------------------------------------------------------------------------------------+
Receipts: 10::: Elapsed time: 0.0357193s
; RPS: 279.961

@dattanchu
Copy link
Owner

I haven't used this library in a while.

I believe there is a fork by ChristophJud that might work as well. Your usage does look correct. It's possible that we are messing up the alignment due to the recent merge because it was only tested on ostream and not Boost Karma. Have you tried not using Boost Karma?

@mgravitt
Copy link
Author

mgravitt commented Jun 26, 2018 via email

@mgravitt
Copy link
Author

I tried the ChristophJud version and tried not using Boost Karma, but I got the same results.

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

2 participants