Skip to content

Row cells not lining up #5

Description

@mgravitt

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions