1
Fork 0
mirror of https://github.com/RGBCube/ZTerm synced 2025-07-28 16:37:46 +00:00

Cleanup whole API

This commit is contained in:
RGBCube 2023-05-18 22:10:40 +03:00
parent 7f5fdc55ea
commit 977f47342e
3 changed files with 49 additions and 31 deletions

View file

@ -16,17 +16,17 @@ const zterm = @import("zterm");
pub fn main() !void {
var sp = zterm.Spinner{
.loading_charset = &[_][]const u8{"⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"},
.loading_message = "Selling all your data to the CCP...",
.charset = &[_][]const u8{"⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"},
.finished_charset = "✓",
.finished_message = "Lock your doors.",
.message = "Selling all your data to the CCP...",
};
try sp.start();
time.sleep(3 * time.ns_per_s);
var stdOut = std.io.getStdOut();
try stdOut.writeAll("\rCalculating very important stuff while selling your data...\n");
sp.setMessage("Calculating very important stuff while selling your data...");
time.sleep(2 * time.ns_per_s);
sp.setMessage("Sold all your data successfully!");
try sp.stop();
}

View file

@ -5,17 +5,13 @@ const Thread = std.Thread;
const time = std.time;
const Spinner = @This();
const default_loading_charset = [_][]const u8{ "|", "/", "-", "\\" };
const default_finished_charset = "";
loading_charset: []const []const u8 = &default_loading_charset,
loading_message: []const u8 = "",
finished_charset: []const u8 = default_finished_charset,
finished_message: []const u8 = "",
// Must be only accessed when thread_lock is held by the current thread.
charset: []const []const u8 = &[_][]const u8{ "|", "/", "-", "\\" },
message: []const u8 = "",
keep_going: Atomic(bool) = Atomic(bool).init(false),
spinner_thread: ?Thread = null,
thread_lock: Mutex = Mutex{},
framerate_ns: u64 = 100 * time.ns_per_ms,
@ -24,16 +20,37 @@ pub fn start(sp: *Spinner) !void {
sp.spinner_thread = try Thread.spawn(.{}, writer, .{sp});
}
pub fn stop(sp: *Spinner) !void {
pub fn stop(sp: *Spinner, config: struct {
charset: ?[]const u8 = null,
message: ?[]const u8 = null,
}) !void {
sp.keep_going.store(false, .SeqCst);
if (sp.spinner_thread) |*thread| thread.join();
(sp.spinner_thread orelse unreachable).join();
var stdErr = std.io.getStdErr();
sp.thread_lock.lock();
defer sp.thread_lock.unlock();
try stdErr.writeAll("\r");
try stdErr.writeAll(sp.finished_charset);
try stdErr.writeAll(config.charset orelse sp.charset[0]);
try stdErr.writeAll(" ");
try stdErr.writeAll(sp.finished_message);
try stdErr.writeAll(config.message orelse sp.message);
try stdErr.writeAll("\n");
}
pub fn setCharset(sp: *Spinner, charset: []const []const u8) void {
sp.thread_lock.lock();
defer sp.thread_lock.unlock();
sp.charset = charset;
}
pub fn setMessage(sp: *Spinner, message: []const u8) void {
sp.thread_lock.lock();
defer sp.thread_lock.unlock();
sp.message = message;
}
fn writer(sp: *Spinner) !void {
@ -41,14 +58,15 @@ fn writer(sp: *Spinner) !void {
var current_char_idx: usize = 0;
while (true) : (current_char_idx += 1) {
if (!sp.keep_going.load(.SeqCst)) break;
if (current_char_idx >= sp.loading_charset.len - 1) current_char_idx = 0;
if (!sp.keep_going.load(.SeqCst)) return;
if (current_char_idx >= sp.charset.len - 1) current_char_idx = 0;
try stdErr.writeAll("\r");
try stdErr.writeAll(sp.loading_charset[current_char_idx]);
sp.thread_lock.lock();
try stdErr.writeAll(sp.charset[current_char_idx]);
try stdErr.writeAll(" ");
try stdErr.writeAll(sp.loading_message);
try stdErr.writeAll(sp.message);
try stdErr.writeAll("\r");
sp.thread_lock.unlock();
time.sleep(sp.framerate_ns);
}

View file

@ -6,17 +6,17 @@ const zterm = struct {
pub fn main() !void {
var sp = zterm.Spinner{
.loading_charset = &[_][]const u8{"", "", "", "", "", "", "", ""},
.loading_message = "Selling all your data to the CCP...",
.finished_charset = "",
.finished_message = "Lock your doors.",
.charset = &[_][]const u8{ "", "", "", "", "", "", "", "" },
.message = "Selling all your data to the CCP...",
};
try sp.start();
time.sleep(3 * time.ns_per_s);
var stdOut = std.io.getStdOut();
try stdOut.writeAll("\rCalculating very important stuff while selling your data...\n");
time.sleep(2 * time.ns_per_s);
sp.setMessage("Calculating very important stuff while selling your data...");
try sp.stop();
time.sleep(2 * time.ns_per_s);
try sp.stop(.{
.charset = "",
.message = "Successfully sold all your data to the CCP! You data is not in safe hands!",
});
}