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, int use_completion)
247 mvwaddstr(bottom, 1, 0, prompt);
251 ret = abook_readline(bottom, y, x, s, limit, use_completion);
263 statusline_ask_boolean(char *msg, int def)
266 char *msg2 = strconcat(msg, def ? " (Y/n)?" : " (y/N)?", NULL);
268 statusline_addstr(msg2);
272 switch( tolower(getch()) ) {
295 mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS);
303 ask_filename(char *prompt)
309 buf = ui_readline(prompt, NULL, -1, 1);
330 display_help(int help)
346 helpw = newwin(LINES - 5, COLS - 6, 2, 3);
350 for( i = 0; tbl[i] != NULL; i++) {
351 waddstr(helpw, tbl[i]);
352 if( ( !( (i+1) % (LINES-8) ) ) ||
353 (tbl[i+1] == NULL) ) {
356 refresh_statusline();
357 if(statusline_msg("Press any key to continue...")
372 extern char *selected;
381 can_resize = TRUE; /* it's safe to resize now */
387 can_resize = FALSE; /* it's not safe to resize anymore */
390 case 'Q': print_stderr(selected_items() ?
391 -1 : list_current_item());
394 display_help(HELP_MAIN);
397 case 'a': add_item(); break;
398 case '\r': edit_item(-1); break;
401 case 'r': ui_remove_items(); break;
402 case 12: refresh_screen(); break;
405 case KEY_UP: scroll_up(); break;
407 case KEY_DOWN: scroll_down(); break;
409 case KEY_PPAGE: page_up(); break;
411 case KEY_NPAGE: page_down(); break;
414 case KEY_HOME: goto_home(); break;
416 case KEY_END: goto_end(); break;
418 case 'w': save_database();
420 case 'l': ui_read_database(); break;
421 case 'i': import_database(); break;
422 case 'e': export_database(); break;
423 case 'C': ui_clear_database(); break;
425 case 'o': ui_open_datafile(); break;
427 case 's': sort_database(); break;
428 case 'S': sort_surname(); break;
430 case '/': ui_find(0); break;
431 case '\\': ui_find(1); break;
433 case ' ': if(curitem >= 0) {
434 selected[curitem] = !selected[curitem];
435 ui_print_number_of_items();
439 case '+': select_all();
442 case '-': select_none();
445 case '*': invert_selection();
448 case 'A': move_curitem(MOVE_ITEM_UP);
450 case 'Z': move_curitem(MOVE_ITEM_DOWN);
453 case 'm': launch_mutt(selected_items() ?
454 -1 : list_current_item());
458 case 'p': ui_print_database(); break;
460 case 'u': launch_wwwbrowser(list_current_item());
474 if(statusline_ask_boolean("Remove selected item(s)", TRUE))
475 remove_selected_items();
484 if(statusline_ask_boolean("Clear WHOLE database", FALSE)) {
494 static char findstr[MAX_FIELD_LEN];
495 int search_fields[] = {NAME, EMAIL, NICK, -1};
503 s = ui_readline("/", findstr, MAX_FIELD_LEN - 1, 0);
504 strncpy(findstr, s, MAX_FIELD_LEN);
508 if( (item = find_item(findstr, curitem + !!next,
509 search_fields )) >= 0 ) {
518 ui_print_number_of_items()
520 char *str = mkstr(" " "|%3d/%3d", selected_items(), items);
522 mvaddstr(0, COLS-strlen(str), str);
531 if(!statusline_ask_boolean("Your current data will be lost - "
532 "Press 'y' to continue", FALSE))
535 load_database(datafile);
544 char *command = options_get_str("print_command");
547 if( list_is_empty() )
550 statusline_addstr("Print All/Selected/Cancel (a/s/C)?");
552 switch( tolower(getch()) ) {
557 if( !selected_items() ) {
558 statusline_msg("No selected items");
561 mode = ENUM_SELECTED;
570 if( ! *command || (handle = popen(command, "w")) == NULL)
573 fexport("text", handle, mode);
584 filename = ask_filename("File to open: ");
586 if( !filename || ! *filename) {
592 if( options_get_int("autosave") )
594 else if(statusline_ask_boolean("Save current database", FALSE))
599 load_database(filename);
602 statusline_msg("Sorry, that specified file appears not to be a valid abook addressbook");
603 load_database(datafile);
606 datafile = strdup(filename);
612 alternative_datafile = TRUE;