Skip to content

Commit

Permalink
Add user-defined output formatting (#33)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
zeroeightysix authored and emersion committed Feb 23, 2019
1 parent a780cfa commit f68c9bf
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
40 changes: 36 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] == '#') {
Expand All @@ -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 = {
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
17 changes: 17 additions & 0 deletions slurp.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>, who is assisted by other
Expand Down

0 comments on commit f68c9bf

Please sign in to comment.