2 * $Id: tailbox.c,v 1.63 2011/06/27 08:19:43 tom Exp $
4 * tailbox.c -- implements the tail box
6 * Copyright 2000-2010,2011 Thomas E. Dickey
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License, version 2.1
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
23 * An earlier version of this program lists as authors
24 * Pasquale De Marco (demarco_p@abramo.it)
37 char line[MAX_LEN + 1];
42 * Return current line of text.
45 get_line(MY_OBJ * obj)
47 FILE *fp = obj->obj.input;
48 int col = -(obj->hscroll);
52 if (((ch = getc(fp)) == EOF) && !feof(fp))
53 dlg_exiterr("Error moving file pointer in get_line().");
54 else if (!feof(fp) && (ch != '\n')) {
55 if ((ch == TAB) && (dialog_vars.tab_correct)) {
56 tmpint = dialog_state.tab_len
57 - ((col + obj->hscroll) % dialog_state.tab_len);
58 for (j = 0; j < tmpint; j++) {
59 if (col >= 0 && col < MAX_LEN)
65 obj->line[col] = (char) ch;
71 } while (!feof(fp) && (ch != '\n'));
75 obj->line[col] = '\0';
81 * Print a new line of text.
84 print_line(MY_OBJ * obj, WINDOW *win, int row, int width)
87 char *line = get_line(obj);
89 (void) wmove(win, row, 0); /* move cursor to correct line */
90 (void) waddch(win, ' ');
91 #ifdef NCURSES_VERSION
92 (void) waddnstr(win, line, MIN((int) strlen(line), width - 2));
94 line[MIN((int) strlen(line), width - 2)] = '\0';
99 /* Clear 'residue' of previous line */
100 for (i = 0; i < width - x; i++)
101 (void) waddch(win, ' ');
105 * Go back 'target' lines in text file. BUFSIZ has to be in 'size_t' range.
108 last_lines(MY_OBJ * obj, int target)
110 FILE *fp = obj->obj.input;
113 char buf[BUFSIZ + 1];
119 if (fseek(fp, 0L, SEEK_END) == -1
120 || (fpos = ftell(fp)) < 0)
121 dlg_exiterr("Error moving file pointer in last_lines().");
126 if (fpos >= BUFSIZ) {
127 size_to_read = BUFSIZ;
129 size_to_read = (size_t) fpos;
131 fpos = fpos - (long) size_to_read;
132 if (fseek(fp, fpos, SEEK_SET) == -1)
133 dlg_exiterr("Error moving file pointer in last_lines().");
134 size_as_read = fread(buf, sizeof(char), size_to_read, fp);
136 dlg_exiterr("Error reading file in last_lines().");
138 if (size_as_read == 0) {
144 offset += (long) size_as_read;
145 for (inx = size_as_read - 1; inx != 0; --inx) {
146 if (buf[inx] == '\n') {
147 if (++count > target)
149 offset = (long) (inx + 1);
153 if (count > target) {
155 } else if (fpos == 0) {
161 if (fseek(fp, fpos + offset, SEEK_SET) == -1)
162 dlg_exiterr("Error moving file pointer in last_lines().");
167 * Print a new page of text.
170 print_page(MY_OBJ * obj, int height, int width)
174 for (i = 0; i < height; i++) {
175 print_line(obj, obj->text, i, width);
177 (void) wnoutrefresh(obj->text);
181 print_last_page(MY_OBJ * obj)
183 int high = getmaxy(obj->obj.win) - (2 * MARGIN + (obj->obj.bg_task ? 1 : 3));
184 int wide = getmaxx(obj->text);
186 last_lines(obj, high);
187 print_page(obj, high, wide);
191 repaint_text(MY_OBJ * obj)
193 FILE *fp = obj->obj.input;
196 getyx(obj->obj.win, cur_y, cur_x);
197 obj->old_hscroll = obj->hscroll;
199 print_last_page(obj);
200 obj->last_pos = ftell(fp);
202 (void) wmove(obj->obj.win, cur_y, cur_x); /* Restore cursor position */
203 wrefresh(obj->obj.win);
207 handle_input(DIALOG_CALLBACK * cb)
209 MY_OBJ *obj = (MY_OBJ *) cb;
210 FILE *fp = obj->obj.input;
213 if (fstat(fileno(fp), &sb) == 0
214 && sb.st_size != obj->last_pos) {
222 handle_my_getc(DIALOG_CALLBACK * cb, int ch, int fkey, int *result)
224 MY_OBJ *obj = (MY_OBJ *) cb;
227 if (!fkey && dlg_char_to_button(ch, obj->buttons) == 0) {
235 *result = DLG_EXIT_OK;
238 case DLGK_BEGIN: /* Beginning of line */
241 case DLGK_GRID_LEFT: /* Scroll left */
242 if (obj->hscroll > 0) {
246 case DLGK_GRID_RIGHT: /* Scroll right */
247 if (obj->hscroll < MAX_LEN)
254 if ((obj->hscroll != obj->old_hscroll))
260 ch = getc(cb->input);
261 (void) ungetc(ch, cb->input);
268 *result = DLG_EXIT_ESC;
280 * Display text from a file in a dialog box, like in a "tail -f".
283 dialog_tailbox(const char *title, const char *file, int height, int width, int bg_task)
286 static DLG_KEYS_BINDING binding[] = {
289 DLG_KEYS_DATA( DLGK_BEGIN, '0' ),
290 DLG_KEYS_DATA( DLGK_BEGIN, KEY_BEG ),
291 DLG_KEYS_DATA( DLGK_GRID_LEFT, 'H' ),
292 DLG_KEYS_DATA( DLGK_GRID_LEFT, 'h' ),
293 DLG_KEYS_DATA( DLGK_GRID_LEFT, KEY_LEFT ),
294 DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'L' ),
295 DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ),
296 DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ),
302 int old_height = height;
303 int old_width = width;
306 int x, y, result, thigh;
307 WINDOW *dialog, *text;
308 const char **buttons = 0;
313 /* Open input file for reading */
314 if ((fd = fopen(file, "rb")) == NULL)
315 dlg_exiterr("Can't open input file in dialog_tailbox().");
320 dlg_auto_sizefile(title, file, &height, &width, 2, min_width);
321 dlg_print_size(height, width);
322 dlg_ctl_size(height, width);
324 x = dlg_box_x_ordinate(width);
325 y = dlg_box_y_ordinate(height);
326 thigh = height - ((2 * MARGIN) + (bg_task ? 0 : 2));
328 dialog = dlg_new_window(height, width, y, x);
330 dlg_mouse_setbase(x, y);
332 /* Create window for text region, used for scrolling text */
333 text = dlg_sub_window(dialog,
335 width - (2 * MARGIN),
339 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
340 dlg_draw_bottom_box(dialog);
341 dlg_draw_title(dialog, title);
342 dlg_draw_helpline(dialog, FALSE);
345 buttons = dlg_exit_label();
346 dlg_button_layout(buttons, &min_width);
347 dlg_draw_buttons(dialog, height - (2 * MARGIN), 0, buttons, FALSE,
351 (void) wmove(dialog, thigh, (MARGIN + 1));
352 (void) wnoutrefresh(dialog);
354 obj = dlg_calloc(MY_OBJ, 1);
355 assert_ptr(obj, "dialog_tailbox");
358 obj->obj.win = dialog;
359 obj->obj.handle_getc = handle_my_getc;
360 obj->obj.handle_input = bg_task ? handle_input : 0;
361 obj->obj.keep_bg = bg_task && dialog_vars.cant_kill;
362 obj->obj.bg_task = bg_task;
364 obj->buttons = buttons;
365 dlg_add_callback(&(obj->obj));
367 dlg_register_window(dialog, "tailbox", binding);
368 dlg_register_buttons(dialog, "tailbox", buttons);
370 /* Print last page of text */
371 dlg_attr_clear(text, thigh, getmaxx(text), dialog_attr);
375 result = DLG_EXIT_OK;
379 ch = dlg_getc(dialog, &fkey);
381 if (fkey && ch == KEY_RESIZE) {
387 dlg_del_window(dialog);
389 dlg_mouse_free_regions();
390 dlg_button_layout(buttons, &min_width);
395 while (handle_my_getc(&(obj->obj), ch, fkey, &result));
397 dlg_mouse_free_regions();