diff options
| author | verdant <im@verdant.ee> | 2026-08-03 15:51:39 +0800 |
|---|---|---|
| committer | verdant <im@verdant.ee> | 2026-08-03 15:51:39 +0800 |
| commit | 4e7e1e21b3eb29a5505dc69f668cf5f9481bd7d5 (patch) | |
| tree | f53a7a31bf322ea1f731ada2a57a087c9e1eb01b /src | |
| parent | b1a3208e940f5fa5c4693c04ca2a423efbda2917 (diff) | |
| download | sf-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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/utils.c | 50 | ||||
| -rw-r--r-- | src/utils.h | 9 |
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 */ |
