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>
41 # define UI_HLINE_CHAR '-'
43 # define UI_HLINE_CHAR ACS_HLINE
51 extern int items, curitem;
52 extern char *datafile;
58 int ui_initialized = FALSE;
60 int should_resize = FALSE;
61 int can_resize = FALSE;
63 WINDOW *top = NULL, *bottom = NULL;
70 top = newwin(LIST_TOP - 1, COLS, 0, 0);
72 bottom = newwin(LINES - LIST_BOTTOM, COLS, LIST_BOTTOM, 0);
90 ioctl (0, TIOCGWINSZ, &winsz);
92 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
93 fprintf(stderr, "Warning: COLS=%d, LINES=%d\n", winsz.ws_col, winsz.ws_row);
97 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
98 #ifdef HAVE_RESIZETERM
99 resizeterm(winsz.ws_row, winsz.ws_col);
102 LINES = winsz.ws_row;
106 should_resize = FALSE;
107 close_list(); /* we need to recreate windows */
113 #endif /* TIOCGWINSZ */
123 should_resize = TRUE;
125 #endif /* SIGWINCH */
131 return ui_initialized;
138 initscr(); cbreak(); noecho();
140 intrflush(stdscr, FALSE);
141 keypad(stdscr, TRUE);
143 fprintf(stderr, "init_abook():\n");
144 fprintf(stderr, " COLS = %d, LINES = %d\n", COLS, LINES);
146 if( LINES < MIN_LINES || COLS < MIN_COLS ) {
147 clear(); refresh(); endwin();
148 fprintf(stderr, "Your terminal size is %dx%d\n", COLS, LINES);
149 fprintf(stderr, "Terminal is too small. Minium terminal size "
151 "%dx%d\n", MIN_COLS, MIN_LINES);
156 signal(SIGWINCH, win_changed);
162 ui_initialized = TRUE;
176 ui_initialized = FALSE;
181 headerline(char *str)
185 mvwhline(top, 1, 0, UI_HLINE_CHAR, COLS);
187 mvwprintw(top, 0, 0, "%s | %s", PACKAGE " " VERSION, str);
198 if( should_resize ) {
205 refresh_statusline();
206 headerline(MAIN_HELPLINE);
214 statusline_msg(char *msg)
217 statusline_addstr(msg);
220 fprintf(stderr, "statusline_msg(\"%s\")\n", msg);
226 statusline_addstr(char *str)
228 mvwaddstr(bottom, 1, 0, str);
234 * function statusline_getnstr
238 * if n >= 0 str is a pointer which points a place where to store
239 * the string, else str is ingnored
241 * the maximum length of the string
242 * If n < 0 function will allocate needed space for the string.
243 * Value 0 is not allowed for n.
245 * if this value is nonzero the fileselector is enabled
248 * If n < 0 a pointer to a newly allocated string is returned.
249 * If n > 0 a nonzero value is returned if user has typed a valid
250 * string. If not NULL value is returned. Never really use the
251 * _pointer_ if n > 0.
256 statusline_getnstr(char *str, int n, int use_filesel)
264 buf = wenter_string(bottom, n,
265 (use_filesel ? ESTR_USE_FILESEL:0) | ESTR_DONT_WRAP);
273 strncpy(str, buf, n);
283 statusline_ask_boolean(char *msg, int def)
286 char *msg2 = strconcat(msg, def ? " (Y/n)?" : " (y/N)?", NULL);
288 statusline_addstr(msg2);
292 switch( tolower(getch()) ) {
315 mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS);
316 mvwhline(bottom, 2, 0, UI_HLINE_CHAR, COLS);
324 ask_filename(char *prompt, int flags)
330 statusline_addstr(prompt);
331 buf = statusline_getnstr(NULL, -1, flags);
349 * help - need to rewrite
356 display_help(int help)
372 helpw = newwin(LINES - 5, COLS - 6, 2, 3);
376 for( i = 0; tbl[i] != NULL; i++) {
377 waddstr(helpw, tbl[i]);
378 if( ( !( (i+1) % (LINES-8) ) ) ||
379 (tbl[i+1] == NULL) ) {
382 refresh_statusline();
383 statusline_msg("Press any key to continue...");
406 can_resize = TRUE; /* it's safe to resize now */
412 can_resize = FALSE; /* it's not safe to resize anymore */
416 display_help(HELP_MAIN);
419 case 'a': add_item(); break;
420 case '\r': edit_item(-1); break;
423 case 'r': ui_remove_items(); break;
424 case 12: refresh_screen(); break;
427 case KEY_UP: scroll_up(); break;
429 case KEY_DOWN: scroll_down(); break;
431 case KEY_PPAGE: page_up(); break;
433 case KEY_NPAGE: page_down(); break;
436 case KEY_HOME: goto_home(); break;
438 case KEY_END: goto_end(); break;
440 case 'w': save_database();
442 case 'l': ui_read_database(); break;
443 case 'i': import_database(); break;
444 case 'e': export_database(); break;
445 case 'C': ui_clear_database(); break;
447 case 'o': ui_open_datafile(); break;
449 case 's': sort_database(); break;
450 case 'S': sort_surname(); break;
452 case '/': ui_find(0); break;
453 case '\\': ui_find(1); break;
455 case ' ': if(curitem >= 0) {
456 selected[curitem] = !selected[curitem];
457 ui_print_number_of_items();
461 case '+': select_all();
464 case '-': select_none();
467 case '*': invert_selection();
470 case 'A': move_curitem(MOVE_ITEM_UP);
472 case 'Z': move_curitem(MOVE_ITEM_DOWN);
475 case 'm': launch_mutt(); break;
477 case 'p': ui_print_database(); break;
479 case 'u': launch_lynx(); break;
491 if(statusline_ask_boolean("Remove selected item(s)", TRUE))
492 remove_selected_items();
501 if(statusline_ask_boolean("Clear WHOLE database", FALSE)) {
511 static char findstr[81];
518 statusline_addstr("/");
519 statusline_getnstr(findstr, 67, 0);
523 if( (item = find_item(findstr, next ? curitem+1 : curitem)) >= 0 ) {
532 ui_print_number_of_items()
534 char *str = mkstr(" " "|%3d/%3d", selected_items(), items);
536 mvaddstr(0, COLS-strlen(str), str);
545 if(!statusline_ask_boolean("Your current data will be lost - "
546 "Press 'y' to continue", FALSE))
549 load_database(datafile);
558 char *command = options_get_str("print_command");
561 if( list_is_empty() )
564 statusline_addstr("Print All/Selected/Cancel (a/s/C)?");
566 switch( tolower(getch()) ) {
571 if( !selected_items() ) {
572 statusline_msg("No selected items");
575 mode = ENUM_SELECTED;
584 if( ! *command || (handle = popen(command, "w")) == NULL)
587 fexport("text", handle, mode);
598 filename = ask_filename("File to open: ", 1);
605 if( options_get_int("autosave") )
607 else if(statusline_ask_boolean("Save current database", FALSE))
612 load_database(filename);
615 statusline_msg("Sorry, that specified file appears not to be a valid abook addressbook");
616 load_database(datafile);
619 datafile = strdup(filename);