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 bool ui_initialized = FALSE;
55 bool should_resize = FALSE;
56 bool 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)
222 statusline_addstr(msg);
225 fprintf(stderr, "statusline_msg(\"%s\")\n", msg);
233 statusline_addstr(char *str)
235 mvwaddstr(bottom, 1, 0, str);
241 * function statusline_getnstr
245 * if n >= 0 str is a pointer which points a place where to store
246 * the string, else str is ignored
248 * the maximum length of the string
249 * If n < 0 function will allocate needed space for the string.
250 * Value 0 is not allowed for n.
252 * if this value is nonzero the fileselector is enabled
255 * If n < 0 a pointer to a newly allocated string is returned.
256 * If n > 0 a nonzero value is returned if user has typed a valid
257 * string. If not NULL value is returned. Never really use the
258 * _pointer_ if n > 0.
263 statusline_getnstr(char *str, int n, int use_filesel)
271 buf = wenter_string(bottom, n,
272 (use_filesel ? ESTR_USE_FILESEL:0) | ESTR_DONT_WRAP);
280 strncpy(str, buf, n);
290 statusline_ask_boolean(char *msg, int def)
293 char *msg2 = strconcat(msg, def ? " (Y/n)?" : " (y/N)?", NULL);
295 statusline_addstr(msg2);
299 switch( tolower(getch()) ) {
322 mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS);
323 mvwhline(bottom, 2, 0, UI_HLINE_CHAR, COLS);
331 ask_filename(char *prompt, int flags)
337 statusline_addstr(prompt);
338 buf = statusline_getnstr(NULL, -1, flags);
361 display_help(int help)
377 helpw = newwin(LINES - 5, COLS - 6, 2, 3);
381 for( i = 0; tbl[i] != NULL; i++) {
382 waddstr(helpw, tbl[i]);
383 if( ( !( (i+1) % (LINES-8) ) ) ||
384 (tbl[i+1] == NULL) ) {
387 refresh_statusline();
388 if(statusline_msg("Press any key to continue...")
403 extern char *selected;
412 can_resize = TRUE; /* it's safe to resize now */
418 can_resize = FALSE; /* it's not safe to resize anymore */
421 case 'Q': print_stderr(selected_items() ?
422 -1 : list_current_item());
425 display_help(HELP_MAIN);
428 case 'a': add_item(); break;
429 case '\r': edit_item(-1); break;
432 case 'r': ui_remove_items(); break;
433 case 12: refresh_screen(); break;
436 case KEY_UP: scroll_up(); break;
438 case KEY_DOWN: scroll_down(); break;
440 case KEY_PPAGE: page_up(); break;
442 case KEY_NPAGE: page_down(); break;
445 case KEY_HOME: goto_home(); break;
447 case KEY_END: goto_end(); break;
449 case 'w': save_database();
451 case 'l': ui_read_database(); break;
452 case 'i': import_database(); break;
453 case 'e': export_database(); break;
454 case 'C': ui_clear_database(); break;
456 case 'o': ui_open_datafile(); break;
458 case 's': sort_database(); break;
459 case 'S': sort_surname(); break;
461 case '/': ui_find(0); break;
462 case '\\': ui_find(1); break;
464 case ' ': if(curitem >= 0) {
465 selected[curitem] = !selected[curitem];
466 ui_print_number_of_items();
470 case '+': select_all();
473 case '-': select_none();
476 case '*': invert_selection();
479 case 'A': move_curitem(MOVE_ITEM_UP);
481 case 'Z': move_curitem(MOVE_ITEM_DOWN);
484 case 'm': launch_mutt(selected_items() ?
485 -1 : list_current_item());
489 case 'p': ui_print_database(); break;
491 case 'u': launch_wwwbrowser(list_current_item());
505 if(statusline_ask_boolean("Remove selected item(s)", TRUE))
506 remove_selected_items();
515 if(statusline_ask_boolean("Clear WHOLE database", FALSE)) {
525 static char findstr[MAX_FIELD_LEN];
526 int search_fields[] = {NAME, EMAIL, NICK, -1};
533 statusline_addstr("/");
534 statusline_getnstr(findstr, MAX_FIELD_LEN - 1, 0);
538 if( (item = find_item(findstr, curitem + !!next,
539 search_fields )) >= 0 ) {
548 ui_print_number_of_items()
550 char *str = mkstr(" " "|%3d/%3d", selected_items(), items);
552 mvaddstr(0, COLS-strlen(str), str);
561 if(!statusline_ask_boolean("Your current data will be lost - "
562 "Press 'y' to continue", FALSE))
565 load_database(datafile);
574 char *command = options_get_str("print_command");
577 if( list_is_empty() )
580 statusline_addstr("Print All/Selected/Cancel (a/s/C)?");
582 switch( tolower(getch()) ) {
587 if( !selected_items() ) {
588 statusline_msg("No selected items");
591 mode = ENUM_SELECTED;
600 if( ! *command || (handle = popen(command, "w")) == NULL)
603 fexport("text", handle, mode);
614 filename = ask_filename("File to open: ", 1);
621 if( options_get_int("autosave") )
623 else if(statusline_ask_boolean("Save current database", FALSE))
628 load_database(filename);
631 statusline_msg("Sorry, that specified file appears not to be a valid abook addressbook");
632 load_database(datafile);
635 datafile = strdup(filename);
641 alternative_datafile = TRUE;