rich_formatter
Rich-based worker formatter with responsive layout for Merlin.
This module provides classes and utilities to format and display logical and physical worker information in a terminal using Rich. It adapts automatically to different terminal widths, providing compact views for narrow screens and detailed tables and panels for wider screens. Key features include:
- Responsive layouts: Adjusts tables and panels based on terminal width.
- Summary panels: Displays aggregated statistics for logical and physical workers, including running, stopped, stalled, and rebooting counts.
- Compact view: Condensed text output for narrow terminals where full tables would not fit.
- Rich styling: Uses colors, icons, and text formatting to make worker statuses and information visually distinct.
Classes:
| Name | Description |
|---|---|
LayoutSize |
Enum defining terminal width categories for responsive layouts. |
ColumnConfig |
Dataclass defining display properties for individual table columns. |
LayoutConfig |
Dataclass defining full layout configuration for a given size. |
ResponsiveLayoutManager |
Selects and provides layout configurations based on terminal width. |
RichWorkerFormatter |
Formats and renders worker information using Rich components with responsive layouts. |
This module depends on the Merlin database interface and worker entities
(LogicalWorkerEntity
and PhysicalWorkerEntity)
to retrieve and display worker information.
ColumnConfig
dataclass
Configuration for an individual table column.
Defines how a column should be displayed within a table, including its label, alignment, width constraints, and optional formatting logic.
Attributes:
| Name | Type | Description |
|---|---|---|
key |
str
|
The field or attribute name mapped to this column. |
title |
str
|
The display name of the column header. |
style |
str
|
Text style to apply (e.g., "white", "bold red"). |
width |
Optional[int]
|
Fixed width of the column, if specified. |
max_width |
Optional[int]
|
Maximum allowed width before truncation or wrapping. |
justify |
str
|
Text alignment ("left", "center", or "right"). |
no_wrap |
bool
|
Whether to prevent text wrapping in this column. |
formatter |
Optional[Callable[[Any], Any]]
|
Optional callable to transform cell values before display. |
Source code in merlin/workers/formatters/rich_formatter.py
LayoutConfig
dataclass
Configuration for rendering worker information at a given layout size.
Determines which elements (tables, panels) are displayed and how they are arranged, based on the current terminal width category.
Attributes:
| Name | Type | Description |
|---|---|---|
size |
LayoutSize
|
The layout size category. |
show_summary_panels |
bool
|
Whether to display summary panels. |
panels_horizontal |
bool
|
If True, display panels side-by-side (horizontal), otherwise stack them vertically. |
physical_worker_columns |
List[ColumnConfig]
|
Column configurations for physical worker tables. |
logical_worker_columns |
List[ColumnConfig]
|
Column configurations for logical worker tables. |
use_compact_view |
bool
|
Whether to enable a simplified, space-saving view of worker information. |
Source code in merlin/workers/formatters/rich_formatter.py
LayoutSize
Bases: Enum
Enumeration of terminal width categories for responsive layouts.
This enum is used to adapt table and panel rendering based on the current terminal width, enabling responsive designs that remain readable across narrow and wide displays.
Attributes:
| Name | Type | Description |
|---|---|---|
COMPACT |
str
|
Very small terminals (< 60 characters wide). |
NARROW |
str
|
Narrow terminals (60-79 characters wide). |
MEDIUM |
str
|
Standard terminals (80-119 characters wide). |
WIDE |
str
|
Wide terminals (>= 120 characters wide). |
Source code in merlin/workers/formatters/rich_formatter.py
ResponsiveLayoutManager
Manage and provide responsive layout configurations for terminal output.
This class adapts the display of worker information (both physical and logical) to different terminal widths. It selects column visibility, ordering, and formatting rules depending on the width category (compact, narrow, medium, wide).
Attributes:
| Name | Type | Description |
|---|---|---|
layouts |
Dict[LayoutSize, LayoutConfig]
|
A mapping of layout sizes to their respective configurations, including column definitions and display options. |
Methods:
| Name | Description |
|---|---|
get_layout_size |
Determine the appropriate layout size category based on terminal width. |
get_layout_config |
Retrieve the full layout configuration object for a given terminal width. |
_format_status |
Format a worker status into a styled Rich Text object with icons. |
Source code in merlin/workers/formatters/rich_formatter.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
__init__()
Initialize the responsive layout manager.
Predefines layout configurations for all supported terminal width categories (compact, narrow, medium, wide). Each configuration specifies which columns should be shown for physical and logical worker tables, whether summary panels are displayed, and how they are arranged.
Source code in merlin/workers/formatters/rich_formatter.py
get_layout_config(width)
Retrieve the layout configuration for a given terminal width.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
|
int
|
The terminal width in characters. |
required |
Returns:
| Type | Description |
|---|---|
LayoutConfig
|
The full layout configuration for the determined size, including column definitions and panel options. |
Source code in merlin/workers/formatters/rich_formatter.py
get_layout_size(width)
Determine the layout size category for a given terminal width.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
|
int
|
The terminal width in characters. |
required |
Returns:
| Type | Description |
|---|---|
LayoutSize
|
The corresponding layout size category (COMPACT, NARROW, MEDIUM, or WIDE). |
Source code in merlin/workers/formatters/rich_formatter.py
RichWorkerFormatter
Bases: WorkerFormatter
Format and display worker information using Rich with responsive layouts.
This class provides a Rich-based implementation of a worker formatter that
adapts to terminal width. It uses responsive tables, panels, and compact
views to display logical and physical worker information in a clear,
visually rich way. Layouts are selected automatically based on terminal
width using a ResponsiveLayoutManager.
Attributes:
| Name | Type | Description |
|---|---|---|
console |
Console
|
A Rich Console object used for displaying output to the terminal. |
layout_manager |
ResponsiveLayoutManager
|
The layout manager responsible for selecting column and panel configurations based on terminal width. |
Methods:
| Name | Description |
|---|---|
format_and_display |
Format and display worker information with responsive Rich components. |
get_worker_statistics |
Compute summary statistics for logical and physical workers. |
_display_compact_view |
Display a simplified worker summary for very narrow terminals. |
_display_summary_panels |
Render summary panels (logical, physical, filters) depending on layout settings. |
_build_responsive_table |
Construct a Rich table using the given column configuration and data. |
_get_physical_worker_data |
Extract detailed physical worker data for table display. |
_get_logical_workers_without_instances_data |
Extract logical workers that have no physical instances. |
_sort_physical_workers |
Sort physical worker data by status (running first), then by worker and instance name. |
_format_status |
Format a worker status with icons and Rich color highlighting. |
_format_uptime_or_downtime |
Return uptime for running workers or downtime for stopped workers in human-readable format. |
_format_time_duration |
Format a time duration into a human-readable string (e.g., "2h 15m"). |
_format_last_heartbeat |
Format last heartbeat timestamp with color coding based on recency. |
_build_summary_panels |
Build summary panels showing filters, logical workers, and physical workers. |
_build_compact_view |
Build a compact text-only view of worker status for very narrowterminals. |
Source code in merlin/workers/formatters/rich_formatter.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 | |
__init__()
Initialize the Rich-based worker formatter.
This constructor sets up the responsive layout manager that determines how worker data will be displayed based on the terminal width.
Source code in merlin/workers/formatters/rich_formatter.py
format_and_display(logical_workers, filters, merlin_db)
Format and display worker information using Rich components.
This method generates a responsive console output of worker statistics, tables, and summary panels. The output adapts to the current terminal width and user-defined filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logical_workers
|
List[LogicalWorkerEntity]
|
A list of logical worker objects to display and summarize. |
required |
filters
|
Dict
|
Active filters applied to the display (e.g., by worker type or status). |
required |
merlin_db
|
MerlinDatabase
|
Reference to the Merlin database, used to query worker-related data. |
required |