File: CompactDisplay.cpp

package info (click to toggle)
drbd-utils 9.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,388 kB
  • sloc: ansic: 43,698; xml: 15,968; cpp: 7,783; sh: 3,699; makefile: 1,353; perl: 353
file content (1218 lines) | stat: -rw-r--r-- 36,161 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
284
285
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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
#include <iostream>
#include <cstdio>

#include <CompactDisplay.h>
#include <DrbdMon.h>
#include <ConfigOption.h>

#include <cstring>
#include <cctype>
#include <cstdio>
#include <cstdarg>

extern "C"
{
    #include <pthread.h>
    #include <errno.h>
}

const std::string CompactDisplay::OPT_NO_HEADER_KEY("no-header");
const std::string CompactDisplay::OPT_NO_HOTKEYS_KEY("no-hotkeys");
const std::string CompactDisplay::OPT_ASCII_KEY("ascii");
const std::string CompactDisplay::OPT_PROBLEMS_KEY("problems");

const std::string CompactDisplay::HDR_SPACER(" | ");
const std::string CompactDisplay::NODE_DSP_PREFIX("Node ");
const std::string CompactDisplay::TRUNC_MARKER("...");

const ConfigOption CompactDisplay::OPT_NO_HEADER(true, OPT_NO_HEADER_KEY);
const ConfigOption CompactDisplay::OPT_NO_HOTKEYS(true, OPT_NO_HOTKEYS_KEY);
const ConfigOption CompactDisplay::OPT_ASCII(true, OPT_ASCII_KEY);
const ConfigOption CompactDisplay::OPT_PROBLEMS(true, OPT_PROBLEMS_KEY);

// Generic formats
const char* CompactDisplay::F_NORM  = "\x1b[0;32m";
const char* CompactDisplay::F_WARN  = "\x1b[1;33;45m";
const char* CompactDisplay::F_ALERT = "\x1b[1;33;41m";
const char* CompactDisplay::F_RESET = "\x1b[0m";

const char* CompactDisplay::F_MARK  = "\x1b[1;33;41m";

const char* CompactDisplay::F_HEADER= "\x1b[1;37;44m";

const char* CompactDisplay::F_RES_NORM   = "\x1b[0;30;42m";
const char* CompactDisplay::F_VOL_NORM   = "\x1b[0;30;46m";
const char* CompactDisplay::F_VOL_CLIENT = "\x1b[0;1;37;44m";
const char* CompactDisplay::F_VOL_MINOR  = "\x1b[0;4;36m";
const char* CompactDisplay::F_CONN_NORM  = "\x1b[0;37;44m";
const char* CompactDisplay::F_PRIMARY    = "\x1b[1;36m";
const char* CompactDisplay::F_SECONDARY  = "\x1b[0;36m";
const char* CompactDisplay::QUORUM_ALERT = "\x1b[1;33;41mQUORUM LOST\x1b[0m";

const int CompactDisplay::QUORUM_ALERT_WIDTH = 11;

// Foreground color for the 'Primary' role highlighting
// of connections (peer resource role)
const char* CompactDisplay::F_CONN_PRI_FG = "\x1b[1;36;44m";

const char* CompactDisplay::F_RES_NAME   = "\x1b[0;4;32m";
const char* CompactDisplay::F_RES_COUNT  = "\x1b[0;32mResources: %6llu\x1b[0m";
const char* CompactDisplay::F_PRB_COUNT  = "\x1b[0;1;31m (%llu degraded)\x1b[0m";

const char* CompactDisplay::F_CURSOR_POS = "\x1b[%u;%uH";
const char* CompactDisplay::F_HOTKEY     = "\x1b[0;30;47m%c\x1b[0;37;44m %s \033[0m";
const char* CompactDisplay::F_ALERT_HOTKEY = "\x1b[0;30;47m%c\x1b[1;33;41m %s \033[0m";

const char* CompactDisplay::F_PAGE       = "Page: %5llu\n";
const int   CompactDisplay::PAGE_POS_R   = 15;
const char* CompactDisplay::F_GOTO_PAGE  = "\x1b[1;37m[Go to page: %5llu]\x1b[0m";
const int   CompactDisplay::GOTO_PAGE_POS_R = 37;
const int   CompactDisplay::GOTO_PAGE_CURSOR_POS = 17;

const char* CompactDisplay::UTF8_PRIMARY   = "\xE2\x9A\xAB";
const char* CompactDisplay::UTF8_SECONDARY = " "; // otherwise UTF-8 E2 9A AA
const char* CompactDisplay::UTF8_CONN_GOOD = "\xE2\x87\x84";
const char* CompactDisplay::UTF8_CONN_BAD  = "\xE2\x86\xAF";
const char* CompactDisplay::UTF8_DISK_GOOD = "\xE2\x97\x8E";
const char* CompactDisplay::UTF8_DISK_BAD  = "\xE2\x9C\x97";
const char* CompactDisplay::UTF8_MARK_OFF  = "\xE2\x9A\xAC";
const char* CompactDisplay::UTF8_MARK_ON   = "\xE2\xA4\xB7";

