xref: /freebsd/contrib/libxo/tests/core/test_12.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*
2  * Copyright (c) 2014, Juniper Networks, Inc.
3  * All rights reserved.
4  * This SOFTWARE is licensed under the LICENSE provided in the
5  * ../Copyright file. By downloading, installing, copying, or otherwise
6  * using the SOFTWARE, you agree to be bound by the terms of that
7  * LICENSE.
8  * Phil Shafer, July 2014
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stdint.h>
14 #include <string.h>
15 
16 #include "xo_config.h"
17 #include "xo.h"
18 #include "xo_encoder.h"
19 
20 int
21 main (int argc, char **argv)
22 {
23     int i, count = 10;
24     int mon = 0;
25     xo_emit_flags_t flags = XOEF_RETAIN;
26     int opt_color = 1;
27 
28     xo_set_program("test_12");
29 
30     argc = xo_parse_args(argc, argv);
31     if (argc < 0)
32 	return 1;
33 
34     for (argc = 1; argv[argc]; argc++) {
35 	if (xo_streq(argv[argc], "xml"))
36 	    xo_set_style(NULL, XO_STYLE_XML);
37 	else if (xo_streq(argv[argc], "json"))
38 	    xo_set_style(NULL, XO_STYLE_JSON);
39 	else if (xo_streq(argv[argc], "text"))
40 	    xo_set_style(NULL, XO_STYLE_TEXT);
41 	else if (xo_streq(argv[argc], "html"))
42 	    xo_set_style(NULL, XO_STYLE_HTML);
43 	else if (xo_streq(argv[argc], "no-color"))
44 	    opt_color = 0;
45 	else if (xo_streq(argv[argc], "pretty"))
46 	    xo_set_flags(NULL, XOF_PRETTY);
47 	else if (xo_streq(argv[argc], "xpath"))
48 	    xo_set_flags(NULL, XOF_XPATH);
49 	else if (xo_streq(argv[argc], "info"))
50 	    xo_set_flags(NULL, XOF_INFO);
51 	else if (xo_streq(argv[argc], "no-retain"))
52 	    flags &= ~XOEF_RETAIN;
53 	else if (xo_streq(argv[argc], "big")) {
54 	    if (argv[argc + 1])
55 		count = atoi(argv[++argc]);
56 	}
57     }
58 
59     xo_set_flags(NULL, XOF_UNITS); /* Always test w/ this */
60     if (opt_color)
61 	xo_set_flags(NULL, XOF_COLOR); /* Force color output */
62     xo_set_file(stdout);
63 
64     xo_open_container("top");
65     xo_open_container("data");
66 
67     xo_emit("{C:fg-red,bg-green}Merry XMas!!{C:}\n");
68 
69     xo_emit("One {C:fg-yellow,bg-blue}{:animal}{C:}, "
70 	    "Two {C:fg-green,bg-yellow}{:animal}{C:}\n",
71           "fish", "fish");
72 
73     const char *fmt1 = "The {C:fg-red}{k:name}{C:reset} is "
74 	"{C:/fg-%s}{:color}{C:reset} til {:time/%02d:%02d}\n";
75     const char *fmt2 = "My {C:fg-red}{:hand}{C:reset} hand is "
76 	"{C:/fg-%s}{:color}{C:reset} til {:time/%02d:%02d}\n";
77 
78     for (i = 0; i < count; i++) {
79 	xo_open_instance("thing");
80 	xo_emit_f(flags, fmt1, "thing", "green", "green", 2, 15);
81 	xo_emit_f(flags, fmt2, "left", "blue", "blue", 3, 45);
82     }
83 
84     xo_open_container("2by4");
85     xo_emit("There is {:4x4} in {:2morrow}\n", "truck", "tomorrow");
86     xo_close_container("2by4");
87 
88 
89     xo_close_container("data");
90     xo_close_container_h(NULL, "top");
91 
92     xo_finish();
93 
94     return 0;
95 }
96