Skip to contentSkip to conversation

client: add generic display function for table rows

PATCH Superseded James Prestwood 1 participant 14 messages Jul 6, 2022

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/14 client: add generic display function for table rows

There was no easy to use API for printing the contents of a table, and was left up to the caller to handle manually. This adds display_table_row which makes displaying tables much easier, including automatic support for line truncation and continuation on the next line.

Lines which are too long will be truncated and displayed on the next line while also taking into account any colored output. This works with any number of columns.

This removes the need for the module to play games with encoding newlines and tabs to make the output look nice.

As a start, this functionality was added to the command display.

---
 client/display.c | 174 ++++++++++++++++++++++++++++++++++++++++++++++-
 client/display.h |   1 +
 2 files changed, 172 insertions(+), 3 deletions(-)
client/display.c +171 -3
index 07cb7bda..7ca3a767 100644
@@ -376,8 +376,176 @@ void display_table_footer(void)
 	display_refresh_check_applicability();
 }
 
-void display_command_line(const char *command_family,
-						const struct command *cmd)
+#define COLOR_END(_str) \
+({ \
+	char *_s = (_str); \
+	while (*_s != 'm' && *_s != '\0') \
+		_s++; \
+	_s - (_str) + 1; \
+})
+
+/*
+ * Finds last space in 's' before 'max' characters, terminates at that index,
+ * and returns a new string to be printed on the next line.
+ *
+ * 'max' should be set to the column width, but is also an out parameter since
+ * this width can be updated if colored escapes are detected.
+ *
+ * Any colored escapes found are set to 'color_out' so they can be re-enabled
+ * on the next line.
+ */
+static char* next_line(char *s, unsigned int *max, char **color_out)
+{
+	unsigned int i;
+	int last_space = -1;
+	int last_color = -1;
+
+	/* Find the last space before 'max', as well as any color */
+	for (i = 0; i <= *max && s[i] != '\0'; i++) {
+		if (s[i] == ' ')
+			last_space = i;
+		else if (s[i] == 0x1b) {
+			/* color escape won't count for column width */
+			*max += COLOR_END(s + i);
+			last_color = i;
+		}
+	}
+
+	/* Reached the end of the string within the column bounds */
+	if (i <= *max)
+		return NULL;
+
+	/* Not anywhere nice to split the line */
+	if (last_space == -1)
+		last_space = *max - 1;
+
+	/*
+	 * Only set the color if it occurred prior to the last space. If after,
+	 * it will get picked up on the next line.
+	 */
+	if (last_color != -1 && last_space >= last_color)
+		*color_out = l_strndup(s + last_color,
+					COLOR_END(s + last_color));
+	else
+		*color_out = NULL;
+
+	s[last_space] = '\0';
+
+	return l_strdup(s + last_space + 1);
+}
+
+struct table_entry {
+	unsigned int width;
+	char *next;
+	char *color;
+};
+
+/*
+ * Appends the next line from 'e' to 'line_buf'. 'done' is only set false when
+ * there are more lines needed for the current entry.
+ */
+static int entry_append(struct table_entry *e, char *line_buf, bool *done)
+{
+	char *value = e->next;
+	unsigned int ret = 0;
+	unsigned int width = e->width;
+
+	/* Empty line */
+	if (!value)
+		return sprintf(line_buf, "%-*s  ", e->width, "");
+
+	/* Color from previous line */
+	if (e->color) {
+		ret = sprintf(line_buf, "%s", e->color);
+		l_free(e->color);
+		e->color = NULL;
+	}
+
+	/* Advance entry to next line, and terminate current */
+	e->next = next_line(value, &width, &e->color);
+
+	if (e->next)
+		*done = false;
+
+	/* Append current line */
+	ret += sprintf(line_buf + ret, "%-*s  ", width, value);
+
+	l_free(value);
+
+	/* Un-color output for next column */
+	if (e->color)
+		ret += sprintf(line_buf + ret, "%s", COLOR_OFF);
+
+	return ret;
+}
+
+/*
+ * Expects an initial margin, number of columns in table, then row data:
+ *
+ * <row width>, <row data>, ...
+ *
+ * The data string can be of any length, and will be split into new lines of
+ * length <row width>.
+ */
+void display_table_row(const char *margin, unsigned int ncolumns, ...)
+{
+	char buf[512];
+	char *str = buf;
+	unsigned int i;
+	struct table_entry entries[ncolumns];
+	va_list va;
+	bool done = true;
+
+	memset(&entries[0], 0, sizeof(entries));
+
+	va_start(va, ncolumns);
+
+	str += sprintf(str, "%s", margin);
+
+	for (i = 0; i < ncolumns; i++) {
+		struct table_entry *e = &entries[i];
+
+		e->width = va_arg(va, unsigned int);
+		e->next = l_strdup(va_arg(va, char*));
+
+		str += entry_append(e, str, &done);
+	}
+
+	va_end(va);
+
+	display("%s\n", buf);
+	str = buf;
+
+	/*
+	 * The first column should now be indented, which effects the entry
+	 * width. Subtract this indentation only from the first column.
+	 */
+	entries[0].width -= strlen(margin) * 2;
+
+	while (!done) {
+		done = true;
+
+		for (i = 0; i < ncolumns; i++) {
+			struct table_entry *e = &entries[i];
+
+			if (i == 0)
+				str += sprintf(str, "%s%s%s", margin,
+						margin, margin);
+
+			str += entry_append(e, str, &done);
+		}
+
+		display("%s\n", buf);
+		str = buf;
+	}
+
+	for (i = 0; i < ncolumns; i++) {
+		if (entries[i].color)
+			l_free(entries[i].color);
+	}
+}
+
+void display_command_line(const char *command_family, const struct command *cmd)
 {
 	char *cmd_line = l_strdup_printf("%s%s%s%s%s%s%s",
 				command_family ? : "",
@@ -388,7 +556,7 @@ void display_command_line(const char *command_family,
 				cmd->arg ? " " : "",
 				cmd->arg ? : "");
 
-	display(MARGIN "%-*s%s\n", 50, cmd_line, cmd->desc ? : "");
+	display_table_row(MARGIN, 2, 50, cmd_line, 30, cmd->desc);
 
 	l_free(cmd_line);
 }
client/display.h +1 -0
index 7747c6a0..c34cab9c 100644
@@ -37,6 +37,7 @@ void display(const char *format, ...)
 		__attribute__((format(printf, 1, 2)));
 void display_table_header(const char *caption, const char *fmt, ...)
 		__attribute__((format(printf, 2, 3)));
+void display_table_row(const char *margin, unsigned int ncolumns, ...);
 void display_table_footer(void);
 void display_error(const char *error);
 void display_command_line(const char *command_family,
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/14 client: remove newline/tab encoding from help description

ad-hoc, ap, and wsc all had descriptions longer than the max width but this is now taken care of automatically. Remove the tab and newline's from the description.

The WSC pin command description was also changed to be more accurate since the pin does not need to be 8 digits.

---
 client/ad-hoc.c | 11 ++++-------
 client/ap.c     |  7 +++----
 client/wsc.c    |  4 ++--
 3 files changed, 9 insertions(+), 13 deletions(-)
client/ad-hoc.c +4 -7
index c90f9dd6..33bff9ca 100644
@@ -215,14 +215,11 @@ static enum cmd_status cmd_stop(const char *device_name, char **argv, int argc)
 static const struct command ad_hoc_commands[] = {
 	{ NULL, "list", NULL, cmd_list, "List devices in Ad-hoc mode", true },
 	{ "<wlan>", "start", "<\"network name\"> <passphrase>", cmd_start,
-		"Start or join an existing\n"
-		"\t\t\t\t\t\t    Ad-Hoc network called\n"
-		"\t\t\t\t\t\t    \"network name\" with a\n"
-		"\t\t\t\t\t\t    passphrase" },
+		"Start or join an existing Ad-Hoc network called "
+		"\"network name\" with a passphrase" },
 	{ "<wlan>", "start_open", "<\"network name\">", cmd_start_open,
-		"Start or join an existing\n"
-		"\t\t\t\t\t\t    open Ad-Hoc network called\n"
-		"\t\t\t\t\t\t    \"network name\"" },
+		"Start or join an existing open Ad-Hoc network called"
+		" \"network name\"" },
 	{ "<wlan>", "stop", NULL,   cmd_stop, "Leave an Ad-Hoc network" },
 	{ }
 };
