]> git.deb.at Git - pkg/abook.git/commitdiff
New function: statusline_ask_boolean
authorJaakko Heinonen <jheinonen@users.sourceforge.net>
Tue, 24 Apr 2001 13:53:43 +0000 (13:53 +0000)
committerJaakko Heinonen <jheinonen@users.sourceforge.net>
Tue, 24 Apr 2001 13:53:43 +0000 (13:53 +0000)
abook.c
ui.c
ui.h

diff --git a/abook.c b/abook.c
index 5277123c377b3769bf0a95ddfb5e77e7f0df7fab..da6192ac1e80763a407edfb26202fb204ae5e530 100644 (file)
--- 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 3286d21b4402a7b52a1b0482ee5c7b44b337bd78..25c6ba1146448eb5b6e84177caeb36adb108ba1b 100644 (file)
--- a/ui.c
+++ b/ui.c
@@ -13,6 +13,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <signal.h>
+#include <ctype.h>
 #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 fa20dd207744934de5d6f503db7d667a15d94eb0..6b6c74291da37f11fb8c0ea5bdd75009178f75b2 100644 (file)
--- 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);