const char* CompactDisplay::ASCII_PRIMARY   = "*";
const char* CompactDisplay::ASCII_SECONDARY = " ";
const char* CompactDisplay::ASCII_CONN_GOOD = "+";
const char* CompactDisplay::ASCII_CONN_BAD  = "/";
const char* CompactDisplay::ASCII_DISK_GOOD = "+";
const char* CompactDisplay::ASCII_DISK_BAD  = "/";
const char* CompactDisplay::ASCII_MARK_OFF  = " ";
const char* CompactDisplay::ASCII_MARK_ON   = "+";

const char* CompactDisplay::ANSI_CLEAR      = "\x1b[H\x1b[2J";
const char* CompactDisplay::ANSI_CLEAR_LINE = "\x1b[K";

const char* CompactDisplay::ANSI_CURSOR_OFF = "\x1b[?25l";
const char* CompactDisplay::ANSI_CURSOR_ON  = "\x1b[?25h";

const char CompactDisplay::HOTKEY_PGUP = '<';
const char CompactDisplay::HOTKEY_PGDN = '>';
const char CompactDisplay::HOTKEY_PGONE = '!';

const char CompactDisplay::KEY_BACKSPACE = 127;
const char CompactDisplay::KEY_CTRL_H    = 8;
const char CompactDisplay::KEY_NEWLINE   = '\n';
const char CompactDisplay::KEY_ENTER     = '\r';
const char CompactDisplay::KEY_ESC       = 0x1B;

const std::string CompactDisplay::LABEL_MESSAGES    = "Messages";
const std::string CompactDisplay::LABEL_MONITOR     = "Monitor";
const std::string CompactDisplay::LABEL_PROBLEMS    = "Problems";
const std::string CompactDisplay::LABEL_STATUS      = "Status";
const std::string CompactDisplay::LABEL_PGUP        = "PgUp";
const std::string CompactDisplay::LABEL_PGDN        = "PgDn";
const std::string CompactDisplay::LABEL_PGZERO      = "Pg1";

const uint16_t CompactDisplay::MIN_SIZE_X =   40;
const uint16_t CompactDisplay::MAX_SIZE_X = 1024;
const uint16_t CompactDisplay::MIN_SIZE_Y =   15;
const uint16_t CompactDisplay::MAX_SIZE_Y = 1024;

const int CompactDisplay::RES_NAME_WIDTH   = 32;
const int CompactDisplay::ROLE_WIDTH       = 10;
const int CompactDisplay::VOL_NR_WIDTH     = 2;
const int CompactDisplay::MINOR_NR_WIDTH   = 4;
const int CompactDisplay::CONN_NAME_WIDTH  = 20;
const int CompactDisplay::CONN_STATE_WIDTH = 20;
const int CompactDisplay::DISK_STATE_WIDTH = 20;
const int CompactDisplay::REPL_STATE_WIDTH = 20;

const uint16_t CompactDisplay::INDENT_STEP_SIZE     = 4;
const uint16_t CompactDisplay::OUTPUT_BUFFER_SIZE   = 1024;

const uint16_t CompactDisplay::MIN_NODENAME_DSP_LENGTH = 4;
const uint32_t CompactDisplay::MAX_YIELD_LOOP = 10;

// @throws std::bad_alloc
CompactDisplay::CompactDisplay(
    DrbdMon& drbdmon_ref,
    ResourcesMap& resources_map_ref,
    MessageLog&   log_ref,
    HotkeysMap&   hotkeys_info_ref,
    const std::string* const node_name_ref
):
    drbdmon(drbdmon_ref),
    resources_map(resources_map_ref),
    log(log_ref),
    hotkeys_info(hotkeys_info_ref),
    node_name(node_name_ref)
{
    // Allocate and initialize the indent buffer
    indent_buffer_mgr = std::unique_ptr<char[]>(new char[INDENT_STEP_SIZE + 1]);
    indent_buffer = indent_buffer_mgr.get();
    for (size_t index = 0; index < INDENT_STEP_SIZE; ++index)
    {
        indent_buffer[index] = ' ';
    }
    indent_buffer[INDENT_STEP_SIZE] = '\0';

    output_buffer_mgr = std::unique_ptr<char[]>(new char[OUTPUT_BUFFER_SIZE]);
    output_buffer = output_buffer_mgr.get();

    // Hide the cursor
    write_text(ANSI_CURSOR_OFF);

    hotkeys_info.append(&HOTKEY_PGONE, &LABEL_PGZERO);
    hotkeys_info.append(&HOTKEY_PGUP, &LABEL_PGUP);
    hotkeys_info.append(&HOTKEY_PGDN, &LABEL_PGDN);

    node_label = std::unique_ptr<std::string>(new std::string(HDR_SPACER));
    *node_label += NODE_DSP_PREFIX;
}

