#include #include using namespace std; void draw_table(const std::vector& headers, const std::vector>& data, const std::vector& col_widths) { clear(); int y = 0; // Build horizontal line (same as Python version) std::string hline = "+"; for (int w : col_widths) { hline += std::string(w + 2, '-') + "+"; } // Top border mvaddstr(y, 0, hline.c_str()); y++; // Header row (centered) mvaddstr(y, 0, "|"); int x = 2; for (size_t i = 0; i < headers.size(); ++i) { int len = static_cast(headers[i].length()); int pad = (col_widths[i] - len) / 2; mvaddstr(y, x + pad, headers[i].c_str()); x += col_widths[i] + 2; mvaddch(y, x - 1, '|'); } y++; // Header separator mvaddstr(y, 0, hline.c_str()); y++; // Data rows (left-aligned) for (const auto& row : data) { mvaddstr(y, 0, "|"); x = 2; for (size_t i = 0; i < row.size(); ++i) { // Truncate if too long (optional safety) std::string cell = row[i]; if (cell.length() > static_cast(col_widths[i])) { cell.resize(col_widths[i]); } mvaddstr(y, x, cell.c_str()); x += col_widths[i] + 2; mvaddch(y, x - 1, '|'); } y++; } // Bottom border mvaddstr(y, 0, hline.c_str()); refresh(); } int main() { initscr(); cbreak(); noecho(); curs_set(0); // hide cursor keypad(stdscr, TRUE); // Table data (exactly like the Python example) std::vector headers = {"ID", "Name", "Score"}; std::vector> data = { {"1", "Alice", "100"}, {"2", "Bob", "95"}, {"3", "Charlie", "85"} }; std::vector col_widths = {6, 20, 10}; // Initial draw draw_table(headers, data, col_widths); // Simulate update after 2 seconds napms(2000); // ncurses sleep (milliseconds) data[0][2] = "110"; // Alice score changed data[2][1] = "Chuck"; // name changed draw_table(headers, data, col_widths); // Show exit message below the table int num_data_rows = static_cast(data.size()); int bottom_y = 3 + num_data_rows; // where bottom border was printed mvaddstr(bottom_y + 2, 0, "Press any key to exit..."); refresh(); getch(); endwin(); return 0; }