Skip to content

Commit 3a41342

Browse files
authored
-h: Refactor out column width code (#5420)
1 parent 697bfa8 commit 3a41342

File tree

13 files changed

+51
-107
lines changed

13 files changed

+51
-107
lines changed

binrz/rz-test/rz-test.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,7 @@ static int help(bool verbose) {
8888
"-x", "[num]", "Number of expected failed tests",
8989
// clang-format on
9090
};
91-
size_t maxFlagAndArgLength = 0;
92-
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
93-
size_t flagLength = strlen(options[i]);
94-
size_t argLength = strlen(options[i + 1]);
95-
size_t flagAndArgLength = flagLength + argLength;
96-
if (flagAndArgLength > maxFlagAndArgLength) {
97-
maxFlagAndArgLength = flagAndArgLength;
98-
}
99-
}
91+
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), NULL);
10092
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
10193
if (i + 1 < RZ_ARRAY_SIZE(options)) {
10294
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength);

librz/include/rz_util/rz_print.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ RZ_API RZ_OWN char *rz_print_json_indent(RZ_NULLABLE const char *s, bool color,
234234
RZ_API char *rz_print_json_human(const char *s);
235235

236236
RZ_API RZ_OWN RzStrBuf *rz_print_colorize_asm_str(RZ_BORROW RzPrint *p, const RzAsmTokenString *toks);
237+
RZ_API size_t rz_print_options_get_max_len(const char **options, size_t options_len,
238+
RZ_NULLABLE size_t *maxDescLength);
237239
RZ_API void rz_print_colored_help_option(const char *flag, const char *arg, const char *desc,
238240
size_t maxFlagAndArgLength);
239241
RZ_API void rz_print_colored_help_option_example(RZ_NULLABLE const char *flag, RZ_NULLABLE const char *arg,

librz/main/rizin.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,7 @@ static int main_help(RZ_BORROW RZ_NONNULL RzCore *core, int line) {
139139
"-z, -zz", "", "Do not load strings or load them even in raw",
140140
// clang-format on
141141
};
142-
size_t maxFlagAndArgLength = 0;
143-
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
144-
size_t flagLength = strlen(options[i]);
145-
size_t argLength = strlen(options[i + 1]);
146-
size_t flagAndArgLength = flagLength + argLength;
147-
if (flagAndArgLength > maxFlagAndArgLength) {
148-
maxFlagAndArgLength = flagAndArgLength;
149-
}
150-
}
142+
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), NULL);
151143
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
152144
if (i + 1 < RZ_ARRAY_SIZE(options)) {
153145
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength);

librz/main/rz-asm.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,7 @@ static int rasm_show_help(int v) {
207207
// clang-format on
208208
};
209209
if (v != 1) {
210-
size_t maxFlagAndArgLength = 0;
211-
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
212-
size_t flagLength = strlen(options[i]);
213-
size_t argLength = strlen(options[i + 1]);
214-
size_t flagAndArgLength = flagLength + argLength;
215-
if (flagAndArgLength > maxFlagAndArgLength) {
216-
maxFlagAndArgLength = flagAndArgLength;
217-
}
218-
}
210+
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), NULL);
219211
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
220212
if (i + 1 < RZ_ARRAY_SIZE(options)) {
221213
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength);

