5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
31 # ifdef HAVE_LINUX_TERMIOS_H
32 # include <linux/termios.h>
35 #ifdef HAVE_SYS_IOCTL_H
36 # include <sys/ioctl.h>
45 extern int items, curitem;
46 extern char *datafile;
48 extern int alternative_datafile;
54 bool ui_initialized = FALSE;
56 bool should_resize = FALSE;
57 bool can_resize = FALSE;
59 WINDOW *top = NULL, *bottom = NULL;
66 top = newwin(LIST_TOP - 1, COLS, 0, 0);
68 bottom = newwin(LINES - LIST_BOTTOM, COLS, LIST_BOTTOM, 0);
86 ioctl (0, TIOCGWINSZ, &winsz);
88 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
89 fprintf(stderr, "Warning: COLS=%d, LINES=%d\n", winsz.ws_col, winsz.ws_row);
93 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
94 #ifdef HAVE_RESIZETERM
95 resizeterm(winsz.ws_row, winsz.ws_col);
102 should_resize = FALSE;
103 close_list(); /* we need to recreate windows */
109 #endif /* TIOCGWINSZ */
119 should_resize = TRUE;
121 #endif /* SIGWINCH */
127 return ui_initialized;
133 if(!is_ui_initialized())
138 intrflush(stdscr, FALSE);
139 keypad(stdscr, TRUE);
147 fprintf(stderr, "init_abook():\n");
148 fprintf(stderr, " COLS = %d, LINES = %d\n", COLS, LINES);
150 if( LINES < MIN_LINES || COLS < MIN_COLS ) {
151 clear(); refresh(); endwin();
152 fprintf(stderr, "Your terminal size is %dx%d\n", COLS, LINES);
153 fprintf(stderr, "Terminal is too small. Minium terminal size "
155 "%dx%d\n", MIN_COLS, MIN_LINES);
160 signal(SIGWINCH, win_changed);
166 ui_initialized = TRUE;
180 ui_initialized = FALSE;
185 headerline(char *str)
189 mvwhline(top, 1, 0, UI_HLINE_CHAR, COLS);
191 mvwprintw(top, 0, 0, "%s | %s", PACKAGE " " VERSION, str);
202 if( should_resize ) {
209 refresh_statusline();
210 headerline(MAIN_HELPLINE);
218 statusline_msg(char *msg)
223 statusline_addstr(msg);
226 fprintf(stderr, "statusline_msg(\"%s\")\n", msg);
234 statusline_addstr(char *str)
236 mvwaddstr(bottom, 1, 0, str);
242 ui_readline(char *prompt, char *s, int limit, bool use_completion)
247 mvwaddstr(bottom, 1, 0, prompt);
251 ret = abook_readline(bottom, y, x, s, limit, use_completion);
260 statusline_ask_boolean(char *msg, int def)
263 char *msg2 = strconcat(msg, def ? " (Y/n)?" : " (y/N)?", NULL);
265 statusline_addstr(msg2);
269 switch( tolower(getch()) ) {
292 mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS);
300 ask_filename(char *prompt)
306 buf = ui_readline(prompt, NULL, -1, 1);
327 display_help(int help)
343 helpw = newwin(LINES - 5, COLS - 6, 2, 3);
347 for( i = 0; tbl[i] != NULL; i++) {
348 waddstr(helpw, tbl[i]);
349 if( ( !( (i+1) % (LINES-8) ) ) ||
350 (tbl[i+1] == NULL) ) {
353 refresh_statusline();
354 if(statusline_msg("Press any key to continue...")
369 extern char *selected;
378 can_resize = TRUE; /* it's safe to resize now */
384 can_resize = FALSE; /* it's not safe to resize anymore */
387 case 'Q': print_stderr(selected_items() ?
388 -1 : list_current_item());
391 display_help(HELP_MAIN);
394 case 'a': add_item(); break;
395 case '\r': edit_item(-1); break;
398 case 'r': ui_remove_items(); break;
399 case 12: refresh_screen(); break;
402 case KEY_UP: scroll_up(); break;
404 case KEY_DOWN: scroll_down(); break;
406 case KEY_PPAGE: page_up(); break;
408 case KEY_NPAGE: page_down(); break;
411 case KEY_HOME: goto_home(); break;
413 case KEY_END: goto_end(); break;
415 case 'w': save_database();
417 case 'l': ui_read_database(); break;
418 case 'i': import_database(); break;
419 case 'e': export_database(); break;
420 case 'C': ui_clear_database(); break;
422 case 'o': ui_open_datafile(); break;
424 case 's': sort_database(); break;
425 case 'S': sort_surname(); break;
427 case '/': ui_find(0); break;
428 case '\\': ui_find(1); break;
430 case ' ': if(curitem >= 0) {
431 selected[curitem] = !selected[curitem];
432 ui_print_number_of_items();
436 case '+': select_all();
439 case '-': select_none();
442 case '*': invert_selection();
445 case 'A': move_curitem(MOVE_ITEM_UP);
447 case 'Z': move_curitem(MOVE_ITEM_DOWN);
450 case 'm': launch_mutt(selected_items() ?
451 -1 : list_current_item());
455 case 'p': ui_print_database(); break;
457 case 'u': launch_wwwbrowser(list_current_item());
471 if(statusline_ask_boolean("Remove selected item(s)", TRUE))
472 remove_selected_items();
481 if(statusline_ask_boolean("Clear WHOLE database", FALSE)) {
491 static char findstr[MAX_FIELD_LEN];
492 int search_fields[] = {NAME, EMAIL, NICK, -1};
500 s = ui_readline("/", findstr, MAX_FIELD_LEN - 1, 0);
501 strncpy(findstr, s, MAX_FIELD_LEN);
505 if( (item = find_item(findstr, curitem + !!next,
506 search_fields )) >= 0 ) {
515 ui_print_number_of_items()
517 char *str = mkstr(" " "|%3d/%3d", selected_items(), items);
519 mvaddstr(0, COLS-strlen(str), str);
528 if(!statusline_ask_boolean("Your current data will be lost - "
529 "Press 'y' to continue", FALSE))
532 load_database(datafile);
541 char *command = opt_get_str(STR_PRINT_COMMAND);
544 if( list_is_empty() )
547 statusline_addstr("Print All/Selected/Cancel (a/s/C)?");
549 switch( tolower(getch()) ) {
554 if( !selected_items() ) {
555 statusline_msg("No selected items");
558 mode = ENUM_SELECTED;
567 if( ! *command || (handle = popen(command, "w")) == NULL)
570 fexport("text", handle, mode);
581 filename = ask_filename("File to open: ");
583 if( !filename || ! *filename) {
589 if( opt_get_bool(BOOL_AUTOSAVE) )
591 else if(statusline_ask_boolean("Save current database", FALSE))
596 load_database(filename);
599 statusline_msg("Sorry, that specified file appears not to be a valid abook addressbook");
600 load_database(datafile);
603 datafile = strdup(filename);
609 alternative_datafile = TRUE;