1 /*- 2 * Copyright (c) 2013-2014 Devin Teske <dteske@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 #include <curses.h> 29 #include <dialog.h> 30 #include <stdarg.h> 31 #include <stdlib.h> 32 #include <string.h> 33 34 #include "dialog_util.h" 35 #include "status.h" 36 37 /* static globals */ 38 static char *status_buf = NULL; 39 static int status_bufsize = -1; 40 static int status_row; 41 static int status_width; 42 43 /* 44 * Print a `one-liner' status message at the bottom of the screen. Messages are 45 * trimmed to fit within the console length (ANSI coloring not accounted for). 46 */ 47 void 48 status_printf(const char *fmt, ...) 49 { 50 int n, attrs; 51 chtype color = dlg_color_pair(dlg_color_table[BUTTON_ACTIVE_ATTR].fg, 52 dlg_color_table[SCREEN_ATTR].bg) | A_BOLD; 53 va_list args; 54 55 status_row = tty_maxrows() - 1; 56 status_width = tty_maxcols(); 57 58 /* NULL is a special convention meaning "erase the old stuff" */ 59 if (fmt == NULL) { 60 move(status_row, 0); 61 clrtoeol(); 62 return; 63 } 64 65 /* Resize buffer if terminal width is greater */ 66 if ((status_width + 1) > status_bufsize) { 67 status_buf = realloc(status_buf, status_width + 1); 68 if (status_buf == NULL) { 69 status_bufsize = -1; 70 return; 71 } 72 status_bufsize = status_width + 1; 73 } 74 75 /* Print the message within a space-filled buffer */ 76 memset(status_buf, ' ', status_width); 77 va_start(args, fmt); 78 n = vsnprintf(status_buf, status_width + 1, fmt, args); 79 va_end(args); 80 81 /* If vsnprintf(3) produced less bytes than the maximum, change the 82 * implicitly-added NUL-terminator into a space and terminate at max */ 83 if (n < status_width) { 84 status_buf[n] = ' '; 85 status_buf[status_width] = '\0'; 86 } 87 88 /* Print text in screen bg, button active fg, and bold */ 89 attrs = getattrs(stdscr); 90 attrset(color); 91 mvaddstr(status_row, 0, status_buf); 92 attrset(attrs); 93 94 /* Seat the cursor over the last character at absolute lower-right */ 95 move(status_row, status_width - 1); 96 refresh(); 97 } 98 99 /* 100 * Free allocated items initialized by status_printf() 101 */ 102 void 103 status_free(void) 104 { 105 if (status_buf != NULL) { 106 free(status_buf); 107 status_buf = NULL; 108 } 109 } 110