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