summaryrefslogtreecommitdiffstats
path: root/lib/libdpv/status.c
blob: 4bf47735653ef90cb2904530a0d5492ec2ea9847 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*-
 * Copyright (c) 2013-2014 Devin Teske <dteske@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <curses.h>
#include <dialog.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#include "dialog_util.h"
#include "status.h"

/* static globals */
static char *status_buf = NULL;
static int status_bufsize = -1;
static int status_row;
static int status_width;

/*
 * Print a `one-liner' status message at the bottom of the screen. Messages are
 * trimmed to fit within the console length (ANSI coloring not accounted for).
 */
void
status_printf(const char *fmt, ...)
{
	int n, attrs;
	chtype color = dlg_color_pair(dlg_color_table[BUTTON_ACTIVE_ATTR].fg,
	    dlg_color_table[SCREEN_ATTR].bg) | A_BOLD;
	va_list args;

	status_row = tty_maxrows() - 1;
	status_width = tty_maxcols();

	/* NULL is a special convention meaning "erase the old stuff" */
	if (fmt == NULL) {
		move(status_row, 0);
		clrtoeol();
		return;
	}

	/* Resize buffer if terminal width is greater */
	if ((status_width + 1) > status_bufsize) {
		status_buf = realloc(status_buf, status_width + 1);
		if (status_buf == NULL) {
			status_bufsize = -1;
			return;
		}
		status_bufsize = status_width + 1;
	}

	/* Print the message within a space-filled buffer */
	memset(status_buf, ' ', status_width);
	va_start(args, fmt);
	n = vsnprintf(status_buf, status_width + 1, fmt, args);
	va_end(args);

	/* If vsnprintf(3) produced less bytes than the maximum, change the
	 * implicitly-added NUL-terminator into a space and terminate at max */
	if (n < status_width) {
		status_buf[n] = ' ';
		status_buf[status_width] = '\0';
	}

	/* Print text in screen bg, button active fg, and bold */
	attrs = getattrs(stdscr);
	attrset(color);
	mvaddstr(status_row, 0, status_buf);
	attrset(attrs);

	/* Seat the cursor over the last character at absolute lower-right */
	move(status_row, status_width - 1);
	refresh();
}

/*
 * Free allocated items initialized by status_printf()
 */
void
status_free(void)
{
	if (status_buf != NULL) {
		free(status_buf);
		status_buf = NULL;
	}
}
OpenPOWER on IntegriCloud