5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
32 # ifdef HAVE_LINUX_TERMIOS_H
33 # include <linux/termios.h>
36 #ifdef HAVE_SYS_IOCTL_H
37 # include <sys/ioctl.h>
44 extern int items, curitem;
45 extern char *datafile;
47 extern int alternative_datafile;
53 int ui_initialized = FALSE;
55 int should_resize = FALSE;
56 int can_resize = FALSE;
58 WINDOW *top = NULL, *bottom = NULL;
65 top = newwin(LIST_TOP - 1, COLS, 0, 0);
67 bottom = newwin(LINES - LIST_BOTTOM, COLS, LIST_BOTTOM, 0);
85 ioctl (0, TIOCGWINSZ, &winsz);
87 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
88 fprintf(stderr, "Warning: COLS=%d, LINES=%d\n", winsz.ws_col, winsz.ws_row);
92 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
93 #ifdef HAVE_RESIZETERM
94 resizeterm(winsz.ws_row, winsz.ws_col);
101 should_resize = FALSE;
102 close_list(); /* we need to recreate windows */
108 #endif /* TIOCGWINSZ */
118 should_resize = TRUE;
120 #endif /* SIGWINCH */
126 return ui_initialized;
132 if(!is_ui_initialized())
137 intrflush(stdscr, FALSE);
138 keypad(stdscr, TRUE);
146 fprintf(stderr, "init_abook():\n");
147 fprintf(stderr, " COLS = %d, LINES = %d\n", COLS, LINES);
149 if( LINES < MIN_LINES || COLS < MIN_COLS ) {
150 clear(); refresh(); endwin();
151 fprintf(stderr, "Your terminal size is %dx%d\n", COLS, LINES);
152 fprintf(stderr, "Terminal is too small. Minium terminal size "
154 "%dx%d\n", MIN_COLS, MIN_LINES);
159 signal(SIGWINCH, win_changed);
165 ui_initialized = TRUE;
179 ui_initialized = FALSE;
184 headerline(char *str)
188 mvwhline(top, 1, 0, UI_HLINE_CHAR, COLS);
190 mvwprintw(top, 0, 0, "%s | %s", PACKAGE " " VERSION, str);
201 if( should_resize ) {
208 refresh_statusline();
209 headerline(MAIN_HELPLINE);
217 statusline_msg(char *msg)
220 statusline_addstr(msg);
223 fprintf(stderr, "statusline_msg(\"%s\")\n", msg);
229 statusline_addstr(char *str)
231 mvwaddstr(bottom, 1, 0, str);
237 * function statusline_getnstr
241 * if n >= 0 str is a pointer which points a place where to store
242 * the string, else str is ignored
244 * the maximum length of the string
245 * If n < 0 function will allocate needed space for the string.
246 * Value 0 is not allowed for n.
248 * if this value is nonzero the fileselector is enabled
251 * If n < 0 a pointer to a newly allocated string is returned.
252 * If n > 0 a nonzero value is returned if user has typed a valid
253 * string. If not NULL value is returned. Never really use the
254 * _pointer_ if n > 0.
259 statusline_getnstr(char *str, int n, int use_filesel)
267 buf = wenter_string(bottom, n,
268 (use_filesel ? ESTR_USE_FILESEL:0) | ESTR_DONT_WRAP);
276 strncpy(str, buf, n);
286 statusline_ask_boolean(char *msg, int def)
289 char *msg2 = strconcat(msg, def ? " (Y/n)?" : " (y/N)?", NULL);
291 statusline_addstr(msg2);
295 switch( tolower(getch()) ) {
318 mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS);
319 mvwhline(bottom, 2, 0, UI_HLINE_CHAR, COLS);
327 ask_filename(char *prompt, int flags)
333 statusline_addstr(prompt);
334 buf = statusline_getnstr(NULL, -1, flags);
357 display_help(int help)
373 helpw = newwin(LINES - 5, COLS - 6, 2, 3);
377 for( i = 0; tbl[i] != NULL; i++) {
378 waddstr(helpw, tbl[i]);
379 if( ( !( (i+1) % (LINES-8) ) ) ||
380 (tbl[i+1] == NULL) ) {
383 refresh_statusline();
384 statusline_msg("Press any key to continue...");
397 extern char *selected;
406 can_resize = TRUE; /* it's safe to resize now */
412 can_resize = FALSE; /* it's not safe to resize anymore */
415 case 'Q': print_stderr(selected_items() ?
416 -1 : list_current_item());
419 display_help(HELP_MAIN);
422 case 'a': add_item(); break;
423 case '\r': edit_item(-1); break;
426 case 'r': ui_remove_items(); break;
427 case 12: refresh_screen(); break;
430 case KEY_UP: scroll_up(); break;
432 case KEY_DOWN: scroll_down(); break;
434 case KEY_PPAGE: page_up(); break;
436 case KEY_NPAGE: page_down(); break;
439 case KEY_HOME: goto_home(); break;
441 case KEY_END: goto_end(); break;
443 case 'w': save_database();
445 case 'l': ui_read_database(); break;
446 case 'i': import_database(); break;
447 case 'e': export_database(); break;
448 case 'C': ui_clear_database(); break;
450 case 'o': ui_open_datafile(); break;
452 case 's': sort_database(); break;
453 case 'S': sort_surname(); break;
455 case '/': ui_find(0); break;
456 case '\\': ui_find(1); break;
458 case ' ': if(curitem >= 0) {
459 selected[curitem] = !selected[curitem];
460 ui_print_number_of_items();
464 case '+': select_all();
467 case '-': select_none();
470 case '*': invert_selection();
473 case 'A': move_curitem(MOVE_ITEM_UP);
475 case 'Z': move_curitem(MOVE_ITEM_DOWN);
478 case 'm': launch_mutt(selected_items() ?
479 -1 : list_current_item());
483 case 'p': ui_print_database(); break;
485 case 'u': launch_wwwbrowser(list_current_item());
499 if(statusline_ask_boolean("Remove selected item(s)", TRUE))
500 remove_selected_items();
509 if(statusline_ask_boolean("Clear WHOLE database", FALSE)) {
519 static char findstr[MAX_FIELD_LEN];
520 int search_fields[] = {NAME, EMAIL, NICK, -1};
527 statusline_addstr("/");
528 statusline_getnstr(findstr, MAX_FIELD_LEN - 1, 0);
532 if( (item = find_item(findstr, curitem + !!next,
533 search_fields )) >= 0 ) {
542 ui_print_number_of_items()
544 char *str = mkstr(" " "|%3d/%3d", selected_items(), items);
546 mvaddstr(0, COLS-strlen(str), str);
555 if(!statusline_ask_boolean("Your current data will be lost - "
556 "Press 'y' to continue", FALSE))
559 load_database(datafile);
568 char *command = options_get_str("print_command");
571 if( list_is_empty() )
574 statusline_addstr("Print All/Selected/Cancel (a/s/C)?");
576 switch( tolower(getch()) ) {
581 if( !selected_items() ) {
582 statusline_msg("No selected items");
585 mode = ENUM_SELECTED;
594 if( ! *command || (handle = popen(command, "w")) == NULL)
597 fexport("text", handle, mode);
608 filename = ask_filename("File to open: ", 1);
615 if( options_get_int("autosave") )
617 else if(statusline_ask_boolean("Save current database", FALSE))
622 load_database(filename);
625 statusline_msg("Sorry, that specified file appears not to be a valid abook addressbook");
626 load_database(datafile);
629 datafile = strdup(filename);
635 alternative_datafile = TRUE;