librz/main/rz-ax.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -248,20 +248,8 @@ static int help(void) {
248248
#undef CF
249249
#undef CA
250250
#undef CR
251-
size_t maxFlagAndArgLength = 0;
252-
size_t maxDescLength = 0;
253-
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 4) {
254-
size_t flagLength = options[i] ? strlen(options[i]) : 0;
255-
size_t argLength = options[i + 1] ? strlen(options[i + 1]) : 0;
256-
size_t flagAndArgLength = flagLength + argLength;
257-
if (flagAndArgLength > maxFlagAndArgLength) {
258-
maxFlagAndArgLength = flagAndArgLength;
259-
}
260-
size_t descLength = strlen(options[i + 2]);
261-
if (descLength > maxDescLength) {
262-
maxDescLength = descLength;
263-
}
264-
}
251+
size_t maxDescLength = SIZE_MAX;
252+
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), &maxDescLength);
265253
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 4) {
266254
if (i + 1 < RZ_ARRAY_SIZE(options)) {
267255
rz_print_colored_help_option_example(options[i], options[i + 1], options[i + 2],

librz/main/rz-bin.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,7 @@ static int rzbin_show_help(int v) {
218218
"-Z", "", "Guess size of binary program",
219219
// clang-format on
220220
};
221-
size_t maxFlagAndArgLength = 0;
222-
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
223-
size_t flagLength = strlen(options[i]);
224-
size_t argLength = strlen(options[i + 1]);
225-
size_t flagAndArgLength = flagLength + argLength;
226-
if (flagAndArgLength > maxFlagAndArgLength) {
227-
maxFlagAndArgLength = flagAndArgLength;
228-
}
229-
}
221+
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), NULL);
230222
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
231223
if (i + 1 < RZ_ARRAY_SIZE(options)) {
232224
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength);

librz/main/rz-diff.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,7 @@ static void rz_diff_show_help(bool usage_only) {
243243
"", "", " symbols | compare symbols found in the files",
244244
// clang-format on
245245
};
246-
size_t maxFlagAndArgLength = 0;
247-
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
248-
size_t flagLength = strlen(options[i]);
249-
size_t argLength = strlen(options[i + 1]);
250-
size_t flagAndArgLength = flagLength + argLength;
251-
if (flagAndArgLength > maxFlagAndArgLength) {
252-
maxFlagAndArgLength = flagAndArgLength;
253-
}
254-
}
246+
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), NULL);
255247
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
256248
if (i + 1 < RZ_ARRAY_SIZE(options)) {
257249
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength);

librz/main/rz-find.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,7 @@ static int show_help(const char *argv0, int line) {
223223
"-Z", "", "Show string found on each search hit",
224224
// clang-format on
225225
};
226-
size_t maxFlagAndArgLength = 0;
227-
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
228-
size_t flagLength = strlen(options[i]);
229-
size_t argLength = strlen(options[i + 1]);
230-
size_t flagAndArgLength = flagLength + argLength;
231-
if (flagAndArgLength > maxFlagAndArgLength) {
232-
maxFlagAndArgLength = flagAndArgLength;
233-
}
234-
}
226+
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), NULL);
235227
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
236228
if (i + 1 < RZ_ARRAY_SIZE(options)) {
237229
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength);

librz/main/rz-gg.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,7 @@ static int usage(int v) {
5050
"-z", "" ,"Output in C string syntax",
5151
// clang-format on
5252
};
53-
size_t maxFlagAndArgLength = 0;
54-
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
55-
size_t flagLength = strlen(options[i]);
56-
size_t argLength = strlen(options[i + 1]);
57-
size_t flagAndArgLength = flagLength + argLength;
58-
if (flagAndArgLength > maxFlagAndArgLength) {
59-
maxFlagAndArgLength = flagAndArgLength;
60-
}
61-
}
53+
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), NULL);
6254
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
6355
if (i + 1 < RZ_ARRAY_SIZE(options)) {
6456
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength);

librz/main/rz-hash.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,7 @@ static void rz_hash_show_help(bool usage_only) {
109109
"", "", "If 's:' prefix is specified",
110110
// clang-format on
111111
};
112-
size_t maxFlagAndArgLength = 0;
113-
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
114-
size_t flagLength = strlen(options[i]);
115-
size_t argLength = strlen(options[i + 1]);
116-
size_t flagAndArgLength = flagLength + argLength;
117-
if (flagAndArgLength > maxFlagAndArgLength) {
118-
maxFlagAndArgLength = flagAndArgLength;
119-
}
120-
}
112+
size_t maxFlagAndArgLength = rz_print_options_get_max_len(options, RZ_ARRAY_SIZE(options), NULL);
121113
for (int i = 0; i < RZ_ARRAY_SIZE(options); i += 3) {
122114
if (i + 1 < RZ_ARRAY_SIZE(options)) {
123115
rz_print_colored_help_option(options[i], options[i + 1], options[i + 2], maxFlagAndArgLength);

0 commit comments

Comments
 (0)