diff --git a/ctpv.c b/ctpv.c index ac56ee9..e4e1506 100644 --- a/ctpv.c +++ b/ctpv.c @@ -118,15 +118,19 @@ static int list(void) size_t len; Preview p, **list = get_previews_list(&len); - const char *t, *s; + const char *e, *t, *s; puts("List of available previews:"); for (size_t i = 0; i < len; i++) { p = *list[i]; + e = p.ext; t = p.type; s = p.subtype; + if (!e) + e = any_type; + if (!t) { t = any_type; s = any_type; @@ -134,10 +138,10 @@ static int list(void) s = any_type; } - printf("\t%s/%s\n", t, s); + printf("\t%-15s .%-6s %s/%s\n", p.name, e, t, s); } - puts("\nNote: '" ANY_TYPE "' means that it matches any mimetype."); + puts("\nNote: '" ANY_TYPE "' means that it matches any."); return OK; } diff --git a/prev/any.sh b/prev/any.sh new file mode 100644 index 0000000..e5541f8 --- /dev/null +++ b/prev/any.sh @@ -0,0 +1 @@ +exiftool "$f" || true diff --git a/prev/file.sh b/prev/file.sh deleted file mode 100644 index 0639965..0000000 --- a/prev/file.sh +++ /dev/null @@ -1 +0,0 @@ -echo "$f" diff --git a/prev/json.sh b/prev/json.sh new file mode 100644 index 0000000..cd63650 --- /dev/null +++ b/prev/json.sh @@ -0,0 +1 @@ +jq -C . "$f" diff --git a/prev/markdown.sh b/prev/markdown.sh new file mode 100644 index 0000000..665333d --- /dev/null +++ b/prev/markdown.sh @@ -0,0 +1 @@ +mdcat --columns "$w" "$f" diff --git a/prev/text.sh b/prev/text.sh new file mode 100644 index 0000000..7223ef2 --- /dev/null +++ b/prev/text.sh @@ -0,0 +1,24 @@ +if exists bat; then + batcmd=bat +elif exists batcat; then + batcmd=batcat +else + batcmd= +fi + +if [ -n "$batcmd" ]; then + "$batcmd" --color always \ + --style plain \ + --paging never \ + --terminal-width "$w" \ + --wrap character \ + -- "$f" +elif exists highlight; then + highlight --replace-tabs=4 --out-format=ansi \ + --style='pablo' --force -- "$f" +elif exists source-highlight; then + source-highlight --tab=4 --out-format=esc \ + --style=esc256.style --failsafe -i "$f" +else + cat "$f" +fi diff --git a/prev/wrapper.sh b/prev/wrapper.sh new file mode 100644 index 0000000..17df139 --- /dev/null +++ b/prev/wrapper.sh @@ -0,0 +1,4 @@ +[ -L "$f" ] && printf 'Symlink: \n%s\n\n' "$(readlink "$f")" + +# Pretend that preview failed so another is run +exit 127 diff --git a/preview.c b/preview.c index 9a77324..b381590 100644 --- a/preview.c +++ b/preview.c @@ -18,31 +18,21 @@ static int cmp_previews(const void *p1, const void *p2) Preview *pr1 = *(Preview **)p1; Preview *pr2 = *(Preview **)p2; - if (pr1->ext && pr2->ext) - return strcmp(pr1->ext, pr2->ext); - else if (!pr1->ext && pr2->ext) - return 1; - else if (pr1->ext && !pr2->ext) - return -1; + int i; - if (!pr1->type && pr2->type) - return 1; - else if (pr1->type && !pr2->type) - return -1; - else if (!pr1->type && !pr2->type) - return 0; + if ((i = pr2->priority - pr1->priority) != 0) + return i; - if (!pr1->subtype && pr2->subtype) - return 1; - else if (pr1->subtype && !pr2->subtype) - return -1; + if ((i = strcmpnull(pr1->ext, pr2->ext)) != 0) + return -i; - int ret = strcmp(pr1->type, pr2->type); + if ((i = strcmpnull(pr1->type, pr2->type)) != 0) + return -i; - if (ret == 0 && pr1->subtype && pr2->subtype) - return strcmp(pr1->subtype, pr2->subtype); + if ((i = strcmpnull(pr1->subtype, pr2->subtype)) != 0) + return i; - return ret; + return i; } void init_previews(Preview *ps, size_t len) @@ -99,23 +89,16 @@ static Preview *find_preview(char const *mimetype, char const *ext, size_t *i) for (; *i < prevs_len; (*i)++) { p = prevs[*i]; - if (!p->ext && !p->type) - return p; - - if (p->ext && !ext) + if (p->ext && strcmpnull(p->ext, ext) != 0) continue; - if (p->ext && strcmp(ext, p->ext) == 0) - return p; - - if (p->type && strcmp(t, p->type) != 0) + if (p->type && strcmpnull(p->type, t) != 0) continue; - if (p->type && !p->subtype) - return p; + if (p->subtype && strcmpnull(p->subtype, s) != 0) + continue; - if (p->subtype && strcmp(s, p->subtype) == 0) - return p; + return p; } return NULL; diff --git a/preview.h b/preview.h index 012acef..ef9cf0a 100644 --- a/preview.h +++ b/preview.h @@ -4,7 +4,8 @@ #include typedef struct { - char *ext, *type, *subtype, *script; + char *name, *ext, *type, *subtype, *script; + int priority; } Preview; typedef struct { diff --git a/previews.h b/previews.h index ad49096..ce7f596 100644 --- a/previews.h +++ b/previews.h @@ -1,4 +1,5 @@ #include +#include #include "gen/prev/scripts.h" #include "preview.h" @@ -7,10 +8,13 @@ * This file is supposed to be included in ctpv.c */ -#define P(e, t, s, f) { e, t, s, f } +#define PP(e, t, s, n, p) { #n, e, t, s, prev_scr_##n##_sh, p } +#define PR(e, t, s, n) PP(e, t, s, n, 0) Preview previews[] = { - P(NULL, "text", "plain", prev_scr_file_sh), + PP(NULL, NULL, NULL, wrapper, INT_MAX), + PR(NULL, "text", NULL, text), + PR(NULL, NULL, NULL, any), + PR("md", NULL, NULL, markdown), + PR(NULL, "application", "json", json), }; - -#undef P diff --git a/utils.c b/utils.c index d3e31cd..a9c7d6d 100644 --- a/utils.c +++ b/utils.c @@ -55,6 +55,18 @@ int spawn(char *args[], pid_t *cpid, int *exitcode, int *fds[2]) return OK; } +int strcmpnull(char const *s1, char const *s2) +{ + if (!s1 && !s2) + return 0; + else if (s1 && !s2) + return 1; + else if (!s1 && s2) + return -1; + + return strcmp(s1, s2); +} + CharVec char_v_new(size_t cap) { CharVec v; diff --git a/utils.h b/utils.h index 69bdc88..be40814 100644 --- a/utils.h +++ b/utils.h @@ -25,6 +25,8 @@ extern char *program; int spawn(char *args[], pid_t *cpid, int *exitcode, int *fds[2]); +int strcmpnull(char const *s1, char const *s2); + CharVec char_v_new(size_t cap); void char_v_append(CharVec *v, char c); void char_v_free(CharVec *v);