CompactDisplay::~CompactDisplay() noexcept
{
    // Show the cursor
    write_text(ANSI_CURSOR_ON);
}

void CompactDisplay::clear()
{
    if (mode == input_mode::PAGE_NR)
    {
        // Page navigation may have turned the cursor on
        write_text(ANSI_CURSOR_OFF);
    }
    write_text(ANSI_CLEAR);

    current_x = 0;
    current_y = 0;
}

void CompactDisplay::initial_display()
{
    clear();
    display_header();

    if (log.has_entries() && dsp_msg_active)
    {
        log.display_messages(stdout);
    }
    else
    {
        if (!log.has_entries())
        {
            dsp_msg_active = false;
        }

        write_fmt("%sReading initial DRBD status...%s\n", F_RES_NAME, F_RESET);
    }

    // End line
    next_line();

    display_hotkeys_info();
    if (mode == input_mode::PAGE_NR)
    {
        page_nav_cursor();
    }
}

void CompactDisplay::status_display()
{
    uint32_t current_page = page;
    page_start = (term_y - 4) * current_page;
    page_end   = (term_y - 4) * (current_page + 1) - 1;

    problem_check();
    clear();
    display_header();

    if (log.has_entries() && dsp_msg_active)
    {
        log.display_messages(stdout);
    }
    else
    {
        if (!log.has_entries())
        {
            dsp_msg_active = false;
        }

        bool resources_listed = list_resources();
        if (!resources_listed)
        {
            if (dsp_problems_active)
            {
                write_fmt("%sNo resources with problems to display.%s\n", F_RES_NAME, F_RESET);
            }
            else
            {
                write_fmt("%sNo active DRBD resources.%s\n", F_RES_NAME, F_RESET);
            }
        }
    }

    // End line
    next_line();

    display_counts();

    display_hotkeys_info();
    if (mode == input_mode::PAGE_NR)
    {
        page_nav_cursor();
    }
}

void CompactDisplay::display_header() const
{
    if (show_header)
    {
        write_fmt("%s%s%s v%s", F_HEADER, ANSI_CLEAR_LINE,
                  DrbdMon::PROGRAM_NAME.c_str(), DrbdMon::VERSION.c_str());

        if (node_name != nullptr)
        {
            if (enable_term_size)
            {
                // "%s v%s" format for PROGRAM_NAME and VERSION
                uint16_t x_pos = DrbdMon::PROGRAM_NAME.length() + DrbdMon::VERSION.length() + 2;
                if (term_x >= x_pos)
                {
                    uint16_t free_x = term_x - x_pos;
                    size_t label_length = node_label->length();
                    if (free_x >= label_length)
                    {
                        free_x -= label_length;
                        size_t name_length = node_name->length();
                        if (free_x > name_length)
                        {
                            // Entire node name fits on the header line
                            write_text(node_label->c_str());
                            write_text(node_name->c_str());
                        }
                        else
                        {
                            if (free_x > MIN_NODENAME_DSP_LENGTH + TRUNC_MARKER.length())
                            {
                                free_x -= TRUNC_MARKER.length() + 1;
                                // Minimum node name length and truncation marker fit on the header line
                                write_text(node_label->c_str());
                                size_t write_length = free_x < name_length ? free_x : name_length;
                                write_buffer(node_name->c_str(), write_length);
                                write_text(TRUNC_MARKER.c_str());
                            }
                        }
                    }
                }
            }
            else
            {
                write_text(node_label->c_str());
                write_text(node_name->c_str());
            }
        }
        write_char('\n');

        // Print right-aligned page number if the terminal size is known
        if (enable_term_size)
        {
            if (mode == input_mode::PAGE_NR)
            {
                page_nav_display();
            }
            write_fmt(F_CURSOR_POS, 2, static_cast<int> (term_x) - PAGE_POS_R);
            write_text(F_RESET);
            write_fmt(F_PAGE, static_cast<unsigned long long> (page) + 1);
        }
        else
        {
            write_char('\n');
        }
    }
}

void CompactDisplay::page_nav_display() const
{
    if (enable_term_size)
    {
        write_fmt(F_CURSOR_POS, 2, static_cast<int> (term_x) - GOTO_PAGE_POS_R);
        write_text(F_RESET);
        write_fmt(F_GOTO_PAGE, static_cast<unsigned long long> (goto_page) > 0 ? goto_page : 1);
    }
}

