Skip to content

Commit

Permalink
added indention size detection
Browse files Browse the repository at this point in the history
  • Loading branch information
likle committed Mar 12, 2022
1 parent 09b54e0 commit 5c087de
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<img style="width:100%;" src="banner.png">

[![Travis Build](https://img.shields.io/travis/com/likle/cargs/master.svg?label=Linux%20%26%20MacOS)](https://app.travis-ci.com/github/likle/cargs)
[![Travis Build](https://img.shields.io/travis/com/likle/cargs/master?label=Linux%2C%20macOS%20%26%20FreeBSD)](https://app.travis-ci.com/github/likle/cargs)
[![Appveyor Build](https://img.shields.io/appveyor/ci/likle/cargs/master.svg?label=Windows)](https://ci.appveyor.com/project/likle/cargs)
[![codecov](https://img.shields.io/codecov/c/github/likle/cargs/master.svg?label=Coverage)](https://codecov.io/gh/likle/cargs)
[![Language Grade: C/C++](https://img.shields.io/lgtm/grade/cpp/g/likle/cargs.svg?label=Code%20Quality)](https://lgtm.com/projects/g/likle/cargs/context:cpp)
Expand Down
48 changes: 42 additions & 6 deletions src/cargs.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
#include <stdio.h>
#include <string.h>

#define CAG_OPTION_PRINT_DISTANCE 4
#define CAG_OPTION_PRINT_MIN_INDENTION 20

static void cag_option_print_value(const cag_option *option,
int *accessor_length, FILE *destination)
size_t *accessor_length, FILE *destination)
{
if (option->value_name != NULL) {
*accessor_length += fprintf(destination, "=%s", option->value_name);
}
}

static void cag_option_print_letters(const cag_option *option, bool *first,
int *accessor_length, FILE *destination)
size_t *accessor_length, FILE *destination)
{
const char *access_letter;
access_letter = option->access_letters;
Expand All @@ -31,7 +34,7 @@ static void cag_option_print_letters(const cag_option *option, bool *first,
}

static void cag_option_print_name(const cag_option *option, bool *first,
int *accessor_length, FILE *destination)
size_t *accessor_length, FILE *destination)
{
if (option->access_name != NULL) {
if (*first) {
Expand All @@ -42,13 +45,46 @@ static void cag_option_print_name(const cag_option *option, bool *first,
}
}

static size_t cag_option_get_print_indention(const cag_option *options,
size_t option_count)
{
size_t option_index, indention, result;
const cag_option *option;

result = CAG_OPTION_PRINT_MIN_INDENTION;

for (option_index = 0; option_index < option_count; ++option_index) {
indention = CAG_OPTION_PRINT_DISTANCE;
option = &options[option_index];
if (option->access_letters != NULL && *option->access_letters) {
indention += strlen(option->access_letters) * 4 - 2;
if (option->access_name != NULL) {
indention += strlen(option->access_name) + 4;
}
} else if (option->access_name != NULL) {
indention += strlen(option->access_name) + 2;
}

if (option->value_name != NULL) {
indention += strlen(option->value_name) + 1;
}

if (indention > result) {
result = indention;
}
}

return result;
}

void cag_option_print(const cag_option *options, size_t option_count,
FILE *destination)
{
size_t option_index;
size_t option_index, indention, i, accessor_length;
const cag_option *option;
bool first;
int i, accessor_length;

indention = cag_option_get_print_indention(options, option_count);

for (option_index = 0; option_index < option_count; ++option_index) {
option = &options[option_index];
Expand All @@ -61,7 +97,7 @@ void cag_option_print(const cag_option *options, size_t option_count,
cag_option_print_name(option, &first, &accessor_length, destination);
cag_option_print_value(option, &accessor_length, destination);

for (i = accessor_length; i < 20; ++i) {
for (i = accessor_length; i < indention; ++i) {
fputs(" ", destination);
}

Expand Down
7 changes: 1 addition & 6 deletions test/option_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -627,18 +627,15 @@ int option_print(void)
{
char buf[255];
const char *expected;
const char *test_file_name;
FILE *test_file;

test_file_name = "option_print.out";
expected = " -s Simple flag\n"
" -a Another simple flag\n"
" -m, -M, -o, -O Multiple access letters\n"
" --long Long parameter name\n"
" -k, --key=VALUE Parameter value\n";

remove(test_file_name);
test_file = fopen(test_file_name, "w+");
test_file = tmpfile();
if (test_file == NULL) {
goto err_open;
}
Expand All @@ -656,14 +653,12 @@ int option_print(void)
goto err_test;
}

remove(test_file_name);
fclose(test_file);
return EXIT_SUCCESS;

err_test:
err_read:
err_seek:
remove(test_file_name);
fclose(test_file);
err_open:
return EXIT_FAILURE;
Expand Down

0 comments on commit 5c087de

Please sign in to comment.