3
0
mirror of https://git.kernel.org/pub/scm/network/wireless/iwd.git synced 2024-11-25 09:39:25 +01:00

client: make COLOR_* macros take a string input

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.
This commit is contained in:
James Prestwood 2022-07-07 10:55:05 -07:00 committed by Denis Kenzior
parent e4c3cfd229
commit 0b68601c10
6 changed files with 29 additions and 30 deletions

View File

@ -301,8 +301,8 @@ static struct l_dbus_message *request_user_password_method_call(
display("Type the network password for %s.\n", display("Type the network password for %s.\n",
proxy_interface_get_identity_str(proxy)); proxy_interface_get_identity_str(proxy));
username_prompt = l_strdup_printf(COLOR_BLUE PROMPT_USERNAME " " username_prompt = l_strdup_printf(COLOR_BLUE(PROMPT_USERNAME " ")
COLOR_OFF "%s\n", username); "%s\n", username);
display("%s", username_prompt); display("%s", username_prompt);
l_free(username_prompt); l_free(username_prompt);

View File

@ -76,7 +76,7 @@ void proxy_properties_display(const struct proxy_interface *proxy,
str = properties[i].tostr(data); str = properties[i].tostr(data);
display_table_row(MARGIN, 3, 8, properties[i].is_read_write ? display_table_row(MARGIN, 3, 8, properties[i].is_read_write ?
COLOR_BOLDGRAY " *" COLOR_OFF : "", COLOR_BOLDGRAY(" *") : "",
name_column_width, properties[i].name, name_column_width, properties[i].name,
value_column_width, str ? : ""); value_column_width, str ? : "");
} }

View File

@ -40,7 +40,7 @@
#include "client/display.h" #include "client/display.h"
#define IWD_PROMPT \ #define IWD_PROMPT \
"\001" COLOR_GREEN "\002" "[iwd]" "\001" COLOR_OFF "\002" "# " "\001" COLOR_GREEN("\002" "[iwd]" "\001") "\002" "# "
#define LINE_LEN 81 #define LINE_LEN 81
static struct l_signal *window_change_signal; 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 (ws.ws_col < LINE_LEN - 1) {
if (display_refresh.enabled) { if (display_refresh.enabled) {
display_refresh.recording = false; 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." "Enlarge window width to at least %u to enable."
"\n" COLOR_OFF, LINE_LEN - 1); "\n"), LINE_LEN - 1);
display_refresh.recording = true; display_refresh.recording = true;
} }
@ -317,7 +317,7 @@ void display(const char *fmt, ...)
void display_error(const char *error) 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); display_text(text);
@ -344,14 +344,14 @@ void display_table_header(const char *caption, const char *fmt, ...)
int caption_pos = int caption_pos =
(int) ((sizeof(dashed_line) - 1) / 2 + strlen(caption) / 2); (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, caption_pos, caption,
LINE_LEN - 2 - caption_pos, LINE_LEN - 2 - caption_pos,
display_refresh.cmd ? get_flasher() : ' '); display_refresh.cmd ? get_flasher() : ' ');
display_text(text); display_text(text);
l_free(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); display_text(text);
l_free(text); l_free(text);
@ -359,12 +359,12 @@ void display_table_header(const char *caption, const char *fmt, ...)
text = l_strdup_vprintf(fmt, args); text = l_strdup_vprintf(fmt, args);
va_end(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); display_text(body);
l_free(body); l_free(body);
l_free(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); display_text(text);
l_free(text); l_free(text);
} }
@ -774,7 +774,7 @@ void display_agent_prompt(const char *label, bool mask_input)
if (mask_input) if (mask_input)
reset_masked_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 (command_is_interactive_mode()) {
if (agent_saved_input) { if (agent_saved_input) {
@ -820,7 +820,7 @@ void display_agent_prompt_release(const char *label)
if (display_refresh.cmd) { if (display_refresh.cmd) {
char *text = rl_copy_text(0, rl_end); char *text = rl_copy_text(0, rl_end);
char *prompt = l_strdup_printf(COLOR_BLUE "%s " COLOR_OFF char *prompt = l_strdup_printf(COLOR_BLUE("%s ")
"%s\n", label, text); "%s\n", label, text);
l_free(text); l_free(text);

View File

@ -22,14 +22,13 @@
struct command; struct command;
struct command_family; 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 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 CLEAR_SCREEN "\x1b[2J"
#define MARGIN " " #define MARGIN " "

View File

@ -318,8 +318,8 @@ static const struct proxy_interface *known_network_proxy_find_by_name(
if (!network_args.type) { if (!network_args.type) {
display("Provided network name is ambiguous. " display("Provided network name is ambiguous. "
"Specify network security type as follows:\n"); "Specify network security type as follows:\n");
display("<\"network name" COLOR_BOLDGRAY ".security" display("<\"network name" COLOR_BOLDGRAY(".security")
COLOR_OFF "\">\n"); "\">\n");
display("\twhere '.security' is [.psk | .8021x | " display("\twhere '.security' is [.psk | .8021x | "
".open]\n"); ".open]\n");
} }

View File

@ -387,12 +387,12 @@ static const char *dbms_tostars(int16_t dbms)
return "****"; return "****";
if (dbms >= -6700) if (dbms >= -6700)
return "***" COLOR_BOLDGRAY "*" COLOR_OFF; return "***" COLOR_BOLDGRAY("*");
if (dbms >= -7500) if (dbms >= -7500)
return "**" COLOR_BOLDGRAY "**" COLOR_OFF; return "**" COLOR_BOLDGRAY("**");
return "*" COLOR_BOLDGRAY "***" COLOR_OFF; return "*" COLOR_BOLDGRAY("***");
} }
#define RSSI_DBMS "rssi-dbms" #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, display_table_row(MARGIN, 4, 2,
network_is_connected(network_i) ? network_is_connected(network_i) ?
COLOR_BOLDGRAY "> " COLOR_OFF: "", COLOR_BOLDGRAY("> ") : "",
32, network_name, 18, network_type, 6, 32, network_name, 18, network_type, 6,
display_signal_as_dbms ? dbms : display_signal_as_dbms ? dbms :
dbms_tostars(network->signal_strength)); dbms_tostars(network->signal_strength));