void CompactDisplay::page_nav_cursor() const
{
    if (enable_term_size)
    {
        write_fmt(F_CURSOR_POS, 2, static_cast<int> (term_x) - GOTO_PAGE_POS_R + GOTO_PAGE_CURSOR_POS);
        write_fmt(ANSI_CURSOR_ON);
    }
}

void CompactDisplay::display_hotkeys_info() const
{
    if (show_hotkeys)
    {
        if (enable_term_size)
        {
            write_fmt(F_CURSOR_POS, static_cast<int> (term_y), 1);
        }

        HotkeysMap::NodesIterator hotkeys_iter(hotkeys_info);
        size_t count = hotkeys_iter.get_size();
        size_t index = 0;
        while (index < count)
        {
            HotkeysMap::Node* node = hotkeys_iter.next();
            const char hotkey = *(node->get_key());
            const std::string& description = *(node->get_value());
            if (index > 0)
            {
                write_char(' ');
            }
            write_fmt(F_HOTKEY, hotkey, description.c_str());
            ++index;
        }

        if (index > 0)
        {
            write_char(' ');
        }
        if (dsp_problems_active)
        {
            write_fmt(F_HOTKEY, 'p', LABEL_STATUS.c_str());
        }
        else
        {
            const char* format = problem_alert ? F_ALERT_HOTKEY : F_HOTKEY;
            write_fmt(format, 'p', LABEL_PROBLEMS.c_str());
        }

        if (dsp_msg_active)
        {
            write_char(' ');
            write_fmt(F_HOTKEY, 'm', LABEL_MONITOR.c_str());
        }
        else
        if (log.has_entries())
        {
            write_char(' ');
            write_fmt(F_ALERT_HOTKEY, 'm', LABEL_MESSAGES.c_str());
        }
        write_fmt(F_CURSOR_POS, 1, 1);
    }
}

void CompactDisplay::display_counts() const
{
    if (enable_term_size)
    {
        write_fmt(F_CURSOR_POS, static_cast<int> (term_y - 1), 1);
    }
    write_fmt(F_RES_COUNT, static_cast<unsigned long long> (resources_map.get_size()));

    if (problem_alert)
    {
        write_fmt(F_PRB_COUNT, static_cast<unsigned long long> (drbdmon.get_problem_count()));
    }
}

void CompactDisplay::problem_check()
{
    problem_alert = drbdmon.get_problem_count() > 0;
}

bool CompactDisplay::list_resources()
{
    bool resources_listed {false};

    // Reset columns tracking
    reset_positions();

    ResourcesMap::ValuesIterator res_iter(resources_map);
    size_t res_count = res_iter.get_size();
    for (size_t res_index = 0; res_index < res_count; ++res_index)
    {
        DrbdResource& res = *(res_iter.next());

        bool marked = res.has_mark_state();
        // If the problem display is active, show only resources that
        // are marked for having warnings or alerts
        if (!dsp_problems_active || marked)
        {
            resources_listed = true;

            // Setup view of the "RES:" label
            const char* f_res = F_RES_NORM;
            if (res.has_alert_state())
            {
                f_res = F_ALERT;
            }
            else
            if (res.has_warn_state())
            {
                f_res = F_WARN;
            }

            // Setup view of the sub-object warning mark
            const char* f_mark = F_RES_NORM;
            const char* mark_icon = mark_off;
            if (res.has_mark_state())
            {
                f_mark = F_MARK;
                mark_icon = mark_on;
            }

            // Setup view of the resource's role
            const char* f_role = F_SECONDARY;
            const char* role_icon = sec_icon;
            if (res.has_role_alert())
            {
                f_role = F_ALERT;
                role_icon = " ";
            }
            else
            if (res.get_role() == DrbdRole::resource_role::PRIMARY)
            {
                f_role = F_PRIMARY;
                role_icon = pri_icon;
            }

            next_line();
            // Marker (1) + "RES:" (4) + RES_NAME_WIDTH + " " (1) + role icon (1) + ROLE_WIDTH
            if (next_column(6 + RES_NAME_WIDTH + ROLE_WIDTH))
            {
                write_fmt(
                    "%s%s%sRES:%s%-*s%s %s%s%-*s%s",
                    f_mark, mark_icon, f_res, F_RES_NAME, RES_NAME_WIDTH, res.get_name().c_str(), F_RESET,
                    f_role, role_icon, ROLE_WIDTH, res.get_role_label(), F_RESET
                );
            }

            // Show quorum alert warning
            if (res.has_quorum_alert())
            {
                if (next_column(1 + QUORUM_ALERT_WIDTH))
                {
                    write_fmt(" %s", QUORUM_ALERT);
                }
            }

            increase_indent();
            list_volumes(res);
            list_connections(res);
            decrease_indent();
        }
    }

    return resources_listed;
}

