feat: coloured search output for a terminal
Started from a terminal, Rigby now shows its search for a human: coloured progress lines (depth/seldepth, playouts, speed, time, a tree fill bar, score in pawns, the PV fading with depth) at 125, 250, 500 ms and then every second, and at the end a table of the most visited root moves with a bar for their share of the visits, their score and prior. Tree reuse and collections are shown as notes, and bestmove is highlighted. The first "uci" command switches to plain UCI output for good. GUIs and match runners send it first and read stdout through a pipe, so they never see the coloured output. NO_COLOR turns it off. On Windows the console gets ANSI processing and UTF-8 output. Checked: the UCI output of a scripted session (two searches with tree reuse, a stalemate root) is identical to main apart from nps and time; go wtime, go infinite with stop, and quit work; the pretty mode was driven in a real console, including the switch to UCI. Search is untouched, so the bench is unchanged. Bench: 546018 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1754753a6e
commit
6490286852
6 files changed
+319
-17
No files matched your search
+200
@@ -0,0 +1,200 @@
|
||||
#include "pretty.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include <io.h>
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace rigby::pretty {
|
||||
|
||||
namespace {
|
||||
|
||||
// ANSI SGR sequences.
|
||||
constexpr const char* RESET = "\x1b[0m";
|
||||
constexpr const char* BOLD = "\x1b[1m";
|
||||
constexpr const char* DIM = "\x1b[90m";
|
||||
constexpr const char* WHITE_ = "\x1b[97m";
|
||||
constexpr const char* CYAN = "\x1b[36m";
|
||||
constexpr const char* BLUE = "\x1b[94m";
|
||||
constexpr const char* MAGENTA = "\x1b[95m";
|
||||
constexpr const char* GREEN = "\x1b[32m";
|
||||
constexpr const char* BRIGHT_GREEN = "\x1b[92m";
|
||||
constexpr const char* YELLOW = "\x1b[93m";
|
||||
constexpr const char* RED = "\x1b[31m";
|
||||
constexpr const char* BRIGHT_RED = "\x1b[91m";
|
||||
|
||||
std::string paint(const char* colour, const std::string& text) { return colour + text + RESET; }
|
||||
|
||||
// Right- or left-aligns `text` (plain ASCII) to `width` columns.
|
||||
std::string pad_left(const std::string& text, size_t width) {
|
||||
return text.size() >= width ? text : std::string(width - text.size(), ' ') + text;
|
||||
}
|
||||
std::string pad_right(const std::string& text, size_t width) {
|
||||
return text.size() >= width ? text : text + std::string(width - text.size(), ' ');
|
||||
}
|
||||
|
||||
std::string format(const char* fmt, double value) {
|
||||
char buf[32];
|
||||
std::snprintf(buf, sizeof(buf), fmt, value);
|
||||
return buf;
|
||||
}
|
||||
|
||||
// 999, 12.3k, 123k, 1.23M, 12.3M, 1.23G
|
||||
std::string count(u64 n) {
|
||||
if (n < 1000) return std::to_string(n);
|
||||
const char* units = "kMGT";
|
||||
double v = double(n);
|
||||
int unit = -1;
|
||||
while (v >= 1000.0 && unit < 3) {
|
||||
v /= 1000.0;
|
||||
unit++;
|
||||
}
|
||||
const std::string digits = v < 10.0 ? format("%.2f", v) : v < 100.0 ? format("%.1f", v) : format("%.0f", v);
|
||||
return digits + units[unit];
|
||||
}
|
||||
|
||||
// 0.38s, 12.4s, 2m05s
|
||||
std::string duration(i64 ms) {
|
||||
if (ms < 10000) return format("%.2fs", double(ms) / 1000.0);
|
||||
if (ms < 60000) return format("%.1fs", double(ms) / 1000.0);
|
||||
const i64 s = ms / 1000;
|
||||
char buf[32];
|
||||
std::snprintf(buf, sizeof(buf), "%lldm%02llds", static_cast<long long>(s / 60), static_cast<long long>(s % 60));
|
||||
return buf;
|
||||
}
|
||||
|
||||
// Pawns with a sign: +0.42, -1.30
|
||||
std::string score_text(int cp) { return format("%+.2f", double(cp) / 100.0); }
|
||||
|
||||
const char* score_colour(int cp) {
|
||||
if (cp >= 150) return BRIGHT_GREEN;
|
||||
if (cp >= 25) return GREEN;
|
||||
if (cp > -25) return WHITE_;
|
||||
if (cp > -150) return RED;
|
||||
return BRIGHT_RED;
|
||||
}
|
||||
|
||||
// A bar `width` cells wide; eighth blocks make it smooth.
|
||||
std::string bar(double fraction, int width) {
|
||||
static constexpr const char* EIGHTHS[] = {"", "▏", "▎", "▍", "▌", "▋", "▊", "▉"};
|
||||
const int eighths = int(std::lround(std::clamp(fraction, 0.0, 1.0) * width * 8));
|
||||
std::string out;
|
||||
for (int i = 0; i < eighths / 8; i++) out += "█";
|
||||
int cells = eighths / 8;
|
||||
if (eighths % 8) {
|
||||
out += EIGHTHS[eighths % 8];
|
||||
cells++;
|
||||
}
|
||||
return out + std::string(size_t(width - cells), ' ');
|
||||
}
|
||||
|
||||
// The tree's fill level as a bar of `width` cells, filled ones coloured by how full it is.
|
||||
std::string tree_bar(int permille, int width) {
|
||||
const int filled = std::clamp((permille * width + 500) / 1000, 0, width);
|
||||
const char* colour = permille < 500 ? GREEN : permille < 900 ? YELLOW : RED;
|
||||
std::string full, empty;
|
||||
for (int i = 0; i < filled; i++) full += "■";
|
||||
for (int i = filled; i < width; i++) empty += "■";
|
||||
return paint(colour, full) + paint(DIM, empty);
|
||||
}
|
||||
|
||||
constexpr int TREE_WIDTH = 8;
|
||||
|
||||
} // namespace
|
||||
|
||||
bool init_terminal() {
|
||||
if (std::getenv("NO_COLOR")) return false;
|
||||
#ifdef _WIN32
|
||||
if (!_isatty(_fileno(stdout))) return false;
|
||||
const HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
DWORD mode = 0;
|
||||
if (!GetConsoleMode(out, &mode) || !SetConsoleMode(out, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) return false;
|
||||
SetConsoleOutputCP(CP_UTF8);
|
||||
return true;
|
||||
#else
|
||||
return isatty(STDOUT_FILENO);
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string banner(const std::string& name, const std::string& version, const std::string& author) {
|
||||
return std::string("\n ") + BOLD + MAGENTA + name + RESET + " " + WHITE_ + version + RESET + DIM + " by " +
|
||||
author + RESET + "\n " + DIM + "MCTS chess engine · type " + RESET + CYAN + "uci" + RESET + DIM +
|
||||
" for UCI mode, or e.g. " + RESET + CYAN + "go movetime 5000" + RESET + "\n";
|
||||
}
|
||||
|
||||
std::string header() {
|
||||
const std::string cols = " " + pad_left("depth", 7) + " " + pad_left("playouts", 8) + " " + pad_left("speed", 9) +
|
||||
" " + pad_left("time", 6) + " " + pad_right("tree", TREE_WIDTH + 5) + " " +
|
||||
pad_left("score", 6) + " pv";
|
||||
return std::string("\n") + DIM + cols + RESET;
|
||||
}
|
||||
|
||||
std::string progress(const Progress& p) {
|
||||
std::string line = " ";
|
||||
line += paint(WHITE_, pad_left(std::to_string(p.depth), 3)) + paint(DIM, pad_right("/" + std::to_string(p.seldepth), 4));
|
||||
line += " " + paint(CYAN, pad_left(count(p.playouts), 8));
|
||||
line += " " + paint(BLUE, pad_left(count(p.nps), 7)) + paint(DIM, "/s");
|
||||
line += " " + paint(WHITE_, pad_left(duration(p.ms), 6));
|
||||
line += " " + tree_bar(p.hashfull, TREE_WIDTH) + paint(DIM, pad_left(std::to_string((p.hashfull + 5) / 10) + "%", 5));
|
||||
line += " " + paint(score_colour(p.cp), std::string(BOLD) + pad_left(score_text(p.cp), 6));
|
||||
line += " ";
|
||||
for (size_t i = 0; i < p.pv.size(); i++) {
|
||||
// The move to play stands out; the rest of the line fades with depth.
|
||||
line += " " + (i == 0 ? paint(WHITE_, std::string(BOLD) + p.pv[i]) : paint(i < 4 ? WHITE_ : DIM, p.pv[i]));
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
std::string root_table(std::vector<RootMove> moves, size_t max_rows) {
|
||||
u64 total = 0;
|
||||
for (const RootMove& m : moves) total += m.visits;
|
||||
std::stable_sort(moves.begin(), moves.end(),
|
||||
[](const RootMove& a, const RootMove& b) { return a.visits > b.visits; });
|
||||
|
||||
constexpr int SHARE_WIDTH = 24;
|
||||
std::string out = std::string("\n") + DIM + " " + pad_right("move", 7) + pad_left("visits", 8) + " " +
|
||||
pad_right("share", SHARE_WIDTH + 8) + pad_left("score", 6) + " " + pad_left("prior", 6) + RESET;
|
||||
|
||||
const size_t rows = std::min(moves.size(), max_rows);
|
||||
for (size_t i = 0; i < rows; i++) {
|
||||
const RootMove& m = moves[i];
|
||||
const double share = total ? double(m.visits) / double(total) : 0.0;
|
||||
const bool best = i == 0;
|
||||
out += "\n ";
|
||||
out += best ? paint(WHITE_, std::string(BOLD) + pad_right(m.move, 7)) : paint(WHITE_, pad_right(m.move, 7));
|
||||
out += paint(CYAN, pad_left(count(m.visits), 8)) + " ";
|
||||
out += paint(best ? MAGENTA : BLUE, bar(share, SHARE_WIDTH));
|
||||
out += paint(DIM, pad_left(format("%.1f%%", share * 100.0), 7)) + " ";
|
||||
out += m.visits ? paint(score_colour(m.cp), pad_left(score_text(m.cp), 6)) : paint(DIM, pad_left("-", 6));
|
||||
out += " " + paint(DIM, pad_left(format("%.1f%%", double(m.prior) * 100.0), 6));
|
||||
}
|
||||
if (moves.size() > rows)
|
||||
out += "\n " + paint(DIM, "... " + std::to_string(moves.size() - rows) + " more moves");
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string note(const std::string& text) { return std::string(" ") + DIM + text + RESET; }
|
||||
|
||||
std::string reused(u64 nodes) {
|
||||
return std::string("\n ") + DIM + "reused " + RESET + CYAN + count(nodes) + RESET + DIM +
|
||||
" tree nodes from the last search" + RESET;
|
||||
}
|
||||
|
||||
std::string bestmove(const std::string& move) {
|
||||
return std::string("\n ") + DIM + "bestmove " + RESET + BOLD + BRIGHT_GREEN + move + RESET + "\n";
|
||||
}
|
||||
|
||||
} // namespace rigby::pretty
|
||||
Reference in new issue
Block a user