client/ap.c +3 -4
index bb583397..b992c792 100644
@@ -416,12 +416,11 @@ static enum cmd_status cmd_get_networks(const char *device_name, char **argv,
 static const struct command ap_commands[] = {
 	{ NULL, "list", NULL, cmd_list, "List devices in AP mode", true },
 	{ "<wlan>", "start", "<\"network name\"> <passphrase>", cmd_start,
-		"Start an access point\n\t\t\t\t\t\t    called \"network "
-		"name\" with\n\t\t\t\t\t\t    a passphrase" },
+		"Start an access point called \"network "
+		"name\" with a passphrase" },
 	{ "<wlan>", "start-profile", "<\"network name\">", cmd_start_profile,
 		"Start an access point based on a disk profile" },
-	{ "<wlan>", "stop", NULL,   cmd_stop, "Stop a started access\n"
-		"\t\t\t\t\t\t    point" },
+	{ "<wlan>", "stop", NULL,   cmd_stop, "Stop a started access point" },
 	{ "<wlan>", "show", NULL, cmd_show, "Show AP info", false },
 	{ "<wlan>", "scan", NULL, cmd_scan, "Start an AP scan", false },
 	{ "<wlan>", "get-networks", NULL, cmd_get_networks,
client/wsc.c +2 -2
index 39950453..1e5912ce 100644
@@ -182,10 +182,10 @@ static enum cmd_status cmd_cancel(const char *device_name,
 static const struct command wsc_commands[] = {
 	{ NULL, "list", NULL, cmd_list, "List WSC-capable devices", true },
 	{ "<wlan>", "push-button", NULL, cmd_push_button, "PushButton mode" },
-	{ "<wlan>", "start-user-pin", "<8 digit PIN>", cmd_start_user_pin,
+	{ "<wlan>", "start-user-pin", "<PIN>", cmd_start_user_pin,
 							"PIN mode" },
 	{ "<wlan>", "start-pin", NULL, cmd_start_pin,
-		"PIN mode with generated\n\t\t\t\t\t\t    8 digit PIN" },
+		"PIN mode with generated 8 digit PIN" },
 	{ "<wlan>", "cancel", NULL,   cmd_cancel, "Aborts WSC operations" },
 	{ }
 };
Signature · 1 line
2.34.1
#2 lore raw

PATCH 3/14 client: update dbus-proxy to use display_table_row

In reply to James Prestwood
---
 client/dbus-proxy.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)
client/dbus-proxy.c +4 -17
index 9f05c0e7..bbb97eb5 100644
@@ -59,7 +59,7 @@ void proxy_properties_display(const struct proxy_interface *proxy,
 	if (!proxy->type->properties)
 		return;
 
-	display_table_header(caption, "%s%-*s  %-*s%-*s", margin,
+	display_table_header(caption, "%s%-*s  %-*s  %-*s", margin,
 				8, "Settable",
 				name_column_width, "Property",
 				value_column_width, "Value");
@@ -69,29 +69,16 @@ void proxy_properties_display(const struct proxy_interface *proxy,
 
 	for (i = 0; properties[i].name; i++) {
 		const char *str;
-		size_t len;
-		size_t j;
 
 		if (!properties[i].tostr)
 			continue;
 
 		str = properties[i].tostr(data);
-		len = str ? strlen(str) : 0;
 
-		display("%s%*s  %-*s%-.*s\n", margin,
-			8, properties[i].is_read_write ?
+		display_table_row(MARGIN, 3, 8, properties[i].is_read_write ?
 				COLOR_BOLDGRAY "       *" COLOR_OFF : "",
-			name_column_width, properties[i].name,
-			value_column_width, str ? : "");
-
-		if (len <= value_column_width)
-			continue;
-
-		/* Display remaining data */
-		for (j = value_column_width; j < len; j += value_column_width)
-			display("%s%*s  %-*s%-.*s\n", margin, 8, "",
-				name_column_width, "", value_column_width,
-				str + j);
+				name_column_width, properties[i].name,
+				value_column_width, str ? : "");
 	}
 }
 
Signature · 1 line
2.34.1
#3 lore raw

PATCH 4/14 client: update station to use display_table_row

In reply to James Prestwood

This includes updating diagnostics too, otherwise the output becomes really nasty.

---
 client/diagnostic.c | 32 +++++++++++++-------------------
 client/station.c    | 42 +++++++++++++++++++-----------------------
 2 files changed, 32 insertions(+), 42 deletions(-)
client/diagnostic.c +13 -19
index 9e255918..6360b7e2 100644
@@ -69,12 +69,13 @@ static bool display_bitrate_100kbps(struct l_dbus_message_iter *variant,
 				int name_column_width, int value_column_width)
 {
 	uint32_t rate;
+	char str[50];
 
 	if (!l_dbus_message_iter_get_variant(variant, "u", &rate))
 		return false;
 
-	display("%s%-*s%-*u Kbit/s\n", margin, name_column_width, key,
-			value_column_width, rate * 100);
+	sprintf(str, "%u Kbit/s", rate * 100);
+	display_table_row(margin, 3, 8, "", name_column_width, key, value_column_width, str);
 
 	return true;
 }
@@ -110,6 +111,7 @@ void diagnostic_display(struct l_dbus_message_iter *dict,
 		uint32_t u_value;
 		int16_t n_value;
 		uint8_t y_value;
+		int bytes;
 
 		map = find_mapping(key, diagnostic_mapping);
 		if (!map)
@@ -132,9 +134,7 @@ void diagnostic_display(struct l_dbus_message_iter *dict,
 							&s_value))
 				goto parse_error;
 
-			sprintf(display_text, "%s%-*s%-*s", margin,
-					name_column_width, key,
-					value_column_width, s_value);
+			bytes = sprintf(display_text, "%s", s_value);
 			break;
 
 		case 'u':
@@ -142,9 +142,7 @@ void diagnostic_display(struct l_dbus_message_iter *dict,
 							&u_value))
 				goto parse_error;
 
-			sprintf(display_text, "%s%-*s%-*u", margin,
-						name_column_width, key,
-						value_column_width, u_value);
+			bytes = sprintf(display_text, "%u", u_value);
 			break;
 
 		case 'n':
@@ -152,9 +150,7 @@ void diagnostic_display(struct l_dbus_message_iter *dict,
 							&n_value))
 				goto parse_error;
 
-			sprintf(display_text, "%s%-*s%-*i", margin,
-						name_column_width, key,
-						value_column_width, n_value);
+			bytes = sprintf(display_text, "%i", n_value);
 			break;
 
 		case 'y':
@@ -162,21 +158,19 @@ void diagnostic_display(struct l_dbus_message_iter *dict,
 							&y_value))
 				goto parse_error;
 
-			sprintf(display_text, "%s%-*s%-*u", margin,
-						name_column_width, key,
-						value_column_width, y_value);
+			bytes = sprintf(display_text, "%u", y_value);
 			break;
 
 		default:
-			display("type %c not handled", map->type);
+			display("type %c not handled\n", map->type);
 			continue;
 		}
 
 		if (map->units)
