diff options
-rwxr-xr-x | contrib/any_position_wrapper.sh | 64 | ||||
-rwxr-xr-x | contrib/net-speed.sh | 2 | ||||
-rw-r--r-- | man/i3status.man | 83 | ||||
-rw-r--r-- | src/print_cpu_usage.c | 11 | ||||
-rw-r--r-- | src/print_ipv6_addr.c | 16 | ||||
-rw-r--r-- | src/print_load.c | 35 | ||||
-rw-r--r-- | src/print_mem.c | 85 | ||||
-rw-r--r-- | src/print_path_exists.c | 23 | ||||
-rw-r--r-- | src/print_run_watch.c | 23 | ||||
-rw-r--r-- | src/print_time.c | 27 |
10 files changed, 202 insertions, 167 deletions
diff --git a/contrib/any_position_wrapper.sh b/contrib/any_position_wrapper.sh new file mode 100755 index 0000000..8d877d4 --- /dev/null +++ b/contrib/any_position_wrapper.sh @@ -0,0 +1,64 @@ +#!/usr/bin/env bash + +# This i3status wrapper allows to add custom information in any position of the statusline +# It was developed for i3bar (JSON format) + +# The idea is to define "holder" modules in i3status config and then replace them + +# In order to make this example work you need to add +# order += "tztime holder__hey_man" +# and +# tztime holder__hey_man { +# format = "holder__hey_man" +# } +# in i3staus config + +# Don't forget that i3status config should contain: +# general { +# output_format = i3bar +# } +# +# and i3 config should contain: +# bar { +# status_command exec /path/to/this/script.sh +# } + +# Make sure jq is installed +# That's it + +# You can easily add multiple custom modules using additional "holders" + +function update_holder { + + local instance="$1" + local replacement="$2" + echo "$json_array" | jq --argjson arg_j "$replacement" "(.[] | (select(.instance==\"$instance\"))) |= \$arg_j" +} + +function remove_holder { + + local instance="$1" + echo "$json_array" | jq "del(.[] | (select(.instance==\"$instance\")))" +} + +function hey_man { + + local rand_val=$((RANDOM % 3)) + if [ $rand_val == 1 ] ; then + local json='{ "full_text": "Hey Man!", "color": "#00FF00" }' + json_array=$(update_holder holder__hey_man "$json") + elif [ $rand_val == 0 ] ; then + local json='{ "full_text": "Hey Man!", "color": "#FF0000" }' + json_array=$(update_holder holder__hey_man "$json") + else + json_array=$(remove_holder holder__hey_man) + fi +} + +i3status | (read line; echo "$line"; read line ; echo "$line" ; read line ; echo "$line" ; while true +do + read line + json_array="$(echo $line | sed -e 's/^,//')" + hey_man + echo ",$json_array" +done) diff --git a/contrib/net-speed.sh b/contrib/net-speed.sh index 71a563f..ed41974 100755 --- a/contrib/net-speed.sh +++ b/contrib/net-speed.sh @@ -24,7 +24,7 @@ # # Auto detect interfaces -ifaces=$(ls /sys/class/net | grep -E '^(eth|wlan|enp|wlp)') +ifaces=$(ls /sys/class/net | grep -E '^(eth|wlan|enp|enx|wlp)') last_time=0 last_rx=0 diff --git a/man/i3status.man b/man/i3status.man index c2849c3..9699529 100644 --- a/man/i3status.man +++ b/man/i3status.man @@ -182,10 +182,10 @@ none:: Does not use any color codes. Separates values by the pipe symbol by default. This should be used with i3bar and can be used for custom scripts. -It's also possible to use the color_good, color_degraded, color_bad directives -to define specific colors per module. If one of these directives is defined -in a module section its value will override the value defined in the general -section just for this module. +It's also possible to use the +color_good+, +color_degraded+, +color_bad+ +directives to define specific colors per module. If one of these directives is +defined in a module section its value will override the value defined in the +general section just for this module. If you don't fancy the vertical separators between modules i3status/i3bar uses by default, you can employ the +separator+ directive to configure how @@ -235,8 +235,8 @@ best available public IPv6 address on your computer). Gets used, free, available and total amount of bytes on the given mounted filesystem. -These values can also be expressed in percentages with the percentage_used, -percentage_free, percentage_avail and percentage_used_of_avail formats. +These values can also be expressed in percentages with the +percentage_used+, ++percentage_free+, +percentage_avail+ and +percentage_used_of_avail+ formats. Byte sizes are presented in a human readable format using a set of prefixes whose type can be specified via the "prefix_type" option. Three sets of @@ -250,16 +250,16 @@ SI prefixes (k, M, G, T) represent multiples of powers of 1000. custom:: The custom prefixes (K, M, G, T) represent multiples of powers of 1024. -It is possible to define a low_threshold that causes the disk text to be -displayed using color_bad. The low_threshold type can be of threshold_type +It is possible to define a +low_threshold+ that causes the disk text to be +displayed using +color_bad+. The +low_threshold+ type can be of +threshold_type+ "bytes_free", "bytes_avail", "percentage_free", or "percentage_avail", where the former two can be prepended by a generic prefix (k, m, g, t) having -prefix_type. So, if you configure low_threshold to 2, threshold_type to +prefix_type. So, if you configure +low_threshold+ to 2, +threshold_type+ to "gbytes_avail", and prefix_type to "binary", and the remaining available disk -space is below 2 GiB, it will be colored bad. If not specified, threshold_type -is assumed to be "percentage_avail" and low_threshold to be set to 0, which +space is below 2 GiB, it will be colored bad. If not specified, +threshold_type+ +is assumed to be "percentage_avail" and +low_threshold+ to be set to 0, which implies no coloring at all. You can customize the output format when below -low_threshold with format_below_threshold. ++low_threshold+ with +format_below_threshold.+ You can define a different format with the option "format_not_mounted" which is used if the path does not exist or is not a mount point. Defaults to "". @@ -283,7 +283,7 @@ which is used if the path does not exist or is not a mount point. Defaults to "" Expands the given path to a pidfile and checks if the process ID found inside is valid (that is, if the process is running). You can use this to check if a specific application, such as a VPN client or your DHCP client is running. -There also is an option "format_down". You can hide the output with +There also is an option +format_down+. You can hide the output with +format_down=""+. *Example order*: +run_watch DHCP+ @@ -294,7 +294,7 @@ There also is an option "format_down". You can hide the output with Checks if the given path exists in the filesystem. You can use this to check if something is active, like for example a VPN tunnel managed by NetworkManager. -There also is an option "format_down". You can hide the output with +There also is an option +format_down+. You can hide the output with +format_down=""+. *Example order*: +path_exists VPN+ @@ -358,14 +358,15 @@ If you want the battery percentage to be shown in another format, use +format_percentage = "%.00f%s"+ If your battery is represented in a non-standard path in /sys, be sure to -modify the "path" property accordingly, i.e. pointing to the uevent file on +modify the +path+ property accordingly, i.e. pointing to the uevent file on your system. The first occurrence of %d gets replaced with the battery number, but you can just hard-code a path as well. It is possible to define a low_threshold that causes the battery text to be -colored red. The low_threshold type can be of threshold_type "time" or -"percentage". So, if you configure low_threshold to 10 and threshold_type to -"time", and your battery lasts another 9 minutes, it will be colored red. +colored +color_bad+. The +low_threshold+ type can be of +threshold_type+ "time" +or "percentage". So, if you configure +low_threshold+ to 10 and +threshold_type+ +to "time", and your battery lasts another 9 minutes, it will be colored ++color_bad+. To show an aggregate of all batteries in the system, use "all" as the number. In this case (for Linux), the /sys path must contain the "%d" sequence. Otherwise, @@ -407,10 +408,10 @@ FULL) is used. === CPU-Temperature Gets the temperature of the given thermal zone. It is possible to -define a max_threshold that will color the temperature red in case the +define a +max_threshold+ that will color the temperature red in case the specified thermal zone is getting too hot. Defaults to 75 degrees C. The -output format when above max_threshold can be customized with -format_above_threshold. +output format when above +max_threshold+ can be customized with ++format_above_threshold+. *Example order*: +cpu_temperature 0+ @@ -427,17 +428,17 @@ format_above_threshold. Gets the percentual CPU usage from +/proc/stat+ (Linux) or +sysctl(3)+ (FreeBSD/OpenBSD). -It is possible to define a max_threshold that will color the load -value red in case the CPU average over the last interval is getting +It is possible to define a +max_threshold+ that will color the load +value with +color_bad+ in case the CPU average over the last interval is getting higher than the configured threshold. Defaults to 95. The output -format when above max_threshold can be customized with -format_above_threshold. +format when above +max_threshold+ can be customized with ++format_above_threshold+. -It is possible to define a degraded_threshold that will color the load -value yellow in case the CPU average over the last interval is getting -higher than the configured threshold. Defaults to 90. The output format +It is possible to define a +degraded_threshold+ that will color the load +value with +color_degraded+ in case the CPU average over the last interval is +getting higher than the configured threshold. Defaults to 90. The output format when above degraded threshold can be customized with -format_above_degraded_threshold. ++format_above_degraded_threshold+. For displaying the Nth CPU usage, you can use the %cpu<N> format string, starting from %cpu0. This feature is currently not supported in FreeBSD. @@ -464,11 +465,11 @@ available. These will print human readable values. It's also possible to prefix the placeholders with +percentage_+ to get a value in percent. It's possible to define a +threshold_degraded+ and a +threshold_critical+ to -color the status bar output in yellow or red, if the available memory falls -below the given threshold. Possible values of the threshold can be any integer, -suffixed with an iec symbol (+T+, +G+, +M+, +K+). Alternatively, the integer -can be suffixed by a percent sign, which then rets evaluated relatively to -total memory. +color the status bar output in +color_degraded+ or +color_bad+, if the available +memory falls below the given threshold. Possible values of the threshold can be +any integer, suffixed with an iec symbol (+T+, +G+, +M+, +K+). Alternatively, +the integer can be suffixed by a percent sign, which then rets evaluated +relatively to total memory. If the +format_degraded+ parameter is given and either the critical or the degraded threshold applies, +format_degraded+ will get used as format string. @@ -510,10 +511,10 @@ methods to distinguish the actually used memory. Gets the system load (number of processes waiting for CPU time in the last 1, 5 and 15 minutes). It is possible to define a max_threshold that will -color the load value red in case the load average of the last minute is +color the load value +color_bad+ in case the load average of the last minute is getting higher than the configured threshold. Defaults to 5. The output -format when above max_threshold can be customized with -format_above_threshold. +format when above +max_threshold+ can be customized with ++format_above_threshold+. *Example order*: +load+ @@ -645,9 +646,9 @@ volume master { Outputs the contents of the specified file. You can use this to check contents of files on your system, for example /proc/uptime. By default the function only reads the first 254 characters of the file, if you want to override this set -the Max_characters option. It will never read beyond the first 4095 characters. -If the file is not found "no file" will be printed, if the file can't be read -"error read" will be printed. +the +Max_characters+ option. It will never read beyond the first 4095 +characters. If the file is not found "no file" will be printed, if the file +can't be read "error read" will be printed. *Example order*: read_file UPTIME @@ -682,7 +683,7 @@ separator:: A boolean value which specifies whether a separator line should be drawn after this block. The default is true, meaning the separator line will be drawn. Note that if you disable the separator line, there will still be a - gap after the block, unless you also use separator_block_width. + gap after the block, unless you also use +separator_block_width+. separator_block_width:: The amount of pixels to leave blank after the block. In the middle of this gap, a separator symbol will be drawn unless separator is disabled. This is diff --git a/src/print_cpu_usage.c b/src/print_cpu_usage.c index 979e082..abf3481 100644 --- a/src/print_cpu_usage.c +++ b/src/print_cpu_usage.c @@ -183,7 +183,8 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const #if defined(__linux__) else if (BEGINS_WITH(walk + 1, "cpu")) { int number = -1; - sscanf(walk + 1, "cpu%d", &number); + int length = strlen("cpu"); + sscanf(walk + 1, "cpu%d%n", &number, &length); if (number == -1) { fprintf(stderr, "i3status: provided CPU number cannot be parsed\n"); } else if (number >= cpu_count) { @@ -194,13 +195,7 @@ void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format, const int cpu_diff_usage = (cpu_diff_total ? (1000 * (cpu_diff_total - cpu_diff_idle) / cpu_diff_total + 5) / 10 : 0); outwalk += sprintf(outwalk, "%02d%s", cpu_diff_usage, pct_mark); } - int padding = 1; - int step = 10; - while (step <= number) { - step *= 10; - padding++; - } - walk += strlen("cpu") + padding; + walk += length; } #endif else { diff --git a/src/print_ipv6_addr.c b/src/print_ipv6_addr.c index 5a0c38d..8fd8669 100644 --- a/src/print_ipv6_addr.c +++ b/src/print_ipv6_addr.c @@ -118,7 +118,6 @@ static char *get_ipv6_addr(void) { } void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down) { - const char *walk; char *addr_string = get_ipv6_addr(); char *outwalk = buffer; @@ -131,18 +130,13 @@ void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, con } START_COLOR("color_good"); - for (walk = format_up; *walk != '\0'; walk++) { - if (*walk != '%') { - *(outwalk++) = *walk; - } else if (BEGINS_WITH(walk + 1, "ip")) { - outwalk += sprintf(outwalk, "%s", addr_string); - walk += strlen("ip"); + placeholder_t placeholders[] = { + {.name = "%ip", .value = addr_string}}; - } else { - *(outwalk++) = '%'; - } - } + const size_t num = sizeof(placeholders) / sizeof(placeholder_t); + buffer = format_placeholders(format_up, &placeholders[0], num); END_COLOR; OUTPUT_FULL_TEXT(buffer); + free(buffer); } diff --git a/src/print_load.c b/src/print_load.c index 5d97a2c..3ffc576 100644 --- a/src/print_load.c +++ b/src/print_load.c @@ -7,6 +7,10 @@ #include <yajl/yajl_gen.h> #include <yajl/yajl_version.h> +#include "i3status.h" + +#define STRING_SIZE 10 + void print_load(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const float max_threshold) { char *outwalk = buffer; /* Get load */ @@ -14,7 +18,6 @@ void print_load(yajl_gen json_gen, char *buffer, const char *format, const char #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(linux) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(sun) || defined(__DragonFly__) double loadavg[3]; const char *selected_format = format; - const char *walk; bool colorful_output = false; if (getloadavg(loadavg, 3) == -1) @@ -27,32 +30,28 @@ void print_load(yajl_gen json_gen, char *buffer, const char *format, const char selected_format = format_above_threshold; } - for (walk = selected_format; *walk != '\0'; walk++) { - if (*walk != '%') { - *(outwalk++) = *walk; + char string_loadavg_1[STRING_SIZE]; + char string_loadavg_5[STRING_SIZE]; + char string_loadavg_15[STRING_SIZE]; - } else if (BEGINS_WITH(walk + 1, "1min")) { - outwalk += sprintf(outwalk, "%1.2f", loadavg[0]); - walk += strlen("1min"); + snprintf(string_loadavg_1, STRING_SIZE, "%1.2f", loadavg[0]); + snprintf(string_loadavg_5, STRING_SIZE, "%1.2f", loadavg[1]); + snprintf(string_loadavg_15, STRING_SIZE, "%1.2f", loadavg[2]); - } else if (BEGINS_WITH(walk + 1, "5min")) { - outwalk += sprintf(outwalk, "%1.2f", loadavg[1]); - walk += strlen("5min"); + placeholder_t placeholders[] = { + {.name = "%1min", .value = string_loadavg_1}, + {.name = "%5min", .value = string_loadavg_5}, + {.name = "%15min", .value = string_loadavg_15}}; - } else if (BEGINS_WITH(walk + 1, "15min")) { - outwalk += sprintf(outwalk, "%1.2f", loadavg[2]); - walk += strlen("15min"); - - } else { - *(outwalk++) = '%'; - } - } + const size_t num = sizeof(placeholders) / sizeof(placeholder_t); + buffer = format_placeholders(selected_format, &placeholders[0], num); if (colorful_output) END_COLOR; *outwalk = '\0'; OUTPUT_FULL_TEXT(buffer); + free(buffer); return; error: diff --git a/src/print_mem.c b/src/print_mem.c index 51dff1c..cbe42a9 100644 --- a/src/print_mem.c +++ b/src/print_mem.c @@ -7,6 +7,8 @@ #include <yajl/yajl_version.h> #include "i3status.h" +#define STRING_SIZE 10 + #define BINARY_BASE 1024UL #if defined(linux) @@ -32,6 +34,10 @@ static int print_bytes_human(char *outwalk, unsigned long bytes, const char *uni } return sprintf(outwalk, "%.*f %s", decimals, base, iec_symbols[exponent]); } + +static int print_percentage(char *outwalk, float percent) { + return snprintf(outwalk, STRING_SIZE, "%.1f%s", percent, pct_mark); +} #endif #if defined(linux) @@ -157,56 +163,45 @@ void print_memory(yajl_gen json_gen, char *buffer, const char *format, const cha selected_format = format_degraded; } - for (walk = selected_format; *walk != '\0'; walk++) { - if (*walk != '%') { - *(outwalk++) = *walk; - - } else if (BEGINS_WITH(walk + 1, "total")) { - outwalk += print_bytes_human(outwalk, ram_total, unit, decimals); - walk += strlen("total"); - - } else if (BEGINS_WITH(walk + 1, "used")) { - outwalk += print_bytes_human(outwalk, ram_used, unit, decimals); - walk += strlen("used"); - - } else if (BEGINS_WITH(walk + 1, "free")) { - outwalk += print_bytes_human(outwalk, ram_free, unit, decimals); - walk += strlen("free"); - - } else if (BEGINS_WITH(walk + 1, "available")) { - outwalk += print_bytes_human(outwalk, ram_available, unit, decimals); - walk += strlen("available"); - - } else if (BEGINS_WITH(walk + 1, "shared")) { - outwalk += print_bytes_human(outwalk, ram_shared, unit, decimals); - walk += strlen("shared"); - - } else if (BEGINS_WITH(walk + 1, "percentage_free")) { - outwalk += sprintf(outwalk, "%.01f%s", 100.0 * ram_free / ram_total, pct_mark); - walk += strlen("percentage_free"); - - } else if (BEGINS_WITH(walk + 1, "percentage_available")) { - outwalk += sprintf(outwalk, "%.01f%s", 100.0 * ram_available / ram_total, pct_mark); - walk += strlen("percentage_available"); - - } else if (BEGINS_WITH(walk + 1, "percentage_used")) { - outwalk += sprintf(outwalk, "%.01f%s", 100.0 * ram_used / ram_total, pct_mark); - walk += strlen("percentage_used"); - - } else if (BEGINS_WITH(walk + 1, "percentage_shared")) { - outwalk += sprintf(outwalk, "%.01f%s", 100.0 * ram_shared / ram_total, pct_mark); - walk += strlen("percentage_shared"); - - } else { - *(outwalk++) = '%'; - } - } + char string_ram_total[STRING_SIZE]; + char string_ram_used[STRING_SIZE]; + char string_ram_free[STRING_SIZE]; + char string_ram_available[STRING_SIZE]; + char string_ram_shared[STRING_SIZE]; + char string_percentage_free[STRING_SIZE]; + char string_percentage_available[STRING_SIZE]; + char string_percentage_used[STRING_SIZE]; + char string_percentage_shared[STRING_SIZE]; + + print_bytes_human(string_ram_total, ram_total, unit, decimals); + print_bytes_human(string_ram_used, ram_used, unit, decimals); + print_bytes_human(string_ram_free, ram_free, unit, decimals); + print_bytes_human(string_ram_available, ram_available, unit, decimals); + print_bytes_human(string_ram_shared, ram_shared, unit, decimals); + print_percentage(string_percentage_free, 100.0 * ram_free / ram_total); + print_percentage(string_percentage_available, 100.0 * ram_available / ram_total); + print_percentage(string_percentage_used, 100.0 * ram_used / ram_total); + print_percentage(string_percentage_shared, 100.0 * ram_shared / ram_total); + + placeholder_t placeholders[] = { + {.name = "%total", .value = string_ram_total}, + {.name = "%used", .value = string_ram_used}, + {.name = "%free", .value = string_ram_free}, + {.name = "%available", .value = string_ram_available}, + {.name = "%shared", .value = string_ram_shared}, + {.name = "%percentage_free", .value = string_percentage_free}, + {.name = "%percentage_available", .value = string_percentage_available}, + {.name = "%percentage_used", .value = string_percentage_used}, + {.name = "%percentage_shared", .value = string_percentage_shared}}; + + const size_t num = sizeof(placeholders) / sizeof(placeholder_t); + buffer = format_placeholders(selected_format, &placeholders[0], num); if (output_color) END_COLOR; - *outwalk = '\0'; OUTPUT_FULL_TEXT(buffer); + free(buffer); return; error: diff --git a/src/print_path_exists.c b/src/print_path_exists.c index 87601b4..504eb6c 100644 --- a/src/print_path_exists.c +++ b/src/print_path_exists.c @@ -7,6 +7,8 @@ #include <sys/stat.h> #include "i3status.h" +#define STRING_SIZE 5 + void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_down) { const char *walk; char *outwalk = buffer; @@ -23,23 +25,18 @@ void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const START_COLOR((exists ? "color_good" : "color_bad")); - for (; *walk != '\0'; walk++) { - if (*walk != '%') { - *(outwalk++) = *walk; + char string_status[STRING_SIZE]; - } else if (BEGINS_WITH(walk + 1, "title")) { - outwalk += sprintf(outwalk, "%s", title); - walk += strlen("title"); + snprintf(string_status, STRING_SIZE, "%s", (exists ? "yes" : "no")); - } else if (BEGINS_WITH(walk + 1, "status")) { - outwalk += sprintf(outwalk, "%s", (exists ? "yes" : "no")); - walk += strlen("status"); + placeholder_t placeholders[] = { + {.name = "%title", .value = title}, + {.name = "%status", .value = string_status}}; - } else { - *(outwalk++) = '%'; - } - } + const size_t num = sizeof(placeholders) / sizeof(placeholder_t); + buffer = format_placeholders(walk, &placeholders[0], num); END_COLOR; OUTPUT_FULL_TEXT(buffer); + free(buffer); } diff --git a/src/print_run_watch.c b/src/print_run_watch.c index f0a0746..d153da4 100644 --- a/src/print_run_watch.c +++ b/src/print_run_watch.c @@ -6,6 +6,8 @@ #include <yajl/yajl_version.h> #include "i3status.h" +#define STRING_SIZE 5 + void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format, const char *format_down) { bool running = process_runs(pidfile); const char *walk; @@ -21,22 +23,15 @@ void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const c START_COLOR((running ? "color_good" : "color_bad")); - for (; *walk != '\0'; walk++) { - if (*walk != '%') { - *(outwalk++) = *walk; - - } else if (BEGINS_WITH(walk + 1, "title")) { - outwalk += sprintf(outwalk, "%s", title); - walk += strlen("title"); + char string_status[STRING_SIZE]; + snprintf(string_status, STRING_SIZE, "%s", (running ? "yes" : "no")); - } else if (BEGINS_WITH(walk + 1, "status")) { - outwalk += sprintf(outwalk, "%s", (running ? "yes" : "no")); - walk += strlen("status"); + placeholder_t placeholders[] = { + {.name = "%title", .value = title}, + {.name = "%status", .value = string_status}}; - } else { - *(outwalk++) = '%'; - } - } + const size_t num = sizeof(placeholders) / sizeof(placeholder_t); + buffer = format_placeholders(walk, &placeholders[0], num); END_COLOR; OUTPUT_FULL_TEXT(buffer); diff --git a/src/print_time.c b/src/print_time.c index 5133c51..67930b7 100644 --- a/src/print_time.c +++ b/src/print_time.c @@ -10,6 +10,8 @@ #include "i3status.h" +#define STRING_SIZE 50 + static bool local_timezone_init = false; static const char *local_timezone = NULL; static const char *current_timezone = NULL; @@ -36,10 +38,8 @@ void set_timezone(const char *tz) { } void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *format, const char *tz, const char *locale, const char *format_time, bool hide_if_equals_localtime, time_t t) { - const char *walk; char *outwalk = buffer; struct tm local_tm, tm; - char timebuf[1024]; if (title != NULL) INSTANCE(title); @@ -61,23 +61,18 @@ void print_time(yajl_gen json_gen, char *buffer, const char *title, const char * setlocale(LC_ALL, locale); } + char string_time[STRING_SIZE]; + if (format_time == NULL) { - strftime(timebuf, sizeof(timebuf), format, &tm); - maybe_escape_markup(timebuf, &outwalk); + strftime(string_time, sizeof(string_time), format, &tm); + maybe_escape_markup(string_time, &outwalk); } else { - for (walk = format; *walk != '\0'; walk++) { - if (*walk != '%') { - *(outwalk++) = *walk; + strftime(string_time, sizeof(string_time), format_time, &tm); + placeholder_t placeholders[] = { + {.name = "%time", .value = string_time}}; - } else if (BEGINS_WITH(walk + 1, "time")) { - strftime(timebuf, sizeof(timebuf), format_time, &tm); - maybe_escape_markup(timebuf, &outwalk); - walk += strlen("time"); - - } else { - *(outwalk++) = '%'; - } - } + const size_t num = sizeof(placeholders) / sizeof(placeholder_t); + buffer = format_placeholders(format_time, &placeholders[0], num); } if (locale != NULL) { |