void CompactDisplay::list_connections(DrbdResource& res)
{
    // The connections list always starts on a new line
    next_line();

    // Short-display all connections that have a good state
    {
        ConnectionsMap::ValuesIterator conn_iter = res.connections_iterator();
        size_t conn_count = conn_iter.get_size();
        for (size_t conn_index = 0; conn_index < conn_count; ++conn_index)
        {
            DrbdConnection& conn = *(conn_iter.next());

            if (!conn.has_mark_state())
            {
                const char* f_conn = F_CONN_NORM;

                // Setup role icon & 'Primary' role highlighting format
                const char* f_role = F_CONN_NORM;
                const char* role_icon = sec_icon;
                if (conn.get_role() == DrbdRole::resource_role::PRIMARY)
                {
                    f_role = F_CONN_PRI_FG;
                    role_icon = pri_icon;
                }

                const std::string& conn_name = conn.get_name();

                // Mark (1) + connection icon (1) + role icon (1) + name length
                if (next_column(3 + conn_name.length()))
                {
                    write_fmt(
                        "%s%s%s%s%s%s%s",
                        f_conn, mark_off, conn_good, f_role, role_icon, conn_name.c_str(), F_RESET
                    );
                }
            }
        }
    }

    {
        ConnectionsMap::ValuesIterator conn_iter = res.connections_iterator();
        size_t conn_count = conn_iter.get_size();
        for (size_t conn_index = 0; conn_index < conn_count; ++conn_index)
        {
            DrbdConnection& conn = *(conn_iter.next());

            if (conn.has_mark_state())
            {
                // Setup connection view format
                const char* f_conn = F_CONN_NORM;
                if (conn.has_alert_state())
                {
                    f_conn = F_ALERT;
                }
                else
                if (conn.has_warn_state())
                {
                    f_conn = F_WARN;
                }

                // Setup connection state view format
                const char* f_conn_state = F_NORM;
                const char* conn_icon = conn_good;
                if (conn.has_connection_alert())
                {
                    f_conn_state = F_ALERT;
                    conn_icon = conn_bad;
                }

                // Setup role state view format
                const char* f_role = F_SECONDARY;
                const char* role_icon = sec_icon;
                if (conn.has_role_alert())
                {
                    f_role = F_ALERT;
                    role_icon = " ";
                }
                else
                if (conn.get_role() == DrbdRole::resource_role::PRIMARY)
                {
                    f_role = F_PRIMARY;
                    role_icon = pri_icon;
                }

                next_line();
                // Mark (1) + connection icon (1) + role icon (1) + CONN_NAME_WIDTH +
                // " " (1) + CONN_STATE_WIDTH + " " (1) + role icon (1) + ROLE_WIDTH
                if (next_column(6 + CONN_NAME_WIDTH + CONN_STATE_WIDTH + ROLE_WIDTH))
                {
                    write_fmt(
                        "%s%s%s%s%s%-*s%s %s%-*s%s %s%s%-*s%s",
                        F_MARK, mark_on, f_conn, conn_icon, role_icon, CONN_NAME_WIDTH,
                        conn.get_name().c_str(), F_RESET, f_conn_state, CONN_STATE_WIDTH,
                        conn.get_connection_state_label(), F_RESET, f_role, role_icon,
                        ROLE_WIDTH, conn.get_role_label(), F_RESET
                    );
                }

                increase_indent();
                list_peer_volumes(conn);
                decrease_indent();
            }
        }
    }
}

void CompactDisplay::list_volumes(DrbdResource& res)
{
    // Short-display all volumes that have a good state
    {
        VolumesMap::ValuesIterator vol_iter = res.volumes_iterator();
        size_t vol_count = vol_iter.get_size();
        for (size_t vol_index = 0; vol_index < vol_count; ++vol_index)
        {
            DrbdVolume& vol = *(vol_iter.next());

            if (!vol.has_warn_state())
            {
                show_volume(vol, false, false);
            }
        }
    }

    // Long-display all volumes that have a faulty state
    {
        VolumesMap::ValuesIterator vol_iter = res.volumes_iterator();
        size_t vol_count = vol_iter.get_size();
        for (size_t vol_index = 0; vol_index < vol_count; ++vol_index)
        {
            DrbdVolume& vol = *(vol_iter.next());

            if (vol.has_warn_state())
            {
                show_volume(vol, false, true);
            }
        }
    }
}

void CompactDisplay::list_peer_volumes(DrbdConnection& conn)
{
    VolumesMap::ValuesIterator peer_vol_iter = conn.volumes_iterator();
    size_t peer_vol_count = peer_vol_iter.get_size();
    for (size_t peer_vol_index = 0; peer_vol_index < peer_vol_count; ++peer_vol_index)
    {
        DrbdVolume& peer_vol = *(peer_vol_iter.next());

        if (peer_vol.has_warn_state())
        {
            show_volume(peer_vol, true, true);
        }
    }
}

