Many additions and improvements

Basic previews, priority setting, better -m option
This commit is contained in:
Nikita Ivanov 2022-05-23 23:45:35 +05:00
parent 790566c72a
commit 8487750f1a
No known key found for this signature in database
GPG Key ID: 6E656AC5B97B5133
12 changed files with 77 additions and 41 deletions

10
ctpv.c
View File

@ -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;
}

1
prev/any.sh Normal file
View File

@ -0,0 +1 @@
exiftool "$f" || true

View File

@ -1 +0,0 @@
echo "$f"

1
prev/json.sh Normal file
View File

@ -0,0 +1 @@
jq -C . "$f"

1
prev/markdown.sh Normal file
View File

@ -0,0 +1 @@
mdcat --columns "$w" "$f"

24
prev/text.sh Normal file
View File

@ -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

4
prev/wrapper.sh Normal file
View File

@ -0,0 +1,4 @@
[ -L "$f" ] && printf 'Symlink: \n%s\n\n' "$(readlink "$f")"
# Pretend that preview failed so another is run
exit 127

View File

@ -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;

View File

@ -4,7 +4,8 @@
#include <stdlib.h>
typedef struct {
char *ext, *type, *subtype, *script;
char *name, *ext, *type, *subtype, *script;
int priority;
} Preview;
typedef struct {

View File

@ -1,4 +1,5 @@
#include <stdlib.h>
#include <limits.h>
#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

12
utils.c
View File

@ -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;

View File

@ -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);