1274116Sdteske/*-
2274116Sdteske * Copyright (c) 2013-2014 Devin Teske <dteske@FreeBSD.org>
3274116Sdteske * All rights reserved.
4274116Sdteske *
5274116Sdteske * Redistribution and use in source and binary forms, with or without
6274116Sdteske * modification, are permitted provided that the following conditions
7274116Sdteske * are met:
8274116Sdteske * 1. Redistributions of source code must retain the above copyright
9274116Sdteske *    notice, this list of conditions and the following disclaimer.
10274116Sdteske * 2. Redistributions in binary form must reproduce the above copyright
11274116Sdteske *    notice, this list of conditions and the following disclaimer in the
12274116Sdteske *    documentation and/or other materials provided with the distribution.
13274116Sdteske *
14274116Sdteske * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15274116Sdteske * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16274116Sdteske * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17274116Sdteske * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18274116Sdteske * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19274116Sdteske * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20274116Sdteske * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21274116Sdteske * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22274116Sdteske * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23274116Sdteske * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24274116Sdteske * SUCH DAMAGE.
25274116Sdteske */
26274116Sdteske
27274116Sdteske#include <sys/cdefs.h>
28274116Sdteske__FBSDID("$FreeBSD$");
29274116Sdteske
30274116Sdteske#include <curses.h>
31274116Sdteske#include <dialog.h>
32274116Sdteske#include <stdarg.h>
33274116Sdteske#include <stdlib.h>
34274116Sdteske#include <string.h>
35274116Sdteske
36274116Sdteske#include "dialog_util.h"
37274116Sdteske#include "status.h"
38274116Sdteske
39274116Sdteske/* static globals */
40274116Sdteskestatic char *status_buf = NULL;
41274116Sdteskestatic int status_bufsize = -1;
42274116Sdteskestatic int status_row;
43274116Sdteskestatic int status_width;
44274116Sdteske
45274116Sdteske/*
46274116Sdteske * Print a `one-liner' status message at the bottom of the screen. Messages are
47274116Sdteske * trimmed to fit within the console length (ANSI coloring not accounted for).
48274116Sdteske */
49274116Sdteskevoid
50274116Sdteskestatus_printf(const char *fmt, ...)
51274116Sdteske{
52274116Sdteske	int n, attrs;
53274116Sdteske	chtype color = dlg_color_pair(dlg_color_table[BUTTON_ACTIVE_ATTR].fg,
54274116Sdteske	    dlg_color_table[SCREEN_ATTR].bg) | A_BOLD;
55274116Sdteske	va_list args;
56274116Sdteske
57274116Sdteske	status_row = tty_maxrows() - 1;
58274116Sdteske	status_width = tty_maxcols();
59274116Sdteske
60274116Sdteske	/* NULL is a special convention meaning "erase the old stuff" */
61274116Sdteske	if (fmt == NULL) {
62274116Sdteske		move(status_row, 0);
63274116Sdteske		clrtoeol();
64274116Sdteske		return;
65274116Sdteske	}
66274116Sdteske
67274116Sdteske	/* Resize buffer if terminal width is greater */
68274116Sdteske	if ((status_width + 1) > status_bufsize) {
69274116Sdteske		status_buf = realloc(status_buf, status_width + 1);
70274116Sdteske		if (status_buf == NULL) {
71274116Sdteske			status_bufsize = -1;
72274116Sdteske			return;
73274116Sdteske		}
74274116Sdteske		status_bufsize = status_width + 1;
75274116Sdteske	}
76274116Sdteske
77274116Sdteske	/* Print the message within a space-filled buffer */
78274116Sdteske	memset(status_buf, ' ', status_width);
79274116Sdteske	va_start(args, fmt);
80274116Sdteske	n = vsnprintf(status_buf, status_width + 1, fmt, args);
81274116Sdteske	va_end(args);
82274116Sdteske
83274116Sdteske	/* If vsnprintf(3) produced less bytes than the maximum, change the
84274116Sdteske	 * implicitly-added NUL-terminator into a space and terminate at max */
85274116Sdteske	if (n < status_width) {
86274116Sdteske		status_buf[n] = ' ';
87274116Sdteske		status_buf[status_width] = '\0';
88274116Sdteske	}
89274116Sdteske
90274116Sdteske	/* Print text in screen bg, button active fg, and bold */
91274116Sdteske	attrs = getattrs(stdscr);
92274116Sdteske	attrset(color);
93274116Sdteske	mvaddstr(status_row, 0, status_buf);
94274116Sdteske	attrset(attrs);
95274116Sdteske
96274116Sdteske	/* Seat the cursor over the last character at absolute lower-right */
97274116Sdteske	move(status_row, status_width - 1);
98274116Sdteske	refresh();
99274116Sdteske}
100274116Sdteske
101274116Sdteske/*
102274116Sdteske * Free allocated items initialized by status_printf()
103274116Sdteske */
104274116Sdteskevoid
105274116Sdteskestatus_free(void)
106274116Sdteske{
107274116Sdteske	if (status_buf != NULL) {
108274116Sdteske		free(status_buf);
109274116Sdteske		status_buf = NULL;
110274116Sdteske	}
111274116Sdteske}
112