Skip to content

Commit 322d9be

Browse files
authored
Allow users to right-pad labels (#14)
This allows users to align values by selecting how much they want to right-pad labels.
1 parent b5c02e2 commit 322d9be

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

args.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,45 @@
1414
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1515
*/
1616
#include <stdbool.h>
17+
#include <stdio.h>
18+
#include <stdlib.h>
1719
#include <string.h>
1820
#include "args.h"
1921

22+
int right_pad = -1;
2023
bool print_help = false;
2124
bool print_version = false;
2225

23-
void parse_args(int argc, char **argv) {
26+
bool parse_args(int argc, char **argv) {
27+
char *arg;
28+
char *end;
2429
for (int i = 1; i < argc; i++) {
25-
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
30+
arg = argv[i];
31+
if (strcmp(arg, "-p") == 0 || strcmp(arg, "--pad") == 0) {
32+
if (i == argc - 1) {
33+
fprintf(stderr, "--pad takes 1 argument\n");
34+
return false;
35+
}
36+
arg = argv[++i];
37+
// NOTE We're ignoring overflows (the safety check isn't a priority for this tool).
38+
right_pad = (int)strtol(arg, &end, 10);
39+
if (end == arg || right_pad < 0) {
40+
fprintf(stderr, "--pad expected a non-negative integer, got %s\n", arg);
41+
return false;
42+
}
43+
} else if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
2644
print_help = true;
27-
} else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
45+
} else if (strcmp(arg, "-v") == 0 || strcmp(arg, "--version") == 0) {
2846
print_version = true;
2947
}
3048
}
49+
return true;
3150
}
3251

3352
const char *help_message = "Usage: fetchfetch [OPTIONS...]\n"
3453
"Fetch the stats of your *fetch tools\n"
3554
"\n"
3655
"Options:\n"
56+
" -p, --pad N Right-pad labels by N spaces to align values\n"
3757
" -h, --help Print this message and exit\n"
3858
" -v, --version Print the version and exit\n";

args.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717
#define FETCH_FETCH_ARGS_H
1818
#include <stdbool.h>
1919

20+
extern int right_pad;
2021
extern bool print_help;
2122
extern bool print_version;
2223
extern const char *help_message;
2324

24-
void parse_args(int argc, char **argv);
25+
/**
26+
* Parses arguments. Returns true if parsing succeeded (and execution should continue),
27+
* false otherwise.
28+
*/
29+
bool parse_args(int argc, char **argv);
2530

2631
#endif

fetchfetch.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,24 @@
1313
* You should have received a copy of the GNU General Public License
1414
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1515
*/
16+
#include <stdbool.h>
1617
#include <stdio.h>
18+
#include <string.h>
1719
#include "args.h"
1820
#include "art.h"
1921
#include "stats.h"
2022
#include "version.h"
2123

2224
int main(int argc, char *argv[]) {
2325
FetchStat *stats;
24-
parse_args(argc, argv);
26+
bool ok = parse_args(argc, argv);
27+
const char *label;
28+
int label_len;
29+
int pad_iter;
2530

31+
if (!ok) {
32+
return 1;
33+
}
2634
if (print_help) {
2735
printf("%s", help_message);
2836
return 0;
@@ -44,7 +52,16 @@ int main(int argc, char *argv[]) {
4452

4553
// NOTE We skip a line because it looks a little better.
4654
if (line_index != 0 && line_index <= STATS_SIZE) {
47-
printf(" %s: %s", stats[line_index - 1].label, stats[line_index - 1].version);
55+
printf(" %s:", label = stats[line_index - 1].label);
56+
if (right_pad != -1) {
57+
label_len = strlen(label);
58+
for (pad_iter = label_len; pad_iter < right_pad; ++pad_iter) {
59+
putchar(' ');
60+
}
61+
} else {
62+
putchar(' ');
63+
}
64+
printf("%s", stats[line_index - 1].version);
4865
}
4966
printf("\n");
5067
}

0 commit comments

Comments
 (0)