void CompactDisplay::show_volume(DrbdVolume& vol, bool peer_volume, bool long_format)
{
    if (long_format)
    {
        // Long format view for volumes that have a faulty state

        // Set disk view format
        const char* f_disk = F_NORM;
        if (vol.has_disk_alert())
        {
            f_disk = F_ALERT;
        }

        // Set replication view format
        const char* f_repl = F_NORM;
        if (vol.has_replication_alert())
        {
            f_repl = F_ALERT;
        }
        else
        if (vol.has_replication_warning())
        {
            f_repl = F_WARN;
        }

        bool show_replication = peer_volume;
        if (peer_volume)
        {
            // Hide the 'Established' replication state
            if (vol.get_replication_state() == DrbdVolume::repl_state::ESTABLISHED)
            {
                show_replication = false;
            }
        }

        bool quorum_alert = vol.has_quorum_alert();

        next_line();

        // Disk icon (1) + VOL_NR_WIDTH + " " (1) + DISK_STATE_WIDTH
        uint16_t vol_entry_width = 2 + VOL_NR_WIDTH + DISK_STATE_WIDTH;

        if (!peer_volume)
        {
            // Local volume, add in minor number fields
            // ":" (1) + MINOR_NR_WIDTH
            vol_entry_width += 1 + MINOR_NR_WIDTH;
        }

        if (show_replication)
        {
            // Add in replication state fields
            // " " (1) + REPL_STATE_WIDTH
            vol_entry_width += 1 + REPL_STATE_WIDTH;
        }

        if (quorum_alert && debug_view)
        {
            vol_entry_width += 1 + QUORUM_ALERT_WIDTH;
        }

        bool show = next_column(vol_entry_width);

        if (show)
        {
            write_fmt(
                "%s%s%*ld",
                F_ALERT, disk_bad, VOL_NR_WIDTH, static_cast<long> (vol.get_volume_nr())
            );
        }

        // Hide the minor number of peer volumes, as it is always unknown
        if (!peer_volume && show)
        {
            // Display minor number
            write_fmt(":%s%*ld", F_VOL_MINOR, MINOR_NR_WIDTH, static_cast<long> (vol.get_minor_nr()));
        }

        // Display disk state
        if (show)
        {
            write_fmt(" %s %-*s%s", f_disk, DISK_STATE_WIDTH, vol.get_disk_state_label(), F_RESET);
        }

        if (show)
        {
            if (show_replication)
            {
                // Display replication state
                write_fmt(
                    " %s%-*s%s", f_repl, REPL_STATE_WIDTH,
                    vol.get_replication_state_label(), F_RESET
                );
            }

            if (quorum_alert && debug_view)
            {
                write_fmt(" %s", QUORUM_ALERT);
            }
        }
    }
    else
    {
        // Short format view of volumes that have a good state
        const char* f_vol = F_VOL_NORM;
        if (vol.get_disk_state() == DrbdVolume::disk_state::DISKLESS)
        {
            f_vol = F_VOL_CLIENT;
        }

        // Disk icon (1) + VOL_NR_WIDTH + ":" (1) + MINOR_NR_WIDTH
        if (next_column(2 + VOL_NR_WIDTH + MINOR_NR_WIDTH))
        {
            write_fmt(
                "%s%s%s%s%*ld:%s%*ld%s",
                F_NORM, disk_good, F_RESET,
                f_vol, VOL_NR_WIDTH, static_cast<long> (vol.get_volume_nr()),
                F_VOL_MINOR, MINOR_NR_WIDTH, static_cast<long> (vol.get_minor_nr()), F_RESET
            );
        }
    }
}

void CompactDisplay::set_terminal_size(uint16_t size_x, uint16_t size_y)
{
    if (size_x >= MIN_SIZE_X)
    {
        if (size_x <= MAX_SIZE_X)
        {
            term_x = size_x;
        }
        else
        {
            term_x = MAX_SIZE_X;
        }
    }
    else
    {
        term_x = MIN_SIZE_X;
    }

    if (size_y >= MIN_SIZE_Y)
    {
        if (size_y <= MAX_SIZE_Y)
        {
            term_y = size_y;
        }
        else
        {
            term_y = MAX_SIZE_Y;
        }
    }
    else
    {
        term_y = MIN_SIZE_Y;
    }
}

void CompactDisplay::increase_indent()
{
    ++indent_level;
}

void CompactDisplay::decrease_indent()
{
    if (indent_level >= 1)
    {
        --indent_level;
    }
}

void CompactDisplay::reset_indent()
{
    indent_level = 0;
}

void CompactDisplay::reset_positions()
{
    reset_indent();
    current_x = 0;
    current_y = 0;
}

