feat: Simplify setting indicator by using enum
This commit is contained in:
parent
9f09cc1b6f
commit
c626969744
3 changed files with 63 additions and 37 deletions
|
@ -1,22 +0,0 @@
|
|||
pub const Moon = "🌑 🌒 🌓 🌔 🌕 🌖 🌗 🌘";
|
||||
pub const Snake = "⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏";
|
||||
pub const SnakeLoad = "⣾ ⣽ ⣻ ⢿ ⡿ ⣟ ⣯ ⣷";
|
||||
pub const Earth = "🌍 🌎 🌏";
|
||||
|
||||
pub const Enum = enum {
|
||||
Moon,
|
||||
Snake,
|
||||
SnakeLoad,
|
||||
Earth,
|
||||
|
||||
pub const EnumTable = [@typeInfo(Enum).Enum.fields.len][:0]const u8{
|
||||
Moon,
|
||||
Snake,
|
||||
SnakeLoad,
|
||||
Earth,
|
||||
};
|
||||
|
||||
pub fn str(self: Enum) [:0]const u8 {
|
||||
return EnumTable[@intFromEnum(self)];
|
||||
}
|
||||
};
|
21
src/main.zig
21
src/main.zig
|
@ -2,25 +2,32 @@ const std = @import("std");
|
|||
const zpinner = @import("zpinner.zig");
|
||||
|
||||
pub fn main() !void {
|
||||
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
||||
|
||||
const stdout_file = std.io.getStdOut().writer();
|
||||
|
||||
var zpin = zpinner.new(stdout_file.any(), .{});
|
||||
zpin.set_indicator(zpinner.indicator.Enum.Moon);
|
||||
zpin.set_indicator(zpinner.Moon);
|
||||
zpin.set_suffix(" calculating route to the moon");
|
||||
try zpin.start();
|
||||
std.time.sleep(5 * std.time.ns_per_s);
|
||||
try zpin.stop();
|
||||
try stdout_file.print("Go straight to /dev/null\n", .{});
|
||||
|
||||
zpin.set_suffix(" counting snakes");
|
||||
zpin.set_indicator(zpinner.indicator.Enum.Snake);
|
||||
zpin.set_indicator(zpinner.Snake);
|
||||
try zpin.start();
|
||||
std.time.sleep(5 * std.time.ns_per_s);
|
||||
zpin.set_indicator(zpinner.indicator.Enum.Moon);
|
||||
zpin.set_indicator(zpinner.Earth);
|
||||
zpin.set_suffix(" calculating route back to earth");
|
||||
std.time.sleep(5 * std.time.ns_per_s);
|
||||
try zpin.stop();
|
||||
zpin.set_suffix(" calculating next week weather");
|
||||
zpin.set_indicator(zpinner.Weather);
|
||||
try zpin.start();
|
||||
std.time.sleep(5 * std.time.ns_per_s);
|
||||
try zpin.stop();
|
||||
zpin.set_suffix(" ZzZzz...");
|
||||
zpin.set_indicator(zpinner.Clock);
|
||||
try zpin.start();
|
||||
std.time.sleep(5 * std.time.ns_per_s);
|
||||
try zpin.stop();
|
||||
|
||||
try stdout_file.print("work done\n", .{});
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
pub const std = @import("std");
|
||||
pub const indicator = @import("indicator.zig");
|
||||
pub const Color = @import("ansi.zig").Color;
|
||||
pub const Style = @import("ansi.zig").Style;
|
||||
pub const cursor = @import("cursor.zig");
|
||||
|
@ -10,8 +9,8 @@ const builtin = @import("builtin");
|
|||
const DefaultOptions = struct {
|
||||
/// delay is the speed of the indicator
|
||||
delay: u64 = std.time.ns_per_s * 0.1,
|
||||
/// chars to loop through see characters.zig
|
||||
indicator: indicator.Enum = indicator.Enum.Snake,
|
||||
/// indicator_type is the type of indicator to use
|
||||
indicator_type: EnumType = EnumType.Snake,
|
||||
/// prefix is the text preppended to the indicator
|
||||
prefix: []const u8 = "",
|
||||
/// suffix is the text appended to the indicator
|
||||
|
@ -100,8 +99,8 @@ const Zpinner = struct {
|
|||
}
|
||||
|
||||
/// Set indicator
|
||||
pub fn set_indicator(self: *Self, indicator_enum: indicator.Enum) void {
|
||||
self.options.indicator = indicator_enum;
|
||||
pub fn set_indicator(self: *Self, indicator_type: EnumType) void {
|
||||
self.options.indicator_type = indicator_type;
|
||||
}
|
||||
|
||||
fn is_tty() bool {
|
||||
|
@ -110,7 +109,7 @@ const Zpinner = struct {
|
|||
|
||||
fn run(self: *Self) !void {
|
||||
while (true) {
|
||||
var splits = std.mem.split(u8, self.options.indicator.str(), " ");
|
||||
var splits = std.mem.split(u8, self.options.indicator_type.str(), " ");
|
||||
while (splits.next()) |character| {
|
||||
if (!self.mutex.tryLock()) {
|
||||
return;
|
||||
|
@ -162,6 +161,48 @@ const Zpinner = struct {
|
|||
}
|
||||
};
|
||||
|
||||
pub const Clock = EnumType.Clock;
|
||||
/// Earth zpinner indicator
|
||||
pub const Earth = EnumType.Earth;
|
||||
/// Moon zpinner indicator
|
||||
pub const Moon = EnumType.Moon;
|
||||
/// Snake zpinner indicator
|
||||
pub const Snake = EnumType.Snake;
|
||||
/// SnakeLoad zpinner indicator
|
||||
pub const SnakeLoad = EnumType.SnakeLoad;
|
||||
/// Weather zpinner indicator
|
||||
pub const Weather = EnumType.Weather;
|
||||
|
||||
/// EnumType for the different indicators
|
||||
const EnumType = enum {
|
||||
Clock,
|
||||
Earth,
|
||||
Moon,
|
||||
Snake,
|
||||
SnakeLoad,
|
||||
Weather,
|
||||
|
||||
pub const EnumTable = [@typeInfo(EnumType).Enum.fields.len][:0]const u8{
|
||||
ClockCharacters,
|
||||
EarthCharacters,
|
||||
MoonCharacters,
|
||||
SnakeCharacters,
|
||||
SnakeLoadCharacters,
|
||||
WeatherCharacters,
|
||||
};
|
||||
|
||||
pub fn str(self: EnumType) [:0]const u8 {
|
||||
return EnumTable[@intFromEnum(self)];
|
||||
}
|
||||
};
|
||||
|
||||
const ClockCharacters = "🕐 🕑 🕒 🕓 🕕 🕖 🕗 🕘 🕙 🕚 🕛 🕝 🕞 🕟 🕠 🕡 🕢 🕣 🕤 🕥 🕦 🕧";
|
||||
const MoonCharacters = "🌑 🌒 🌓 🌔 🌕 🌖 🌗 🌘";
|
||||
const SnakeCharacters = "⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏";
|
||||
const SnakeLoadCharacters = "⣾ ⣽ ⣻ ⢿ ⡿ ⣟ ⣯ ⣷";
|
||||
const EarthCharacters = "🌍 🌎 🌏";
|
||||
const WeatherCharacters = "☀️ 🌤️ ⛅️ 🌥️ ☁️ 🌦️ 🌧️ ⛈️ 🌩️ 🌨️";
|
||||
|
||||
test "empty options struct contains default delay value" {
|
||||
const expectedValue = std.time.ns_per_s * 0.1;
|
||||
|
||||
|
@ -170,11 +211,11 @@ test "empty options struct contains default delay value" {
|
|||
try std.testing.expectEqual(@as(u64, defaultOptions.delay), @as(u64, expectedValue));
|
||||
}
|
||||
test "empty options struct contains default snake charset" {
|
||||
const expectedValue = indicator.Snake;
|
||||
const expectedValue = SnakeCharacters;
|
||||
|
||||
const opts = Options{};
|
||||
|
||||
try std.testing.expectEqual(opts.indicator.str(), expectedValue);
|
||||
try std.testing.expectEqual(opts.indicator_type.str(), expectedValue);
|
||||
}
|
||||
test "empty options struct contains default empty prefix" {
|
||||
const expectedValue = "";
|
||||
|
|
Loading…
Reference in a new issue