mirror of
https://github.com/RGBCube/ZTerm
synced 2025-07-28 08:27:45 +00:00
Rename to ZTerm
This commit is contained in:
parent
c0ab824528
commit
0ec36b9a3f
5 changed files with 81 additions and 78 deletions
|
@ -1,6 +1,6 @@
|
|||
# ZSpinner
|
||||
# ZTerm
|
||||
|
||||
Zig spinner library for activity indication.
|
||||
Terminal abstraction library for Zig.
|
||||
|
||||
## License
|
||||
|
||||
|
|
|
@ -3,11 +3,11 @@ const std = @import("std");
|
|||
pub fn build(b: *std.build.Builder) void {
|
||||
const mode = b.standardReleaseOptions();
|
||||
|
||||
const lib = b.addStaticLibrary("zspinner", "src/lib.zig");
|
||||
const lib = b.addStaticLibrary("zterm", "zterm.zig");
|
||||
lib.setBuildMode(mode);
|
||||
lib.install();
|
||||
|
||||
const main_tests = b.addTest("src/lib.zig");
|
||||
const main_tests = b.addTest("zterm.zig");
|
||||
main_tests.setBuildMode(mode);
|
||||
|
||||
const test_step = b.step("test", "Run library tests");
|
||||
|
|
76
src/Spinner.zig
Normal file
76
src/Spinner.zig
Normal file
|
@ -0,0 +1,76 @@
|
|||
const Spinner = @This();
|
||||
|
||||
const std = @import("std");
|
||||
const Mutex = std.Thread.Mutex;
|
||||
const time = std.time;
|
||||
|
||||
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,
|
||||
|
||||
// The number of nanoseconds to wait between frames.
|
||||
framerate: u64,
|
||||
|
||||
charset: [][]const u8,
|
||||
message: []const u8,
|
||||
|
||||
pub fn new(framerate: ?u64, charset: ?[][]const u8, message: ?[]const u8) Spinner {
|
||||
return Spinner{
|
||||
.stop_lock = Mutex{},
|
||||
.stopped_lock = Mutex{},
|
||||
.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 stop(sp: *Spinner) void {
|
||||
sp.stop_lock.lock();
|
||||
sp.stopped_lock.lock();
|
||||
}
|
||||
|
||||
fn animateSpinnerOnce(sp: *Spinner) void {
|
||||
var stdOut = std.io.getStdOut();
|
||||
|
||||
for (sp.charset) |frame| {
|
||||
stdOut.write(frame);
|
||||
stdOut.write(" ");
|
||||
stdOut.write(sp.message);
|
||||
|
||||
// Jumps to the start of the line.
|
||||
stdOut.write("\r");
|
||||
stdOut.flush();
|
||||
|
||||
time.sleep(sp.framerate);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
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();
|
||||
}
|
74
src/lib.zig
74
src/lib.zig
|
@ -1,74 +0,0 @@
|
|||
const std = @import("std");
|
||||
const Mutex = std.Thread.Mutex;
|
||||
const time = std.time;
|
||||
|
||||
const default_frame_rate = 150 * time.ns_per_ms;
|
||||
const default_charset = [_][]const u8{ "|", "/", "-", "\\" };
|
||||
|
||||
pub const Spinner = struct {
|
||||
// When locked, the spinner will stop spinning.
|
||||
stop_lock: Mutex,
|
||||
// Used to check if stopped.
|
||||
stopped_lock: Mutex,
|
||||
// The number of nanoseconds to wait between frames.
|
||||
framerate: u64,
|
||||
charset: [][]const u8,
|
||||
message: []const u8,
|
||||
|
||||
pub fn new(framerate: ?u64, charset: ?[][]const u8, message: ?[]const u8) Spinner {
|
||||
return Spinner{
|
||||
.stop_lock = Mutex{},
|
||||
.stopped_lock = Mutex{},
|
||||
.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 stop(sp: *Spinner) void {
|
||||
sp.stop_lock.lock();
|
||||
sp.stopped_lock.lock();
|
||||
}
|
||||
|
||||
fn animateSpinnerOnce(sp: *Spinner) void {
|
||||
var stdOut = std.io.getStdOut();
|
||||
|
||||
for (sp.charset) |frame| {
|
||||
stdOut.write(frame);
|
||||
stdOut.write(" ");
|
||||
stdOut.write(sp.message);
|
||||
|
||||
// Jumps to the start of the line.
|
||||
stdOut.write("\r");
|
||||
stdOut.flush();
|
||||
|
||||
time.sleep(sp.framerate);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
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();
|
||||
}
|
1
zterm.zig
Normal file
1
zterm.zig
Normal file
|
@ -0,0 +1 @@
|
|||
const Spinner = @import("src/Spinner.zig");
|
Loading…
Add table
Add a link
Reference in a new issue