Skip to main content
explained more about use of enum
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284

Consider the user (part 1)

That could easily be done by turning those styles into an enum and creating a std::array of the actual style strings. For example, we might define the enum like this:

enum class Style{BasicStyle, LineStyle, DoubleLineStyle, InvisibleStyle};

Create the styles like this:

const std::array<ConsoleTable::TableStyle, 4> ConsoleTable::st = {{
    {"-", "|", {"+", "+", "+"}, {"+", "+", "+"}, {"+", "+", "+"}},
    {"━", "┃", {"┏", "┳", "┓"}, {"┣", "╋", "┫"}, {"┗", "┻", "┛"}},
    {"═", "║", {"╔", "╦", "╗"}, {"╠", "╬", "╣"}, {"╚", "╩", "╝"}},
    {" ", " ", {" ", " ", " "}, {" ", " ", " "}, {" ", " ", " "}},
}};

And now we can use them like this:

void ConsoleTable::setStyle(Style n) {
    style = static_cast<int>(n);
}

Naturally, the corresponding changes would need to be made wherever style was used.

Consider the user (part 2)

Consider the user

That could easily be done by turning those styles into an enum and creating a std::array of the actual style strings.

Consider the user (part 1)

That could easily be done by turning those styles into an enum and creating a std::array of the actual style strings. For example, we might define the enum like this:

enum class Style{BasicStyle, LineStyle, DoubleLineStyle, InvisibleStyle};

Create the styles like this:

const std::array<ConsoleTable::TableStyle, 4> ConsoleTable::st = {{
    {"-", "|", {"+", "+", "+"}, {"+", "+", "+"}, {"+", "+", "+"}},
    {"━", "┃", {"┏", "┳", "┓"}, {"┣", "╋", "┫"}, {"┗", "┻", "┛"}},
    {"═", "║", {"╔", "╦", "╗"}, {"╠", "╬", "╣"}, {"╚", "╩", "╝"}},
    {" ", " ", {" ", " ", " "}, {" ", " ", " "}, {" ", " ", " "}},
}};

And now we can use them like this:

void ConsoleTable::setStyle(Style n) {
    style = static_cast<int>(n);
}

Naturally, the corresponding changes would need to be made wherever style was used.

Consider the user (part 2)

added code for an update function
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284

One way to do that:

bool ConsoleTable::update(const std::string &item, 
                          const std::string &col, 
                          const std::string &data) 
{
    auto row{std::find_if(rows.begin(), rows.end(), 
             [&item](auto &r){return r.front() == item;}) };
    auto hdrcol{std::find(headers.begin(), headers.end(), col)};
    if ((row != rows.end()) && (hdrcol != headers.end())) {
        (*row)[hdrcol - headers.begin()] = data;
        return true;
    }
    return false;
}

One way to do that:

bool ConsoleTable::update(const std::string &item, 
                          const std::string &col, 
                          const std::string &data) 
{
    auto row{std::find_if(rows.begin(), rows.end(), 
             [&item](auto &r){return r.front() == item;}) };
    auto hdrcol{std::find(headers.begin(), headers.end(), col)};
    if ((row != rows.end()) && (hdrcol != headers.end())) {
        (*row)[hdrcol - headers.begin()] = data;
        return true;
    }
    return false;
}
removed incorrect grammar nit
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
table.update("Australia", "Capitol""Capital", "Brisbane");

(Minor nit: it's spelled "capitol" in this context.)

table.update("Australia", "Capitol", "Brisbane");

(Minor nit: it's spelled "capitol" in this context.)

table.update("Australia", "Capital", "Brisbane");
Source Link
Edward
  • 67.2k
  • 4
  • 120
  • 284
Loading