feat: live framed search dashboard for a terminal
The terminal output is now a framed dashboard that is redrawn in place ten times a second instead of a growing list of lines: - the root position as a coloured board, with the best move's squares highlighted; - the score, white's expected score as an evaluation bar, depth, playouts, speed, time with a progress bar (time or playout limit), tree fill and notes on tree reuse and collections; - the principal variation in SAN with move numbers, wrapped and fading with depth; - the most visited root moves (SAN) with a bar for their share of the visits, score and prior; - a spinner while searching, "done" at the end, and bestmove in SAN. Each frame moves the cursor back to the top of the previous one and clears below it; lines the user typed under the dashboard (e.g. stop) are counted and drawn over. The box adapts to the terminal's width and height (fewer root moves, then no blank lines, the board only when it fits). RIGBY_PRETTY=1 forces the dashboard into a pipe. Checked: UCI output is unchanged (node-limited searches identical to main, including tree reuse and a stalemate root; time-limited ones differ only in node counts); go wtime, go infinite + stop and quit work; the dashboard was driven in a real console, including the redraw, a reused tree and the switch to UCI, and its colours were checked rendered to HTML. 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
31a35fd84f
commit
c859163e2c
6 files changed
+606
-181
No files matched your search
+482
-109
@@ -5,6 +5,8 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "eval.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
@@ -15,6 +17,7 @@
|
||||
#include <io.h>
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
@@ -22,28 +25,58 @@ 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";
|
||||
// ---------------------------------------------------------------------------
|
||||
// Colours (24-bit ANSI)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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 fg(int r, int g, int b) {
|
||||
return "\x1b[38;2;" + std::to_string(r) + ";" + std::to_string(g) + ";" + std::to_string(b) + "m";
|
||||
}
|
||||
std::string pad_right(const std::string& text, size_t width) {
|
||||
return text.size() >= width ? text : text + std::string(width - text.size(), ' ');
|
||||
std::string bg(int r, int g, int b) {
|
||||
return "\x1b[48;2;" + std::to_string(r) + ";" + std::to_string(g) + ";" + std::to_string(b) + "m";
|
||||
}
|
||||
|
||||
const std::string RESET = "\x1b[0m";
|
||||
const std::string BOLD = "\x1b[1m";
|
||||
|
||||
const std::string BORDER = fg(98, 104, 150);
|
||||
const std::string TITLE = BOLD + fg(205, 145, 255);
|
||||
const std::string LABEL = fg(125, 128, 150);
|
||||
const std::string FAINT = fg(85, 88, 105);
|
||||
const std::string VALUE = fg(235, 236, 245);
|
||||
const std::string CYAN = fg(110, 205, 235);
|
||||
const std::string BLUE = fg(125, 155, 255);
|
||||
const std::string VIOLET = fg(185, 130, 255);
|
||||
const std::string GREEN = fg(120, 215, 120);
|
||||
const std::string BRIGHT_GREEN = fg(80, 245, 125);
|
||||
const std::string YELLOW = fg(245, 200, 90);
|
||||
const std::string RED = fg(235, 110, 110);
|
||||
const std::string BRIGHT_RED = fg(255, 75, 85);
|
||||
|
||||
const std::string& score_colour(int cp) {
|
||||
if (cp >= 150) return BRIGHT_GREEN;
|
||||
if (cp >= 25) return GREEN;
|
||||
if (cp > -25) return VALUE;
|
||||
if (cp > -150) return RED;
|
||||
return BRIGHT_RED;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Text helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// Visible columns of plain UTF-8 text (every code point counts as one column).
|
||||
int columns(const std::string& text) {
|
||||
int n = 0;
|
||||
for (unsigned char c : text)
|
||||
if ((c & 0xC0) != 0x80) n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
std::string repeat(const std::string& s, int n) {
|
||||
std::string out;
|
||||
for (int i = 0; i < n; i++) out += s;
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string format(const char* fmt, double value) {
|
||||
@@ -76,125 +109,465 @@ std::string duration(i64 ms) {
|
||||
return buf;
|
||||
}
|
||||
|
||||
// Pawns with a sign: +0.42, -1.30
|
||||
std::string score_text(int cp) { return format("%+.2f", double(cp) / 100.0); }
|
||||
std::string pawns(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 line of coloured text that knows how many columns it takes.
|
||||
struct Line {
|
||||
std::string text;
|
||||
int width = 0;
|
||||
|
||||
Line& add(const std::string& plain, const std::string& colour = "") {
|
||||
text += colour.empty() ? plain : colour + plain + RESET;
|
||||
width += columns(plain);
|
||||
return *this;
|
||||
}
|
||||
Line& pad_to(int target) {
|
||||
if (width < target) add(std::string(size_t(target - width), ' '));
|
||||
return *this;
|
||||
}
|
||||
Line& append(const Line& other) {
|
||||
text += other.text;
|
||||
width += other.width;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
std::string pad_left(const std::string& plain, int width) {
|
||||
const int n = columns(plain);
|
||||
return n >= width ? plain : std::string(size_t(width - n), ' ') + plain;
|
||||
}
|
||||
std::string pad_right(const std::string& plain, int width) {
|
||||
const int n = columns(plain);
|
||||
return n >= width ? plain : plain + std::string(size_t(width - n), ' ');
|
||||
}
|
||||
|
||||
// A bar `width` cells wide; eighth blocks make it smooth.
|
||||
std::string bar(double fraction, int width) {
|
||||
// A smooth bar: `fraction` of `width` cells filled in `fill`, the rest in `track`
|
||||
// (both foreground colours); the cell at the edge uses eighth blocks.
|
||||
Line bar(double fraction, int width, const std::string& fill, const std::string& track, const std::string& track_bg) {
|
||||
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++;
|
||||
const int full = eighths / 8;
|
||||
Line l;
|
||||
if (full) l.add(repeat("█", full), fill);
|
||||
int used = full;
|
||||
if (eighths % 8 && used < width) {
|
||||
l.text += track_bg + fill + EIGHTHS[eighths % 8] + RESET;
|
||||
l.width++;
|
||||
used++;
|
||||
}
|
||||
return out + std::string(size_t(width - cells), ' ');
|
||||
if (used < width) l.add(repeat("█", width - used), track);
|
||||
return l;
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
// ---------------------------------------------------------------------------
|
||||
// Terminal
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
constexpr int TREE_WIDTH = 8;
|
||||
bool stdin_is_terminal = false;
|
||||
|
||||
} // namespace
|
||||
struct TermSize {
|
||||
int cols = 100;
|
||||
int rows = 40;
|
||||
};
|
||||
|
||||
bool init_terminal() {
|
||||
if (std::getenv("NO_COLOR")) return false;
|
||||
TermSize terminal_size() {
|
||||
#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;
|
||||
CONSOLE_SCREEN_BUFFER_INFO info;
|
||||
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info))
|
||||
return {info.srWindow.Right - info.srWindow.Left + 1, info.srWindow.Bottom - info.srWindow.Top + 1};
|
||||
#else
|
||||
return isatty(STDOUT_FILENO);
|
||||
winsize w{};
|
||||
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0 && w.ws_col) return {int(w.ws_col), int(w.ws_row)};
|
||||
#endif
|
||||
return {};
|
||||
}
|
||||
|
||||
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";
|
||||
}
|
||||
// ---------------------------------------------------------------------------
|
||||
// Box drawing
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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;
|
||||
}
|
||||
struct Box {
|
||||
int width; // total columns, borders included
|
||||
std::vector<std::string> lines;
|
||||
|
||||
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]));
|
||||
int inner() const { return width - 4; }
|
||||
|
||||
void top(const Line& left, const Line& right) {
|
||||
// ╭─ left ───── right ─╮
|
||||
const int fill = width - 8 - left.width - right.width;
|
||||
lines.push_back(BORDER + "╭─ " + RESET + left.text + BORDER + " " + repeat("─", std::max(fill, 1)) + " " +
|
||||
RESET + right.text + BORDER + " ─╮" + RESET);
|
||||
}
|
||||
return line;
|
||||
void section(const Line& title) {
|
||||
// ├─ title ─────┤
|
||||
const int fill = width - 5 - title.width;
|
||||
lines.push_back(BORDER + "├─ " + RESET + title.text + BORDER + " " + repeat("─", std::max(fill, 0)) + "┤" +
|
||||
RESET);
|
||||
}
|
||||
void row(Line content) {
|
||||
content.pad_to(inner());
|
||||
lines.push_back(BORDER + "│ " + RESET + content.text + BORDER + " │" + RESET);
|
||||
}
|
||||
void bottom() { lines.push_back(BORDER + "╰" + repeat("─", width - 2) + "╯" + RESET); }
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Board
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
constexpr int BOARD_WIDTH = 2 + 8 * 3;
|
||||
|
||||
Line board_row(const Position& pos, int rank, Move best) {
|
||||
static constexpr const char* GLYPHS[] = {"♟", "♞", "♝", "♜", "♛", "♚"};
|
||||
static const std::string LIGHT = bg(168, 140, 110), DARK = bg(122, 95, 73);
|
||||
static const std::string LIGHT_HI = bg(176, 172, 82), DARK_HI = bg(136, 138, 58);
|
||||
static const std::string WHITE_PIECE = BOLD + fg(255, 255, 255), BLACK_PIECE = BOLD + fg(18, 16, 22);
|
||||
|
||||
Line l;
|
||||
l.add(std::to_string(rank + 1) + " ", LABEL);
|
||||
for (int file = 0; file < 8; file++) {
|
||||
const int sq = rank * 8 + file;
|
||||
const bool light = (file + rank) % 2 == 1;
|
||||
const bool hi = best && (sq == best.from() || sq == best.to());
|
||||
const std::string& square = hi ? (light ? LIGHT_HI : DARK_HI) : (light ? LIGHT : DARK);
|
||||
const int piece = pos.piece_on(sq);
|
||||
if (piece == NO_PIECE) {
|
||||
l.text += square + " " + RESET;
|
||||
} else {
|
||||
l.text += square + (piece_color(piece) == WHITE ? WHITE_PIECE : BLACK_PIECE) + " " +
|
||||
GLYPHS[piece_type(piece)] + " " + RESET;
|
||||
}
|
||||
l.width += 3;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
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; });
|
||||
Line board_files() {
|
||||
Line l;
|
||||
l.add(" ");
|
||||
for (char f = 'a'; f <= 'h'; f++) l.add(std::string(" ") + f + " ", LABEL);
|
||||
return l;
|
||||
}
|
||||
|
||||
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;
|
||||
// ---------------------------------------------------------------------------
|
||||
// Statistics column, nine lines next to the board
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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));
|
||||
constexpr int LABEL_WIDTH = 10;
|
||||
|
||||
Line label(const std::string& text) {
|
||||
Line l;
|
||||
l.add(pad_right(text, LABEL_WIDTH), LABEL);
|
||||
return l;
|
||||
}
|
||||
|
||||
std::vector<Line> stats(const Dashboard& d, int width) {
|
||||
std::vector<Line> out;
|
||||
|
||||
// Score, and white's expected score as a bar.
|
||||
Line score = label("score");
|
||||
score.add(pawns(d.cp), BOLD + score_colour(d.cp));
|
||||
score.add(d.root.stm == WHITE ? " white to move" : " black to move", FAINT);
|
||||
out.push_back(score);
|
||||
|
||||
const double q = eval::cp_to_q(d.cp);
|
||||
const double white = d.root.stm == WHITE ? (q + 1.0) / 2.0 : (1.0 - q) / 2.0;
|
||||
Line eval_bar = label("white");
|
||||
const int bar_width = std::max(width - LABEL_WIDTH - 5, 4);
|
||||
eval_bar.append(bar(white, bar_width, fg(240, 240, 245), fg(55, 55, 68), bg(55, 55, 68)));
|
||||
eval_bar.add(pad_left(format("%.0f%%", white * 100.0), 5), VALUE);
|
||||
out.push_back(eval_bar);
|
||||
|
||||
out.push_back(Line{});
|
||||
|
||||
Line depth = label("depth");
|
||||
depth.add(std::to_string(d.depth), VALUE).add(" avg ", FAINT).add(std::to_string(d.seldepth), VALUE).add(" max", FAINT);
|
||||
out.push_back(depth);
|
||||
|
||||
Line playouts = label("playouts");
|
||||
playouts.add(pad_right(count(d.playouts), 8), CYAN).add("speed ", LABEL).add(count(d.nps), BLUE).add("/s", FAINT);
|
||||
out.push_back(playouts);
|
||||
|
||||
Line time = label("time");
|
||||
time.add(duration(d.ms), VALUE);
|
||||
double progress = -1.0;
|
||||
if (d.time_limit_ms) {
|
||||
time.add(" / " + duration(d.time_limit_ms), FAINT);
|
||||
progress = double(d.ms) / double(d.time_limit_ms);
|
||||
} else if (d.node_limit) {
|
||||
time.add(" " + count(d.playouts) + "/" + count(d.node_limit), FAINT);
|
||||
progress = double(d.playouts) / double(d.node_limit);
|
||||
} else {
|
||||
time.add(d.done ? " stopped" : " infinite", FAINT);
|
||||
}
|
||||
if (moves.size() > rows)
|
||||
out += "\n " + paint(DIM, "... " + std::to_string(moves.size() - rows) + " more moves");
|
||||
out.push_back(time);
|
||||
|
||||
Line progress_bar = label("");
|
||||
if (progress >= 0.0)
|
||||
progress_bar.append(bar(d.done ? 1.0 : progress, width - LABEL_WIDTH, CYAN, fg(45, 50, 66), bg(45, 50, 66)));
|
||||
out.push_back(progress_bar);
|
||||
|
||||
// Tree memory: one square per tenth.
|
||||
Line tree = label("tree");
|
||||
const int filled = std::clamp((d.hashfull + 50) / 100, 0, 10);
|
||||
const std::string& colour = d.hashfull < 500 ? GREEN : d.hashfull < 900 ? YELLOW : RED;
|
||||
tree.add(repeat("■", filled), colour).add(repeat("■", 10 - filled), fg(55, 58, 72));
|
||||
tree.add(pad_left(std::to_string((d.hashfull + 5) / 10) + "%", 5), VALUE);
|
||||
out.push_back(tree);
|
||||
|
||||
Line notes = label("");
|
||||
std::string text;
|
||||
if (d.reused) text += "reused " + count(d.reused);
|
||||
if (d.collections) text += std::string(text.empty() ? "" : " · ") + "collected " + std::to_string(d.collections) + "×";
|
||||
notes.add(text, FAINT);
|
||||
out.push_back(notes);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string note(const std::string& text) { return std::string(" ") + DIM + text + RESET; }
|
||||
// ---------------------------------------------------------------------------
|
||||
// Principal variation in SAN, wrapped
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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::vector<Line> pv_lines(const Dashboard& d, int width, int max_lines) {
|
||||
struct Token {
|
||||
std::string text;
|
||||
std::string colour;
|
||||
};
|
||||
std::vector<Token> tokens;
|
||||
Position pos = d.root;
|
||||
for (size_t i = 0; i < d.pv.size(); i++) {
|
||||
if (pos.stm == WHITE) tokens.push_back({std::to_string(pos.fullmove) + ".", FAINT});
|
||||
else if (i == 0) tokens.push_back({std::to_string(pos.fullmove) + "...", FAINT});
|
||||
const std::string colour = i == 0 ? BOLD + fg(255, 255, 255) : i < 5 ? VALUE : i < 10 ? LABEL : FAINT;
|
||||
tokens.push_back({san(pos, d.pv[i]), colour});
|
||||
pos.make_move(d.pv[i]);
|
||||
}
|
||||
|
||||
std::vector<Line> lines(1);
|
||||
for (const Token& t : tokens) {
|
||||
const int need = (lines.back().width ? 1 : 0) + columns(t.text);
|
||||
if (lines.back().width + need > width) {
|
||||
if (int(lines.size()) == max_lines) {
|
||||
lines.back().add(" …", FAINT);
|
||||
break;
|
||||
}
|
||||
lines.emplace_back();
|
||||
}
|
||||
if (lines.back().width) lines.back().add(" ");
|
||||
lines.back().add(t.text, t.colour);
|
||||
}
|
||||
if (d.pv.empty()) lines.back().add("…", FAINT);
|
||||
return lines;
|
||||
}
|
||||
|
||||
std::string bestmove(const std::string& move) {
|
||||
return std::string("\n ") + DIM + "bestmove " + RESET + BOLD + BRIGHT_GREEN + move + RESET + "\n";
|
||||
// ---------------------------------------------------------------------------
|
||||
// Root moves table
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
void root_moves(Box& box, const Dashboard& d, size_t rows) {
|
||||
std::vector<RootMove> moves = d.moves;
|
||||
std::stable_sort(moves.begin(), moves.end(), [](const RootMove& a, const RootMove& b) { return a.visits > b.visits; });
|
||||
u64 total = 0;
|
||||
for (const RootMove& m : moves) total += m.visits;
|
||||
|
||||
const int bar_width = std::max(box.inner() - 8 - 8 - 2 - 7 - 8 - 8, 6);
|
||||
Line header;
|
||||
header.add(pad_right("move", 8) + pad_left("visits", 8) + " " + pad_right("share", bar_width) + pad_left("%", 7) +
|
||||
pad_left("score", 8) + pad_left("prior", 8),
|
||||
FAINT);
|
||||
box.row(header);
|
||||
|
||||
for (size_t i = 0; i < std::min(rows, moves.size()); i++) {
|
||||
const RootMove& m = moves[i];
|
||||
const double share = total ? double(m.visits) / double(total) : 0.0;
|
||||
Line l;
|
||||
l.add(pad_right(san(d.root, m.move), 8), i == 0 ? BOLD + fg(255, 255, 255) : VALUE);
|
||||
l.add(pad_left(count(m.visits), 8), CYAN).add(" ");
|
||||
l.append(bar(share, bar_width, i == 0 ? VIOLET : BLUE, fg(40, 44, 58), bg(40, 44, 58)));
|
||||
l.add(pad_left(format("%.1f%%", share * 100.0), 7), VALUE);
|
||||
if (m.visits) l.add(pad_left(pawns(m.cp), 8), score_colour(m.cp));
|
||||
else l.add(pad_left("-", 8), FAINT);
|
||||
l.add(pad_left(format("%.1f%%", double(m.prior) * 100.0), 8), LABEL);
|
||||
box.row(l);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> render(const Dashboard& d, int frame) {
|
||||
static constexpr const char* SPINNER[] = {"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"};
|
||||
const TermSize term = terminal_size();
|
||||
|
||||
Box box{std::clamp(term.cols - 1, 52, 104), {}};
|
||||
const int inner = box.inner();
|
||||
const bool show_board = inner >= BOARD_WIDTH + 3 + 34;
|
||||
const int stats_width = show_board ? inner - BOARD_WIDTH - 3 : inner;
|
||||
|
||||
// Fit the height: fewer root moves first, then no blank lines.
|
||||
const int pv_max = 2;
|
||||
const int available = term.rows - 1;
|
||||
int fixed = 1 + 1 + 9 + 1 + 1 + pv_max + 1 + 1 + 1;
|
||||
bool blanks = true;
|
||||
int rows = std::clamp(available - fixed, 0, 8);
|
||||
if (rows < 3) {
|
||||
blanks = false;
|
||||
fixed -= 2;
|
||||
rows = std::clamp(available - fixed, 0, 8);
|
||||
}
|
||||
const bool show_moves = rows >= 1;
|
||||
|
||||
Line title;
|
||||
title.add("♞ Rigby", TITLE);
|
||||
Line status;
|
||||
if (d.done) status.add("✓ done", BOLD + BRIGHT_GREEN);
|
||||
else status.add(std::string(SPINNER[frame % 10]) + " searching", YELLOW);
|
||||
box.top(title, status);
|
||||
if (blanks) box.row(Line{});
|
||||
|
||||
const Move best = d.pv.empty() ? Move() : d.pv.front();
|
||||
const std::vector<Line> right = stats(d, stats_width);
|
||||
for (int i = 0; i < 9; i++) {
|
||||
Line l;
|
||||
if (show_board) {
|
||||
l = i < 8 ? board_row(d.root, 7 - i, best) : board_files();
|
||||
l.pad_to(BOARD_WIDTH).add(" ");
|
||||
}
|
||||
l.append(right[size_t(i)]);
|
||||
box.row(l);
|
||||
}
|
||||
if (blanks) box.row(Line{});
|
||||
|
||||
Line pv_title;
|
||||
pv_title.add("principal variation", LABEL);
|
||||
box.section(pv_title);
|
||||
for (const Line& l : pv_lines(d, inner, pv_max)) box.row(l);
|
||||
|
||||
if (show_moves) {
|
||||
Line moves_title;
|
||||
moves_title.add("root moves", LABEL).add(" · " + std::to_string(d.moves.size()) + " legal", FAINT);
|
||||
box.section(moves_title);
|
||||
root_moves(box, d, size_t(rows));
|
||||
}
|
||||
box.bottom();
|
||||
return box.lines;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Public interface
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
bool init_terminal() {
|
||||
if (std::getenv("NO_COLOR")) return false;
|
||||
// RIGBY_PRETTY forces the dashboard even into a pipe, e.g. for `less -R`.
|
||||
const bool forced = std::getenv("RIGBY_PRETTY") != nullptr;
|
||||
#ifdef _WIN32
|
||||
if (!forced && !_isatty(_fileno(stdout))) return false;
|
||||
const HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
DWORD mode = 0;
|
||||
const bool vt = GetConsoleMode(out, &mode) && SetConsoleMode(out, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
|
||||
if (!forced && !vt) return false;
|
||||
SetConsoleOutputCP(CP_UTF8);
|
||||
stdin_is_terminal = _isatty(_fileno(stdin));
|
||||
return true;
|
||||
#else
|
||||
if (!forced && !isatty(STDOUT_FILENO)) return false;
|
||||
stdin_is_terminal = isatty(STDIN_FILENO);
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::atomic<int>& typed_lines() {
|
||||
static std::atomic<int> lines{0};
|
||||
return lines;
|
||||
}
|
||||
|
||||
std::string banner(const std::string& name, const std::string& version, const std::string& author) {
|
||||
Box box{58, {}};
|
||||
Line title;
|
||||
title.add("♞ " + name, TITLE).add(" " + version, VALUE);
|
||||
Line right;
|
||||
right.add("MCTS chess engine", LABEL);
|
||||
box.top(title, right);
|
||||
box.row(Line{});
|
||||
Line by;
|
||||
by.add("by ", FAINT).add(author, VALUE);
|
||||
box.row(by);
|
||||
Line help;
|
||||
help.add("try ", FAINT).add("go movetime 5000", CYAN).add(" or ", FAINT).add("go infinite", CYAN).add(" + ", FAINT)
|
||||
.add("stop", CYAN);
|
||||
box.row(help);
|
||||
Line uci;
|
||||
uci.add("type ", FAINT).add("uci", CYAN).add(" for plain UCI output", FAINT);
|
||||
box.row(uci);
|
||||
box.bottom();
|
||||
std::string out = "\n";
|
||||
for (const std::string& l : box.lines) out += " " + l + "\n";
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string LiveView::draw(const Dashboard& d) {
|
||||
const std::vector<std::string> lines = render(d, frame_++);
|
||||
std::string out;
|
||||
if (lines_) {
|
||||
// Back to the first line of the previous dashboard (and over any line
|
||||
// the user typed below it), then clear everything from there down.
|
||||
const int typed = stdin_is_terminal ? typed_lines().exchange(0) : 0;
|
||||
out += "\x1b[" + std::to_string(lines_ + typed) + "F\x1b[J";
|
||||
}
|
||||
for (size_t i = 0; i < lines.size(); i++) out += (i ? "\n" : "") + lines[i];
|
||||
lines_ = int(lines.size());
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string note(const std::string& text) { return " " + LABEL + text + RESET; }
|
||||
|
||||
std::string bestmove(const Position& root, Move move) {
|
||||
return " " + LABEL + "bestmove " + RESET + BOLD + BRIGHT_GREEN + san(root, move) + RESET + FAINT + " (" +
|
||||
move.to_uci() + ")" + RESET + "\n";
|
||||
}
|
||||
|
||||
std::string san(const Position& pos, Move m) {
|
||||
if (!m) return "--";
|
||||
std::string s;
|
||||
if (m.is_castle()) {
|
||||
s = file_of(m.to()) > file_of(m.from()) ? "O-O" : "O-O-O";
|
||||
} else {
|
||||
const int type = piece_type(pos.piece_on(m.from()));
|
||||
if (type == PAWN) {
|
||||
if (m.is_capture()) s += char('a' + file_of(m.from()));
|
||||
} else {
|
||||
s += "PNBRQK"[type];
|
||||
// Disambiguate between pieces of the same type that reach the same square.
|
||||
MoveList legal;
|
||||
generate_legal(pos, legal);
|
||||
bool clash = false, same_file = false, same_rank = false;
|
||||
for (Move o : legal) {
|
||||
if (o == m || o.to() != m.to() || o.from() == m.from()) continue;
|
||||
if (piece_type(pos.piece_on(o.from())) != type) continue;
|
||||
clash = true;
|
||||
same_file |= file_of(o.from()) == file_of(m.from());
|
||||
same_rank |= rank_of(o.from()) == rank_of(m.from());
|
||||
}
|
||||
if (clash) {
|
||||
if (!same_file) s += char('a' + file_of(m.from()));
|
||||
else if (!same_rank) s += char('1' + rank_of(m.from()));
|
||||
else s += square_name(m.from());
|
||||
}
|
||||
}
|
||||
if (m.is_capture()) s += 'x';
|
||||
s += square_name(m.to());
|
||||
if (m.is_promotion()) s += std::string("=") + "NBRQ"[m.promo_type() - KNIGHT];
|
||||
}
|
||||
|
||||
Position next = pos;
|
||||
next.make_move(m);
|
||||
if (next.in_check()) {
|
||||
MoveList replies;
|
||||
generate_legal(next, replies);
|
||||
s += replies.empty() ? '#' : '+';
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
} // namespace rigby::pretty
|
||||
Reference in new issue
Block a user