void CompactDisplay::indent()
{
    if (!(enable_term_size && term_y > MIN_SIZE_Y) ||
        (current_y >= page_start && current_y <= page_end))
    {
        for (uint16_t counter = 0; counter < indent_level; ++counter)
        {
            write_text(indent_buffer);
        }
    }
    current_x += INDENT_STEP_SIZE * indent_level;
}

bool CompactDisplay::next_column(uint16_t length)
{
    bool display_flag = false;
    if (enable_term_size && term_y > MIN_SIZE_Y)
    {
        display_flag = current_y >= page_start && current_y <= page_end;

        // If this column is at the start of a new line,
        // print the indent
        if (current_x == 0)
        {
            indent();
            current_x += length;
        }
        else
        {
            // Check whether the column fits in at the end
            // of the current line
            uint16_t end_x = current_x + length + 1;
            if (end_x < term_x)
            {
                if (display_flag)
                {
                    write_char(' ');
                }
                current_x = end_x;
            }
            else
            {
                next_line();
                display_flag = current_y >= page_start && current_y <= page_end;
                indent();
                current_x += length;
            }
        }
    }
    else
    {
        display_flag = true;
        if (current_x == 0)
        {
            indent();
            current_x += length;
        }
        else
        {
            // No terminal size restrictions,
            // allow an infinite number of columns
            write_char(' ');
        }
    }
    return display_flag;
}

void CompactDisplay::next_line()
{
    if (current_x > 0)
    {
        if (!(enable_term_size && term_y > MIN_SIZE_Y) ||
            (current_y >= page_start && current_y <= page_end))
        {
            write_char('\n');
        }
        current_x = 0;
        ++current_y;
    }
}

void CompactDisplay::set_utf8(bool enable)
{
    enable_utf8 = enable;
    if (enable_utf8)
    {
        pri_icon  = UTF8_PRIMARY;
        sec_icon  = UTF8_SECONDARY;
        conn_good = UTF8_CONN_GOOD;
        conn_bad  = UTF8_CONN_BAD;
        disk_good = UTF8_DISK_GOOD;
        disk_bad  = UTF8_DISK_BAD;
        mark_off  = UTF8_MARK_OFF;
        mark_on   = UTF8_MARK_ON;
    }
    else
    {
        pri_icon  = ASCII_PRIMARY;
        sec_icon  = ASCII_SECONDARY;
        conn_good = ASCII_CONN_GOOD;
        conn_bad  = ASCII_CONN_BAD;
        disk_good = ASCII_DISK_GOOD;
        disk_bad  = ASCII_DISK_BAD;
        mark_off  = ASCII_MARK_OFF;
        mark_on   = ASCII_MARK_ON;
    }
}

// @throws std::bad_alloc
void CompactDisplay::announce_options(Configurator& collector)
{
    Configurable& owner = dynamic_cast<Configurable&> (*this);
    collector.add_config_option(owner, OPT_NO_HEADER);
    collector.add_config_option(owner, OPT_NO_HOTKEYS);
    collector.add_config_option(owner, OPT_ASCII);
    collector.add_config_option(owner, OPT_PROBLEMS);
}

void CompactDisplay::options_help() noexcept
{
    std::fputs("DrbdMon display configuration options:\n", stderr);
    std::fputs("  --ascii          Use only ASCII characters (no Unicode)\n", stderr);
    std::fputs("  --no-header      Do not display the DrbdMon header line\n", stderr);
    std::fputs("  --no-hotkeys     Do not display the hotkeys line\n", stderr);
    std::fputs("  --problems       Start with the problems view\n", stderr);
    std::fputc('\n', stderr);
    std::fflush(stderr);
}

// @throws std::bad_alloc
void CompactDisplay::set_flag(std::string& key)
{
    if (key == OPT_NO_HEADER.key)
    {
        show_header = false;
    }
    else
    if (key == OPT_NO_HOTKEYS.key)
    {
        show_hotkeys = false;
    }
    else
    if (key == OPT_ASCII.key)
    {
        set_utf8(false);
    }
    else
    if (key == OPT_PROBLEMS.key)
    {
        dsp_problems_active = true;
    }
}

// @throws std::bad_alloc
void CompactDisplay::set_option(std::string& key, std::string& value)
{
    // no-op; the CompactDisplay instance does not have any options
    // with configurable values at this time
}

