diff --git a/ctpv.c b/ctpv.c index 938b00c..e7acf07 100644 --- a/ctpv.c +++ b/ctpv.c @@ -8,6 +8,7 @@ #include #include "error.h" +#include "utils.h" #include "server.h" #include "preview.h" #include "previews.h" @@ -211,9 +212,34 @@ static int list(void) size_t len; Preview p, **list = get_previews_list(&len); - const char *e, *t, *s; + const char *n, *e, *t, *s; + + const char header_name[] = "Name", header_ext[] = "Extension", + header_mime[] = "MIME type"; + + int width_name = 0, width_ext = 0; + + for (size_t i = 0; i < len + 1; i++) { + if (i < len) { + p = *list[i]; + n = p.name; + e = p.ext; + } else { + n = header_name; + e = header_ext; + } + + int name_len = strlennull(n); + int ext_len = strlennull(e); + width_name = MAX(width_name, name_len); + width_ext = MAX(width_ext, ext_len); + } + + width_name += 2, width_ext += 2; puts("List of available previews:"); + printf("\t%-*s %-*s %s\n", width_name, header_name, width_ext, header_ext, + header_mime); for (size_t i = 0; i < len; i++) { p = *list[i]; @@ -231,10 +257,12 @@ static int list(void) s = any_type; } - printf("\t%-15s .%-6s %s/%s\n", p.name, e, t, s); + printf("\t%-*s .%-*s %s/%s\n", width_name, p.name, width_ext - 1, e, t, + s); } puts("\nNote: '" ANY_TYPE "' means that it matches any."); + return OK; } diff --git a/utils.c b/utils.c index bf4d775..49222fb 100644 --- a/utils.c +++ b/utils.c @@ -82,6 +82,11 @@ int strcmpnull(char const *s1, char const *s2) return strcmp(s1, s2); } +int strlennull(char const *s) +{ + return s ? strlen(s) : 0; +} + int get_cache_dir(char *buf, size_t len, char *name) { char *home, *cache_d, cache_d_buf[FILENAME_MAX]; diff --git a/utils.h b/utils.h index 16bb720..58fa8b0 100644 --- a/utils.h +++ b/utils.h @@ -29,6 +29,8 @@ int spawn(char *args[], pid_t *cpid, int *exitcode, int (*cfunc)(const void *), const void *carg); int strcmpnull(char const *s1, char const *s2); +int strlennull(char const *s); + int get_cache_dir(char *buf, size_t len, char *name); int mkpath(char* file_path, mode_t mode);