From 471825b0ac0c465e1942db08fd125f422a35f890 Mon Sep 17 00:00:00 2001 From: CismonX Date: Sat, 30 Oct 2021 23:14:10 +0800 Subject: [PATCH] Refactor sixdraw code. --- examples/sixdraw.c | 77 ++++++++++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 27 deletions(-) diff --git a/examples/sixdraw.c b/examples/sixdraw.c index dd4fbe3..4edf333 100644 --- a/examples/sixdraw.c +++ b/examples/sixdraw.c @@ -2,8 +2,8 @@ * sixdraw.c - draw on your terminal * * Requires sixel graphics and 1016 mouse mode to run on your terminal. - * These features are not widely supported. To save yourself from trouble, - * use a latest version of XTerm or mintty. + * These features are not widely supported. + * To save yourself from trouble, use a latest version of XTerm or mintty. * * Before 1016 mode was introduced in XTerm patch #359, it is also possible * to report mouse position in pixels using DEC locator (which is also rarely @@ -89,8 +89,11 @@ struct sixdraw_ctx { }; static inline void -print_error(struct sixdraw_ctx const *ctx, char const *format, ...) -{ +print_error( + struct sixdraw_ctx const *ctx, + char const *format, + ... +) { char msg[1024]; va_list args; va_start(args, format); @@ -100,8 +103,9 @@ print_error(struct sixdraw_ctx const *ctx, char const *format, ...) } static void -terminate(struct sixdraw_ctx *ctx) -{ +terminate( + struct sixdraw_ctx *ctx +) { ctlseqs_matcher_free(ctx->matcher); ctlseqs_reader_free(ctx->reader); @@ -139,8 +143,9 @@ terminate(struct sixdraw_ctx *ctx) } static bool -get_winsize(struct sixdraw_ctx *ctx) -{ +get_winsize( + struct sixdraw_ctx *ctx +) { struct winsize ws = { 0 }; if (ioctl(ctx->in_fd, TIOCGWINSZ, &ws) != 0) { print_error(ctx, "failed to get terminal window size"); @@ -150,17 +155,20 @@ get_winsize(struct sixdraw_ctx *ctx) print_error(ctx, "failed to get terminal window size (in pixels)"); return false; } - ctx->rows = ws.ws_row; - ctx->ch_width = ws.ws_xpixel / ws.ws_col; - ctx->ch_height = ws.ws_ypixel / ws.ws_row; - ctx->canvas_width = ctx->ch_width * ws.ws_col; + ctx->rows = ws.ws_row; + ctx->ch_width = ws.ws_xpixel / ws.ws_col; + ctx->ch_height = ws.ws_ypixel / ws.ws_row; + ctx->canvas_width = ctx->ch_width * ws.ws_col; ctx->canvas_height = ctx->ch_height * ws.ws_row - ctx->ch_height * 2; return true; } static bool -decrqm(struct sixdraw_ctx *ctx, unsigned mode, char const *name) -{ +decrqm( + struct sixdraw_ctx *ctx, + unsigned mode, + char const *name +) { ssize_t retval; union ctlseqs_value *result = ctx->result; fprintf(ctx->out_file, CTLSEQS_DECRQM("%d"), mode); @@ -181,7 +189,9 @@ decrqm(struct sixdraw_ctx *ctx, unsigned mode, char const *name) } static long -xtversion(struct sixdraw_ctx *ctx) { +xtversion( + struct sixdraw_ctx *ctx +) { ssize_t retval; union ctlseqs_value *result = ctx->result; fprintf(ctx->out_file, CTLSEQS_XTVERSION()); @@ -209,8 +219,11 @@ xtversion(struct sixdraw_ctx *ctx) { } static void -print_sixel_dot(struct sixdraw_ctx *ctx, unsigned x, unsigned y) -{ +print_sixel_dot( + struct sixdraw_ctx *ctx, + unsigned x, + unsigned y +) { if (x >= ctx->canvas_width || y >= ctx->canvas_height) { return; } @@ -224,15 +237,21 @@ print_sixel_dot(struct sixdraw_ctx *ctx, unsigned x, unsigned y) row = y % ctx->ch_height; col = x % ctx->ch_width; unsigned seq_size = ctx->sixel_init_size; - seq_size += sprintf(sixel_seq + seq_size, "%.*s!%u?%c" CTLSEQS_ST, - row / 6, "------------------------", col, (1 << row % 6) + 0x3F); + seq_size += sprintf( + sixel_seq + seq_size, + "%.*s!%u?%c" CTLSEQS_ST, + row / 6, "------------------------", col, (1 << row % 6) + 0x3F + ); fwrite(sixel_seq, seq_size, 1, ctx->out_file); } static bool -init(struct sixdraw_ctx *ctx, int argc, char **argv) -{ +init( + struct sixdraw_ctx *ctx, + int argc, + char *argv[] +) { *ctx = (struct sixdraw_ctx) { .prog_name = argc > 0 ? argv[0] : "sixdraw", .out_file = stdout, @@ -303,8 +322,9 @@ init(struct sixdraw_ctx *ctx, int argc, char **argv) } static bool -prepare(struct sixdraw_ctx *ctx) -{ +prepare( + struct sixdraw_ctx *ctx +) { // Check whether we're running on a terminal. if (!isatty(ctx->in_fd) || !isatty(ctx->out_fd)) { print_error(ctx, "this program can only run in a terminal"); @@ -443,8 +463,9 @@ prepare(struct sixdraw_ctx *ctx) } static bool -draw(struct sixdraw_ctx *ctx) -{ +draw( + struct sixdraw_ctx *ctx +) { fprintf( ctx->out_file, CTLSEQS_CUP("%d", "1") "Canvas size: %ux%u. Line color: #%06X.", @@ -478,8 +499,10 @@ draw(struct sixdraw_ctx *ctx) } int -main(int argc, char **argv) -{ +main( + int argc, + char *argv[] +) { int status; struct sixdraw_ctx ctx; if (init(&ctx, argc, argv) && prepare(&ctx)) {