linting
Some checks failed
CI / build-test-release (push) Failing after 7m15s

This commit is contained in:
Jorijn van der Graaf 2026-07-23 01:24:42 +02:00
commit 8892154b28
70 changed files with 2780 additions and 596 deletions

View file

@ -1,5 +1,5 @@
//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
module;
#include <stdio.h>
@ -22,21 +22,21 @@ import std;
import :Progress;
namespace {
std::mutex g_mutex;
std::atomic<int> g_total{0};
std::atomic<int> g_done{0};
Crafter::Progress::Verbosity g_verbosity = Crafter::Progress::Verbosity::Default;
bool g_isTty = false;
bool g_lineDirty = false; // status line has uncleared content
bool g_finalized = false;
std::chrono::steady_clock::time_point g_startTime = std::chrono::steady_clock::now();
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();
int TerminalWidth() {
std::int32_t TerminalWidth() {
#if defined(_WIN32)
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO info;
if (h != INVALID_HANDLE_VALUE && GetConsoleScreenBufferInfo(h, &info)) {
int w = info.srWindow.Right - info.srWindow.Left + 1;
std::int32_t w = info.srWindow.Right - info.srWindow.Left + 1;
if (w > 0) return w;
}
#else
@ -46,21 +46,21 @@ namespace {
}
#endif
if (const char* col = std::getenv("COLUMNS")) {
try { int c = std::stoi(col); if (c > 0) return c; } catch (...) {}
try { std::int32_t c = std::stoi(col); if (c > 0) return c; } catch (...) {}
}
return 80;
}
// Caller holds g_mutex.
// Caller holds StateMutex.
void RenderStatus(std::string_view label) {
if (g_verbosity != Crafter::Progress::Verbosity::Default || !g_isTty) return;
int done = g_done.load(std::memory_order_relaxed);
int total = g_total.load(std::memory_order_relaxed);
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);
std::string prefix = std::format("[{}/{}] ", done, total);
int width = TerminalWidth();
int avail = width - static_cast<int>(prefix.size()) - 1;
std::int32_t width = TerminalWidth();
std::int32_t avail = width - static_cast<std::int32_t>(prefix.size()) - 1;
std::string trimmed{label};
if (avail > 0 && static_cast<int>(trimmed.size()) > avail) {
if (avail > 0 && static_cast<std::int32_t>(trimmed.size()) > avail) {
trimmed.resize(static_cast<std::size_t>(avail));
}
// \r returns to col 0, \033[2K erases the whole line. No newline.
@ -68,15 +68,15 @@ namespace {
std::fputs(prefix.c_str(), stdout);
std::fputs(trimmed.c_str(), stdout);
std::fflush(stdout);
g_lineDirty = true;
LineDirty = true;
}
// Caller holds g_mutex.
// Caller holds StateMutex.
void ClearLineLocked() {
if (g_lineDirty) {
if (LineDirty) {
std::fputs("\r\033[2K", stdout);
std::fflush(stdout);
g_lineDirty = false;
LineDirty = false;
}
}
}
@ -84,66 +84,64 @@ namespace {
namespace Crafter::Progress {
void SetVerbosity(Verbosity v) {
std::lock_guard lock(g_mutex);
g_verbosity = v;
g_isTty = CRAFTER_PROGRESS_ISATTY(STDOUT_FILENO) != 0;
g_startTime = std::chrono::steady_clock::now();
g_total.store(0);
g_done.store(0);
g_finalized = false;
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;
}
Verbosity GetVerbosity() {
std::lock_guard lock(g_mutex);
return g_verbosity;
std::lock_guard lock(StateMutex);
return ActiveVerbosity;
}
Task::Task(std::string label) : label_(std::move(label)) {
g_total.fetch_add(1, std::memory_order_relaxed);
std::lock_guard lock(g_mutex);
if (g_verbosity == Verbosity::Default && g_isTty) {
Total.fetch_add(1, std::memory_order_relaxed);
std::lock_guard lock(StateMutex);
if (ActiveVerbosity == Verbosity::Default && IsTty) {
RenderStatus(label_);
}
}
Task::~Task() {
int done = g_done.fetch_add(1, std::memory_order_relaxed) + 1;
std::lock_guard lock(g_mutex);
if (g_verbosity == Verbosity::Default) {
if (g_isTty) {
std::int32_t done = Done.fetch_add(1, std::memory_order_relaxed) + 1;
std::lock_guard lock(StateMutex);
if (ActiveVerbosity == Verbosity::Default) {
if (IsTty) {
RenderStatus(label_);
} else {
// Non-TTY: one append-only line per completed task (ninja-style).
int total = g_total.load(std::memory_order_relaxed);
std::int32_t total = Total.load(std::memory_order_relaxed);
std::println("[{}/{}] {}", done, total, label_);
}
}
}
void EchoCommand(std::string_view command) {
std::lock_guard lock(g_mutex);
if (g_verbosity == Verbosity::Verbose) {
std::lock_guard lock(StateMutex);
if (ActiveVerbosity == Verbosity::Verbose) {
std::println("$ {}", command);
}
}
void Clear() {
std::lock_guard lock(g_mutex);
std::lock_guard lock(StateMutex);
ClearLineLocked();
}
void Finalize() {
std::lock_guard lock(g_mutex);
if (g_finalized) return;
g_finalized = true;
std::lock_guard lock(StateMutex);
if (Finalized) return;
Finalized = true;
ClearLineLocked();
if (g_verbosity == Verbosity::Quiet) return;
int done = g_done.load();
if (ActiveVerbosity == Verbosity::Quiet) return;
std::int32_t done = Done.load();
if (done == 0) return; // Nothing happened (cached build); stay silent.
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - g_startTime);
std::println("Built {} step{} in {}ms",
done, done == 1 ? "" : "s", elapsed.count());
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());
}
} // namespace Crafter::Progress