ref: ea9682b12b2f563cbc92a795fe1818119d5c0240
parent: 97c05c3efced5687cf2f1900058cda582fe4d39a
author: cancel <cancel@cancel.fm>
date: Mon Dec 17 23:52:46 EST 2018
Add more file save as logic
--- a/term_util.c
+++ b/term_util.c
@@ -1,4 +1,5 @@
#include "term_util.h"
+#include <ctype.h>
#include <form.h>
#include <menu.h>
@@ -26,15 +27,14 @@
}
void heapstr_set_cstrlen(Heapstr* hs, char const* cstr, Usz len) {
heapstr_reserve(hs, len + 1);
- memcpy(hs->str, cstr, len + 1);
+ memcpy(hs->str, cstr, len);
+ hs->str[len] = 0;
}
void heapstr_set_cstr(Heapstr* hs, char const* cstr) {
Usz len = strlen(cstr);
heapstr_set_cstrlen(hs, cstr, len);
}
-Usz heapstr_len(Heapstr const* hs) {
- return strlen(hs->str);
-}
+Usz heapstr_len(Heapstr const* hs) { return strlen(hs->str); }
void term_util_init_colors() {
if (has_colors()) {
@@ -416,9 +416,50 @@
case CTRL_PLUS('h'):
form_driver(qf->ncurses_form, REQ_DEL_PREV);
return false;
+ case ' ':
+ case '\r':
+ case KEY_ENTER: {
+ out_action->any.type = Qform_action_type_submitted;
+ return true;
+ } break;
default:
form_driver(qf->ncurses_form, key);
return false;
}
return false;
+}
+
+static Usz size_without_trailing_spaces(char const* str) {
+ Usz size = strlen(str);
+ for (;;) {
+ if (size == 0)
+ break;
+ if (!isspace(str[size - 1]))
+ break;
+ --size;
+ }
+ return size;
+}
+
+FIELD* qform_find_field(Qform const* qf, int id) {
+ Usz count = qf->fields_count;
+ for (Usz i = 0; i < count; ++i) {
+ FIELD* f = qf->ncurses_fields[i];
+ if ((int)(intptr_t)field_userptr(f) == id)
+ return f;
+ }
+ return NULL;
+}
+
+bool qform_get_text_line(Qform const* qf, int id, Heapstr* out) {
+ FIELD* f = qform_find_field(qf, id);
+ if (!f)
+ return false;
+ form_driver(qf->ncurses_form, REQ_VALIDATION);
+ char* buf = field_buffer(f, 0);
+ if (!buf)
+ return false;
+ Usz trimmed = size_without_trailing_spaces(buf);
+ heapstr_set_cstrlen(out, buf, trimmed);
+ return true;
}
--- a/term_util.h
+++ b/term_util.h
@@ -99,6 +99,7 @@
typedef enum {
Qform_action_type_canceled,
+ Qform_action_type_submitted,
} Qform_action_type;
typedef struct {
Qform_action_type type;
@@ -138,5 +139,6 @@
void qform_push_to_nav(Qform* qf);
void qform_set_title(Qform* qf, char const* title);
bool qform_drive(Qform* qf, int key, Qform_action* out_action);
+bool qform_get_text_line(Qform const* qf, int id, Heapstr* out);
extern Qnav_stack qnav_stack;
--- a/tui_main.c
+++ b/tui_main.c
@@ -1982,6 +1982,16 @@
case Qform_action_type_canceled:
qnav_stack_pop();
break;
+ case Qform_action_type_submitted: {
+ switch (qform_id(qf)) {
+ case Save_as_form_id: {
+ if (qform_get_text_line(qf, Save_as_name_id, &file_name)) {
+ fprintf(stderr, "new file name: %s\n", file_name.str);
+ ged_state.filename = file_name.str;
+ }
+ } break;
+ }
+ } break;
}
}
} break;