fen.c: add an -s and -S option to show a move indicator on the board - chess-puzzles - chess puzzle book generator
 (HTM) git clone git://git.codemadness.org/chess-puzzles
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit fea5413be3348ec3c167a10fcc0820a50d0d070e
 (DIR) parent c7fb7a700cbaa565d988cfb7e49f25c85159648f
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Tue, 22 Jul 2025 15:19:49 +0200
       
       fen.c: add an -s and -S option to show a move indicator on the board
       
       ... also a fix: add -h and -H to usage, it was missing.
       
       Diffstat:
         M fen.1                               |       8 +++++++-
         M fen.c                               |      34 +++++++++++++++++++++++++++----
       
       2 files changed, 37 insertions(+), 5 deletions(-)
       ---
 (DIR) diff --git a/fen.1 b/fen.1
       @@ -1,4 +1,4 @@
       -.Dd February 29, 2024
       +.Dd July 22, 2025
        .Dt FEN 1
        .Os
        .Sh NAME
       @@ -10,6 +10,7 @@
        .Op Fl l
        .Op Fl m mapping
        .Op Fl o Ar ascii | fen | pgn | speak | svg | tty
       +.Op Fl sS
        .Op Fl t theme
        .Op Ar FEN
        .Op Ar moves
       @@ -66,6 +67,11 @@ Text representation of the board suitable for a terminal.
        The terminal requires UTF-8 support for chess symbols and it uses truecolor for
        the board theme.
        .El
       +.It Fl s
       +Show an indicator on the board which side it is to move (svg and tty), default
       +is on.
       +.It Fl S
       +Do not show an indicator on the board which side it is to move (svg and tty).
        .It Fl t Ar theme
        Use a colour theme for certain output formats, supported are the names: brown
        (default), green, grey.
 (DIR) diff --git a/fen.c b/fen.c
       @@ -105,6 +105,7 @@ struct board {
        
                int flipboard;           /* flip board ? default: 0 */
                int showcoords;          /* board coordinates? default: 1 */
       +        int showside;            /* show indicator for which side to move: default: 1 */
                int highlights;          /* highlight moves and checks? default: 1 */
                struct theme *theme;     /* board theme */
        };
       @@ -135,6 +136,7 @@ board_init(struct board *b)
                b->movenumber = 1;
                b->flipboard = 0;
                b->showcoords = 1;
       +        b->showside = 1;
                b->highlights = 1;
                b->theme = &themes[0]; /* use first theme as default */
        }
       @@ -495,6 +497,19 @@ output_svg(struct board *b)
                        }
                }
        
       +        if (b->showside) {
       +                /* circle indicator for which side to move */
       +                fputs("<circle cx=\"354\" stroke-width=\"1\" r=\"5\" fill=\"", stdout);
       +                if (b->side_to_move == 'w') {
       +                        fputs("white\" stroke=\"black\" cy=\"", stdout);
       +                        printf("%d", b->flipboard ? 6 : 354);
       +                } else {
       +                        fputs("black\" stroke=\"white\" cy=\"", stdout);
       +                        printf("%d", b->flipboard ? 354 : 6);
       +                }
       +                fputs("\"></circle>", stdout);
       +        }
       +
                fputs("</svg>\n", stdout);
        }
        
       @@ -590,10 +605,19 @@ output_tty(struct board *b)
                                putchar(xtofile(y));
                                fputs("  ", stdout);
                        }
       -                fputs(" ", stdout);
                } else {
       -                fputs("                            ", stdout);
       +                fputs("                           ", stdout);
                }
       +        if (b->showside) {
       +                if (b->side_to_move == 'w') {
       +                        printf("\x1b[30;47m%c", b->side_to_move);
       +                } else {
       +                        printf("\x1b[37;40m%c", b->side_to_move);
       +                }
       +        } else {
       +                putchar(' ');
       +        }
       +
                printf("\x1b[0m"); /* reset */
                printf("\n");
        }
       @@ -1406,8 +1430,8 @@ board_playmoves(struct board *b, const char *moves)
        void
        usage(char *argv0)
        {
       -        fprintf(stderr, "usage: %s [-cCfF] [-l] [-m mapping] "
       -                "[-o ascii|fen|pgn|speak|svg|tty] [-t default|green|grey] "
       +        fprintf(stderr, "usage: %s [-cCfFhH] [-l] [-m mapping] "
       +                "[-o ascii|fen|pgn|speak|svg|tty] [-sS] [-t default|green|grey] "
                        "[FEN] [moves]\n", argv0);
                exit(1);
        }
       @@ -1626,6 +1650,8 @@ main(int argc, char *argv[])
                                        if (outputmode == ModeInvalid)
                                                usage(argv[0]);
                                        goto next;
       +                        case 's': board.showside = 1; break;
       +                        case 'S': board.showside = 0; break;
                                case 't': /* theme name */
                                        if (i + 1 >= argc)
                                                usage(argv[0]);