summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorkatalx2026-01-28 20:46:43 -0500
committerkatalx2026-01-28 20:46:43 -0500
commit583bcd490a055f013b244a6cd7e6ecb6765260e2 (patch)
tree30b6d9d0237d9ae6ea4e57db964d4d409dc152b9 /main.c
parent86a10e053154cb52ab7d051b98a6366c6165c02b (diff)
read stdin into opt array
Diffstat (limited to 'main.c')
-rw-r--r--main.c45
1 files changed, 31 insertions, 14 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: