From dc8b65f269fabbe6322934ae1011db1845d38414 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sun, 23 Dec 2018 22:58:23 -0500 Subject: [PATCH] implemented bare minimum file picker --- inc/x11.h | 7 +++++++ src/pick.c | 43 ++++++++++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/inc/x11.h b/inc/x11.h index 33fbcee..f416a6e 100644 --- a/inc/x11.h +++ b/inc/x11.h @@ -185,4 +185,11 @@ static KeySym x11_getkey(XConf* x, XEvent* e) { return key; } +static void x11_draw_string(XConf* x, XftFont* font, int posx, int posy, int color, char* str) { + XftColor clr; + xftcolor(x, &clr, color); + XftDrawStringUtf8(x->xft, &clr, font, posx, posy, (const FcChar8*)str, strlen(str)); + XftColorFree(x->display, x->visual, x->colormap, &clr); +} + #pragma GCC diagnostic pop diff --git a/src/pick.c b/src/pick.c index 743c93a..5f59fe4 100644 --- a/src/pick.c +++ b/src/pick.c @@ -16,7 +16,8 @@ typedef struct { char Query[8192] = {0}; size_t QueryIdx = 0; vec_t Choices = {0}; -size_t ChoiceIdx = 2; +size_t ChoiceIdx = 0; +size_t Offset = 0; static char* rdline(FILE* fin) { if (feof(fin) || ferror(fin)) @@ -117,19 +118,25 @@ static void xkeypress(XConf* x, XEvent* e) { x->running = false; ChoiceIdx = SIZE_MAX; } else if (key == XK_Up) { - if (ChoiceIdx <= vec_size(&Choices)) - ChoiceIdx++; - } else if (key == XK_Down) { if (ChoiceIdx > 0) ChoiceIdx--; + if (ChoiceIdx < Offset) + Offset--; + } else if (key == XK_Down) { + size_t nlines = ((x->height - x->font->height - 4) / x->font->height) - 1; + size_t maxidx = Offset + nlines; + if (ChoiceIdx+1 < vec_size(&Choices)) + ChoiceIdx++; + if (ChoiceIdx > maxidx) + Offset++; } else if (key == XK_BackSpace) { if (QueryIdx > 0) Query[--QueryIdx] = '\0'; } else if (key >= 0x20 && key <= 0x7F) { if (QueryIdx < sizeof(Query)-1) Query[QueryIdx++] = key; - score(); } + score(); } static void xbtnpress(XConf* x, XEvent* e) { @@ -146,21 +153,35 @@ static void xresize(XConf* x, XEvent* e) { } static void redraw(XConf* x) { + /* draw the background colors and border */ size_t fheight = x->font->height; x11_draw_rect(x, Palette[EditBg], 0, 0, x->width, x->height); x11_draw_rect(x, Palette[TagsBg], 0, 0, x->width, fheight + 4); x11_draw_rect(x, Palette[HorBdr], 0, fheight + 4, x->width, 1); + /* get size of the query when printed */ XGlyphInfo glyphinfo; - XftTextExtentsUtf8 (x->display, x->font, (const FcChar8*)Query, strlen(Query), &glyphinfo); + XftTextExtentsUtf8(x->display, x->font, (const FcChar8*)Query, strlen(Query), &glyphinfo); int offset = 0; if (glyphinfo.width >= (x->width - 4)) offset = ((x->width - 4) - glyphinfo.width); - XftColor clr; - xftcolor(x, &clr, Palette[TagsFg]); - XftDrawStringUtf8(x->xft, &clr, x->font, offset + 2, fheight, (const FcChar8*)Query, strlen(Query)); - XftColorFree(x->display, x->visual, x->colormap, &clr); + /* draw the query and the cursor to the query region */ + int posx = 2 + glyphinfo.width + offset; + x11_draw_rect(x, Palette[TagsCsr], posx-1, 2, 3, 3); + x11_draw_rect(x, Palette[TagsCsr], posx, 2, 1, fheight); + x11_draw_rect(x, Palette[TagsCsr], posx-1, 2+fheight-3, 3, 3); + x11_draw_string(x, x->font, offset + 2, fheight, Palette[TagsFg], Query); + + /* draw the scored and sorted results */ + size_t nlines = ((x->height - x->font->height - 4) / x->font->height) - 1; + for (int i = Offset, y = 2 * fheight + 4; ((size_t)i < vec_size(&Choices)) && ((size_t)i <= Offset+nlines); i++, y += fheight) { + bool selected = ((size_t)i == ChoiceIdx); + Choice* choice = (Choice*)vec_at(&Choices, i); + if (selected) + x11_draw_rect(x, Palette[EditSel], 0, y - x->font->ascent, x->width, fheight); + x11_draw_string(x, x->font, 2, y, Palette[TagsFg], choice->string); + } x11_flip(x); } @@ -190,7 +211,7 @@ int main(void) { load_choices(); if (vec_size(&Choices) > 1) filter(); - Choice* choice = (Choice*)vec_at(&Choices, ChoiceIdx-2); + Choice* choice = (Choice*)vec_at(&Choices, ChoiceIdx); if (vec_size(&Choices) && ChoiceIdx != SIZE_MAX) printf("%s\n", choice->string); return 0; -- 2.52.0