2 * $Id: tailbox.c,v 1.79 2020/11/21 21:31:49 tom Exp $
4 * tailbox.c -- implements the tail box
6 * Copyright 2000-2019,2020 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 if (fseek(fp, 0L, SEEK_END) == -1
110 || (fpos = ftell(fp)) < 0)
111 dlg_exiterr("Error moving file pointer in last_lines().");
122 char buf[BUFSIZ + 1];
124 if (fpos >= BUFSIZ) {
125 size_to_read = BUFSIZ;
127 size_to_read = (size_t) fpos;
129 fpos = fpos - (long) size_to_read;
130 if (fseek(fp, fpos, SEEK_SET) == -1)
131 dlg_exiterr("Error moving file pointer in last_lines().");
132 size_as_read = fread(buf, sizeof(char), size_to_read, fp);
134 dlg_exiterr("Error reading file in last_lines().");
136 if (size_as_read == 0) {
142 offset += (long) size_as_read;
143 for (inx = size_as_read - 1; inx != 0; --inx) {
144 if (buf[inx] == '\n') {
145 if (++count > target)
147 offset = (long) (inx + 1);
151 if (count > target) {
153 } else if (fpos == 0) {
159 if (fseek(fp, fpos + offset, SEEK_SET) == -1)
160 dlg_exiterr("Error moving file pointer in last_lines().");
165 * Print a new page of text.
168 print_page(MY_OBJ * obj, int height, int width)
172 for (i = 0; i < height; i++) {
173 print_line(obj, obj->text, i, width);
175 (void) wnoutrefresh(obj->text);
179 print_last_page(MY_OBJ * obj)
181 int high = getmaxy(obj->obj.win) - (2 * MARGIN + (obj->obj.bg_task ? 1 : 3));
182 int wide = getmaxx(obj->text);
184 last_lines(obj, high);
185 print_page(obj, high, wide);
189 repaint_text(MY_OBJ * obj)
191 FILE *fp = obj->obj.input;
194 getyx(obj->obj.win, cur_y, cur_x);
195 obj->old_hscroll = obj->hscroll;
197 print_last_page(obj);
198 obj->last_pos = ftell(fp);
200 (void) wmove(obj->obj.win, cur_y, cur_x); /* Restore cursor position */
201 wrefresh(obj->obj.win);
205 handle_input(DIALOG_CALLBACK * cb)
207 MY_OBJ *obj = (MY_OBJ *) cb;
208 FILE *fp = obj->obj.input;
211 if (fstat(fileno(fp), &sb) == 0
212 && sb.st_size != obj->last_pos) {
220 valid_callback(DIALOG_CALLBACK * cb)
224 for (p = dialog_state.getc_callbacks; p != 0; p = p->next) {
234 handle_my_getc(DIALOG_CALLBACK * cb, int ch, int fkey, int *result)
236 MY_OBJ *obj = (MY_OBJ *) cb;
239 if (!valid_callback(cb))
242 if (!fkey && dlg_char_to_button(ch, obj->buttons) == 0) {
250 *result = DLG_EXIT_OK;
253 case DLGK_BEGIN: /* Beginning of line */
256 case DLGK_GRID_LEFT: /* Scroll left */
257 if (obj->hscroll > 0) {
261 case DLGK_GRID_RIGHT: /* Scroll right */
262 if (obj->hscroll < MAX_LEN)
266 if (is_DLGK_MOUSE(ch)) {
267 *result = dlg_ok_buttoncode(ch - M_EVENT);
268 if (*result != DLG_EXIT_ERROR) {
278 if ((obj->hscroll != obj->old_hscroll))
284 ch = getc(cb->input);
285 (void) ungetc(ch, cb->input);
292 *result = DLG_EXIT_ESC;
304 * Display text from a file in a dialog box, like in a "tail -f".
307 dialog_tailbox(const char *title,
308 const char *filename,
314 static DLG_KEYS_BINDING binding[] = {
317 DLG_KEYS_DATA( DLGK_BEGIN, '0' ),
318 DLG_KEYS_DATA( DLGK_BEGIN, KEY_BEG ),
319 DLG_KEYS_DATA( DLGK_GRID_LEFT, 'H' ),
320 DLG_KEYS_DATA( DLGK_GRID_LEFT, 'h' ),
321 DLG_KEYS_DATA( DLGK_GRID_LEFT, KEY_LEFT ),
322 DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'L' ),
323 DLG_KEYS_DATA( DLGK_GRID_RIGHT, 'l' ),
324 DLG_KEYS_DATA( DLGK_GRID_RIGHT, KEY_RIGHT ),
330 int old_height = height;
331 int old_width = width;
334 int x, y, result = DLG_EXIT_UNKNOWN, thigh;
335 WINDOW *dialog, *text;
336 const char **buttons = 0;
341 DLG_TRACE(("# tailbox args:\n"));
342 DLG_TRACE2S("title", title);
343 DLG_TRACE2S("filename", filename);
344 DLG_TRACE2N("height", height);
345 DLG_TRACE2N("width", width);
346 DLG_TRACE2N("bg_task", bg_task);
348 /* Open input file for reading */
349 if ((fd = fopen(filename, "rb")) == NULL)
350 dlg_exiterr("Can't open input file in dialog_tailbox().");
355 dlg_auto_sizefile(title, filename, &height, &width, 2, min_width);
356 dlg_print_size(height, width);
357 dlg_ctl_size(height, width);
359 x = dlg_box_x_ordinate(width);
360 y = dlg_box_y_ordinate(height);
361 thigh = height - ((2 * MARGIN) + (bg_task ? 0 : 2));
363 dialog = dlg_new_window(height, width, y, x);
365 dlg_mouse_setbase(x, y);
367 /* Create window for text region, used for scrolling text */
368 text = dlg_sub_window(dialog,
370 width - (2 * MARGIN),
374 dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
375 dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
376 dlg_draw_title(dialog, title);
377 dlg_draw_helpline(dialog, FALSE);
380 buttons = dlg_exit_label();
381 dlg_button_layout(buttons, &min_width);
382 dlg_draw_buttons(dialog, height - (2 * MARGIN), 0, buttons, FALSE,
386 (void) wmove(dialog, thigh, (MARGIN + 1));
387 (void) wnoutrefresh(dialog);
389 obj = dlg_calloc(MY_OBJ, 1);
390 assert_ptr(obj, "dialog_tailbox");
393 obj->obj.win = dialog;
394 obj->obj.handle_getc = handle_my_getc;
395 obj->obj.handle_input = bg_task ? handle_input : 0;
396 obj->obj.keep_bg = bg_task && dialog_vars.cant_kill;
397 obj->obj.bg_task = (bool) bg_task;
399 obj->buttons = buttons;
400 dlg_add_callback(&(obj->obj));
402 dlg_register_window(dialog, "tailbox", binding);
403 dlg_register_buttons(dialog, "tailbox", buttons);
405 /* Print last page of text */
406 dlg_attr_clear(text, thigh, getmaxx(text), dialog_attr);
409 dlg_trace_win(dialog);
411 result = DLG_EXIT_OK;
415 ch = dlg_mouse_wgetch(dialog, &fkey);
417 if (fkey && ch == KEY_RESIZE) {
418 dlg_will_resize(dialog);
423 _dlg_resize_cleanup(dialog);
424 dlg_button_layout(buttons, &min_width);
429 while (handle_my_getc(&(obj->obj), ch, fkey, &result));
431 dlg_mouse_free_regions();