2 * $Id: tailbox.c,v 1.56 2010/04/28 20:52:20 tom Exp $
4 * tailbox.c -- implements the tail box
6 * Copyright 2000-2009,2010 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)
36 char line[MAX_LEN + 1];
40 * Return current line of text.
43 get_line(MY_OBJ * obj)
45 FILE *fp = obj->obj.input;
46 int col = -(obj->hscroll);
50 if (((ch = getc(fp)) == EOF) && !feof(fp))
51 dlg_exiterr("Error moving file pointer in get_line().");
52 else if (!feof(fp) && (ch != '\n')) {
53 if ((ch == TAB) && (dialog_vars.tab_correct)) {
54 tmpint = dialog_state.tab_len
55 - ((col + obj->hscroll) % dialog_state.tab_len);
56 for (j = 0; j < tmpint; j++) {
57 if (col >= 0 && col < MAX_LEN)
63 obj->line[col] = (char) ch;
69 } while (!feof(fp) && (ch != '\n'));
73 obj->line[col] = '\0';
79 * Print a new line of text.
82 print_line(MY_OBJ * obj, WINDOW *win, int row, int width)
85 char *line = get_line(obj);
87 (void) wmove(win, row, 0); /* move cursor to correct line */
88 (void) waddch(win, ' ');
89 #ifdef NCURSES_VERSION
90 (void) waddnstr(win, line, MIN((int) strlen(line), width - 2));
92 line[MIN((int) strlen(line), width - 2)] = '\0';
97 /* Clear 'residue' of previous line */
98 for (i = 0; i < width - x; i++)
99 (void) waddch(win, ' ');
103 * Go back 'target' lines in text file. BUFSIZ has to be in 'size_t' range.
106 last_lines(MY_OBJ * obj, int target)
108 FILE *fp = obj->obj.input;
111 char buf[BUFSIZ + 1];
116 if (fseek(fp, 0, SEEK_END) == -1
117 || (fpos = ftell(fp)) < 0)
118 dlg_exiterr("Error moving file pointer in last_lines().");
123 if (fpos >= BUFSIZ) {
124 size_to_read = BUFSIZ;
128 fpos = fpos - size_to_read;
129 if (fseek(fp, fpos, SEEK_SET) == -1)
130 dlg_exiterr("Error moving file pointer in last_lines().");
131 (void) fread(buf, (size_t) size_to_read, 1, fp);
133 dlg_exiterr("Error reading file in last_lines().");
135 offset += size_to_read;
136 for (inx = size_to_read - 1; inx >= 0; --inx) {
137 if (buf[inx] == '\n') {
138 if (++count > target)
144 if (count > target) {
146 } else if (fpos == 0) {
152 if (fseek(fp, fpos + offset, SEEK_SET) == -1)
153 dlg_exiterr("Error moving file pointer in last_lines().");
158 * Print a new page of text.
161 print_page(MY_OBJ * obj, int height, int width)
165 for (i = 0; i < height; i++) {
166 print_line(obj, obj->text, i, width);
168 (void) wnoutrefresh(obj->text);
172 print_last_page(MY_OBJ * obj)
174 int high = getmaxy(obj->obj.win) - (2 * MARGIN + (obj->obj.bg_task ? 1 : 3));
175 int wide = getmaxx(obj->text);
177 last_lines(obj, high);
178 print_page(obj, high, wide);
182 repaint_text(MY_OBJ * obj)
186 getyx(obj->obj.win, cur_y, cur_x);
187 obj->old_hscroll = obj->hscroll;
188 print_last_page(obj);
189 (void) wmove(obj->obj.win, cur_y, cur_x); /* Restore cursor position */
190 wrefresh(obj->obj.win);
194 handle_my_getc(DIALOG_CALLBACK * cb, int ch, int fkey, int *result)
196 MY_OBJ *obj = (MY_OBJ *) cb;
199 if (!fkey && dlg_char_to_button(ch, obj->buttons) == 0) {
207 *result = DLG_EXIT_OK;
210 case DLGK_BEGIN: /* Beginning of line */
213 case DLGK_GRID_LEFT: /* Scroll left */
214 if (obj->hscroll > 0) {
218 case DLGK_GRID_RIGHT: /* Scroll right */
219 if (obj->hscroll < MAX_LEN)
226 if ((obj->hscroll != obj->old_hscroll))
232 ch = getc(cb->input);
233 (void) ungetc(ch, cb->input);
234 if ((ch != EOF) || (obj->hscroll != obj->old_hscroll)) {
240 *result = DLG_EXIT_ESC;
252 * Display text from a file in a dialog box, like in a "tail -f".
255 dialog_tailbox(const char *title, const char *file, int height, int width, int bg_task)
258 static DLG_KEYS_BINDING binding[] = {
260 DLG_KEYS_DATA( DLGK_BEGIN, '0' ),
261 DLG_KEYS_DATA( DLGK_BEGIN, KEY_BEG ),
262 DLG_KEYS_DATA( DLGK_GRID_LEFT, 'H' ),
263 DLG_KEYS_DATA( DLGK_GRID_LEFT, 'h' ),
264 DLG_KEYS_DATA( DLGK_GRID_LEFT, KEY_LEFT ),
265 DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'L' ),
266 DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ),
267 DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ),
273 int old_height = height;
274 int old_width = width;
277 int x, y, result, thigh;
278 WINDOW *dialog, *text;
279 const char **buttons = 0;
284 /* Open input file for reading */
285 if ((fd = fopen(file, "rb")) == NULL)
286 dlg_exiterr("Can't open input file in dialog_tailbox().");
291 dlg_auto_sizefile(title, file, &height, &width, 2, min_width);
292 dlg_print_size(height, width);
293 dlg_ctl_size(height, width);
295 x = dlg_box_x_ordinate(width);
296 y = dlg_box_y_ordinate(height);
297 thigh = height - ((2 * MARGIN) + (bg_task ? 0 : 2));
299 dialog = dlg_new_window(height, width, y, x);
301 dlg_mouse_setbase(x, y);
303 /* Create window for text region, used for scrolling text */
304 text = dlg_sub_window(dialog,
306 width - (2 * MARGIN),
310 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
311 dlg_draw_bottom_box(dialog);
312 dlg_draw_title(dialog, title);
315 buttons = dlg_exit_label();
316 dlg_button_layout(buttons, &min_width);
317 dlg_draw_buttons(dialog, height - (2 * MARGIN), 0, buttons, FALSE,
321 (void) wmove(dialog, thigh, (MARGIN + 1));
322 (void) wnoutrefresh(dialog);
324 obj = dlg_calloc(MY_OBJ, 1);
325 assert_ptr(obj, "dialog_tailbox");
328 obj->obj.win = dialog;
329 obj->obj.handle_getc = handle_my_getc;
330 obj->obj.keep_bg = bg_task && dialog_vars.cant_kill;
331 obj->obj.bg_task = bg_task;
333 obj->buttons = buttons;
334 dlg_add_callback(&(obj->obj));
336 dlg_register_window(dialog, "tailbox", binding);
337 dlg_register_buttons(dialog, "tailbox", buttons);
339 /* Print last page of text */
340 dlg_attr_clear(text, thigh, getmaxx(text), dialog_attr);
344 result = DLG_EXIT_OK;
348 ch = dlg_getc(dialog, &fkey);
350 if (fkey && ch == KEY_RESIZE) {
356 dlg_del_window(dialog);
358 dlg_mouse_free_regions();
359 dlg_button_layout(buttons, &min_width);
364 while (handle_my_getc(&(obj->obj), ch, fkey, &result));
366 dlg_mouse_free_regions();