Crafter.Build/implementations/Crafter.Build-Progress.cpp

147 lines
4.6 KiB
C++
Raw Normal View History

2026-07-23 01:24:42 +02:00
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
2026-04-29 03:27:11 +02:00
module;
#include <stdio.h>
#include <stdlib.h>
#if defined(_WIN32)
#include <io.h>
#include <windows.h>
#ifndef STDOUT_FILENO
#define STDOUT_FILENO _fileno(stdout)
#endif
#define CRAFTER_PROGRESS_ISATTY _isatty
#else
#include <unistd.h>
#include <sys/ioctl.h>
#define CRAFTER_PROGRESS_ISATTY isatty
#endif
export module Crafter.Build:Progress_impl;
import std;
import :Progress;
namespace {
2026-07-23 01:24:42 +02:00
std::mutex StateMutex;
std::atomic<std::int32_t> Total{0};
std::atomic<std::int32_t> Done{0};
Crafter::Progress::Verbosity ActiveVerbosity = Crafter::Progress::Verbosity::Default;
bool IsTty = false;
bool LineDirty = false; // status line has uncleared content
bool Finalized = false;
std::chrono::steady_clock::time_point StartTime = std::chrono::steady_clock::now();
2026-04-29 03:27:11 +02:00
2026-07-23 01:24:42 +02:00
std::int32_t TerminalWidth() {
2026-04-29 03:27:11 +02:00
#if defined(_WIN32)
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO info;
if (h != INVALID_HANDLE_VALUE && GetConsoleScreenBufferInfo(h, &info)) {
2026-07-23 01:24:42 +02:00
std::int32_t w = info.srWindow.Right - info.srWindow.Left + 1;
2026-04-29 03:27:11 +02:00
if (w > 0) return w;
}
#else
winsize ws{};
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0 && ws.ws_col > 0) {
return ws.ws_col;
}
#endif
if (const char* col = std::getenv("COLUMNS")) {
2026-07-23 01:24:42 +02:00
try { std::int32_t c = std::stoi(col); if (c > 0) return c; } catch (...) {}
2026-04-29 03:27:11 +02:00
}
return 80;
}
2026-07-23 01:24:42 +02:00
// Caller holds StateMutex.
2026-04-29 03:27:11 +02:00
void RenderStatus(std::string_view label) {
2026-07-23 01:24:42 +02:00
if (ActiveVerbosity != Crafter::Progress::Verbosity::Default || !IsTty) return;
std::int32_t done = Done.load(std::memory_order_relaxed);
std::int32_t total = Total.load(std::memory_order_relaxed);
2026-04-29 03:27:11 +02:00
std::string prefix = std::format("[{}/{}] ", done, total);
2026-07-23 01:24:42 +02:00
std::int32_t width = TerminalWidth();
std::int32_t avail = width - static_cast<std::int32_t>(prefix.size()) - 1;
2026-04-29 03:27:11 +02:00
std::string trimmed{label};
2026-07-23 01:24:42 +02:00
if (avail > 0 && static_cast<std::int32_t>(trimmed.size()) > avail) {
2026-04-29 03:27:11 +02:00
trimmed.resize(static_cast<std::size_t>(avail));
}
// \r returns to col 0, \033[2K erases the whole line. No newline.
std::fputs("\r\033[2K", stdout);
std::fputs(prefix.c_str(), stdout);
std::fputs(trimmed.c_str(), stdout);
std::fflush(stdout);
2026-07-23 01:24:42 +02:00
LineDirty = true;
2026-04-29 03:27:11 +02:00
}
2026-07-23 01:24:42 +02:00
// Caller holds StateMutex.
2026-04-29 03:27:11 +02:00
void ClearLineLocked() {
2026-07-23 01:24:42 +02:00
if (LineDirty) {
2026-04-29 03:27:11 +02:00
std::fputs("\r\033[2K", stdout);
std::fflush(stdout);
2026-07-23 01:24:42 +02:00
LineDirty = false;
2026-04-29 03:27:11 +02:00
}
}
}
namespace Crafter::Progress {
void SetVerbosity(Verbosity v) {
2026-07-23 01:24:42 +02:00
std::lock_guard lock(StateMutex);
ActiveVerbosity = v;
IsTty = CRAFTER_PROGRESS_ISATTY(STDOUT_FILENO) != 0;
StartTime = std::chrono::steady_clock::now();
Total.store(0);
Done.store(0);
Finalized = false;
2026-04-29 03:27:11 +02:00
}
Verbosity GetVerbosity() {
2026-07-23 01:24:42 +02:00
std::lock_guard lock(StateMutex);
return ActiveVerbosity;
2026-04-29 03:27:11 +02:00
}
Task::Task(std::string label) : label_(std::move(label)) {
2026-07-23 01:24:42 +02:00
Total.fetch_add(1, std::memory_order_relaxed);
std::lock_guard lock(StateMutex);
if (ActiveVerbosity == Verbosity::Default && IsTty) {
2026-04-29 03:27:11 +02:00
RenderStatus(label_);
}
}
Task::~Task() {
2026-07-23 01:24:42 +02:00
std::int32_t done = Done.fetch_add(1, std::memory_order_relaxed) + 1;
std::lock_guard lock(StateMutex);
if (ActiveVerbosity == Verbosity::Default) {
if (IsTty) {
2026-04-29 03:27:11 +02:00
RenderStatus(label_);
} else {
// Non-TTY: one append-only line per completed task (ninja-style).
2026-07-23 01:24:42 +02:00
std::int32_t total = Total.load(std::memory_order_relaxed);
2026-04-29 03:27:11 +02:00
std::println("[{}/{}] {}", done, total, label_);
}
}
}
void EchoCommand(std::string_view command) {
2026-07-23 01:24:42 +02:00
std::lock_guard lock(StateMutex);
if (ActiveVerbosity == Verbosity::Verbose) {
2026-04-29 03:27:11 +02:00
std::println("$ {}", command);
}
}
void Clear() {
2026-07-23 01:24:42 +02:00
std::lock_guard lock(StateMutex);
2026-04-29 03:27:11 +02:00
ClearLineLocked();
}
void Finalize() {
2026-07-23 01:24:42 +02:00
std::lock_guard lock(StateMutex);
if (Finalized) return;
Finalized = true;
2026-04-29 03:27:11 +02:00
ClearLineLocked();
2026-07-23 01:24:42 +02:00
if (ActiveVerbosity == Verbosity::Quiet) return;
std::int32_t done = Done.load();
2026-04-29 03:27:11 +02:00
if (done == 0) return; // Nothing happened (cached build); stay silent.
2026-07-23 01:24:42 +02:00
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - StartTime);
std::println("Built {} step{} in {}ms", done, done == 1 ? "" : "s", elapsed.count());
2026-04-29 03:27:11 +02:00
}
} // namespace Crafter::Progress