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 6 commits
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
44 changes: 40 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 = NULL;
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 All @@ -476,6 +509,10 @@ int main(int argc, char *argv[]) {
}
}

if (format == NULL) {
format = "%x,%y %wx%h";
}

wl_list_init(&state.outputs);
wl_list_init(&state.seats);

Expand Down Expand Up @@ -597,7 +634,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