aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorverdant <im@verdant.ee>2026-08-03 15:51:39 +0800
committerverdant <im@verdant.ee>2026-08-03 15:51:39 +0800
commit4e7e1e21b3eb29a5505dc69f668cf5f9481bd7d5 (patch)
treef53a7a31bf322ea1f731ada2a57a087c9e1eb01b
parentb1a3208e940f5fa5c4693c04ca2a423efbda2917 (diff)
downloadsf-4e7e1e21b3eb29a5505dc69f668cf5f9481bd7d5.tar.gz
sf-4e7e1e21b3eb29a5505dc69f668cf5f9481bd7d5.zip
Add utils.h utils.c to provide get_input()
Print the prompt at the status bar, handle character, backspace, enter and escape. Modify the variable buf in place.
-rw-r--r--src/utils.c50
-rw-r--r--src/utils.h9
2 files changed, 59 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
new file mode 100644
index 0000000..9137e21
--- /dev/null
+++ b/src/utils.c
@@ -0,0 +1,50 @@
+#include "utils.h"
+#include "fm.h"
+#include "log.h"
+#include <limits.h>
+#include <ncurses.h>
+
+int
+get_input(const char *prompt, char *buf)
+{
+ if (!prompt || !buf)
+ return -1;
+
+ int ch;
+ int i = 0;
+ curs_set(1);
+
+ while ((ch = getch())) {
+ werase(status_window->win);
+
+ if (i < 0)
+ i = 0;
+
+ if (ch >= 32 && ch <= 126) {
+ buf[i++] = ch;
+ } else if (ch == KEY_BACKSPACE || ch == 127 || ch == '\b') {
+ buf[--i] = '\0';
+ } else if (ch == 27) {
+ werase(status_window->win);
+ curs_set(0);
+ return 0;
+ } else if (ch == KEY_ENTER || ch == '\n' || ch == '\r') {
+ if (i < PATH_MAX) {
+ buf[++i] = '\0';
+ curs_set(0);
+ return 0;
+ } else {
+ return -1;
+ }
+ }
+
+ mvwprintw(status_window->win, 0, 0, "%s%s", prompt, buf);
+ wrefresh(status_window->win);
+
+ LOG_WRITE_INFO("%s", buf);
+ }
+
+ curs_set(0);
+
+ return 0;
+}
diff --git a/src/utils.h b/src/utils.h
new file mode 100644
index 0000000..2e99a21
--- /dev/null
+++ b/src/utils.h
@@ -0,0 +1,9 @@
+#ifndef SF_UTILS_H
+#define SF_UTILS_H
+
+#include "fm.h"
+#include "wm.h"
+
+int get_input(const char *prompt, char *buf);
+
+#endif /* SF_UTILS_H */