mirror of
https://github.com/NikitaIvanovV/ctpv.git
synced 2025-01-22 07:58:34 +01:00
Many additions and improvements
Basic previews, priority setting, better -m option
This commit is contained in:
parent
790566c72a
commit
8487750f1a
10
ctpv.c
10
ctpv.c
@ -118,15 +118,19 @@ static int list(void)
|
|||||||
|
|
||||||
size_t len;
|
size_t len;
|
||||||
Preview p, **list = get_previews_list(&len);
|
Preview p, **list = get_previews_list(&len);
|
||||||
const char *t, *s;
|
const char *e, *t, *s;
|
||||||
|
|
||||||
puts("List of available previews:");
|
puts("List of available previews:");
|
||||||
|
|
||||||
for (size_t i = 0; i < len; i++) {
|
for (size_t i = 0; i < len; i++) {
|
||||||
p = *list[i];
|
p = *list[i];
|
||||||
|
e = p.ext;
|
||||||
t = p.type;
|
t = p.type;
|
||||||
s = p.subtype;
|
s = p.subtype;
|
||||||
|
|
||||||
|
if (!e)
|
||||||
|
e = any_type;
|
||||||
|
|
||||||
if (!t) {
|
if (!t) {
|
||||||
t = any_type;
|
t = any_type;
|
||||||
s = any_type;
|
s = any_type;
|
||||||
@ -134,10 +138,10 @@ static int list(void)
|
|||||||
s = any_type;
|
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;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1
prev/any.sh
Normal file
1
prev/any.sh
Normal file
@ -0,0 +1 @@
|
|||||||
|
exiftool "$f" || true
|
@ -1 +0,0 @@
|
|||||||
echo "$f"
|
|
1
prev/json.sh
Normal file
1
prev/json.sh
Normal file
@ -0,0 +1 @@
|
|||||||
|
jq -C . "$f"
|
1
prev/markdown.sh
Normal file
1
prev/markdown.sh
Normal file
@ -0,0 +1 @@
|
|||||||
|
mdcat --columns "$w" "$f"
|
24
prev/text.sh
Normal file
24
prev/text.sh
Normal 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
4
prev/wrapper.sh
Normal 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
|
47
preview.c
47
preview.c
@ -18,31 +18,21 @@ static int cmp_previews(const void *p1, const void *p2)
|
|||||||
Preview *pr1 = *(Preview **)p1;
|
Preview *pr1 = *(Preview **)p1;
|
||||||
Preview *pr2 = *(Preview **)p2;
|
Preview *pr2 = *(Preview **)p2;
|
||||||
|
|
||||||
if (pr1->ext && pr2->ext)
|
int i;
|
||||||
return strcmp(pr1->ext, pr2->ext);
|
|
||||||
else if (!pr1->ext && pr2->ext)
|
|
||||||
return 1;
|
|
||||||
else if (pr1->ext && !pr2->ext)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (!pr1->type && pr2->type)
|
if ((i = pr2->priority - pr1->priority) != 0)
|
||||||
return 1;
|
return i;
|
||||||
else if (pr1->type && !pr2->type)
|
|
||||||
return -1;
|
|
||||||
else if (!pr1->type && !pr2->type)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (!pr1->subtype && pr2->subtype)
|
if ((i = strcmpnull(pr1->ext, pr2->ext)) != 0)
|
||||||
return 1;
|
return -i;
|
||||||
else if (pr1->subtype && !pr2->subtype)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
int ret = strcmp(pr1->type, pr2->type);
|
if ((i = strcmpnull(pr1->type, pr2->type)) != 0)
|
||||||
|
return -i;
|
||||||
|
|
||||||
if (ret == 0 && pr1->subtype && pr2->subtype)
|
if ((i = strcmpnull(pr1->subtype, pr2->subtype)) != 0)
|
||||||
return strcmp(pr1->subtype, pr2->subtype);
|
return i;
|
||||||
|
|
||||||
return ret;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_previews(Preview *ps, size_t len)
|
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)++) {
|
for (; *i < prevs_len; (*i)++) {
|
||||||
p = prevs[*i];
|
p = prevs[*i];
|
||||||
|
|
||||||
if (!p->ext && !p->type)
|
if (p->ext && strcmpnull(p->ext, ext) != 0)
|
||||||
return p;
|
|
||||||
|
|
||||||
if (p->ext && !ext)
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (p->ext && strcmp(ext, p->ext) == 0)
|
if (p->type && strcmpnull(p->type, t) != 0)
|
||||||
return p;
|
|
||||||
|
|
||||||
if (p->type && strcmp(t, p->type) != 0)
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (p->type && !p->subtype)
|
if (p->subtype && strcmpnull(p->subtype, s) != 0)
|
||||||
return p;
|
continue;
|
||||||
|
|
||||||
if (p->subtype && strcmp(s, p->subtype) == 0)
|
return p;
|
||||||
return p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -4,7 +4,8 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char *ext, *type, *subtype, *script;
|
char *name, *ext, *type, *subtype, *script;
|
||||||
|
int priority;
|
||||||
} Preview;
|
} Preview;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
12
previews.h
12
previews.h
@ -1,4 +1,5 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
#include "gen/prev/scripts.h"
|
#include "gen/prev/scripts.h"
|
||||||
#include "preview.h"
|
#include "preview.h"
|
||||||
@ -7,10 +8,13 @@
|
|||||||
* This file is supposed to be included in ctpv.c
|
* 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[] = {
|
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
12
utils.c
@ -55,6 +55,18 @@ int spawn(char *args[], pid_t *cpid, int *exitcode, int *fds[2])
|
|||||||
return OK;
|
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 char_v_new(size_t cap)
|
||||||
{
|
{
|
||||||
CharVec v;
|
CharVec v;
|
||||||
|
2
utils.h
2
utils.h
@ -25,6 +25,8 @@ extern char *program;
|
|||||||
|
|
||||||
int spawn(char *args[], pid_t *cpid, int *exitcode, int *fds[2]);
|
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);
|
CharVec char_v_new(size_t cap);
|
||||||
void char_v_append(CharVec *v, char c);
|
void char_v_append(CharVec *v, char c);
|
||||||
void char_v_free(CharVec *v);
|
void char_v_free(CharVec *v);
|
||||||
|
Loading…
Reference in New Issue
Block a user