2 * $Id: tailbox.c,v 1.72 2018/06/19 22:57:01 tom Exp $
4 * tailbox.c -- implements the tail box
6 * Copyright 2000-2012,2018 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 + 2];
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 (void) waddnstr(win, line, MIN((int) strlen(line), width - 2));
95 /* Clear 'residue' of previous line */
96 for (i = 0; i < width - x; i++)
97 (void) waddch(win, ' ');
101 * Go back 'target' lines in text file. BUFSIZ has to be in 'size_t' range.
104 last_lines(MY_OBJ * obj, int target)
106 FILE *fp = obj->obj.input;
109 char buf[BUFSIZ + 1];
115 if (fseek(fp, 0L, SEEK_END) == -1
116 || (fpos = ftell(fp)) < 0)
117 dlg_exiterr("Error moving file pointer in last_lines().");
122 if (fpos >= BUFSIZ) {
123 size_to_read = BUFSIZ;
125 size_to_read = (size_t) fpos;
127 fpos = fpos - (long) size_to_read;
128 if (fseek(fp, fpos, SEEK_SET) == -1)
129 dlg_exiterr("Error moving file pointer in last_lines().");
130 size_as_read = fread(buf, sizeof(char), size_to_read, fp);
132 dlg_exiterr("Error reading file in last_lines().");
134 if (size_as_read == 0) {
140 offset += (long) size_as_read;
141 for (inx = size_as_read - 1; inx != 0; --inx) {
142 if (buf[inx] == '\n') {
143 if (++count > target)
145 offset = (long) (inx + 1);
149 if (count > target) {
151 } else if (fpos == 0) {
157 if (fseek(fp, fpos + offset, SEEK_SET) == -1)
158 dlg_exiterr("Error moving file pointer in last_lines().");
163 * Print a new page of text.
166 print_page(MY_OBJ * obj, int height, int width)
170 for (i = 0; i < height; i++) {
171 print_line(obj, obj->text, i, width);
173 (void) wnoutrefresh(obj->text);
177 print_last_page(MY_OBJ * obj)
179 int high = getmaxy(obj->obj.win) - (2 * MARGIN + (obj->obj.bg_task ? 1 : 3));
180 int wide = getmaxx(obj->text);
182 last_lines(obj, high);
183 print_page(obj, high, wide);
187 repaint_text(MY_OBJ * obj)
189 FILE *fp = obj->obj.input;
192 getyx(obj->obj.win, cur_y, cur_x);
193 obj->old_hscroll = obj->hscroll;
195 print_last_page(obj);
196 obj->last_pos = ftell(fp);
198 (void) wmove(obj->obj.win, cur_y, cur_x); /* Restore cursor position */
199 wrefresh(obj->obj.win);
203 handle_input(DIALOG_CALLBACK * cb)
205 MY_OBJ *obj = (MY_OBJ *) cb;
206 FILE *fp = obj->obj.input;
209 if (fstat(fileno(fp), &sb) == 0
210 && sb.st_size != obj->last_pos) {
218 valid_callback(DIALOG_CALLBACK * cb)
222 for (p = dialog_state.getc_callbacks; p != 0; p = p->next) {
232 handle_my_getc(DIALOG_CALLBACK * cb, int ch, int fkey, int *result)
234 MY_OBJ *obj = (MY_OBJ *) cb;
237 if (!valid_callback(cb))
240 if (!fkey && dlg_char_to_button(ch, obj->buttons) == 0) {
248 *result = DLG_EXIT_OK;
251 case DLGK_BEGIN: /* Beginning of line */
254 case DLGK_GRID_LEFT: /* Scroll left */
255 if (obj->hscroll > 0) {
259 case DLGK_GRID_RIGHT: /* Scroll right */
260 if (obj->hscroll < MAX_LEN)
267 if ((obj->hscroll != obj->old_hscroll))
273 ch = getc(cb->input);
274 (void) ungetc(ch, cb->input);
281 *result = DLG_EXIT_ESC;
293 * Display text from a file in a dialog box, like in a "tail -f".
296 dialog_tailbox(const char *title,
297 const char *filename,
303 static DLG_KEYS_BINDING binding[] = {
306 DLG_KEYS_DATA( DLGK_BEGIN, '0' ),
307 DLG_KEYS_DATA( DLGK_BEGIN, KEY_BEG ),
308 DLG_KEYS_DATA( DLGK_GRID_LEFT, 'H' ),
309 DLG_KEYS_DATA( DLGK_GRID_LEFT, 'h' ),
310 DLG_KEYS_DATA( DLGK_GRID_LEFT, KEY_LEFT ),
311 DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'L' ),
312 DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ),
313 DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ),
319 int old_height = height;
320 int old_width = width;
323 int x, y, result, thigh;
324 WINDOW *dialog, *text;
325 const char **buttons = 0;
330 DLG_TRACE(("# tailbox args:\n"));
331 DLG_TRACE2S("title", title);
332 DLG_TRACE2S("filename", filename);
333 DLG_TRACE2N("height", height);
334 DLG_TRACE2N("width", width);
335 DLG_TRACE2N("bg_task", bg_task);
337 /* Open input file for reading */
338 if ((fd = fopen(filename, "rb")) == NULL)
339 dlg_exiterr("Can't open input file in dialog_tailbox().");
344 dlg_auto_sizefile(title, filename, &height, &width, 2, min_width);
345 dlg_print_size(height, width);
346 dlg_ctl_size(height, width);
348 x = dlg_box_x_ordinate(width);
349 y = dlg_box_y_ordinate(height);
350 thigh = height - ((2 * MARGIN) + (bg_task ? 0 : 2));
352 dialog = dlg_new_window(height, width, y, x);
354 dlg_mouse_setbase(x, y);
356 /* Create window for text region, used for scrolling text */
357 text = dlg_sub_window(dialog,
359 width - (2 * MARGIN),
363 dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
364 dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
365 dlg_draw_title(dialog, title);
366 dlg_draw_helpline(dialog, FALSE);
369 buttons = dlg_exit_label();
370 dlg_button_layout(buttons, &min_width);
371 dlg_draw_buttons(dialog, height - (2 * MARGIN), 0, buttons, FALSE,
375 (void) wmove(dialog, thigh, (MARGIN + 1));
376 (void) wnoutrefresh(dialog);
378 obj = dlg_calloc(MY_OBJ, 1);
379 assert_ptr(obj, "dialog_tailbox");
382 obj->obj.win = dialog;
383 obj->obj.handle_getc = handle_my_getc;
384 obj->obj.handle_input = bg_task ? handle_input : 0;
385 obj->obj.keep_bg = bg_task && dialog_vars.cant_kill;
386 obj->obj.bg_task = bg_task;
388 obj->buttons = buttons;
389 dlg_add_callback(&(obj->obj));
391 dlg_register_window(dialog, "tailbox", binding);
392 dlg_register_buttons(dialog, "tailbox", buttons);
394 /* Print last page of text */
395 dlg_attr_clear(text, thigh, getmaxx(text), dialog_attr);
398 dlg_trace_win(dialog);
400 result = DLG_EXIT_OK;
404 ch = dlg_getc(dialog, &fkey);
406 if (fkey && ch == KEY_RESIZE) {
407 dlg_will_resize(dialog);
413 dlg_del_window(dialog);
415 dlg_mouse_free_regions();
416 dlg_button_layout(buttons, &min_width);
421 while (handle_my_getc(&(obj->obj), ch, fkey, &result));
423 dlg_mouse_free_regions();