From: Jaakko Heinonen Date: Tue, 24 Apr 2001 13:53:43 +0000 (+0000) Subject: New function: statusline_ask_boolean X-Git-Tag: upstream/0.6.1~2^2~384 X-Git-Url: https://git.deb.at/w?a=commitdiff_plain;h=af90a0834fb7f729d77b1312f58c80b187043218;p=pkg%2Fabook.git New function: statusline_ask_boolean --- diff --git a/abook.c b/abook.c index 5277123..da6192a 100644 --- a/abook.c +++ b/abook.c @@ -85,15 +85,9 @@ quit_abook() { if( options_get_int("autosave") ) save_database(); - else { - statusline_addstr("Save database (y/N)"); - switch( getch() ) { - case 'y': - case 'Y': - save_database(); - default: break; - } - } + else if( statusline_ask_boolean("Save database", FALSE) ) + save_database(); + close_config(); close_database(); diff --git a/ui.c b/ui.c index 3286d21..25c6ba1 100644 --- a/ui.c +++ b/ui.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "abook.h" #include "ui.h" #include "edit.h" @@ -278,6 +279,34 @@ statusline_getnstr(char *str, int n, int use_filesel) return buf; } +int +statusline_ask_boolean(char *msg, int def) +{ + int ret; + char *msg2 = strconcat(msg, def ? " (Y/n)?" : " (y/N)?", NULL); + + statusline_addstr(msg2); + + free(msg2); + + switch( tolower(getch()) ) { + case 'n': + ret = FALSE; + break; + case 'y': + ret = TRUE; + break; + default: + ret = def; + break; + } + + clear_statusline(); + + return ret; +} + + void refresh_statusline() { @@ -456,20 +485,11 @@ get_commands() void ui_remove_items() { - if( items < 1 || curitem < 0 ) + if(list_is_empty()) return; - statusline_addstr("Remove selected item(s) (Y/n)"); - switch( getch() ) { - case '\r': - case 'y': - case 'Y': break; - default: - clear_statusline(); - return; - } - - remove_selected_items(); + if(statusline_ask_boolean("Remove selected item(s)", TRUE)) + remove_selected_items(); clear_statusline(); refresh_list(); @@ -478,19 +498,10 @@ ui_remove_items() void ui_clear_database() { - - statusline_addstr("Clear WHOLE database (y/N)"); - switch( getch() ) { - case 'y': - case 'Y': break; - default: - clear_statusline(); - return; + if(statusline_ask_boolean("Clear WHOLE database", FALSE)) { + close_database(); + refresh_list(); } - - close_database(); - - refresh_screen(); } void @@ -530,16 +541,10 @@ ui_print_number_of_items() void ui_read_database() { - if(items > 0) { - statusline_addstr("Your current data will be lost - Press 'y' to continue"); - switch( getch() ) { - case 'y': - case 'Y': break; - default: clear_statusline(); - return; - } - clear_statusline(); - } + if(items > 0) + if(!statusline_ask_boolean("Your current data will be lost - " + "Press 'y' to continue", FALSE)) + return; load_database(datafile); refresh_list(); @@ -552,14 +557,8 @@ ui_print_database() FILE *handle; char *command = options_get_str("print_command"); - statusline_addstr("Print addressbook? (y/N)"); - switch( getch() ) { - case 'y': - case 'Y': - break; - default: clear_statusline(); return; - } - clear_statusline(); + if(!statusline_ask_boolean("Print addressbook", FALSE)) + return; if( ! *command || (handle = popen(command, "w")) == NULL) return; @@ -584,15 +583,8 @@ ui_open_datafile() if( options_get_int("autosave") ) save_database(); - else { - statusline_addstr("Save current database (y/N)"); - switch( getch() ) { - case 'y': - case 'Y': - save_database(); - default: break; - } - } + else if(statusline_ask_boolean("Save current database", FALSE)) + save_database(); close_database(); diff --git a/ui.h b/ui.h index fa20dd2..6b6c742 100644 --- a/ui.h +++ b/ui.h @@ -7,7 +7,6 @@ enum { HELP_MAIN, HELP_EDITOR }; - int is_ui_initialized(); int init_ui(); @@ -16,6 +15,7 @@ void headerline(char *str); void refresh_screen(); void statusline_msg(char *msg); char *ask_filename(char *prompt, int flags); +int statusline_ask_boolean(char *msg, int def); void clear_statusline(); void display_help(int help); void statusline_addstr(char *str);