Add ability to change previews priority in config

This commit is contained in:
Nikita Ivanov 2022-06-09 10:54:37 +05:00
parent 8f2b3a403b
commit 0d5c32baf5
No known key found for this signature in database
GPG Key ID: 6E656AC5B97B5133
5 changed files with 46 additions and 13 deletions

View File

@ -10,7 +10,7 @@
*/
#define PNAME(n) prev_scr_##n##_sh
#define PP(e, t, s, n, p) { #n, e, t, s, PNAME(n), p, LEN(PNAME(n)) }
#define PP(e, t, s, n, o) { #n, e, t, s, PNAME(n), o, 0, LEN(PNAME(n)) }
#define PR(e, t, s, n) PP(e, t, s, n, 0)
Preview b_previews[] = {

View File

@ -50,12 +50,22 @@ static void add_preview(char *name, char *script, char *type, char *subtype,
.type = type,
.subtype = subtype,
.ext = ext,
.priority = 1 /* custom previews are always prioritized */
.order = 1 /* custom previews are always prioritized */
};
vectorPreview_append(previews, p);
}
static void add_priority(char *name, int priority)
{
for (size_t i = 0; i < previews->len; i++) {
if (strcmp(previews->buf[i].name, name) != 0)
continue;
previews->buf[i].priority = priority;
}
}
static inline void next_token(void)
{
token = lexer_get_token(lexer);
@ -151,8 +161,15 @@ static int new_preview(void)
static int priority(Token tok)
{
PARSEERROR(tok, "priority is not supported yet");
return STAT_ERR;
Token name = token;
EXPECT(TOK_STR);
Token number = token;
EXPECT(TOK_INT);
add_priority(get_str(name), number.val.i);
return STAT_OK;
}
static int command(void)

View File

@ -234,6 +234,11 @@ static inline Token read_new_line(Lexer *ctx)
return tok;
}
static inline int issymbol(int c)
{
return isalnum(c) || c == '_' || c == '-';
}
static inline Token read_symbol(Lexer *ctx)
{
char c = peek_char(ctx);
@ -242,7 +247,7 @@ static inline Token read_symbol(Lexer *ctx)
return get_tok(ctx, TOK_NULL);
size_t p = get_text_buf_len(ctx);
read_while(ctx, isalnum, 1);
read_while(ctx, issymbol, 1);
Token tok = get_tok(ctx, TOK_STR);
tok.val.sp = p;
@ -250,11 +255,16 @@ static inline Token read_symbol(Lexer *ctx)
return tok;
}
static inline Token read_digit(Lexer *ctx)
static inline Token read_int(Lexer *ctx)
{
char c = peek_char(ctx);
int positive = 1;
if (!isdigit(c))
if (peek_char(ctx) == '-') {
positive = 0;
next_char(ctx);
}
if (!isdigit(peek_char(ctx)))
return get_tok(ctx, TOK_NULL);
size_t len = get_text_buf_len(ctx);
@ -263,6 +273,9 @@ static inline Token read_digit(Lexer *ctx)
int i = atoi(get_text_buf_at(ctx, len));
set_text_buf_len(ctx, len);
if (!positive)
i *= -1;
Token tok = get_tok(ctx, TOK_INT);
tok.val.i = i;
@ -366,7 +379,7 @@ Token lexer_get_token(Lexer *ctx)
ATTEMPT_READ(ctx, read_new_line);
ATTEMPT_READ(ctx, read_symbol);
ATTEMPT_READ(ctx, read_digit);
ATTEMPT_READ(ctx, read_int);
ATTEMPT_READ(ctx, read_block);
PARSEERROR((*ctx), "cannot handle character: %c", peek_char(ctx));

View File

@ -25,7 +25,7 @@ static int cmp_previews(const void *p1, const void *p2)
int i;
if ((i = pr2->priority - pr1->priority) != 0)
if ((i = pr2->order - pr1->order) != 0)
return i;
if ((i = strcmpnull(pr1->ext, pr2->ext)) != 0)
@ -37,6 +37,9 @@ static int cmp_previews(const void *p1, const void *p2)
if ((i = strcmpnull(pr1->subtype, pr2->subtype)) != 0)
return i;
if ((i = pr2->priority - pr1->priority) != 0)
return i;
return i;
}

View File

@ -9,7 +9,7 @@
typedef struct {
char *name, *ext, *type, *subtype, *script;
int priority;
int order, priority;
size_t script_len;
} Preview;