| 473 | } // end unnamed namespace |
| 474 | |
| 475 | string SolutionArray::info(const vector<string>& keys, int rows, int width) |
| 476 | { |
| 477 | fmt::memory_buffer b; |
| 478 | int col_width = 12; // targeted maximum column width |
| 479 | vector<string> components; |
| 480 | if (keys.size()) { |
| 481 | components = keys; |
| 482 | } else { |
| 483 | components = componentNames(); |
| 484 | } |
| 485 | try { |
| 486 | // build columns |
| 487 | vector<long int> index; |
| 488 | for (const auto ix : m_active) { |
| 489 | index.push_back(ix); |
| 490 | } |
| 491 | vector<vector<string>> cols = {integerColumn("", index, rows, col_width)}; |
| 492 | vector<vector<string>> tail; // trailing columns in reverse order |
| 493 | size_t size = cols.back().size(); |
| 494 | |
| 495 | // assemble columns fitting within a maximum width; if this width is exceeded, |
| 496 | // a "..." separator is inserted close to the center. Accordingly, the matrix |
| 497 | // needs to be assembled from two halves. |
| 498 | int front = 0; |
| 499 | int back = len(components) - 1; |
| 500 | int fLen = len(cols.back()[0]); |
| 501 | int bLen = 0; |
| 502 | int sep = 5; // separator width |
| 503 | bool done = false; |
| 504 | while (!done && front <= back) { |
| 505 | string key; |
| 506 | while (bLen + sep <= fLen && front <= back) { |
| 507 | // add trailing columns |
| 508 | key = components[back]; |
| 509 | auto col = formatColumn(key, getComponent(key), rows, col_width); |
| 510 | if (len(col[0]) + fLen + bLen + sep > width) { |
| 511 | done = true; |
| 512 | break; |
| 513 | } |
| 514 | tail.push_back(col); |
| 515 | bLen += len(tail.back()[0]); |
| 516 | back--; |
| 517 | } |
| 518 | if (done || front > back) { |
| 519 | break; |
| 520 | } |
| 521 | while (fLen <= bLen + sep && front <= back) { |
| 522 | // add leading columns |
| 523 | key = components[front]; |
| 524 | auto col = formatColumn(key, getComponent(key), rows, col_width); |
| 525 | if (len(col[0]) + fLen + bLen + sep > width) { |
| 526 | done = true; |
| 527 | break; |
| 528 | } |
| 529 | cols.push_back(col); |
| 530 | fLen += len(cols.back()[0]); |
| 531 | front++; |
| 532 | } |