1
Fork 0
mirror of https://github.com/RGBCube/ZTerm synced 2025-07-29 08:57:46 +00:00

Fix code,

This commit is contained in:
RGBCube 2023-05-17 22:42:09 +03:00
parent 51a1224a51
commit acc8f529ef
2 changed files with 32 additions and 37 deletions

View file

@ -1,76 +1,61 @@
const std = @import("std"); const std = @import("std");
const Mutex = std.Thread.Mutex; const AtomicBool = std.atomic.Atomic(bool);
const Mutex = Thread.Mutex;
const Thread = std.Thread;
const time = std.time; const time = std.time;
const Spinner = @This(); const Spinner = @This();
const default_frame_rate = 150 * time.ns_per_ms; const default_frame_rate = 150 * time.ns_per_ms;
const default_charset = [_][]const u8{ "|", "/", "-", "\\" }; const default_charset = [_][]const u8{ "|", "/", "-", "\\" };
// When locked, the spinner will stop spinning. keep_going: AtomicBool,
stop_lock: Mutex, spinner_thread: ?Thread,
// Used to check if stopped.
stopped_lock: Mutex,
// The number of nanoseconds to wait between frames. // The number of nanoseconds to wait between frames.
framerate: u64, framerate: u64,
charset: [][]const u8, charset: [4][]const u8,
message: []const u8, message: []const u8,
pub fn new(framerate: ?u64, charset: ?[][]const u8, message: ?[]const u8) Spinner { pub fn new(framerate: ?u64, charset: ?[4][]const u8, message: ?[]const u8) Spinner {
return Spinner{ return Spinner{
.stop_lock = Mutex{}, .keep_going = AtomicBool.init(false),
.stopped_lock = Mutex{}, .spinner_thread = null,
.framerate = framerate orelse default_frame_rate, .framerate = framerate orelse default_frame_rate,
.charset = charset orelse default_charset, .charset = charset orelse default_charset,
.message = message orelse "", .message = message orelse "",
}; };
} }
pub fn start(sp: *Spinner) void { pub fn start(sp: *Spinner) !void {
sp.stop_lock.unlock(); sp.keep_going.store(true, .SeqCst);
sp.stopped_lock.unlock(); sp.spinner_thread = try Thread.spawn(.{}, writer, .{sp});
async sp.writer();
} }
pub fn stop(sp: *Spinner) void { pub fn stop(sp: *Spinner) void {
sp.stop_lock.lock(); sp.keep_going.store(false, .SeqCst);
sp.stopped_lock.lock(); (sp.spinner_thread orelse unreachable).join();
} }
fn animateSpinnerOnce(sp: *Spinner) void { fn animateSpinnerOnce(sp: *Spinner) !void {
var stdOut = std.io.getStdOut(); var stdOut = std.io.getStdOut();
for (sp.charset) |frame| { for (sp.charset) |frame| {
stdOut.write(frame); _ = try stdOut.write(frame);
stdOut.write(" "); _ = try stdOut.write(" ");
stdOut.write(sp.message); _ = try stdOut.write(sp.message);
// Jumps to the start of the line. // Jumps to the start of the line.
stdOut.write("\r"); _ = try stdOut.write("\r");
stdOut.flush();
time.sleep(sp.framerate); time.sleep(sp.framerate);
} }
} }
fn writer(sp: *Spinner) void { fn writer(sp: *Spinner) !void {
while (true) { while (true) {
// Stops if it has been locked by .stop(). if (!sp.keep_going.load(.SeqCst)) break;
if (sp.stop_lock.tryLock()) {
break;
}
sp.stop_lock.unlock();
sp.animateSpinnerOnce(); try sp.animateSpinnerOnce();
} }
sp.stopped_lock.lock();
}
test "asd" {
var sp = Spinner.new(null, null, "Loading...");
sp.start();
time.sleep(5 * time.ns_per_s);
sp.stop();
} }

View file

@ -1 +1,11 @@
const Spinner = @import("src/Spinner.zig"); const Spinner = @import("src/Spinner.zig");
const time = @import("std").time;
pub fn main() !void {
var sp = Spinner.new(null, null, "Loading...");
try sp.start();
time.sleep(5 * time.ns_per_s);
sp.stop();
}