diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 29bd40a..688edbd 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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.) diff --git a/src/common/cli/directives/commands/install.c b/src/common/cli/directives/commands/install.c index 7fce7a0..e1ed3df 100644 --- a/src/common/cli/directives/commands/install.c +++ b/src/common/cli/directives/commands/install.c @@ -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); } diff --git a/src/common/cli/output.c b/src/common/cli/output.c index 0d3a67c..4262120 100644 --- a/src/common/cli/output.c +++ b/src/common/cli/output.c @@ -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(""); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/src/os-common/posix/console.c b/src/os-common/posix/console.c index 2019c0c..f8b21b5 100644 --- a/src/os-common/posix/console.c +++ b/src/os-common/posix/console.c @@ -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");