[svn-commits] russell: branch russell/data_retrieval r112559 - /team/russell/data_retrieval...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Wed Apr 2 19:08:27 CDT 2008


Author: russell
Date: Wed Apr  2 19:08:26 2008
New Revision: 112559

URL: http://svn.digium.com/view/asterisk?view=rev&rev=112559
Log:
Add a little bit more magic to the CLI bridge to data providers to align values
of all key/value pairs at the same depth.

Modified:
    team/russell/data_retrieval/main/data.c

Modified: team/russell/data_retrieval/main/data.c
URL: http://svn.digium.com/view/asterisk/team/russell/data_retrieval/main/data.c?view=diff&rev=112559&r1=112558&r2=112559
==============================================================================
--- team/russell/data_retrieval/main/data.c (original)
+++ team/russell/data_retrieval/main/data.c Wed Apr  2 19:08:26 2008
@@ -130,11 +130,11 @@
 
 	if (!node)
 		return NULL;
-	
+
 	while ((child_node = AST_LIST_REMOVE_HEAD(&node->children, entry))) {
 		ast_data_node_destroy(child_node);
 	}
-	
+
 	ast_data_node_destroy(AST_LIST_NEXT(node, entry));
 
 	if (node->node_type == AST_DATA_NODE_STR) {
@@ -411,12 +411,25 @@
 	return results;
 }
 
+#define CLI_MAX_LEVELS	16
+
+struct cli_indent_levels {
+	unsigned int indent[CLI_MAX_LEVELS];
+};
+
 static void cli_print_node(struct ast_cli_args *a, struct ast_str **buf, 
-	const struct ast_data_node *node, int level)
+	const struct ast_data_node *node, const struct cli_indent_levels *cli_indents, 
+	unsigned int level)
 {
 	int i;
 	enum ast_data_node_type node_type;
-	const struct ast_data_node *next = NULL;
+	const struct ast_data_node *next = NULL, *child;
+	const char *name;
+	size_t len;
+
+	if (level == ARRAY_LEN(cli_indents->indent)) {
+		return;
+	}
 
 	if (!node)
 		return;
@@ -431,7 +444,16 @@
 		ast_str_append(buf, 0, "> ");
 	}
 
-	ast_str_append(buf, 0, "%s: ", ast_data_node_get_friendly_name(node));
+	name = ast_data_node_get_friendly_name(node);
+	len = strlen(name);
+
+	ast_str_append(buf, 0, "%s: ", name);
+	if (len < cli_indents->indent[level]) {
+		int i;
+		for (i = 0; i < cli_indents->indent[level] - len; i++) {
+			ast_str_append(buf, 0, " ");
+		}
+	}
 
 	node_type = ast_data_node_get_type(node);
 
@@ -455,16 +477,36 @@
 		ast_cli(a->fd, "===\n");
 	}
 
-	cli_print_node(a, buf, ast_data_node_get_child(node), level + 1);
+	for (child = ast_data_node_get_child(node); child; child = ast_data_node_get_next(child)) {
+		cli_print_node(a, buf, child, cli_indents, level + 1);
+	}
 
 	next = ast_data_node_get_next(node);
-
 	if (level == 1 && (node_type == AST_DATA_NODE_GROUPING || !next)) {
 		ast_cli(a->fd, "===\n");
 	}
-
-	if (next)
-		cli_print_node(a, buf, next, level);
+}
+
+static void calc_cli_indent_levels(const struct ast_data_node *node, 
+	struct cli_indent_levels *cli_indents, unsigned int level)
+{
+	size_t len;
+	const struct ast_data_node *child;
+
+	if (level == ARRAY_LEN(cli_indents->indent)) {
+		ast_log(LOG_ERROR, "Data tree goes deeper than max depth which is '%lu'\n",
+			ARRAY_LEN(cli_indents->indent));
+		return;
+	}
+
+	len = strlen(ast_data_node_get_friendly_name(node));
+	if (len > cli_indents->indent[level]) {
+		cli_indents->indent[level] = len;
+	}
+
+	for (child = ast_data_node_get_child(node); child; child = ast_data_node_get_next(child)) {
+		calc_cli_indent_levels(child, cli_indents, level + 1);
+	}
 }
 
 static char *data_providers_complete(struct ast_cli_args *a, int req_pos)
@@ -497,6 +539,7 @@
 	const char *provider_name;
 	struct ast_data_node *results, *query;
 	struct ast_str *buf;
+	struct cli_indent_levels cli_indents;
 
 	if (cmd == CLI_INIT) {
 		e->command = "data get";
@@ -532,13 +575,16 @@
 		return CLI_FAILURE;
 	}
 
+	memset(&cli_indents, 0, sizeof(cli_indents));
+	calc_cli_indent_levels(results, &cli_indents, 0);
+
 	ast_cli(a->fd,
 		"================================================================\n"
 		"=== Query: %s\n"
 		"================================================================\n"
 		"", provider_name);
 
-	cli_print_node(a, &buf, results, 0);
+	cli_print_node(a, &buf, results, &cli_indents, 0);
 
 	ast_cli(a->fd,
 		"================================================================\n"




More information about the svn-commits mailing list