xref: /freebsd/contrib/libxo/tests/core/test_12.c (revision cd0d51baaa4509a1db83251a601d34404d20c990)
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     argc = xo_parse_args(argc, argv);
29     if (argc < 0)
30 	return 1;
31 
32     for (argc = 1; argv[argc]; argc++) {
33 	if (xo_streq(argv[argc], "xml"))
34 	    xo_set_style(NULL, XO_STYLE_XML);
35 	else if (xo_streq(argv[argc], "json"))
36 	    xo_set_style(NULL, XO_STYLE_JSON);
37 	else if (xo_streq(argv[argc], "text"))
38 	    xo_set_style(NULL, XO_STYLE_TEXT);
39 	else if (xo_streq(argv[argc], "html"))
40 	    xo_set_style(NULL, XO_STYLE_HTML);
41 	else if (xo_streq(argv[argc], "no-color"))
42 	    opt_color = 0;
43 	else if (xo_streq(argv[argc], "pretty"))
44 	    xo_set_flags(NULL, XOF_PRETTY);
45 	else if (xo_streq(argv[argc], "xpath"))
46 	    xo_set_flags(NULL, XOF_XPATH);
47 	else if (xo_streq(argv[argc], "info"))
48 	    xo_set_flags(NULL, XOF_INFO);
49 	else if (xo_streq(argv[argc], "no-retain"))
50 	    flags &= ~XOEF_RETAIN;
51 	else if (xo_streq(argv[argc], "big")) {
52 	    if (argv[argc + 1])
53 		count = atoi(argv[++argc]);
54 	}
55     }
56 
57     xo_set_flags(NULL, XOF_UNITS); /* Always test w/ this */
58     if (opt_color)
59 	xo_set_flags(NULL, XOF_COLOR); /* Force color output */
60     xo_set_file(stdout);
61 
62     xo_open_container("top");
63     xo_open_container("data");
64 
65     xo_emit("{C:fg-red,bg-green}Merry XMas!!{C:}\n");
66 
67     xo_emit("One {C:fg-yellow,bg-blue}{:animal}{C:}, "
68 	    "Two {C:fg-green,bg-yellow}{:animal}{C:}\n",
69           "fish", "fish");
70 
71     const char *fmt1 = "The {C:fg-red}{k:name}{C:reset} is "
72 	"{C:/fg-%s}{:color}{C:reset} til {:time/%02d:%02d}\n";
73     const char *fmt2 = "My {C:fg-red}{:hand}{C:reset} hand is "
74 	"{C:/fg-%s}{:color}{C:reset} til {:time/%02d:%02d}\n";
75 
76     for (i = 0; i < count; i++) {
77 	xo_open_instance("thing");
78 	xo_emit_f(flags, fmt1, "thing", "green", "green", 2, 15);
79 	xo_emit_f(flags, fmt2, "left", "blue", "blue", 3, 45);
80     }
81 
82     xo_open_container("2by4");
83     xo_emit("There is {:4x4} in {:2morrow}\n", "truck", "tomorrow");
84     xo_close_container("2by4");
85 
86 
87     xo_close_container("data");
88     xo_close_container_h(NULL, "top");
89 
90     xo_finish();
91 
92     return 0;
93 }
94