-			display("%s %s\n", display_text,
-					(const char *)map->units);
-		else
-			display("%s\n", display_text);
+			sprintf(display_text + bytes, " %s", map->units);
+
+		display_table_row(margin, 3, 8, "", name_column_width,
+					key, value_column_width, display_text);
 	}
 
 	return;
client/station.c +19 -23

102 lines not on this page · raw message

Signature · 1 line
2.34.1
#4 lore raw

PATCH 5/14 client: dpp: display table footer and set to auto update

In reply to James Prestwood

The table footer never got added when the properties did. In addition set the 'show' command to update since these properties could change.

---
 client/dpp.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
client/dpp.c +3 -1
index af69d2c0..cd05a33b 100644
@@ -287,6 +287,8 @@ static enum cmd_status cmd_show(const char *device_name,
 	proxy_properties_display(proxy, caption, MARGIN, 20, 47);
 	l_free(caption);
 
+	display_table_footer();
+
 	return CMD_STATUS_DONE;
 }
 
@@ -297,7 +299,7 @@ static const struct command dpp_commands[] = {
 	{ "<wlan>", "start-configurator", NULL, cmd_start_configurator,
 							"Starts a DPP Configurator" },
 	{ "<wlan>", "stop", NULL, cmd_stop, "Aborts DPP operations" },
-	{ "<wlan>", "show", NULL, cmd_show, "Shows the DPP state" },
+	{ "<wlan>", "show", NULL, cmd_show, "Shows the DPP state", true },
 	{ }
 };
 
