HeaderLayout.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef HEADER_HeaderLayout
  2. #define HEADER_HeaderLayout
  3. /*
  4. htop - HeaderLayout.h
  5. (C) 2021 htop dev team
  6. Released under the GNU GPLv2+, see the COPYING file
  7. in the source distribution for its full text.
  8. */
  9. #include <assert.h>
  10. #include <stddef.h>
  11. #include <stdint.h>
  12. #include "Macros.h"
  13. #include "XUtils.h"
  14. typedef enum HeaderLayout_ {
  15. HF_INVALID = -1,
  16. HF_ONE_100,
  17. HF_TWO_50_50,
  18. HF_TWO_33_67,
  19. HF_TWO_67_33,
  20. HF_THREE_33_34_33,
  21. HF_THREE_25_25_50,
  22. HF_THREE_25_50_25,
  23. HF_THREE_50_25_25,
  24. HF_THREE_40_30_30,
  25. HF_THREE_30_40_30,
  26. HF_THREE_30_30_40,
  27. HF_THREE_40_20_40,
  28. HF_FOUR_25_25_25_25,
  29. LAST_HEADER_LAYOUT
  30. } HeaderLayout;
  31. static const struct {
  32. uint8_t columns;
  33. const uint8_t widths[4];
  34. const char* name;
  35. const char* description;
  36. } HeaderLayout_layouts[LAST_HEADER_LAYOUT] = {
  37. [HF_ONE_100] = { 1, { 100, 0, 0, 0 }, "one_100", "1 column - full width", },
  38. [HF_TWO_50_50] = { 2, { 50, 50, 0, 0 }, "two_50_50", "2 columns - 50/50 (default)", },
  39. [HF_TWO_33_67] = { 2, { 33, 67, 0, 0 }, "two_33_67", "2 columns - 33/67", },
  40. [HF_TWO_67_33] = { 2, { 67, 33, 0, 0 }, "two_67_33", "2 columns - 67/33", },
  41. [HF_THREE_33_34_33] = { 3, { 33, 34, 33, 0 }, "three_33_34_33", "3 columns - 33/34/33", },
  42. [HF_THREE_25_25_50] = { 3, { 25, 25, 50, 0 }, "three_25_25_50", "3 columns - 25/25/50", },
  43. [HF_THREE_25_50_25] = { 3, { 25, 50, 25, 0 }, "three_25_50_25", "3 columns - 25/50/25", },
  44. [HF_THREE_50_25_25] = { 3, { 50, 25, 25, 0 }, "three_50_25_25", "3 columns - 50/25/25", },
  45. [HF_THREE_40_30_30] = { 3, { 40, 30, 30, 0 }, "three_40_30_30", "3 columns - 40/30/30", },
  46. [HF_THREE_30_40_30] = { 3, { 30, 40, 30, 0 }, "three_30_40_30", "3 columns - 30/40/30", },
  47. [HF_THREE_30_30_40] = { 3, { 30, 30, 40, 0 }, "three_30_30_40", "3 columns - 30/30/40", },
  48. [HF_THREE_40_20_40] = { 3, { 40, 20, 40, 0 }, "three_40_20_40", "3 columns - 40/20/40", },
  49. [HF_FOUR_25_25_25_25] = { 4, { 25, 25, 25, 25 }, "four_25_25_25_25", "4 columns - 25/25/25/25", },
  50. };
  51. static inline size_t HeaderLayout_getColumns(HeaderLayout hLayout) {
  52. /* assert the layout is initialized */
  53. assert(0 <= hLayout);
  54. assert(hLayout < LAST_HEADER_LAYOUT);
  55. assert(HeaderLayout_layouts[hLayout].name[0]);
  56. assert(HeaderLayout_layouts[hLayout].description[0]);
  57. return HeaderLayout_layouts[hLayout].columns;
  58. }
  59. static inline const char* HeaderLayout_getName(HeaderLayout hLayout) {
  60. /* assert the layout is initialized */
  61. assert(0 <= hLayout);
  62. assert(hLayout < LAST_HEADER_LAYOUT);
  63. assert(HeaderLayout_layouts[hLayout].name[0]);
  64. assert(HeaderLayout_layouts[hLayout].description[0]);
  65. return HeaderLayout_layouts[hLayout].name;
  66. }
  67. static inline HeaderLayout HeaderLayout_fromName(const char* name) {
  68. for (size_t i = 0; i < LAST_HEADER_LAYOUT; i++) {
  69. if (String_eq(HeaderLayout_layouts[i].name, name))
  70. return (HeaderLayout) i;
  71. }
  72. return LAST_HEADER_LAYOUT;
  73. }
  74. #endif /* HEADER_HeaderLayout */