From 90827b77aee01621ec83e5fb6ed38ef5597ed33d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Szafraniec?= Date: Wed, 24 Dec 2025 15:51:17 +0100 Subject: [PATCH 1/2] Implement rulers --- include/slurp.h | 3 +++ lock.c | 1 - main.c | 25 ++++++++++++++++++++++++- render.c | 30 ++++++++++++++++++++++++++++++ slurp.1.scd | 9 +++++++++ 5 files changed, 66 insertions(+), 2 deletions(-) diff --git a/include/slurp.h b/include/slurp.h index 711f7ed..b921a2f 100644 --- a/include/slurp.h +++ b/include/slurp.h @@ -42,12 +42,15 @@ struct slurp_state { uint32_t border; uint32_t selection; uint32_t choice; + uint32_t ruler; } colors; const char *font_family; uint32_t border_weight; + uint32_t ruler_weight; bool display_dimensions; + bool display_rulers; bool single_point; bool restrict_selection; bool crosshairs; diff --git a/lock.c b/lock.c index ff4cda3..4fb06bb 100644 --- a/lock.c +++ b/lock.c @@ -53,4 +53,3 @@ bool acquire_lock() { return true; } - diff --git a/main.c b/main.c index 84a2e63..87a99c4 100644 --- a/main.c +++ b/main.c @@ -720,12 +720,15 @@ static const char usage[] = "\n" " -h Show help message and quit.\n" " -d Display dimensions of selection.\n" + " -D Display rulers.\n" " -b #rrggbbaa Set background color.\n" " -c #rrggbbaa Set border color.\n" " -s #rrggbbaa Set selection color.\n" " -B #rrggbbaa Set option box color.\n" + " -R #rrggbbaa Set ruler color.\n" " -F s Set the font family for the dimensions.\n" " -w n Set border weight.\n" + " -W n Set ruler weight.\n" " -f s Set output format.\n" " -o Select a display output.\n" " -p Select a single point.\n" @@ -893,9 +896,12 @@ int main(int argc, char *argv[]) { .border = BORDER_COLOR, .selection = SELECTION_COLOR, .choice = BG_COLOR, + .ruler = BG_COLOR, }, + .ruler_weight = 2, .border_weight = 2, .display_dimensions = false, + .display_rulers = false, .restrict_selection = false, .resizing_selection = false, .fixed_aspect_ratio = false, @@ -907,7 +913,7 @@ int main(int argc, char *argv[]) { char *format = "%x,%y %wx%h\n"; bool output_boxes = false; int w, h; - while ((opt = getopt(argc, argv, "hdb:c:s:B:w:proa:f:F:x")) != -1) { + while ((opt = getopt(argc, argv, "hdDb:R:c:s:B:w:W:proa:f:F:x")) != -1) { switch (opt) { case 'h': printf("%s", usage); @@ -915,6 +921,12 @@ int main(int argc, char *argv[]) { case 'd': state.display_dimensions = true; break; + case 'D': + state.display_rulers = true; + break; + case 'R': + state.colors.ruler = parse_color(optarg); + break; case 'b': state.colors.background = parse_color(optarg); break; @@ -943,6 +955,16 @@ int main(int argc, char *argv[]) { } break; } + case 'W': { + errno = 0; + char *endptr; + state.ruler_weight = strtol(optarg, &endptr, 10); + if (*endptr || errno) { + fprintf(stderr, "Error: expected numeric argument for -w\n"); + exit(EXIT_FAILURE); + } + break; + } case 'p': state.single_point = true; break; @@ -1145,3 +1167,4 @@ int main(int argc, char *argv[]) { return status; } + diff --git a/render.c b/render.c index 5ab5d53..9b3fb3c 100644 --- a/render.c +++ b/render.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -19,6 +20,13 @@ static void draw_rect(cairo_t *cairo, struct slurp_box *box, uint32_t color) { box->width, box->height); } +// Treat width and height as X2 and Y2; +static void draw_line(cairo_t *cairo, struct slurp_box *box, uint32_t color) { + set_source_u32(cairo, color); + cairo_move_to(cairo, box->x, box->y); + cairo_line_to(cairo, box->width, box->height); +} + void render(struct slurp_output *output) { struct slurp_state *state = output->state; struct pool_buffer *buffer = output->current_buffer; @@ -74,6 +82,28 @@ void render(struct slurp_output *output) { draw_rect(cairo, sel_box, state->colors.border); cairo_stroke(cairo); + if (state->display_rulers) { + bool revert_x = sel_box->x < current_selection->anchor_x; + bool revert_y = sel_box->y < current_selection->anchor_y; + + struct slurp_box vertical_line; + vertical_line.x = sel_box->x + (revert_x ? -1 : sel_box->width + 1); + vertical_line.y = revert_y ? current_selection->current_output->height : 0; + vertical_line.width = sel_box->x + (revert_x ? -1 : sel_box->width + 1); + vertical_line.height = sel_box->y + (revert_y ? -1 : sel_box->height + 1); + + struct slurp_box horizontal_line; + horizontal_line.x = revert_x ? current_selection->current_output->width : 0; + horizontal_line.y = sel_box->y + (revert_y ? -1 : sel_box->height + 1); + horizontal_line.width = sel_box->x + (revert_x ? -1 : sel_box->width + 1); + horizontal_line.height = sel_box->y + (revert_y ? -1 : sel_box->height + 1); + + cairo_set_line_width(cairo, state->ruler_weight); + draw_line(cairo, &vertical_line, state->colors.ruler); + draw_line(cairo, &horizontal_line, state->colors.ruler); + cairo_stroke(cairo); + } + if (state->display_dimensions) { cairo_select_font_face(cairo, state->font_family, CAIRO_FONT_SLANT_NORMAL, diff --git a/slurp.1.scd b/slurp.1.scd index e79aaf6..c5a43e8 100644 --- a/slurp.1.scd +++ b/slurp.1.scd @@ -31,6 +31,9 @@ held, the selection is moved instead of being resized. *-d* Display dimensions of selection. +*-D* + Display rulers. + *-b* _color_ Set background color. Default is #FFFFFF40. See *COLORS* for more detail. @@ -44,6 +47,9 @@ held, the selection is moved instead of being resized. Set color for highlighting predefined rectangles from standard input when not selected. Default is #FFFFFF40. +*-R* _color_ + Set ruler color. Default is #00000000. See *COLORS* for more detail. + *-F* _font family_ Set the font family name when displaying the dimensions box. Only useful when combined with the -d option. The available font family names guaranteed @@ -53,6 +59,9 @@ held, the selection is moved instead of being resized. *-w* _weight_ Set border weight. Default is 2. +*-W* _weight_ + Set ruler weight. Default is 2. + *-f* _format_ Set format. See *FORMAT* for more detail. From 4742544429e0310e4ddc8dee7c67f8a7a22ed961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Szafraniec?= Date: Thu, 25 Dec 2025 03:01:58 +0100 Subject: [PATCH 2/2] Fix screen positioning issues --- render.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/render.c b/render.c index 9b3fb3c..74168a1 100644 --- a/render.c +++ b/render.c @@ -86,17 +86,20 @@ void render(struct slurp_output *output) { bool revert_x = sel_box->x < current_selection->anchor_x; bool revert_y = sel_box->y < current_selection->anchor_y; + uint32_t screen_x = current_selection->current_output->logical_geometry.x; + uint32_t screen_y = current_selection->current_output->logical_geometry.y; + struct slurp_box vertical_line; vertical_line.x = sel_box->x + (revert_x ? -1 : sel_box->width + 1); - vertical_line.y = revert_y ? current_selection->current_output->height : 0; + vertical_line.y = screen_y + (revert_y ? current_selection->current_output->height : 0); vertical_line.width = sel_box->x + (revert_x ? -1 : sel_box->width + 1); vertical_line.height = sel_box->y + (revert_y ? -1 : sel_box->height + 1); struct slurp_box horizontal_line; - horizontal_line.x = revert_x ? current_selection->current_output->width : 0; + horizontal_line.x = screen_x + (revert_x ? current_selection->current_output->width : 0); horizontal_line.y = sel_box->y + (revert_y ? -1 : sel_box->height + 1); - horizontal_line.width = sel_box->x + (revert_x ? -1 : sel_box->width + 1); - horizontal_line.height = sel_box->y + (revert_y ? -1 : sel_box->height + 1); + horizontal_line.width = vertical_line.width; + horizontal_line.height = vertical_line.height; cairo_set_line_width(cairo, state->ruler_weight); draw_line(cairo, &vertical_line, state->colors.ruler);