Signature · 1 line
2.34.1
#5 lore raw

PATCH 6/14 client: check NULL return for DPP cmd_show

In reply to James Prestwood

If the DPP interface goes away this could return NULL which was unchecked. Caught by static analysis.

---
 client/dpp.c | 5 +++++
 1 file changed, 5 insertions(+)
client/dpp.c +5 -0
index cd05a33b..a659f545 100644
@@ -284,6 +284,11 @@ static enum cmd_status cmd_show(const char *device_name,
 			device_proxy_find(device_name, IWD_DPP_INTERFACE);
 	char *caption = l_strdup_printf("%s: %s", "DPP", device_name);
 
+	if (!proxy) {
+		display("No DPP interface on device: '%s'\n", device_name);
+		return CMD_STATUS_INVALID_VALUE;
+	}
+
 	proxy_properties_display(proxy, caption, MARGIN, 20, 47);
 	l_free(caption);
 
Signature · 1 line
2.34.1
#6 lore raw

PATCH 7/14 client: update CLEAR_SCREEN to be consistent with others

In reply to James Prestwood

Clear screen used a \033 escape sequence while others used a \x

---
 client/display.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
client/display.h +1 -1
index c34cab9c..8e597bb5 100644
@@ -30,7 +30,7 @@ struct command_family;
 #define COLOR_BLUE	"\x1B[94m"
 #define COLOR_YELLOW	"\x1b[33m"
 #define COLOR_OFF	"\x1B[0m"
-#define CLEAR_SCREEN	"\033[2J"
+#define CLEAR_SCREEN	"\x1b[2J"
 #define MARGIN		"  "
 
 void display(const char *format, ...)
Signature · 1 line
2.34.1
#7 lore raw

PATCH 8/14 client: make COLOR_* macros take a string input

In reply to James Prestwood

The existing color code escape sequences required the user to set the color, write the string, then unset with COLOR_OFF. Instead the macros can be made to take the string itself and automatically terminate the color with COLOR_OFF. This makes for much more concise strings.

---
 client/agent.c          |  4 ++--
 client/dbus-proxy.c     |  2 +-
 client/display.c        | 22 +++++++++++-----------
 client/display.h        | 19 +++++++++----------
 client/known-networks.c |  4 ++--
 client/station.c        |  8 ++++----
 6 files changed, 29 insertions(+), 30 deletions(-)
client/agent.c +2 -2
index 037ae785..6f2c9874 100644
@@ -301,8 +301,8 @@ static struct l_dbus_message *request_user_password_method_call(
 	display("Type the network password for %s.\n",
 				proxy_interface_get_identity_str(proxy));
 
-	username_prompt = l_strdup_printf(COLOR_BLUE PROMPT_USERNAME " "
-						COLOR_OFF "%s\n", username);
+	username_prompt = l_strdup_printf(COLOR_BLUE(PROMPT_USERNAME " ")
+						"%s\n", username);
 	display("%s", username_prompt);
 	l_free(username_prompt);
 
client/dbus-proxy.c +1 -1
index bbb97eb5..1534488e 100644
@@ -76,7 +76,7 @@ void proxy_properties_display(const struct proxy_interface *proxy,
 		str = properties[i].tostr(data);
 
 		display_table_row(MARGIN, 3, 8, properties[i].is_read_write ?
-				COLOR_BOLDGRAY "       *" COLOR_OFF : "",
+				COLOR_BOLDGRAY("       *") : "",
 				name_column_width, properties[i].name,
 				value_column_width, str ? : "");
 	}
client/display.c +11 -11
index 7ca3a767..772f523b 100644
@@ -40,7 +40,7 @@
 #include "client/display.h"
 
 #define IWD_PROMPT \
-	"\001" COLOR_GREEN "\002" "[iwd]" "\001" COLOR_OFF "\002" "# "
+	"\001" COLOR_GREEN("\002" "[iwd]" "\001") "\002" "# "
 #define LINE_LEN 81
 
 static struct l_signal *window_change_signal;
@@ -234,9 +234,9 @@ static void display_refresh_check_feasibility(void)
 	if (ws.ws_col < LINE_LEN - 1) {
 		if (display_refresh.enabled) {
 			display_refresh.recording = false;
-			display(COLOR_YELLOW "Auto-refresh is disabled. "
+			display(COLOR_YELLOW("Auto-refresh is disabled. "
 				"Enlarge window width to at least %u to enable."
-				"\n" COLOR_OFF, LINE_LEN - 1);
+				"\n"), LINE_LEN - 1);
 			display_refresh.recording = true;
 		}
 
@@ -317,7 +317,7 @@ void display(const char *fmt, ...)
 
 void display_error(const char *error)
 {
-	char *text = l_strdup_printf(COLOR_RED "%s" COLOR_OFF "\n", error);
+	char *text = l_strdup_printf(COLOR_RED("%s\n"), error);
 
 	display_text(text);
 
@@ -344,14 +344,14 @@ void display_table_header(const char *caption, const char *fmt, ...)
 	int caption_pos =
 		(int) ((sizeof(dashed_line) - 1) / 2 + strlen(caption) / 2);
 
-	text = l_strdup_printf("%*s" COLOR_BOLDGRAY "%*c" COLOR_OFF "\n",
+	text = l_strdup_printf("%*s" COLOR_BOLDGRAY("%*c") "\n",
 				caption_pos, caption,
 				LINE_LEN - 2 - caption_pos,
 				display_refresh.cmd ? get_flasher() : ' ');
 	display_text(text);
 	l_free(text);
 
-	text = l_strdup_printf("%s%s%s\n", COLOR_GRAY, dashed_line, COLOR_OFF);
+	text = l_strdup_printf(COLOR_GRAY("%s\n"), dashed_line);
 	display_text(text);
 	l_free(text);
 
@@ -359,12 +359,12 @@ void display_table_header(const char *caption, const char *fmt, ...)
 	text = l_strdup_vprintf(fmt, args);
 	va_end(args);
 
-	body = l_strdup_printf("%s%s%s\n", COLOR_BOLDGRAY, text, COLOR_OFF);
+	body = l_strdup_printf(COLOR_BOLDGRAY("%s\n"), text);
 	display_text(body);
 	l_free(body);
 	l_free(text);
 
-	text = l_strdup_printf("%s%s%s\n", COLOR_GRAY, dashed_line, COLOR_OFF);
+	text = l_strdup_printf(COLOR_GRAY("%s\n"), dashed_line);
 	display_text(text);
 	l_free(text);
 }
@@ -767,7 +767,7 @@ void display_agent_prompt(const char *label, bool mask_input)
 	if (mask_input)
 		reset_masked_input();
 
-	prompt = l_strdup_printf(COLOR_BLUE "%s " COLOR_OFF, label);
+	prompt = l_strdup_printf(COLOR_BLUE("%s "), label);
 
 	if (command_is_interactive_mode()) {
 		if (agent_saved_input) {
@@ -813,8 +813,8 @@ void display_agent_prompt_release(const char *label)
 
 	if (display_refresh.cmd) {
 		char *text = rl_copy_text(0, rl_end);
-		char *prompt = l_strdup_printf(COLOR_BLUE "%s " COLOR_OFF
-							"%s\n", label, text);
+		char *prompt = l_strdup_printf(COLOR_BLUE("%s ")
+						"%s\n", label, text);
 		l_free(text);
 
 		l_queue_push_tail(display_refresh.redo_entries, prompt);
client/display.h +9 -10
index 8e597bb5..c1a3cb75 100644
@@ -22,16 +22,15 @@
 
 struct command;
 struct command_family;
-
-#define COLOR_BOLDGRAY	"\x1B[1;90m"
-#define COLOR_GRAY	"\x1b[90m"
-#define COLOR_GREEN	"\x1b[32m"
-#define COLOR_RED	"\x1B[0;91m"
-#define COLOR_BLUE	"\x1B[94m"
-#define COLOR_YELLOW	"\x1b[33m"
-#define COLOR_OFF	"\x1B[0m"
-#define CLEAR_SCREEN	"\x1b[2J"
-#define MARGIN		"  "
+#define COLOR_OFF		"\x1B[0m"
+#define COLOR_BOLDGRAY(s)	"\x1B[1;90m" s COLOR_OFF
+#define COLOR_GRAY(s)		"\x1b[90m" s COLOR_OFF
+#define COLOR_GREEN(s)		"\x1b[32m" s COLOR_OFF
+#define COLOR_RED(s)		"\x1B[0;91m" s COLOR_OFF
+#define COLOR_BLUE(s)		"\x1B[94m" s COLOR_OFF
+#define COLOR_YELLOW(s)		"\x1b[33m" s COLOR_OFF
+#define CLEAR_SCREEN		"\x1b[2J"
+#define MARGIN			"  "
 
 void display(const char *format, ...)
 		__attribute__((format(printf, 1, 2)));
client/known-networks.c +2 -2
index 45f60af2..49e69ddd 100644
@@ -318,8 +318,8 @@ static const struct proxy_interface *known_network_proxy_find_by_name(
 		if (!network_args.type) {
 			display("Provided network name is ambiguous. "
 				"Specify network security type as follows:\n");
-			display("<\"network name" COLOR_BOLDGRAY ".security"
-							COLOR_OFF "\">\n");
+			display("<\"network name" COLOR_BOLDGRAY(".security")
+							"\">\n");
 			display("\twhere '.security' is [.psk | .8021x | "
 								".open]\n");
 		}
client/station.c +4 -4
index 1a573674..64becdbd 100644
@@ -387,12 +387,12 @@ static const char *dbms_tostars(int16_t dbms)
 		return "****";
 
 	if (dbms >= -6700)
-		return "***" COLOR_BOLDGRAY "*" COLOR_OFF;
+		return "***" COLOR_BOLDGRAY("*");
 
 	if (dbms >= -7500)
-		return "**" COLOR_BOLDGRAY "**" COLOR_OFF;
+		return "**" COLOR_BOLDGRAY("**");
 
-	return "*" COLOR_BOLDGRAY "***" COLOR_OFF;
+	return "*" COLOR_BOLDGRAY("***");
 }
 
 #define RSSI_DBMS "rssi-dbms"
@@ -440,7 +440,7 @@ static void ordered_networks_display(struct l_queue *ordered_networks)
 
 		display_table_row(MARGIN, 4, 2,
 				network_is_connected(network_i) ?
-				COLOR_BOLDGRAY "> " COLOR_OFF: "",
+				COLOR_BOLDGRAY("> ") : "",
 				32, network_name, 18, network_type, 6,
 				display_signal_as_dbms ? dbms :
 				dbms_tostars(network->signal_strength));
Signature · 1 line
2.34.1
#8 lore raw

PATCH 9/14 client: update ap to use display_table_row

In reply to James Prestwood
---
 client/ap.c | 32 +++++++++++++++++---------------
 1 file changed, 17 insertions(+), 15 deletions(-)
client/ap.c +17 -15
index b992c792..37a15fc3 100644
@@ -161,9 +161,7 @@ static void display_ap_inline(const char *margin, const void *data)
 	if (!identity)
 		return;
 
-	display("%s%-*s%-*s\n", margin,
-			20, identity,
-			8, get_started_tostr(ap));
+	display_table_row(margin, 2, 20, identity, 8, get_started_tostr(ap));
 }
 
 static enum cmd_status cmd_list(const char *device_name, char **argv, int argc)
@@ -173,9 +171,8 @@ static enum cmd_status cmd_list(const char *device_name, char **argv, int argc)
 		proxy_interface_find_all(IWD_ACCESS_POINT_INTERFACE,
 						NULL, NULL);
 
-	display_table_header("Devices in Access Point Mode", MARGIN "%-*s%-*s",
-				20, "Name",
-				8, "Started");
+	display_table_header("Devices in Access Point Mode",
+				MARGIN "%-*s  %-*s", 20, "Name", 8, "Started");
 
 	if (!match) {
 		display("No devices in access point mode available.\n");
@@ -265,10 +262,10 @@ static void ap_get_diagnostics_callback(struct l_dbus_message *message,
 	}
 
 	while (l_dbus_message_iter_next_entry(&array, &iter)) {
-		sprintf(client_num, "Client %u", idx++);
-		display_table_header(client_num, "            %-*s%-*s",
+		sprintf(client_num, "STA %u", idx++);
+		display_table_header("", MARGIN "%-*s  %-*s  %-*s", 8, client_num,
 					20, "Property", 20, "Value");
-		diagnostic_display(&iter, "            ", 20, 20);
+		diagnostic_display(&iter, MARGIN, 20, 20);
 		display_table_footer();
 	}
 }
@@ -286,10 +283,11 @@ static enum cmd_status cmd_show(const char *device_name, char **argv, int argc)
 	}
 
 	proxy_properties_display(ap_i, "Access Point Interface", MARGIN, 20, 20);
-	display_table_footer();
 
-	if (!ap_diagnostic)
+	if (!ap_diagnostic) {
+		display_table_footer();
 		return CMD_STATUS_DONE;
+	}
 
 	proxy_interface_method_call(ap_diagnostic, "GetDiagnostics", "",
 					ap_get_diagnostics_callback);
@@ -355,14 +353,18 @@ static void ap_display_network(struct l_dbus_message_iter *iter,
 			if (!l_dbus_message_iter_get_variant(&variant, "s", &s))
 				goto parse_error;
 
-			display("%s%-*s%-*s\n", margin, name_width, key,
+			display_table_row(margin, 2, name_width, key,
 						value_width, s);
 		} else if (!strcmp(key, "SignalStrength")) {
+			char signal[6];
+
 			if (!l_dbus_message_iter_get_variant(&variant, "n", &n))
 				goto parse_error;
 
-			display("%s%-*s%-*i\n", margin, name_width, key,
-						value_width, n);
+			snprintf(signal, sizeof(signal), "%i", n);
+
+			display_table_row(margin, 2, name_width, key,
+						value_width, signal);
 		}
 	}
 
@@ -386,7 +388,7 @@ static void ap_get_networks_callback(struct l_dbus_message *message,
 		return;
 	}
 
-	display_table_header("Networks", "            %-*s%-*s",
+	display_table_header("Networks", "            %-*s  %-*s",
 					20, "Property", 20, "Value");
 	while (l_dbus_message_iter_next_entry(&array, &iter)) {
 		ap_display_network(&iter, "            ", 20, 20);
Signature · 1 line
2.34.1
#9 lore raw

PATCH 10/14 client: update known-networks to use display_table_row

In reply to James Prestwood
---
 client/known-networks.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
client/known-networks.c +4 -4
index 49e69ddd..56c34a2e 100644
@@ -210,9 +210,9 @@ static void known_network_display_inline(const char *margin, const void *data)
 		l_strdup(format_iso8601(network->last_connected,
 						"%b %e, %l:%M %p"));
 
-	display("%s%-*s%-*s%-*s%-*s\n",
-		margin, 32, network->name, 11, network->type,
-		9, get_hidden_tostr(network), 19, last_connected ? : "-");
+	display_table_row(margin, 4, 32, network->name, 11, network->type,
+			9, get_hidden_tostr(network),
+			19, last_connected ? : "-");
 
 	l_free(last_connected);
 }
@@ -263,7 +263,7 @@ static void check_errors_method_callback(struct l_dbus_message *message,
 
 static enum cmd_status cmd_list(const char *entity, char **args, int argc)
 {
-	display_table_header("Known Networks", MARGIN "%-*s%-*s%-*s%-*s",
+	display_table_header("Known Networks", MARGIN "%-*s  %-*s  %-*s  %-*s",
 					32, "Name", 11, "Security", 9, "Hidden",
 					19, "Last connected");
 
Signature · 1 line
2.34.1
#10 lore raw

PATCH 11/14 client: update command table header

In reply to James Prestwood

The table header needs to be adjusted to include spaces between columns.

---
 client/command.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
client/command.c +8 -8
index 74f11c4a..c0576e97 100644
@@ -509,16 +509,16 @@ static void list_commands(const char *command_family,
 
 static void list_cmd_options(void)
 {
-	display(MARGIN "--%-*s%s\n", 48, COMMAND_OPTION_USERNAME,
+	display(MARGIN "--%-*s  %s\n", 48, COMMAND_OPTION_USERNAME,
 					"Provide username");
-	display(MARGIN "--%-*s%s\n", 48, COMMAND_OPTION_PASSWORD,
+	display(MARGIN "--%-*s  %s\n", 48, COMMAND_OPTION_PASSWORD,
 					"Provide password");
-	display(MARGIN "--%-*s%s\n", 48, COMMAND_OPTION_PASSPHRASE,
+	display(MARGIN "--%-*s  %s\n", 48, COMMAND_OPTION_PASSPHRASE,
 					"Provide passphrase");
-	display(MARGIN "--%-*s%s\n", 48, COMMAND_OPTION_DONTASK,
+	display(MARGIN "--%-*s  %s\n", 48, COMMAND_OPTION_DONTASK,
 					"Don't ask for missing\n"
-					"\t\t\t\t\t\t    credentials");
-	display(MARGIN "--%-*s%s\n", 48, "help", "Display help");
+					"\t\t\t\t\t\t      credentials");
+	display(MARGIN "--%-*s  %s\n", 48, "help", "Display help");
 }
 
 static void list_cmd_families(void)
@@ -542,12 +542,12 @@ static void command_display_help(void)
 	display(MARGIN "iwctl [--options] [commands]\n");
 	display_table_footer();
 
-	display_table_header("Available options", MARGIN "%-*s%-*s",
+	display_table_header("Available options", MARGIN "%-*s  %-*s",
 					50, "Options", 28, "Description");
 	list_cmd_options();
 	display_table_footer();
 
-	display_table_header("Available commands", MARGIN "%-*s%-*s",
+	display_table_header("Available commands", MARGIN "%-*s  %-*s",
 					50, "Commands", 28, "Description");
 	list_cmd_families();
 	display_table_footer();
Signature · 1 line
2.34.1
#11 lore raw

PATCH 12/14 client: update device to use display_table_row

In reply to James Prestwood
---
 client/device.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
client/device.c +9 -9
index 5b971883..9f8d731c 100644
@@ -54,9 +54,10 @@ static void display_device(const struct proxy_interface *proxy)
 	l_free(caption);
 
 	if (device->adapter) {
-		display("%s%*s  %-*s%-*s\n", MARGIN, 8, "", 20, "Adapter", 47,
-			proxy_interface_get_identity_str(
+		display_table_row(MARGIN, 3, 8, "", 20, "Adapter", 47,
+				proxy_interface_get_identity_str(
 						device->adapter) ? : "");
+
 	}
 
 	display_table_footer();
@@ -216,12 +217,11 @@ static void display_device_inline(const char *margin, const void *data)
 	else
 		adapter_str = "-";
 
-	display("%s%-*s%-*s%-*s%-*s%-*s\n", margin,
-		20, device->name ? : "",
-		20, device->address ? : "",
-		10, get_powered_tostr(device),
-		10, adapter_str,
-		10, device->mode);
+	display_table_row(margin, 5, 20, device->name ? : "",
+				20, device->address ? : "",
+				10, get_powered_tostr(device),
+				10, adapter_str,
+				10, device->mode);
 }
 
 static const char *device_identity(void *data)
@@ -368,7 +368,7 @@ static void check_errors_method_callback(struct l_dbus_message *message,
 static enum cmd_status cmd_list(const char *device_name,
 						char **argv, int argc)
 {
-	display_table_header("Devices", MARGIN "%-*s%-*s%-*s%-*s%-*s",
+	display_table_header("Devices", MARGIN "%-*s  %-*s  %-*s  %-*s  %-*s",
 				20, "Name", 20, "Address", 10, "Powered",
 				10, "Adapter", 10, "Mode");
 
Signature · 1 line
2.34.1
#12 lore raw

PATCH 13/14 client: update adapter table header

In reply to James Prestwood

The table header needs to be adjusted to include spaces between columns.

The 'adapter list' command was also updated to shorted a few columns which were quite long for the data that is actually displayed.

---
 client/adapter.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
client/adapter.c +7 -5
index 6a0d3455..394eae0d 100644
@@ -185,9 +185,10 @@ static void display_adapter_inline(const char *margin, const void *data)
 {
 	const struct adapter *adapter = data;
 
-	display("%s%-*s%-*s%-.*s%-.*s\n", margin,
-		19, adapter->name ? : "-", 10, get_powered_tostr(adapter),
-		20, adapter->vendor ? : "-", 20, adapter->model ? : "-");
+	display_table_row(margin, 4, 8, adapter->name ? : "-",
+				8, get_powered_tostr(adapter),
+				20, adapter->vendor ? : "-",
+				20, adapter->model ? : "-");
 }
 
 static void *adapter_create(void)
@@ -268,8 +269,9 @@ static const struct proxy_interface *get_adapter_proxy_by_name(
 static enum cmd_status cmd_list(const char *adapter_name,
 						char **argv, int argc)
 {
-	display_table_header("Adapters", MARGIN "%-*s%-*s%-*s%-*s", 19, "Name",
-				10, "Powered", 20, "Vendor", 20, "Model");
+	display_table_header("Adapters", MARGIN "%-*s  %-*s  %-*s  %-*s",
+				8, "Name", 8, "Powered",
+				20, "Vendor", 20, "Model");
 
 	proxy_interface_display_list(adapter_interface_type.interface);
 
Signature · 1 line
2.34.1
#13 lore raw

PATCH 14/14 client: update ad-hoc table header

In reply to James Prestwood

The table header needs to be adjusted to include spaces between columns.

---
 client/ad-hoc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
client/ad-hoc.c +1 -1
index 33bff9ca..49c97f74 100644
@@ -117,7 +117,7 @@ static enum cmd_status cmd_list(const char *device_name, char **argv, int argc)
 	struct l_queue *match =
 		proxy_interface_find_all(IWD_AD_HOC_INTERFACE, NULL, NULL);
 
-	display_table_header("Devices in Ad-Hoc Mode", MARGIN "%-*s%-*s",
+	display_table_header("Devices in Ad-Hoc Mode", MARGIN "%-*s  %-*s",
 				20, "Name", 8, "Started");
 
 	if (!match) {
Signature · 1 line
2.34.1