summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c45
-rw-r--r--ui.c12
-rw-r--r--ui.h5
3 files changed, 41 insertions, 21 deletions
diff --git a/main.c b/main.c
index 111aeed..e48326b 100644
--- a/main.c
+++ b/main.c
@@ -2,7 +2,9 @@
* a simple fuzzy-selection menu made with Xlib
*/
+#include <err.h>
#include <stdio.h>
+#include <sys/stat.h>
#define ARENA_IMPL
#define STR_IMPL
@@ -48,22 +50,38 @@ txt_delete(DynStr *s, int i, int n)
}
int
+read_all(FILE *f, DynStr *out, Arena *a)
+{
+ char buf[256];
+ while (!feof(f)) {
+ size_t n = fread(buf, 1, sizeof buf, f);
+ if (ferror(f)) return -1;
+ DA_APUSH_MULT(out, a, buf, n);
+ }
+ return 0;
+}
+
+int
main(int argc, char **argv)
{
ui_init(argc, argv);
+ Arena a = { 0 };
DynStr input = { 0 };
int inpi = 0;
int seli = 0;
- Str optv[] = {
- S("she shit on my thang until i vomit"),
- S("what"),
- S("who are you"),
- S("why are you"),
- S("where'd you come from")
- };
- int optc = sizeof optv / sizeof *optv;
- if (!optc) goto done;
+
+ UiOpts opt = { 0 };
+ DynStr buf = { 0 };
+ if (read_all(stdin, &buf, &a)) err(1, "stdin");
+ for (Str src = { buf.v, buf.n }; src.n > 0; ) {
+ Cut c = str_cut(src, '\n');
+ DA_APUSH(&opt, &a, c.head);
+ src = c.tail;
+ }
+
+ if (!opt.n) goto done;
+
for (UiEvent ev; ui_wait_event(&ev); ) {
switch (ev.type) {
case UI_KEY_DOWN:
@@ -74,14 +92,14 @@ main(int argc, char **argv)
inpi = txt_delete(&input, inpi, 1);
goto draw;
case UIK_RETURN: {
- Str o = optv[seli];
+ Str o = opt.v[seli];
printf("%.*s\n", (int)o.n, o.s);
} goto done;
case UIK_DOWN:
- seli = umod(seli + 1, optc);
+ seli = umod(seli + 1, opt.n);
goto draw;
case UIK_UP:
- seli = umod(seli - 1, optc);
+ seli = umod(seli - 1, opt.n);
goto draw;
case UIK_LEFT:
if (inpi > 0) inpi--;
@@ -108,8 +126,7 @@ main(int argc, char **argv)
(Str) { input.v, input.n },
inpi,
seli,
- optv,
- optc
+ opt
);
break;
default:
diff --git a/ui.c b/ui.c
index aa3bad6..26bf56a 100644
--- a/ui.c
+++ b/ui.c
@@ -51,7 +51,7 @@ ui_init(int argc, char **argv)
XSetBackground(dsp, gc, bg);
XSetForeground(dsp, gc, fg);
- XSelectInput(dsp, win, ButtonPressMask | KeyPressMask | ExposureMask);
+ XSelectInput(dsp, win, KeyPressMask | ExposureMask);
XMapRaised(dsp, win);
freg = XLoadFont(dsp, "-*-new century schoolbook-medium-r-*-*-24-*-*-*-*-*-*-*");
@@ -103,7 +103,7 @@ ui_wait_event(UiEvent *e)
}
void
-ui_draw(Str input, int inpi, int seli, Str *optv, int optc)
+ui_draw(Str input, int inpi, int seli, UiOpts o)
{
XWindowAttributes attr;
XClearWindow(dsp, win);
@@ -120,23 +120,23 @@ ui_draw(Str input, int inpi, int seli, Str *optv, int optc)
/* draw options */
XSetFont(dsp, gc, freg);
- for (int i = 0; i < optc; i++) {
+ for (int i = 0; i < o.n; i++) {
if (i == seli) {
int w = XTextWidth(XQueryFont(dsp,
XGContextFromGC(gc)),
- optv[i].s, optv[i].n);
+ o.v[i].s, o.v[i].n);
XFillRectangle(dsp, win, gc,
16, 28 + i * 24 + 8,
w, 24);
XSetForeground(dsp, gc, bg);
XDrawString(dsp, win, gc,
16, 48 + i * 24 + 8,
- optv[i].s, optv[i].n);
+ o.v[i].s, o.v[i].n);
XSetForeground(dsp, gc, fg);
} else {
XDrawString(dsp, win, gc,
16, 48 + i * 24 + 8,
- optv[i].s, optv[i].n);
+ o.v[i].s, o.v[i].n);
}
}
}
diff --git a/ui.h b/ui.h
index 25ac537..6c22238 100644
--- a/ui.h
+++ b/ui.h
@@ -2,6 +2,7 @@
#define UI_H
#include "str.h"
+#include "dynarr.h"
typedef enum {
UI_REDRAW,
@@ -37,9 +38,11 @@ typedef struct {
};
} UiEvent;
+typedef DYNARR(Str) UiOpts;
+
void ui_init(int argc, char **argv);
void ui_fini(void);
-void ui_draw(Str input, int inpi, int seli, Str *optv, int optc);
+void ui_draw(Str input, int inpi, int seli, UiOpts o);
int ui_wait_event(UiEvent *e);
#endif