Skip to content

Commit

Permalink
bug fixes & prettier CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro-Salerno committed Nov 9, 2024
1 parent f2de386 commit f23f3bb
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 33 deletions.
2 changes: 0 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ This project is in very early development and I'm going to have very little time
- Add useful features to Plugin SDK (e.g., config loading and defaults using [src/common/config.c](src/common/config.c))
- Fix the probably million of bugs, crashes and undefined behaviour (e.g., `posix_fs_file_gettype` seems to never return `TM_FS_FILETYPE_REGULAR` wtf?)
- Add all commented commands in [src/common/cli/directives/lookup.c](src/common/cli/directives/lookup.c) (e.g., `update` should just download the package and reinstall it since we have no way to check the version, `sync`/`update-all` should do the same for repos and for packages (in this order), `remove-repo` should just remove the repo's directory, and so on)
- Make CLI prettier (**IMPORTANTE:** Must keep everything PORTABLE)
- Remove code repetition (especially with dynamically allocated arrays that I basically reimplemented everywhere because it was faster)
- Give OS-specific and OS-common implementations the ability to detect if something wrong is happening (e.g., segmentation faults, termination, etc.) and gracefully terminate the execution. Again, remember that this MUST BE PORTABLE, so if it makes use of any OS-specific features, it should be encapsulated in an interface under [include/os](include/os)
- POSIX: fix text in white terminals
- Add meta-recipes (recipes that allow people to download multiple packages at once)
- Update `.clang-format` to avoid atrocities such as `(0 == <\n>` in conditions
- Make includes more efficient (remove duplicates, make it clearer when something is included implicitly, etc.)
Expand Down
2 changes: 1 addition & 1 deletion src/common/cli/directives/commands/install.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ static bool gen_repos_list(char ***repos_list,
TM_FS_FILETYPE_REGULAR == rcp_file_type */) {
if (repos_buf_sz - 1 == i) {
repos_buf_sz *= 2;
repos = (char **)realloc(repos, repos_buf_sz);
repos = (char **)realloc(repos, repos_buf_sz * sizeof(char *));
mem_chkoom(repos);
}

Expand Down
89 changes: 60 additions & 29 deletions src/common/cli/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,61 @@

static bool last_is_newline = false;

static size_t print_word(char *word, size_t rem, size_t offset, csz_t csz) {
size_t len = strlen(word);

while (true) {
if (rem > len) {
printf("%s", word);

if (rem >= len + 1) {
printf(" ");
rem--;
}

rem -= len;
return rem;
}

puts("");
rem = csz.columns - offset;
cli_out_space(offset);
}

return rem;
}

static size_t aligned_putch(char c, size_t cwidth, size_t pad, size_t used) {
if (used == cwidth) {
cli_out_newline();
cli_out_space(pad);
used = pad;
}

putchar(c);
return used + 1;
}

static void aligned_vprintf(const char *fmt, va_list args, size_t pad) {
size_t used = pad;
size_t cwidth = os_console_get_sz().columns;

for (size_t i = 0; fmt[i]; i++) {
if ('%' == fmt[i] && 's' == fmt[i + 1]) {
char *str = va_arg(args, char *);

for (size_t j = 0; str[j]; j++) {
used = aligned_putch(str[j], cwidth, pad, used);
}

i++;
continue;
}

used = aligned_putch(fmt[i], cwidth, pad, used);
}
}

void cli_out_newline() {
if (!last_is_newline) {
puts("");
Expand All @@ -47,7 +102,7 @@ void cli_out_progress(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
os_console_set_color(TM_COLOR_TEXT, true);
vprintf(fmt, args);
aligned_vprintf(fmt, args, 3);
va_end(args);

os_console_set_color(TM_COLOR_RESET, false);
Expand All @@ -61,7 +116,7 @@ void cli_out_success(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
os_console_set_color(TM_COLOR_GREEN, true);
vprintf(fmt, args);
aligned_vprintf(fmt, args, 3);
va_end(args);

os_console_set_color(TM_COLOR_RESET, false);
Expand All @@ -76,7 +131,7 @@ void cli_out_error(const char *fmt, ...) {
va_start(args, fmt);
os_console_set_color(TM_COLOR_RED, true);
printf("ERROR: ");
vprintf(fmt, args);
aligned_vprintf(fmt, args, 10);
va_end(args);

os_console_set_color(TM_COLOR_RESET, false);
Expand All @@ -91,7 +146,7 @@ void cli_out_warning(const char *fmt, ...) {
va_start(args, fmt);
os_console_set_color(TM_COLOR_YELLOW, true);
printf("WARNING: ");
vprintf(fmt, args);
aligned_vprintf(fmt, args, 12);
va_end(args);

os_console_set_color(TM_COLOR_RESET, false);
Expand All @@ -105,7 +160,7 @@ void cli_out_prompt(const char *fmt, ...) {

va_list args;
va_start(args, fmt);
vprintf(fmt, args);
aligned_vprintf(fmt, args, 3);
va_end(args);

os_console_set_color(TM_COLOR_RESET, false);
Expand All @@ -120,30 +175,6 @@ void cli_out_space(size_t num) {
last_is_newline = false;
}

static size_t print_word(char *word, size_t rem, size_t offset, csz_t csz) {
size_t len = strlen(word);

while (true) {
if (rem > len) {
printf("%s", word);

if (rem >= len + 1) {
printf(" ");
rem--;
}

rem -= len;
return rem;
}

puts("");
rem = csz.columns - offset;
cli_out_space(offset);
}

return rem;
}

void cli_out_tab_words(size_t offset, const char *text, csz_t csz) {
char *buf = malloc(strlen(text) + 1);
strcpy(buf, text);
Expand Down
2 changes: 1 addition & 1 deletion src/os-common/posix/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void posix_console_set_color(color_t color, bool bold) {
ansi_color = 36;
break;
case TM_COLOR_TEXT:
ansi_color = 37;
ansi_color = 39;
break;
case TM_COLOR_RESET:
printf("\033[m");
Expand Down

0 comments on commit f23f3bb

Please sign in to comment.