Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user-defined output formatting #33

Merged
merged 7 commits into from
Feb 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 48 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,10 @@ 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";

static char format[128] = "%x,%y %wx%h";

uint32_t parse_color(const char *color) {
if (color[0] == '#') {
Expand All @@ -431,6 +434,43 @@ uint32_t parse_color(const char *color) {
return res;
}

static void format_result(const char *format, char *buffer, const int buflen, struct slurp_box result) {
int bufferPos = 0;
for (int i = 0; format[i] != 0; i++) {
char c = format[i];
if (c == '%') {
char next = format[i + 1];

char replacement[128] = {0};
switch (next) {
case 'x':
sprintf(replacement, "%u", result.x);
break;
case 'y':
sprintf(replacement, "%u", result.y);
break;
case 'w':
sprintf(replacement, "%u", result.width);
break;
case 'h':
sprintf(replacement, "%u", result.height);
break;
}

if (replacement != NULL) {
strncpy(buffer + bufferPos, replacement, buflen - bufferPos - 1);
bufferPos = bufferPos + strlen(replacement);
i++;
continue;
}
}

buffer[bufferPos] = c;
bufferPos++;
}
buffer[buflen - 1] = '\0';
}

int main(int argc, char *argv[]) {
struct slurp_state state = {
.colors = {
Expand All @@ -443,7 +483,7 @@ int main(int argc, char *argv[]) {
};

int opt;
while ((opt = getopt(argc, argv, "hdb:c:s:w:")) != -1) {
while ((opt = getopt(argc, argv, "hdb:c:s:w:f:")) != -1) {
switch (opt) {
case 'h':
printf("%s", usage);
Expand All @@ -460,6 +500,9 @@ int main(int argc, char *argv[]) {
case 's':
state.colors.selection = parse_color(optarg);
break;
case 'f':
strncpy(format, optarg, 128);
break;
case 'w': {
errno = 0;
char *endptr;
Expand Down Expand Up @@ -597,7 +640,8 @@ 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);
char output_result[256] = {0};
format_result(format, output_result, 256, state.result);
printf("%s\n", output_result);
return EXIT_SUCCESS;
}
15 changes: 15 additions & 0 deletions slurp.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,25 @@ 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

# AUTHORS

Maintained by Simon Ser <[email protected]>, who is assisted by other
Expand Down