Skip to content

Commit aef7bb0

Browse files
committed
fix: add formatter
1 parent 64c827f commit aef7bb0

8 files changed

Lines changed: 475 additions & 264 deletions

File tree

.clang-format

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
Language: C
3+
BasedOnStyle: LLVM
4+
5+
# Indentation
6+
IndentWidth: 4
7+
TabWidth: 4
8+
UseTab: Never
9+
IndentCaseLabels: true
10+
IndentPPDirectives: BeforeHash
11+
NamespaceIndentation: None
12+
13+
# Alignment
14+
AlignConsecutiveAssignments: true
15+
AlignConsecutiveDeclarations: true
16+
AlignConsecutiveMacros: true
17+
AlignConsecutiveBitFields: true
18+
AlignEscapedNewlines: Left
19+
AlignOperands: true
20+
AlignTrailingComments: true
21+
22+
# Braces
23+
BreakBeforeBraces: Allman
24+
25+
# Line Breaking
26+
ColumnLimit: 80
27+
AllowShortBlocksOnASingleLine: Empty
28+
AllowShortFunctionsOnASingleLine: Inline
29+
AllowShortIfStatementsOnASingleLine: Never
30+
AllowShortLoopsOnASingleLine: false
31+
AllowShortCaseLabelsOnASingleLine: false
32+
BreakBeforeBinaryOperators: None
33+
BreakBeforeTernaryOperators: true
34+
35+
# Spaces
36+
SpaceAfterCStyleCast: false
37+
SpaceAfterLogicalNot: false
38+
SpaceBeforeAssignmentOperators: true
39+
SpaceBeforeParens: ControlStatements
40+
SpaceInEmptyBlock: false
41+
SpacesInParentheses: false
42+
SpacesInSquareBrackets: false
43+
SpacesInCStyleCastParentheses: false
44+
45+
# Pointers & References
46+
PointerAlignment: Right
47+
ReferenceAlignment: Pointer
48+
DerivePointerAlignment: false
49+
50+
# Includes
51+
SortIncludes: true
52+
IncludeBlocks: Regroup
53+
54+
# Other
55+
ReflowComments: true
56+
InsertBraces: true
57+
MaxEmptyLinesToKeep: 1
58+
SeparateDefinitionBlocks: Always

src/display.c

Lines changed: 103 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,75 +5,101 @@
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

6385
void 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

115152
void 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

121159
void 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
}

src/display.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,30 @@
1111
#include "memory.h"
1212
#include "utils.h"
1313

14-
/* ============================================================================
14+
/*
15+
* ============================================================================
1516
* Constants
16-
* ============================================================================ */
17+
* ============================================================================
18+
*/
1719

1820
/* Column widths for formatting */
19-
#define COL_WIDTH_LABEL 5
20-
#define COL_WIDTH_VALUE 12
21-
#define COL_WIDTH_HUMAN 10
21+
#define COL_WIDTH_LABEL 5
22+
#define COL_WIDTH_VALUE 12
23+
#define COL_WIDTH_HUMAN 10
2224

2325
/* ANSI color codes (optional) */
24-
#define COLOR_RESET "\033[0m"
25-
#define COLOR_BOLD "\033[1m"
26-
#define COLOR_RED "\033[31m"
27-
#define COLOR_GREEN "\033[32m"
28-
#define COLOR_YELLOW "\033[33m"
29-
#define COLOR_BLUE "\033[34m"
26+
#define COLOR_RESET "\033[0m"
27+
#define COLOR_BOLD "\033[1m"
28+
#define COLOR_RED "\033[31m"
29+
#define COLOR_GREEN "\033[32m"
30+
#define COLOR_YELLOW "\033[33m"
31+
#define COLOR_BLUE "\033[34m"
3032

31-
/* ============================================================================
33+
/*
34+
* ============================================================================
3235
* Function Prototypes
33-
* ============================================================================ */
36+
* ============================================================================
37+
*/
3438

3539
/**
3640
* Print memory information header

0 commit comments

Comments
 (0)