aboutsummaryrefslogtreecommitdiff
path: root/contrib/bsddialog/examples_library/gauge.c
blob: 3b38a80f3f2ca0174c0cc2dad7cf318c2379d81a (plain) (blame)
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
/*-
 * SPDX-License-Identifier: CC0-1.0
 *
 * Written in 2023 by Alfonso Sabato Siciliano.
 * To the extent possible under law, the author has dedicated all copyright
 * and related and neighboring rights to this software to the public domain
 * worldwide. This software is distributed without any warranty, see:
 *   <http://creativecommons.org/publicdomain/zero/1.0/>.
 */

#include <bsddialog.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static void sender(int fd)
{
	int i;

	for (i = 1; i <= 10; i++) {
		sleep(1);
		dprintf(fd, "SEP\n");
		dprintf(fd, "%d\n", i * 10);
		dprintf(fd, "In Progress... [%d / 10]\n", i);
		dprintf(fd, "SEP\n");
	}
	sleep(1);
	dprintf(fd, "EOF\n");
}

int main()
{
	int rv, fd[2];
	struct bsddialog_conf conf;

	/* add checks and sync */
	pipe(fd);
	if (fork() == 0) {
		close(fd[0]);
		sender(fd[1]);
		exit (0);
	}
	close(fd[1]);

	if (bsddialog_init() == BSDDIALOG_ERROR) {
		printf("Error: %s\n", bsddialog_geterror());
		return (1);
	}
	bsddialog_initconf(&conf);
	conf.title = "gauge";
	rv = bsddialog_gauge(&conf, "Example", 7, 30, 0, fd[0], "SEP", "EOF");
	bsddialog_end();
	if(rv == BSDDIALOG_ERROR)
		printf("Error: %s\n", bsddialog_geterror());

	return (0);
}