From f68c9bf48b62b363e13c2a525f684e7cdd3fb858 Mon Sep 17 00:00:00 2001 From: Ridan Vandenbergh Date: Sat, 23 Feb 2019 19:37:19 +0100 Subject: [PATCH] Add user-defined output formatting (#33) * Add user-defined formatting * Add default format to man page * Use snake case for buffer_pos * Style * Remove size limitation on format * Print formatted string directly * Optimisations --- main.c | 40 ++++++++++++++++++++++++++++++++++++---- slurp.1.scd | 17 +++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/main.c b/main.c index efad285..89709fb 100644 --- a/main.c +++ b/main.c @@ -411,7 +411,8 @@ static const char usage[] = " -b #rrggbbaa Set background color.\n" " -c #rrggbbaa Set border color.\n" " -s #rrggbbaa Set selection color.\n" - " -w n Set border weight.\n"; + " -w n Set border weight.\n" + " -f s Set output format.\n"; uint32_t parse_color(const char *color) { if (color[0] == '#') { @@ -431,6 +432,34 @@ uint32_t parse_color(const char *color) { return res; } +static void print_formatted_result(const struct slurp_box *result, const char *format) { + for (size_t i = 0; format[i] != 0; i++) { + char c = format[i]; + if (c == '%') { + char next = format[i + 1]; + + i++; // Skip the next character (x, y, w or h) + switch (next) { + case 'x': + printf("%u", result->x); + continue; + case 'y': + printf("%u", result->y); + continue; + case 'w': + printf("%u", result->width); + continue; + case 'h': + printf("%u", result->height); + continue; + } + i--; // If no case was executed, revert i back - we don't need to skip the next character. + } + printf("%c", c); + } + printf("\n"); +} + int main(int argc, char *argv[]) { struct slurp_state state = { .colors = { @@ -443,7 +472,8 @@ int main(int argc, char *argv[]) { }; int opt; - while ((opt = getopt(argc, argv, "hdb:c:s:w:")) != -1) { + char *format = "%x,%y %wx%h"; + while ((opt = getopt(argc, argv, "hdb:c:s:w:f:")) != -1) { switch (opt) { case 'h': printf("%s", usage); @@ -460,6 +490,9 @@ int main(int argc, char *argv[]) { case 's': state.colors.selection = parse_color(optarg); break; + case 'f': + format = optarg; + break; case 'w': { errno = 0; char *endptr; @@ -597,7 +630,6 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - printf("%d,%d %dx%d\n", state.result.x, state.result.y, - state.result.width, state.result.height); + print_formatted_result(&state.result, format); return EXIT_SUCCESS; } diff --git a/slurp.1.scd b/slurp.1.scd index 7df00c4..5473d56 100644 --- a/slurp.1.scd +++ b/slurp.1.scd @@ -34,10 +34,27 @@ select, or click to cancel the selection. *-w* _weight_ Set border weight. +*-f* _format_ + Set format. See *FORMAT* for more detail. + # COLORS Colors may be specified in #RRGGBB or #RRGGBBAA format. The # is optional. +# FORMAT + +Interpreted sequences are: + +%x The x-coordinate of the selection + +%y The y-coordinate of the selection + +%w The width of the selection + +%h The height of the selection + +The default format is "%x,%y %wx%h". + # AUTHORS Maintained by Simon Ser , who is assisted by other