5 * by JH <jheinonen@bigfoot.com>
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;
51 int ui_initialized = FALSE;
53 int should_resize = FALSE;
54 int can_resize = FALSE;
56 WINDOW *top = NULL, *bottom = NULL;
63 top = newwin(LIST_TOP - 1, COLS, 0, 0);
65 bottom = newwin(LINES - LIST_BOTTOM, COLS, LIST_BOTTOM, 0);
83 ioctl (0, TIOCGWINSZ, &winsz);
85 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
86 fprintf(stderr, "Warning: COLS=%d, LINES=%d\n", winsz.ws_col, winsz.ws_row);
90 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
91 #ifdef HAVE_RESIZETERM
92 resizeterm(winsz.ws_row, winsz.ws_col);
99 should_resize = FALSE;
100 close_list(); /* we need to recreate windows */
106 #endif /* TIOCGWINSZ */
116 should_resize = TRUE;
118 #endif /* SIGWINCH */
124 return ui_initialized;
131 initscr(); cbreak(); noecho();
133 intrflush(stdscr, FALSE);
134 keypad(stdscr, TRUE);
136 fprintf(stderr, "init_abook():\n");
137 fprintf(stderr, " COLS = %d, LINES = %d\n", COLS, LINES);
139 if( LINES < MIN_LINES || COLS < MIN_COLS ) {
140 clear(); refresh(); endwin();
141 fprintf(stderr, "Your terminal size is %dx%d\n", COLS, LINES);
142 fprintf(stderr, "Terminal is too small. Minium terminal size "
144 "%dx%d\n", MIN_COLS, MIN_LINES);
149 signal(SIGWINCH, win_changed);
155 ui_initialized = TRUE;
169 ui_initialized = FALSE;
174 headerline(char *str)
178 mvwhline(top, 1, 0, UI_HLINE_CHAR, COLS);
180 mvwprintw(top, 0, 0, "%s | %s", PACKAGE " " VERSION, str);
191 if( should_resize ) {
198 refresh_statusline();
199 headerline(MAIN_HELPLINE);
207 statusline_msg(char *msg)
210 statusline_addstr(msg);
213 fprintf(stderr, "statusline_msg(\"%s\")\n", msg);
219 statusline_addstr(char *str)
221 mvwaddstr(bottom, 1, 0, str);
227 * function statusline_getnstr
231 * if n >= 0 str is a pointer which points a place where to store
232 * the string, else str is ingnored
234 * the maximum length of the string
235 * If n < 0 function will allocate needed space for the string.
236 * Value 0 is not allowed for n.
238 * if this value is nonzero the fileselector is enabled
241 * If n < 0 a pointer to a newly allocated string is returned.
242 * If n > 0 a nonzero value is returned if user has typed a valid
243 * string. If not NULL value is returned. Never really use the
244 * _pointer_ if n > 0.
249 statusline_getnstr(char *str, int n, int use_filesel)
257 buf = wenter_string(bottom, n,
258 (use_filesel ? ESTR_USE_FILESEL:0) | ESTR_DONT_WRAP);
266 strncpy(str, buf, n);
276 statusline_ask_boolean(char *msg, int def)
279 char *msg2 = strconcat(msg, def ? " (Y/n)?" : " (y/N)?", NULL);
281 statusline_addstr(msg2);
285 switch( tolower(getch()) ) {
308 mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS);
309 mvwhline(bottom, 2, 0, UI_HLINE_CHAR, COLS);
317 ask_filename(char *prompt, int flags)
323 statusline_addstr(prompt);
324 buf = statusline_getnstr(NULL, -1, flags);
342 * help - need to rewrite
349 display_help(int help)
365 helpw = newwin(LINES - 5, COLS - 6, 2, 3);
369 for( i = 0; tbl[i] != NULL; i++) {
370 waddstr(helpw, tbl[i]);
371 if( ( !( (i+1) % (LINES-8) ) ) ||
372 (tbl[i+1] == NULL) ) {
375 refresh_statusline();
376 statusline_msg("Press any key to continue...");
399 can_resize = TRUE; /* it's safe to resize now */
405 can_resize = FALSE; /* it's not safe to resize anymore */
409 display_help(HELP_MAIN);
412 case 'a': add_item(); break;
413 case '\r': edit_item(-1); break;
416 case 'r': ui_remove_items(); break;
417 case 12: refresh_screen(); break;
420 case KEY_UP: scroll_up(); break;
422 case KEY_DOWN: scroll_down(); break;
424 case KEY_PPAGE: page_up(); break;
426 case KEY_NPAGE: page_down(); break;
429 case KEY_HOME: goto_home(); break;
431 case KEY_END: goto_end(); break;
433 case 'w': save_database();
435 case 'l': ui_read_database(); break;
436 case 'i': import_database(); break;
437 case 'e': export_database(); break;
438 case 'C': ui_clear_database(); break;
440 case 'o': ui_open_datafile(); break;
442 case 's': sort_database(); break;
443 case 'S': sort_surname(); break;
445 case '/': ui_find(0); break;
446 case '\\': ui_find(1); break;
448 case ' ': if(curitem >= 0) {
449 selected[curitem] = !selected[curitem];
450 ui_print_number_of_items();
454 case '+': select_all();
457 case '-': select_none();
460 case '*': invert_selection();
463 case 'A': move_curitem(MOVE_ITEM_UP);
465 case 'Z': move_curitem(MOVE_ITEM_DOWN);
468 case 'm': launch_mutt(list_current_item());
472 case 'p': ui_print_database(); break;
474 case 'u': launch_wwwbrowser(list_current_item());
488 if(statusline_ask_boolean("Remove selected item(s)", TRUE))
489 remove_selected_items();
498 if(statusline_ask_boolean("Clear WHOLE database", FALSE)) {
508 static char findstr[81];
515 statusline_addstr("/");
516 statusline_getnstr(findstr, 67, 0);
520 if( (item = find_item(findstr, next ? curitem+1 : curitem)) >= 0 ) {
529 ui_print_number_of_items()
531 char *str = mkstr(" " "|%3d/%3d", selected_items(), items);
533 mvaddstr(0, COLS-strlen(str), str);
542 if(!statusline_ask_boolean("Your current data will be lost - "
543 "Press 'y' to continue", FALSE))
546 load_database(datafile);
555 char *command = options_get_str("print_command");
558 if( list_is_empty() )
561 statusline_addstr("Print All/Selected/Cancel (a/s/C)?");
563 switch( tolower(getch()) ) {
568 if( !selected_items() ) {
569 statusline_msg("No selected items");
572 mode = ENUM_SELECTED;
581 if( ! *command || (handle = popen(command, "w")) == NULL)
584 fexport("text", handle, mode);
595 filename = ask_filename("File to open: ", 1);
602 if( options_get_int("autosave") )
604 else if(statusline_ask_boolean("Save current database", FALSE))
609 load_database(filename);
612 statusline_msg("Sorry, that specified file appears not to be a valid abook addressbook");
613 load_database(datafile);
616 datafile = strdup(filename);