mirror of
https://github.com/RGBCube/ZTerm
synced 2025-07-28 16:37:46 +00:00
Fix code,
This commit is contained in:
parent
51a1224a51
commit
acc8f529ef
2 changed files with 32 additions and 37 deletions
|
@ -1,76 +1,61 @@
|
|||
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 Spinner = @This();
|
||||
|
||||
const default_frame_rate = 150 * time.ns_per_ms;
|
||||
const default_charset = [_][]const u8{ "|", "/", "-", "\\" };
|
||||
|
||||
// When locked, the spinner will stop spinning.
|
||||
stop_lock: Mutex,
|
||||
// Used to check if stopped.
|
||||
stopped_lock: Mutex,
|
||||
keep_going: AtomicBool,
|
||||
spinner_thread: ?Thread,
|
||||
|
||||
// The number of nanoseconds to wait between frames.
|
||||
framerate: u64,
|
||||
|
||||
charset: [][]const u8,
|
||||
charset: [4][]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{
|
||||
.stop_lock = Mutex{},
|
||||
.stopped_lock = Mutex{},
|
||||
.keep_going = AtomicBool.init(false),
|
||||
.spinner_thread = null,
|
||||
.framerate = framerate orelse default_frame_rate,
|
||||
.charset = charset orelse default_charset,
|
||||
.message = message orelse "",
|
||||
};
|
||||
}
|
||||
|
||||
pub fn start(sp: *Spinner) void {
|
||||
sp.stop_lock.unlock();
|
||||
sp.stopped_lock.unlock();
|
||||
async sp.writer();
|
||||
pub fn start(sp: *Spinner) !void {
|
||||
sp.keep_going.store(true, .SeqCst);
|
||||
sp.spinner_thread = try Thread.spawn(.{}, writer, .{sp});
|
||||
}
|
||||
|
||||
pub fn stop(sp: *Spinner) void {
|
||||
sp.stop_lock.lock();
|
||||
sp.stopped_lock.lock();
|
||||
sp.keep_going.store(false, .SeqCst);
|
||||
(sp.spinner_thread orelse unreachable).join();
|
||||
}
|
||||
|
||||
fn animateSpinnerOnce(sp: *Spinner) void {
|
||||
fn animateSpinnerOnce(sp: *Spinner) !void {
|
||||
var stdOut = std.io.getStdOut();
|
||||
|
||||
for (sp.charset) |frame| {
|
||||
stdOut.write(frame);
|
||||
stdOut.write(" ");
|
||||
stdOut.write(sp.message);
|
||||
_ = try stdOut.write(frame);
|
||||
_ = try stdOut.write(" ");
|
||||
_ = try stdOut.write(sp.message);
|
||||
|
||||
// Jumps to the start of the line.
|
||||
stdOut.write("\r");
|
||||
stdOut.flush();
|
||||
_ = try stdOut.write("\r");
|
||||
|
||||
time.sleep(sp.framerate);
|
||||
}
|
||||
}
|
||||
|
||||
fn writer(sp: *Spinner) void {
|
||||
fn writer(sp: *Spinner) !void {
|
||||
while (true) {
|
||||
// Stops if it has been locked by .stop().
|
||||
if (sp.stop_lock.tryLock()) {
|
||||
break;
|
||||
}
|
||||
sp.stop_lock.unlock();
|
||||
if (!sp.keep_going.load(.SeqCst)) break;
|
||||
|
||||
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();
|
||||
}
|
||||
|
|
10
zterm.zig
10
zterm.zig
|
@ -1 +1,11 @@
|
|||
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();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue