From bdfa4301d7fde603838409bc2bf414291a8d00c4 Mon Sep 17 00:00:00 2001 From: Sascha Kruse Date: Sat, 20 Oct 2012 15:35:04 +0200 Subject: [PATCH] add descriptions to usage --- dunst.c | 74 +++++++++++++++++----------------- options.c | 117 +++++++++++++++++++++++++++--------------------------- options.h | 16 ++++---- 3 files changed, 103 insertions(+), 104 deletions(-) diff --git a/dunst.c b/dunst.c index ff578a5..624b7cb 100644 --- a/dunst.c +++ b/dunst.c @@ -1399,24 +1399,24 @@ void load_options(char *cmdline_config_path) load_ini_file(config_file); #endif - font = option_get_string("global", "font", "-fn", font); - format = option_get_string("global", "format", "-format", format); - sort = option_get_bool("global", "sort", "-sort", sort); - indicate_hidden = option_get_bool("global", "indicate_hidden", "-indicate_hidden", indicate_hidden); - word_wrap = option_get_bool("global", "word_wrap", "-word_wrap", word_wrap); - idle_threshold = option_get_int("global", "idle_threshold", "-idle_threshold", idle_threshold); - monitor = option_get_int("global", "monitor", "-mon", monitor); + font = option_get_string("global", "font", "-fn", font, "The font dunst should use."); + format = option_get_string("global", "format", "-format", format, "The format template for the notifictions"); + sort = option_get_bool("global", "sort", "-sort", sort, "Sort notifications by urgency and date?"); + indicate_hidden = option_get_bool("global", "indicate_hidden", "-indicate_hidden", indicate_hidden, "Show how many notificaitons are hidden?"); + word_wrap = option_get_bool("global", "word_wrap", "-word_wrap", word_wrap, "Truncating long lines or do word wrap"); + idle_threshold = option_get_int("global", "idle_threshold", "-idle_threshold", idle_threshold, "Don't timeout notifications if user is longer idle than threshold"); + monitor = option_get_int("global", "monitor", "-mon", monitor, "On which monitor should the notifications be displayed"); { - char *c = option_get_string("global", "follow", "-follow", ""); + char *c = option_get_string("global", "follow", "-follow", "", "Follow mouse, keyboard or none?"); if (strlen(c) > 0) { parse_follow_mode(c); free(c); } } - geom = option_get_string("global", "geometry", "-geom/-geometry", geom); - line_height = option_get_int("global", "line_height", "-lh/-line_height", line_height); + geom = option_get_string("global", "geometry", "-geom/-geometry", geom, "Geometry for the window"); + line_height = option_get_int("global", "line_height", "-lh/-line_height", line_height, "Add additional padding above and beneath text"); { - char *c = option_get_string("global", "modifier", NULL, ""); + char *c = option_get_string("global", "modifier", NULL, "", ""); if (strlen(c) > 0) { deprecated_dunstrc_shortcuts = True; KeySym mod = string_to_mask(c); @@ -1426,12 +1426,12 @@ void load_options(char *cmdline_config_path) free(c); } } - close_ks.str = option_get_string("global", "key", NULL, close_ks.str); - close_all_ks.str = option_get_string("global", "all_key", NULL, close_all_ks.str); - history_ks.str = option_get_string("global", "history_key", NULL, history_ks.str); - bounce_freq = option_get_double("global", "bounce_freq", "-bounce_freq", bounce_freq); + close_ks.str = option_get_string("global", "key", NULL, close_ks.str, ""); + close_all_ks.str = option_get_string("global", "all_key", NULL, close_all_ks.str, ""); + history_ks.str = option_get_string("global", "history_key", NULL, history_ks.str, ""); + bounce_freq = option_get_double("global", "bounce_freq", "-bounce_freq", bounce_freq, "Make long text bounce from side to side"); { - char *c = option_get_string("global", "alignment", "-align/-alignment", ""); + char *c = option_get_string("global", "alignment", "-align/-alignment", "", "Align notifications left/center/right"); if (strlen(c) > 0) { if (strcmp(c, "left") == 0) align = left; @@ -1444,12 +1444,12 @@ void load_options(char *cmdline_config_path) free(c); } } - show_age_threshold = option_get_int("global", "show_age_threshold", "-show_age_threshold", show_age_threshold); - sticky_history = option_get_bool("global", "sticky_history", "-sticky_history", sticky_history); - separator_height = option_get_int("global", "separator_height", "-sep_height/-separator_height", separator_height); - transparency = option_get_int("global", "transparency", "-transparency", transparency); + show_age_threshold = option_get_int("global", "show_age_threshold", "-show_age_threshold", show_age_threshold, "When should the age of the notification be displayed?"); + sticky_history = option_get_bool("global", "sticky_history", "-sticky_history", sticky_history, "Don't timeout notifications popped up from history"); + separator_height = option_get_int("global", "separator_height", "-sep_height/-separator_height", separator_height, "height of the separator line"); + transparency = option_get_int("global", "transparency", "-transparency", transparency, "Transparency. range 0-100"); { - char *c = option_get_string("global", "separator_color", "-sep_color/-separator_color", ""); + char *c = option_get_string("global", "separator_color", "-sep_color/-separator_color", "", "Color of the separator line (or 'auto')"); if (strlen(c) > 0) { if (strcmp(c, "auto") == 0) sep_color = AUTO; @@ -1461,21 +1461,21 @@ void load_options(char *cmdline_config_path) } } - lowbgcolor = option_get_string("urgency_low", "background", "-lb", lowbgcolor); - lowfgcolor = option_get_string("urgency_low", "foreground", "-lf", lowfgcolor); - timeouts[LOW] = option_get_int("urgency_low", "timeout", "-lto", timeouts[LOW]); - normbgcolor = option_get_string("urgency_normal", "background", "-nb", normbgcolor); - normfgcolor = option_get_string("urgency_normal", "foreground", "-nf", normfgcolor); - timeouts[NORM] = option_get_int("urgency_normal", "timeout", "-nto", timeouts[NORM]); - critbgcolor = option_get_string("urgency_critical", "background", "-cb", critbgcolor); - critfgcolor = option_get_string("urgency_critical", "foreground", "-cf", critfgcolor); - timeouts[CRIT] = option_get_int("urgency_critical", "timeout", "-cto", timeouts[CRIT]); + lowbgcolor = option_get_string("urgency_low", "background", "-lb", lowbgcolor, "Background color for notifcations with low urgency"); + lowfgcolor = option_get_string("urgency_low", "foreground", "-lf", lowfgcolor, "Foreground color for notifications with low urgency"); + timeouts[LOW] = option_get_int("urgency_low", "timeout", "-lto", timeouts[LOW], "Timeout for notifications with low urgency"); + normbgcolor = option_get_string("urgency_normal", "background", "-nb", normbgcolor, "Background color for notifications with normal urgency"); + normfgcolor = option_get_string("urgency_normal", "foreground", "-nf", normfgcolor, "Foreground color for notifications with normal urgency"); + timeouts[NORM] = option_get_int("urgency_normal", "timeout", "-nto", timeouts[NORM], "Timeout for notifications with normal urgency"); + critbgcolor = option_get_string("urgency_critical", "background", "-cb", critbgcolor, "Background color for notifications with critical urgency"); + critfgcolor = option_get_string("urgency_critical", "foreground", "-cf", critfgcolor, "Foreground color for notifications with ciritical urgency"); + timeouts[CRIT] = option_get_int("urgency_critical", "timeout", "-cto", timeouts[CRIT], "Timeout for notifications with critical urgency"); - close_ks.str = option_get_string("shortcuts", "close", "-key", close_ks.str); - close_all_ks.str = option_get_string("shortcuts", "close_all", "-all_key", close_all_ks.str); - history_ks.str = option_get_string("shortcuts", "history", "-history_key", history_ks.str); + close_ks.str = option_get_string("shortcuts", "close", "-key", close_ks.str, "Shortcut for closing one notification"); + close_all_ks.str = option_get_string("shortcuts", "close_all", "-all_key", close_all_ks.str, "Shortcut for closing all notifications"); + history_ks.str = option_get_string("shortcuts", "history", "-history_key", history_ks.str, "Shortcut to pop the last notification from history"); - print_notifications = cmdline_get_bool("-print", False); + print_notifications = cmdline_get_bool("-print", False, "Print notifications to cmdline (DEBUG)"); char *cur_section = NULL; for (;;) { @@ -1544,15 +1544,15 @@ int main(int argc, char *argv[]) cmdline_load(argc, argv); - if (cmdline_get_bool("-v/-version", False) || cmdline_get_bool("--version", False)) { + if (cmdline_get_bool("-v/-version", False, "Print version") || cmdline_get_bool("--version", False, "Print version")) { print_version(); } char *cmdline_config_path; - cmdline_config_path = cmdline_get_string("-conf/-config", NULL); + cmdline_config_path = cmdline_get_string("-conf/-config", NULL, "Path to configuration file"); load_options(cmdline_config_path); - if (cmdline_get_bool("-h/-help", False) || cmdline_get_bool("--help", False)) { + if (cmdline_get_bool("-h/-help", False, "Print help") || cmdline_get_bool("--help", False, "Print help")) { usage(EXIT_SUCCESS); } diff --git a/options.c b/options.c index 541184b..225b47d 100644 --- a/options.c +++ b/options.c @@ -38,7 +38,7 @@ static int cmdline_argc; static char **cmdline_argv; static char *usage_str = NULL; -static void cmdline_usage_append(char *key, char *type); +static void cmdline_usage_append(char *key, char *type, char *description); static int cmdline_find_option(char *key); @@ -307,46 +307,56 @@ int cmdline_find_option(char *key) return -1; } -char *cmdline_get_string(char *key, char *def) +static char *cmdline_get_value(char *key) { - cmdline_usage_append(key, "string"); int idx = cmdline_find_option(key); if (idx < 0) { - return def; + return NULL; } if (idx + 1 >= cmdline_argc || cmdline_argv[idx+1][0] == '-') { /* the argument is missing */ fprintf(stderr, "Warning: %s, missing argument. Ignoring\n", key); - return def; + return NULL; } - return cmdline_argv[idx+1]; } -int cmdline_get_int(char *key, int def) +char *cmdline_get_string(char *key, char *def, char *description) { - cmdline_usage_append(key, "double"); - char *str = cmdline_get_string(key, NULL); + cmdline_usage_append(key, "string", description); + char *str = cmdline_get_value(key); + + if (str) + return str; + else + return def; +} + +int cmdline_get_int(char *key, int def, char *description) +{ + cmdline_usage_append(key, "double", description); + char *str = cmdline_get_value(key); + if (str == NULL) return def; else return atoi(str); } -double cmdline_get_double(char *key, double def) +double cmdline_get_double(char *key, double def, char *description) { - cmdline_usage_append(key, "double"); - char *str = cmdline_get_string(key, NULL); + cmdline_usage_append(key, "double", description); + char *str = cmdline_get_value(key); if (str == NULL) return def; else return atof(str); } -int cmdline_get_bool(char *key, int def) +int cmdline_get_bool(char *key, int def, char *description) { - cmdline_usage_append(key, ""); + cmdline_usage_append(key, "", description); int idx = cmdline_find_option(key); if (idx > 0) return true; @@ -354,12 +364,12 @@ int cmdline_get_bool(char *key, int def) return def; } -char *option_get_string(char *ini_section, char *ini_key, char *cmdline_key, char *def) +char *option_get_string(char *ini_section, char *ini_key, char *cmdline_key, char *def, char *description) { char *val = NULL; if (cmdline_key) { - val = cmdline_get_string(cmdline_key, NULL); + val = cmdline_get_string(cmdline_key, NULL, description); } if (val) { @@ -370,48 +380,38 @@ char *option_get_string(char *ini_section, char *ini_key, char *cmdline_key, cha } -int option_get_int(char *ini_section, char *ini_key, char *cmdline_key, int def) +int option_get_int(char *ini_section, char *ini_key, char *cmdline_key, int def, char *description) { - /* we have to get this twice in order to check wether the actual value - * is the same as the first default value - */ - int val, val2; - if (cmdline_key) { - val = cmdline_get_int(cmdline_key, 1); - val2 = cmdline_get_int(cmdline_key, 0); - } + /* *str is only used to check wether the cmdline option is actually set. */ + char *str = cmdline_get_value(cmdline_key); - if (cmdline_key && val == val2) { - /* the cmdline option has been set */ - return val; - } else { + /* we call cmdline_get_int even when the option isn't set in order to + * add the usage info */ + int val = cmdline_get_int(cmdline_key, def, description); + + if (!str) return ini_get_int(ini_section, ini_key, def); - } -} - -double option_get_double(char *ini_section, char *ini_key, char *cmdline_key, double def) -{ - /* see option_get_int */ - double val, val2; - if (cmdline_key) { - val = cmdline_get_double(cmdline_key, 1); - val2 = cmdline_get_double(cmdline_key, 0); - } - - if (cmdline_key && val == val2) { + else return val; - } else { - return ini_get_double(ini_section, ini_key, def); - } - } -int option_get_bool(char *ini_section, char *ini_key, char *cmdline_key, int def) +double option_get_double(char *ini_section, char *ini_key, char *cmdline_key, double def, char *description) +{ + char *str = cmdline_get_value(cmdline_key); + double val = cmdline_get_double(cmdline_key, def, description); + + if (!str) + return ini_get_int(ini_section, ini_key, def); + else + return val; +} + +int option_get_bool(char *ini_section, char *ini_key, char *cmdline_key, int def, char *description) { int val; if (cmdline_key) - val = cmdline_get_bool(cmdline_key, false); + val = cmdline_get_bool(cmdline_key, false, description); if (cmdline_key && val) { /* this can only be true if the value has been set, @@ -422,24 +422,23 @@ int option_get_bool(char *ini_section, char *ini_key, char *cmdline_key, int def return ini_get_bool(ini_section, ini_key, def); } -void cmdline_usage_append(char *key, char *type) +void cmdline_usage_append(char *key, char *type, char *description) { - static int add_linebreak = 2; - + char *key_type; + if (type && strlen(type) > 0) + asprintf(&key_type, "%s (%s)", key, type); + else + asprintf(&key_type, "%s", key); if (!usage_str) { - asprintf(&usage_str, "[%s %s]", key, type); + asprintf(&usage_str, "%-40s - %s\n", key_type, description); + free(key_type); return; } char *tmp; - add_linebreak--; - if (add_linebreak == 0) { - asprintf(&tmp, "%s[%s %s]\n", usage_str, key, type); - add_linebreak = 3; - } else { - asprintf(&tmp, "%s[%s %s] ", usage_str, key, type); - } + asprintf(&tmp, "%s%-40s - %s\n", usage_str, key_type, description); + free(key_type); free(usage_str); usage_str = tmp; diff --git a/options.h b/options.h index 76a04ba..e752e9e 100644 --- a/options.h +++ b/options.h @@ -13,16 +13,16 @@ void free_ini(void); void cmdline_load(int argc, char *argv[]); /* for all cmdline_get_* key can be either "-key" or "-key/-longkey" */ -char *cmdline_get_string(char *key, char *def); -int cmdline_get_int(char *key, int def); -double cmdline_get_double(char *key, double def); -int cmdline_get_bool(char *key, int def); +char *cmdline_get_string(char *key, char *def, char *description); +int cmdline_get_int(char *key, int def, char *description); +double cmdline_get_double(char *key, double def, char *description); +int cmdline_get_bool(char *key, int def, char *description); char *cmdline_create_usage(void); -char *option_get_string(char *ini_section, char *ini_key, char *cmdline_key, char *def); -int option_get_int(char *ini_section, char *ini_key, char *cmdline_key, int def); -double option_get_double(char *ini_section, char *ini_key, char *cmdline_key, double def); -int option_get_bool(char *ini_section, char *ini_key, char *cmdline_key, int def); +char *option_get_string(char *ini_section, char *ini_key, char *cmdline_key, char *def, char *description); +int option_get_int(char *ini_section, char *ini_key, char *cmdline_key, int def, char *description); +double option_get_double(char *ini_section, char *ini_key, char *cmdline_key, double def, char *description); +int option_get_bool(char *ini_section, char *ini_key, char *cmdline_key, int def, char *description); /* returns the next known section. * if section == NULL returns first section.