void CompactDisplay::key_pressed(const char key)
{
    switch (key)
    {
        case HOTKEY_PGDN:
            ++page;
            status_display();
            break;
        case HOTKEY_PGUP:
            --page;
            status_display();
            break;
        case '%':
            // Toggle debug view
            debug_view = !debug_view;
            status_display();
            break;
        case 'm':
            if (dsp_msg_active)
            {
                dsp_msg_active = false;
            }
            else
            if (log.has_entries())
            {
                dsp_msg_active = true;
            }
            status_display();
            break;
        case 'p':
            if (dsp_problems_active)
            {
                dsp_problems_active = false;
            }
            else
            {
                dsp_problems_active = true;
            }
            // fall-through
        case HOTKEY_PGONE:
            page = 0;
            status_display();
            break;
        default:
            if (isdigit(key) != 0)
            {
                uint32_t digit = static_cast<unsigned char> (key) - static_cast<unsigned char> ('0');
                if (mode == input_mode::HOTKEYS && enable_term_size)
                {
                    goto_page = digit;
                    mode = input_mode::PAGE_NR;
                    page_nav_display();
                    page_nav_cursor();
                }
                else
                if (mode == input_mode::PAGE_NR)
                {
                    uint32_t new_goto_page = (goto_page * 10) + digit;
                    if (new_goto_page <= static_cast<uint32_t> (1) << 16)
                    {
                        goto_page = new_goto_page;
                        page_nav_display();
                        page_nav_cursor();
                    }
                }
            }
            else
            if (mode == input_mode::PAGE_NR)
            {
                if (key == KEY_BACKSPACE || key == KEY_CTRL_H)
                {
                    goto_page = goto_page / 10;
                    page_nav_display();
                    page_nav_cursor();
                }
                else
                if (key == KEY_NEWLINE || key == KEY_ENTER)
                {
                    if (goto_page > 0)
                    {
                        --goto_page;
                    }
                    if (goto_page < static_cast<uint32_t> (1) << 16)
                    {
                        page = goto_page;
                        goto_page = 0;
                    }
                    mode = input_mode::HOTKEYS;
                    write_text(ANSI_CURSOR_OFF);
                    status_display();
                }
                else
                if (key == KEY_ESC)
                {
                    goto_page = 0;
                    mode = input_mode::HOTKEYS;
                    write_text(ANSI_CURSOR_OFF);
                    status_display();
                }
            }
            break;
    }
}

/**
 * Writes a single character to the output_fd file descriptor
 *
 * @param ch The character to write
 */
void CompactDisplay::write_char(const char ch) const noexcept
{
    // Repeat temporarily failing write() calls until the byte has been written
    uint32_t loop_guard {0};
    while (write(output_fd, static_cast<const void*> (&ch), 1) != 1)
    {
        if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
        {
            break;
        }

        if (loop_guard < MAX_YIELD_LOOP)
        {
            // Attempt to yield to other processes before retrying
            static_cast<void> (sched_yield());
            ++loop_guard;
        }
        else
        {
            // If yielding to other processes did not lead to any progress,
            // suspend for a while
            static_cast<void> (nanosleep(&write_retry_delay, nullptr));
        }
    }
}

/**
 * Writes a text string to the output_fd file descriptor
 *
 * @param text The text string to write
 */
void CompactDisplay::write_text(const char* text) const noexcept
{
    size_t length = std::strlen(text);
    write_buffer(text, length);
}

/**
 * Formats a text string and writes the result to the output_fd file descriptor
 *
 * @param format Format string
 * @param ... Arguments for the format string
 */
void CompactDisplay::write_fmt(const char* format, ...) const noexcept
{
    va_list vars;
    va_start(vars, format);
    size_t safe_length = 0;
    {
        size_t unsafe_length = vsnprintf(output_buffer, OUTPUT_BUFFER_SIZE, format, vars);
        safe_length = unsafe_length < OUTPUT_BUFFER_SIZE ? unsafe_length : OUTPUT_BUFFER_SIZE;
    }
    va_end(vars);
    write_buffer(output_buffer, safe_length);
}

/**
 * Writes buffered data to the output_fd file descriptor
 *
 * Write attempts that fail temporarily or are only partially successful are retried until the
 * all the buffered data has been written.
 *
 * @param buffer The buffered data to write
 * @param length Length of the buffered data in the (possibly larger) buffer
 */
void CompactDisplay::write_buffer(const char* buffer, size_t length) const noexcept
{
    uint32_t loop_guard {0};
    ssize_t written {0};
    while (length > 0)
    {
        // Repeat temporarily failing write() calls until the entire contents of the buffer have been written
        written = write(output_fd, static_cast<const void*> (buffer), length);
        if (written > 0)
        {
            length -= written;
        }
        else
        if (written == -1 && (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR))
        {
            break;
        }

        if (written <= 0)
        {
            if (loop_guard < MAX_YIELD_LOOP)
            {
                // Attempt to yield to other processes before retrying
                static_cast<void> (sched_yield());
                ++loop_guard;
            }
            else
            {
                // If yielding to other processes did not lead to any progress,
                // suspend for a while
                static_cast<void> (nanosleep(&write_retry_delay, nullptr));
            }
        }
    }
}