feat: implement stopwatch timer
Replace Hello World with a live stopwatch that prints elapsed time in HH:MM:SS.mmm format, updating every 10ms with color output.
This commit is contained in:
@@ -1,6 +1,27 @@
|
||||
#include <fmt/core.h>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include "fmt/std.h"
|
||||
|
||||
auto main(int argc, char const* argv[]) -> int {
|
||||
fmt::print("Hello, World!\n");
|
||||
using namespace std::chrono;
|
||||
using namespace std::literals;
|
||||
|
||||
auto start = steady_clock::now();
|
||||
|
||||
while (true) {
|
||||
auto elapsed = steady_clock::now() - start;
|
||||
auto totalSec = duration_cast<seconds>(elapsed).count();
|
||||
auto h = totalSec / 3600;
|
||||
auto m = (totalSec % 3600) / 60;
|
||||
auto s = totalSec % 60;
|
||||
auto ms = duration_cast<milliseconds>(elapsed % 1s).count();
|
||||
|
||||
fmt::print("\033[32m{:02}:{:02}:{:02}\033[0m.\033[90m{:03}\033[0m\r",
|
||||
h, m, s, ms);
|
||||
|
||||
std::this_thread::sleep_for(10ms);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user