aboutsummaryrefslogtreecommitdiff
path: root/src/Input.c
diff options
context:
space:
mode:
authorfrosty <gabriel@bwaaa.monster>2026-02-23 00:37:47 -0500
committerfrosty <gabriel@bwaaa.monster>2026-02-23 00:37:47 -0500
commit3add5ed8899a8f4a442c249913602de6b7d60732 (patch)
treee4f864b1f4d41aa258c8b678b00a7d9ed69640f7 /src/Input.c
downloadinsel-3add5ed8899a8f4a442c249913602de6b7d60732.tar.gz
billions must commit
Diffstat (limited to 'src/Input.c')
-rw-r--r--src/Input.c112
1 files changed, 112 insertions, 0 deletions
diff --git a/src/Input.c b/src/Input.c
new file mode 100644
index 0000000..23a474d
--- /dev/null
+++ b/src/Input.c
@@ -0,0 +1,112 @@
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <stdio.h>
+
+#include "Globals.h"
+#include "Terminal.h"
+#include "Input.h"
+
+int matches(const char *s) {
+ if (input_len == 0) return 1;
+ const char *p = s;
+ if (opts.insensitive) {
+ while (*p) {
+ if (strncasecmp(p, input, input_len) == 0) return 1;
+ p++;
+ }
+ } else {
+ while (*p) {
+ if (strncmp(p, input, input_len) == 0) return 1;
+ p++;
+ }
+ }
+ return 0;
+}
+
+void filter_items(void) {
+ filtered.count = 0;
+ for (size_t i = 0; i < all_items.count; i++) {
+ if (matches(all_items.items[i])) {
+ if (filtered.count >= filtered.capacity) {
+ size_t new_cap = filtered.capacity == 0 ? INITIAL_CAPACITY : filtered.capacity * 2;
+ filtered.items = realloc(filtered.items, new_cap * sizeof(char *));
+ filtered.capacity = new_cap;
+ }
+ filtered.items[filtered.count++] = all_items.items[i];
+ }
+ }
+ if ((size_t)cursor >= filtered.count) cursor = filtered.count > 0 ? (int)filtered.count - 1 : 0;
+ if (scroll > cursor) scroll = cursor;
+ if (scroll < cursor - DEFAULT_LINES + 1) scroll = cursor - DEFAULT_LINES + 1;
+ if (scroll < 0) scroll = 0;
+ needs_redraw = 1;
+}
+
+void handle_input(void) {
+ char c;
+ if (read(tty_fd, &c, 1) <= 0) return;
+ if (c == 27) {
+ char seq[3];
+ ssize_t n = read(tty_fd, &seq[0], 1);
+ if (n > 0 && seq[0] == '[') {
+ if (read(tty_fd, &seq[1], 1) > 0) {
+ switch (seq[1]) {
+ case 'A':
+ if (cursor > 0) cursor--;
+ break;
+ case 'B':
+ if (cursor < (int)filtered.count - 1) cursor++;
+ break;
+ case 'H':
+ case 'F':
+ cursor = 0;
+ break;
+ case 'G':
+ cursor = filtered.count > 0 ? filtered.count - 1 : 0;
+ break;
+ case '5':
+ case '6':
+ if (read(tty_fd, &seq[2], 1) > 0 && seq[2] == '~') {
+ int jump = DEFAULT_LINES;
+ if (seq[1] == '5') {
+ cursor -= jump;
+ if (cursor < 0) cursor = 0;
+ } else {
+ cursor += jump;
+ if (cursor >= (int)filtered.count) cursor = filtered.count > 0 ? (int)filtered.count - 1 : 0;
+ }
+ }
+ break;
+ }
+ }
+ } else {
+ exit(1);
+ }
+ if (cursor < scroll) scroll = cursor;
+ if (cursor > scroll + DEFAULT_LINES - 1) scroll = cursor - DEFAULT_LINES + 1;
+ needs_redraw = 1;
+ } else if (c == 127 || c == 8) {
+ if (input_len > 0) input[--input_len] = '\0';
+ filter_items();
+ } else if (c == '\n' || c == '\r') {
+ if (filtered.count > 0) {
+ dprintf(tty_out_fd, "\033[?1049l\033[2J\033[?25h\033[0m");
+ dprintf(orig_stdout, "%s\n", filtered.items[cursor]);
+ exit(0);
+ }
+ } else if (c == 4) {
+ exit(1);
+ } else if (c >= 32 && c < 127) {
+ if ((int)input_len + 2 > (int)input_capacity) {
+ size_t new_cap = input_capacity == 0 ? INITIAL_CAPACITY : input_capacity * 2;
+ input = realloc(input, new_cap);
+ memset(input + input_capacity, 0, new_cap - input_capacity);
+ input_capacity = new_cap;
+ }
+ input[input_len++] = c;
+ input[input_len] = '\0';
+ filter_items();
+ }
+}