55 * License: MIT
66 */
77
8+ #include "display.h"
9+
810#include <stdio.h>
911#include <string.h>
10- #include "display.h"
1112
12- /* ============================================================================
13+ /*
14+ * ============================================================================
1315 * Helper Functions
14- * ============================================================================ */
16+ * ============================================================================
17+ */
1518
16- static void print_value (uint64_t bytes , const options_t * opts ) {
17- if (opts -> unit == UNIT_HUMAN ) {
19+ static void print_value (uint64_t bytes , const options_t * opts )
20+ {
21+ if (opts -> unit == UNIT_HUMAN )
22+ {
1823 char buf [32 ];
1924 format_human (bytes , buf , sizeof (buf ));
2025 printf (" %11s" , buf );
21- } else {
26+ }
27+ else
28+ {
2229 double val = convert_unit (bytes , opts -> unit );
23- if (opts -> unit == UNIT_BYTES ) {
30+ if (opts -> unit == UNIT_BYTES )
31+ {
2432 printf (" %12llu" , (unsigned long long )bytes );
25- } else {
33+ }
34+ else
35+ {
2636 printf (" %12.0f" , val );
2737 }
2838 }
2939}
3040
31- /* ============================================================================
41+ /*
42+ * ============================================================================
3243 * Header Functions
33- * ============================================================================ */
34-
35- void print_header ( const options_t * opts ) {
36- if ( opts -> unit == UNIT_HUMAN ) {
37- if ( opts -> wide ) {
38- printf ( "%-7s %11s %11s %11s %11s %11s %11s %11s %11s\n" ,
39- "" , "total" , "used" , "free" , "active" , "inactive" ,
40- "wired" , "compressed" , "available" );
41- } else {
42- printf ("%-7s %11s %11s %11s %11s %11s %11s\n " ,
43- "" , "total " , "used " , "free " , "shared " , "buff/cache " ,
44- "available" );
44+ * ============================================================================
45+ */
46+
47+ void print_header ( const options_t * opts )
48+ {
49+ if ( opts -> unit == UNIT_HUMAN )
50+ {
51+ if ( opts -> wide )
52+ {
53+ printf ("%-7s %11s %11s %11s %11s %11s %11s %11s %11s\n" , " " ,
54+ "total " , "used " , "free " , "active " , "inactive " , "wired " ,
55+ "compressed" , " available" );
4556 }
46- } else {
47- if (opts -> wide ) {
48- printf ("%-7s %12s %12s %12s %12s %12s %12s %12s %12s\n" ,
49- "" , "total" , "used" , "free" , "active" , "inactive" ,
50- "wired" , "compressed" , "available" );
51- } else {
52- printf ("%-7s %12s %12s %12s %12s %12s %12s\n" ,
53- "" , "total" , "used" , "free" , "shared" , "buff/cache" ,
54- "available" );
57+ else
58+ {
59+ printf ("%-7s %11s %11s %11s %11s %11s %11s\n" , "" , "total" , "used" ,
60+ "free" , "shared" , "buff/cache" , "available" );
61+ }
62+ }
63+ else
64+ {
65+ if (opts -> wide )
66+ {
67+ printf ("%-7s %12s %12s %12s %12s %12s %12s %12s %12s\n" , "" ,
68+ "total" , "used" , "free" , "active" , "inactive" , "wired" ,
69+ "compressed" , "available" );
70+ }
71+ else
72+ {
73+ printf ("%-7s %12s %12s %12s %12s %12s %12s\n" , "" , "total" , "used" ,
74+ "free" , "shared" , "buff/cache" , "available" );
5575 }
5676 }
5777}
5878
59- /* ============================================================================
79+ /*
80+ * ============================================================================
6081 * Display Functions
61- * ============================================================================ */
82+ * ============================================================================
83+ */
6284
6385void print_numeric (const mem_info_t * mem , const swap_info_t * swap ,
64- const options_t * opts ) {
86+ const options_t * opts )
87+ {
6588 /* Print memory row */
6689 printf ("%-7s" , "Mem:" );
6790 print_value (mem -> total , opts );
6891 print_value (mem -> used , opts );
6992 print_value (mem -> free , opts );
7093
71- if (opts -> wide ) {
94+ if (opts -> wide )
95+ {
7296 print_value (mem -> active , opts );
7397 print_value (mem -> inactive , opts );
7498 print_value (mem -> wired , opts );
7599 print_value (mem -> compressed , opts );
76- } else {
100+ }
101+ else
102+ {
77103 /* "shared" - using compressed as approximation */
78104 print_value (mem -> compressed , opts );
79105 /* "buff/cache" */
@@ -88,23 +114,34 @@ void print_numeric(const mem_info_t *mem, const swap_info_t *swap,
88114 print_value (swap -> used , opts );
89115 print_value (swap -> free , opts );
90116
91- if (opts -> wide ) {
117+ if (opts -> wide )
118+ {
92119 /* Pad remaining columns for alignment */
93120 int num_cols = 5 ;
94- for (int i = 0 ; i < num_cols ; i ++ ) {
95- if (opts -> unit == UNIT_HUMAN ) {
121+ for (int i = 0 ; i < num_cols ; i ++ )
122+ {
123+ if (opts -> unit == UNIT_HUMAN )
124+ {
96125 printf (" %11s" , "" );
97- } else {
126+ }
127+ else
128+ {
98129 printf (" %12s" , "" );
99130 }
100131 }
101- } else {
132+ }
133+ else
134+ {
102135 /* Pad remaining columns */
103136 int num_cols = 3 ;
104- for (int i = 0 ; i < num_cols ; i ++ ) {
105- if (opts -> unit == UNIT_HUMAN ) {
137+ for (int i = 0 ; i < num_cols ; i ++ )
138+ {
139+ if (opts -> unit == UNIT_HUMAN )
140+ {
106141 printf (" %11s" , "" );
107- } else {
142+ }
143+ else
144+ {
108145 printf (" %12s" , "" );
109146 }
110147 }
@@ -113,14 +150,16 @@ void print_numeric(const mem_info_t *mem, const swap_info_t *swap,
113150}
114151
115152void print_human (const mem_info_t * mem , const swap_info_t * swap ,
116- const options_t * opts ) {
153+ const options_t * opts )
154+ {
117155 /* Human format uses same function, just with UNIT_HUMAN */
118156 print_numeric (mem , swap , opts );
119157}
120158
121159void print_totals (const mem_info_t * mem , const swap_info_t * swap ,
122- const options_t * opts ) {
123- uint64_t total_mem = mem -> total + swap -> total ;
160+ const options_t * opts )
161+ {
162+ uint64_t total_mem = mem -> total + swap -> total ;
124163 uint64_t total_used = mem -> used + swap -> used ;
125164 uint64_t total_free = mem -> free + swap -> free ;
126165
@@ -131,30 +170,40 @@ void print_totals(const mem_info_t *mem, const swap_info_t *swap,
131170 printf ("\n" );
132171}
133172
134- void print_separator (const options_t * opts ) {
173+ void print_separator (const options_t * opts )
174+ {
135175 int width ;
136- if (opts -> wide ) {
176+ if (opts -> wide )
177+ {
137178 width = opts -> unit == UNIT_HUMAN ? 97 : 115 ;
138- } else {
179+ }
180+ else
181+ {
139182 width = opts -> unit == UNIT_HUMAN ? 67 : 79 ;
140183 }
141184
142- for (int i = 0 ; i < width ; i ++ ) {
185+ for (int i = 0 ; i < width ; i ++ )
186+ {
143187 putchar ('-' );
144188 }
145189 putchar ('\n' );
146190}
147191
148- void print_memory_info (const system_memory_t * sys_mem , const options_t * opts ) {
192+ void print_memory_info (const system_memory_t * sys_mem , const options_t * opts )
193+ {
149194 print_header (opts );
150195
151- if (opts -> unit == UNIT_HUMAN ) {
196+ if (opts -> unit == UNIT_HUMAN )
197+ {
152198 print_human (& sys_mem -> mem , & sys_mem -> swap , opts );
153- } else {
199+ }
200+ else
201+ {
154202 print_numeric (& sys_mem -> mem , & sys_mem -> swap , opts );
155203 }
156204
157- if (opts -> totals ) {
205+ if (opts -> totals )
206+ {
158207 print_totals (& sys_mem -> mem , & sys_mem -> swap , opts );
159208 }
160209}
0 commit comments