5 * by JH <jheinonen@bigfoot.com>
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>
40 # define UI_HLINE_CHAR '-'
42 # define UI_HLINE_CHAR ACS_HLINE
50 extern int items, curitem;
51 extern char *datafile;
57 int ui_initialized = FALSE;
59 int should_resize = FALSE;
60 int can_resize = FALSE;
62 WINDOW *top = NULL, *bottom = NULL;
69 top = newwin(LIST_TOP - 1, COLS, 0, 0);
71 bottom = newwin(LINES - LIST_BOTTOM, COLS, LIST_BOTTOM, 0);
89 ioctl (0, TIOCGWINSZ, &winsz);
91 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
92 fprintf(stderr, "Warning: COLS=%d, LINES=%d\n", winsz.ws_col, winsz.ws_row);
96 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
97 #ifdef HAVE_RESIZETERM
98 resizeterm(winsz.ws_row, winsz.ws_col);
101 LINES = winsz.ws_row;
105 should_resize = FALSE;
106 close_list(); /* we need to recreate windows */
112 #endif /* TIOCGWINSZ */
122 should_resize = TRUE;
124 #endif /* SIGWINCH */
130 return ui_initialized;
137 initscr(); cbreak(); noecho();
139 intrflush(stdscr, FALSE);
140 keypad(stdscr, TRUE);
142 fprintf(stderr, "init_abook():\n");
143 fprintf(stderr, " COLS = %d, LINES = %d\n", COLS, LINES);
145 if( LINES < MIN_LINES || COLS < MIN_COLS ) {
146 clear(); refresh(); endwin();
147 fprintf(stderr, "Your terminal size is %dx%d\n", COLS, LINES);
148 fprintf(stderr, "Terminal is too small. Minium terminal size "
150 "%dx%d\n", MIN_COLS, MIN_LINES);
155 signal(SIGWINCH, win_changed);
161 ui_initialized = TRUE;
175 ui_initialized = FALSE;
180 headerline(char *str)
184 mvwhline(top, 1, 0, UI_HLINE_CHAR, COLS);
186 mvwprintw(top, 0, 0, "%s | %s", PACKAGE " " VERSION, str);
197 if( should_resize ) {
204 refresh_statusline();
205 headerline(MAIN_HELPLINE);
213 statusline_msg(char *msg)
216 statusline_addstr(msg);
219 fprintf(stderr, "statusline_msg(\"%s\")\n", msg);
225 statusline_addstr(char *str)
227 mvwaddstr(bottom, 1, 0, str);
233 * function statusline_getnstr
237 * if n >= 0 str is a pointer which points a place where to store
238 * the string, else str is ingnored
240 * the maximum length of the string
241 * If n < 0 function will allocate needed space for the string.
242 * Value 0 is not allowed for n.
244 * if this value is nonzero the fileselector is enabled
247 * If n < 0 a pointer to a newly allocated string is returned.
248 * If n > 0 a nonzero value is returned if user has typed a valid
249 * string. If not NULL value is returned. Never really use the
250 * _pointer_ if n > 0.
255 statusline_getnstr(char *str, int n, int use_filesel)
263 buf = wenter_string(bottom, n,
264 (use_filesel ? ESTR_USE_FILESEL:0) | ESTR_DONT_WRAP);
272 strncpy(str, buf, n);
286 mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS);
287 mvwhline(bottom, 2, 0, UI_HLINE_CHAR, COLS);
295 ask_filename(char *prompt, int flags)
301 statusline_addstr(prompt);
302 buf = statusline_getnstr(NULL, -1, flags);
320 * help - need to rewrite
327 display_help(int help)
343 helpw = newwin(LINES - 5, COLS - 6, 2, 3);
347 for( i = 0; tbl[i] != NULL; i++) {
348 waddstr(helpw, tbl[i]);
349 if( ( !( (i+1) % (LINES-8) ) ) ||
350 (tbl[i+1] == NULL) ) {
353 refresh_statusline();
354 statusline_msg("Press any key to continue...");
377 can_resize = TRUE; /* it's safe to resize now */
383 can_resize = FALSE; /* it's not safe to resize anymore */
387 display_help(HELP_MAIN);
390 case 'a': add_item(); break;
391 case '\r': edit_item(-1); break;
394 case 'r': ui_remove_items(); break;
395 case 12: refresh_screen(); break;
398 case KEY_UP: scroll_up(); break;
400 case KEY_DOWN: scroll_down(); break;
402 case KEY_PPAGE: page_up(); break;
404 case KEY_NPAGE: page_down(); break;
407 case KEY_HOME: goto_home(); break;
409 case KEY_END: goto_end(); break;
411 case 'w': save_database();
413 case 'l': ui_read_database(); break;
414 case 'i': import_database(); break;
415 case 'e': export_database(); break;
416 case 'C': ui_clear_database(); break;
418 case 'o': ui_open_datafile(); break;
420 case 's': sort_database(); break;
421 case 'S': sort_surname(); break;
423 case '/': ui_find(0); break;
424 case '\\': ui_find(1); break;
426 case ' ': if(curitem >= 0) {
427 selected[curitem] = !selected[curitem];
428 ui_print_number_of_items();
432 case '+': select_all();
435 case '-': select_none();
438 case '*': invert_selection();
441 case 'A': move_curitem(MOVE_ITEM_UP);
443 case 'Z': move_curitem(MOVE_ITEM_DOWN);
446 case 'm': launch_mutt(); break;
448 case 'p': ui_print_database(); break;
450 case 'u': launch_lynx(); break;
459 if( items < 1 || curitem < 0 )
462 statusline_addstr("Remove selected item(s) (Y/n)");
472 remove_selected_items();
482 statusline_addstr("Clear WHOLE database (y/N)");
500 static char findstr[81];
507 statusline_addstr("/");
508 statusline_getnstr(findstr, 67, 0);
512 if( (item = find_item(findstr, next ? curitem+1 : curitem)) >= 0 ) {
521 ui_print_number_of_items()
523 char *str = mkstr(" " "|%3d/%3d", selected_items(), items);
525 mvaddstr(0, COLS-strlen(str), str);
534 statusline_addstr("Your current data will be lost - Press 'y' to continue");
538 default: clear_statusline();
544 load_database(datafile);
553 char *command = options_get_str("print_command");
555 statusline_addstr("Print addressbook? (y/N)");
560 default: clear_statusline(); return;
564 if( ! *command || (handle = popen(command, "w")) == NULL)
567 fexport("text", handle);
578 filename = ask_filename("File to open: ", 1);
585 if( options_get_int("autosave") )
588 statusline_addstr("Save current database (y/N)");
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);