From 4b6bf69783415aec852fe9ba5b7d8090c9219a23 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sat, 25 May 2019 12:15:04 -0400 Subject: Refactor scoreboard formatting --- play.c | 84 +++++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 44 insertions(+), 40 deletions(-) (limited to 'play.c') diff --git a/play.c b/play.c index 3083383..520b563 100644 --- a/play.c +++ b/play.c @@ -99,62 +99,73 @@ enum { ScoreWidth = 10, NameWidth = 31, DateWidth = 10, - ScoresWidth = RankWidth + 2 + ScoreWidth + 2 + NameWidth + 2 + DateWidth, - ScoresY = 0, - ScoresX = 2, - NameX = ScoresX + RankWidth + 2 + ScoreWidth + 2, - ScoresTop = 15, + BoardWidth = RankWidth + 2 + ScoreWidth + 2 + NameWidth + 2 + DateWidth, + BoardY = 0, + BoardX = 2, + NameX = BoardX + RankWidth + 2 + ScoreWidth + 2, + BoardLen = 15, }; -static const char ScoresTitle[] = "TOP SCORES"; -static const char *formatScore(size_t i) { +static char board[BoardWidth + 1]; + +static char *boardTitle(const char *title) { + snprintf( + board, sizeof(board), + "%*s", (int)(BoardWidth + strlen(title)) / 2, title + ); + return board; +} + +static char *boardLine(void) { + for (uint i = 0; i < BoardWidth; ++i) { + board[i] = '='; + } + board[BoardWidth] = '\0'; + return board; +} + +static char *boardScore(size_t i) { struct tm *time = localtime(&scores[i].date); if (!time) err(EX_SOFTWARE, "localtime"); - char date[DateWidth + 1]; strftime(date, sizeof(date), "%F", time); - - static char buf[ScoresWidth + 1]; snprintf( - buf, sizeof(buf), + board, sizeof(board), "%*zu. %*u %-*s %*s", RankWidth, 1 + i, ScoreWidth, scores[i].score, NameWidth, scores[i].name, DateWidth, date ); - return buf; + return board; } -static void draw(size_t new) { - mvaddstr( - ScoresY, ScoresX + (ScoresWidth - sizeof(ScoresTitle) + 2) / 2, - ScoresTitle - ); - mvhline(ScoresY + 1, ScoresX, '=', ScoresWidth); +static void draw(const char *title, size_t new) { + mvaddstr(BoardY + 0, BoardX, boardTitle(title)); + mvaddstr(BoardY + 1, BoardX, boardLine()); int newY = -1; - for (size_t i = 0; i < ScoresTop; ++i) { + for (size_t i = 0; i < BoardLen; ++i) { if (!scores[i].score) break; - if (i == new) newY = ScoresY + 2 + i; + if (i == new) newY = BoardY + 2 + i; attr_set(i == new ? A_BOLD : A_NORMAL, 0, NULL); - mvaddstr(ScoresY + 2 + i, ScoresX, formatScore(i)); + mvaddstr(BoardY + 2 + i, BoardX, boardScore(i)); } if (new == ScoresLen) return; - if (new >= ScoresTop) { - newY = ScoresY + ScoresTop + 5; - mvhline(newY - 3, ScoresX, '=', ScoresWidth); - mvaddstr(newY - 2, ScoresX, formatScore(new - 2)); - mvaddstr(newY - 1, ScoresX, formatScore(new - 1)); + if (new >= BoardLen) { + newY = BoardY + BoardLen + 5; + mvaddstr(newY - 3, BoardX, boardLine()); + mvaddstr(newY - 2, BoardX, boardScore(new - 2)); + mvaddstr(newY - 1, BoardX, boardScore(new - 1)); attr_set(A_BOLD, 0, NULL); - mvaddstr(newY, ScoresX, formatScore(new)); + mvaddstr(newY, BoardX, boardScore(new)); attr_set(A_NORMAL, 0, NULL); if (new + 1 < ScoresLen && scores[new + 1].score) { - mvaddstr(newY + 1, ScoresX, formatScore(new + 1)); + mvaddstr(newY + 1, BoardX, boardScore(new + 1)); } if (new + 2 < ScoresLen && scores[new + 2].score) { - mvaddstr(newY + 2, ScoresX, formatScore(new + 2)); + mvaddstr(newY + 2, BoardX, boardScore(new + 2)); } } move(newY, NameX); @@ -174,20 +185,13 @@ int main(int argc, char *argv[]) { if (path) { FILE *file = fopen(path, "r"); if (!file) err(EX_NOINPUT, "%s", path); - scoresRead(file); - printf( - "%*s\n", - ScoresWidth / 2 + (int)(sizeof(ScoresTitle) + 2) / 2, - ScoresTitle - ); - for (uint i = 0; i < ScoresWidth; ++i) putchar('='); - putchar('\n'); + printf("%s\n", boardTitle("TOP SCORES")); + printf("%s\n", boardLine()); for (size_t i = 0; i < ScoresLen; ++i) { if (!scores[i].score) break; - printf("%s\n", formatScore(i)); + printf("%s\n", boardScore(i)); } - return EX_OK; } @@ -214,7 +218,7 @@ int main(int argc, char *argv[]) { size_t index = scoresInsert(new); curse(); - draw(index); + draw("TOP SCORES", index); if (index < ScoresLen) { attr_set(A_BOLD, 0, NULL); while (!new.name[0]) { -- cgit 1.4.1 f='/src/commit/www/temp.causal.agency/up.c?id=dbcf4c53325774c83d29cdd96a3e822e71029221&follow=1'>Call sandbox in CGI modeJune McEnroe Otherwise upload won't actually work. 2021-09-22Support HTTP PUT in upJune McEnroe For use by Palaver[1]. Unfortunately, at least in the current App Store version of Palaver, this doesn't work correctly with basic auth. [1]: https://palaverapp.com/guides/commands/set.html#ui-image_service 2021-09-22Remove default faviconJune McEnroe I hate these things and also this one sucks. 2021-09-21Use Z_FILTERED strategyJune McEnroe 2021-09-21Recalculate various lengths only as neededJune McEnroe This actually speeds things up quite a bit, saving roughly a second on a big PNG screenshot. Almost all the remaining time is spent in deflate. 2021-09-21Rewrite pngo, add explicit optionsJune McEnroe Interesting to see how my code habits have changed. 2021-09-16Fix /* **/ comment matchingJune McEnroe 2021-09-15Remove typer, add downgrade to READMEJune McEnroe 2021-09-15Set bot mode on downgradeJune McEnroe 2021-09-15Enter capsicum in downgradeJune McEnroe 2021-09-15Factor out common parts of downgrade messagesJune McEnroe Also bump the message cap to 1024 because that is ostensibly useful for replying to older messages. 2021-09-14Add downgrade IRC botJune McEnroe 2021-09-14Sort by title if authors matchJune McEnroe There are probably better things to sort by but title definitely always exists. 2021-09-13Swap-remove tags as they're foundJune McEnroe This makes it even faster. From ~1s on a sqlite3.c amalgamation to ~0.85s. 2021-09-12Replace htagml regex with strncmpJune McEnroe Since ctags only ever produces regular expressions of the form /^re$/ or /^re/ with no other special characters, instead unescape the pattern and simply use strncmp. Running on a sqlite3.c amalgamation, the regex version takes ~37s while the strncmp version takes ~1s, producing identical output. Big win! 2021-09-11Also defer printing comment for lone close-parensJune McEnroe 2021-09-10Publish "git-comment"June McEnroe 2021-09-10Add git comment --pretty optionJune McEnroe 2021-09-08Defer printing comment if line is blank or closing braceJune McEnroe This fixes badly indented comments. 2021-09-08Up default min-repeat to 30 linesJune McEnroe 2021-09-08Handle dirty lines in git-commentJune McEnroe 2021-09-08Document and install git-commentJune McEnroe 2021-09-08Add repeat and all options to git-commentJune McEnroe 2021-09-08Add group threshold to git-commentJune McEnroe