From c6f9d7a411acae86eec4634954a94485bdd8a06e Mon Sep 17 00:00:00 2001 From: ache Date: Wed, 6 Feb 2019 08:03:45 +0100 Subject: Add or edit contact --- main.c | 37 ++++++++++++++++++++++++++++++++++++- readline.c | 19 ++++++++++++++----- readline.h | 2 +- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index d3359d0..41efec3 100644 --- a/main.c +++ b/main.c @@ -11,6 +11,7 @@ #include #include +#include #include "wind.h" @@ -240,7 +241,7 @@ int main(int argc, char* argv[]){ if( !tabed ) { contact* found = NULL; move(LINES-1,0); - readline_n(); + readline_n("> ", ""); int r = findContactFromName(msg_win_str, &found); if( r == 0 ) { @@ -263,6 +264,40 @@ int main(int argc, char* argv[]){ resize(); } break; + case 'e': + if( tabed ) { + contact* selected = getContactSelected(); + readline_n("Edit contact | Name : ", selected->name); + strcpy(selected->name, msg_win_str); + readline_n("Edit contact | Display name : ", selected->display_name); + strcpy(selected->display_name, msg_win_str); + writeContactList(config.CONTACT_FILENAME); + showContactListW(); + } + break; + case 'a': + if( !tabed ) { + contact newContact = {"", "", "", 0, NULL}; + readline_n("New contact | Name : ", ""); + strcpy(newContact.name, msg_win_str); + readline_n("New contact | Display name : ", ""); + strcpy(newContact.display_name, msg_win_str); + readline_n("New contact | Number : ", ""); + strcpy(newContact.number, msg_win_str); + + addContact(&newContact); + + if( *config.CONTACT_FILENAME == '\0' ) { + wordexp_t p; + r = wordexp("~/.mesms_contacts.csv", &p, 0); + if( r == 0 && p.we_wordc > 0 ) { + strcpy(config.CONTACT_FILENAME, p.we_wordv[0]); + } + } + writeContactList(config.CONTACT_FILENAME); + showContactListW(); + } + break; case 'i': case 's': { char* number = NULL; diff --git a/readline.c b/readline.c index bc37677..cb82047 100644 --- a/readline.c +++ b/readline.c @@ -103,6 +103,7 @@ static void msg_win_redisplay(bool for_resize) } static void got_command(char *line) { + fprintf(stderr, "got_command\n"); if( line ) if (*line != '\0') add_history(line); @@ -172,9 +173,19 @@ void init_readline(void) } -void readline_n(void) { +void readline_n(const char* info, const char* str) { curs_set(2); resize(); + + if( info ) { + rl_callback_handler_install(info, got_command); + } + + for( ; *str ; str++ ) { + if( *str != '\n' && *str != '\f' && *str != KEY_RESIZE ) { + forward_to_readline(*str); + } + } while (!should_exit) { // Using getch() here instead would refresh stdscr, overwriting the // initial contents of the other windows on startup @@ -183,13 +194,13 @@ void readline_n(void) { if( c == '\n') should_exit = 1; if (c == KEY_RESIZE) - ;//resize(); + resize(); else if (c == '\f') { // Ctrl-L -- redraw screen. // Makes the next refresh repaint the screen from scratch CHECK(clearok, curscr, TRUE); // Resize and reposition windows in case that got messed up // somehow - //resize(); + resize(); } else forward_to_readline(c); @@ -197,5 +208,3 @@ void readline_n(void) { should_exit = 0; } - - diff --git a/readline.h b/readline.h index cf96e11..769d385 100644 --- a/readline.h +++ b/readline.h @@ -15,7 +15,7 @@ -void readline_n(void); +void readline_n(const char*, const char*); void init_readline(void); -- cgit v1.2.3