From aabcc8c71a0e616d41db99130535767cbbd2b723 Mon Sep 17 00:00:00 2001 From: yut23 Date: Tue, 10 Dec 2024 18:06:08 -0500 Subject: [PATCH] aoc_lib: get field width for values in repr for Grid --- aoc_lib/ds/grid.hpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/aoc_lib/ds/grid.hpp b/aoc_lib/ds/grid.hpp index e536201..39cf8f8 100644 --- a/aoc_lib/ds/grid.hpp +++ b/aoc_lib/ds/grid.hpp @@ -320,13 +320,17 @@ Grid(std::vector> &&) -> Grid; template std::ostream &print_repr(std::ostream &os, const aoc::ds::Grid &grid, const bool result) { + // Get the current field width (from std::setw()) which we will use for the + // individual values. Also set it to 0, as it shouldn't apply to the + // initial '['. + auto field_width = os.width(0); for (int i = 0; const auto &row : grid) { os << (i == 0 ? '[' : ' ') << '['; for (int j = 0; const auto &value : row) { if (j != 0) { os << " "; } - os << std::setw(12) << pretty_print::repr(value, result); + os << std::setw(field_width) << pretty_print::repr(value, result); ++j; } ++i;