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");
|
const zpinner = @import("zpinner.zig");
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
|
||||||
|
|
||||||
const stdout_file = std.io.getStdOut().writer();
|
const stdout_file = std.io.getStdOut().writer();
|
||||||
|
|
||||||
var zpin = zpinner.new(stdout_file.any(), .{});
|
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");
|
zpin.set_suffix(" calculating route to the moon");
|
||||||
try zpin.start();
|
try zpin.start();
|
||||||
std.time.sleep(5 * std.time.ns_per_s);
|
std.time.sleep(5 * std.time.ns_per_s);
|
||||||
try zpin.stop();
|
try zpin.stop();
|
||||||
try stdout_file.print("Go straight to /dev/null\n", .{});
|
|
||||||
|
|
||||||
zpin.set_suffix(" counting snakes");
|
zpin.set_suffix(" counting snakes");
|
||||||
zpin.set_indicator(zpinner.indicator.Enum.Snake);
|
zpin.set_indicator(zpinner.Snake);
|
||||||
try zpin.start();
|
try zpin.start();
|
||||||
std.time.sleep(5 * std.time.ns_per_s);
|
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");
|
zpin.set_suffix(" calculating route back to earth");
|
||||||
std.time.sleep(5 * std.time.ns_per_s);
|
std.time.sleep(5 * std.time.ns_per_s);
|
||||||
try zpin.stop();
|
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", .{});
|
try stdout_file.print("work done\n", .{});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
pub const std = @import("std");
|
pub const std = @import("std");
|
||||||
pub const indicator = @import("indicator.zig");
|
|
||||||
pub const Color = @import("ansi.zig").Color;
|
pub const Color = @import("ansi.zig").Color;
|
||||||
pub const Style = @import("ansi.zig").Style;
|
pub const Style = @import("ansi.zig").Style;
|
||||||
pub const cursor = @import("cursor.zig");
|
pub const cursor = @import("cursor.zig");
|
||||||
|
@ -10,8 +9,8 @@ const builtin = @import("builtin");
|
||||||
const DefaultOptions = struct {
|
const DefaultOptions = struct {
|
||||||
/// delay is the speed of the indicator
|
/// delay is the speed of the indicator
|
||||||
delay: u64 = std.time.ns_per_s * 0.1,
|
delay: u64 = std.time.ns_per_s * 0.1,
|
||||||
/// chars to loop through see characters.zig
|
/// indicator_type is the type of indicator to use
|
||||||
indicator: indicator.Enum = indicator.Enum.Snake,
|
indicator_type: EnumType = EnumType.Snake,
|
||||||
/// prefix is the text preppended to the indicator
|
/// prefix is the text preppended to the indicator
|
||||||
prefix: []const u8 = "",
|
prefix: []const u8 = "",
|
||||||
/// suffix is the text appended to the indicator
|
/// suffix is the text appended to the indicator
|
||||||
|
@ -100,8 +99,8 @@ const Zpinner = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set indicator
|
/// Set indicator
|
||||||
pub fn set_indicator(self: *Self, indicator_enum: indicator.Enum) void {
|
pub fn set_indicator(self: *Self, indicator_type: EnumType) void {
|
||||||
self.options.indicator = indicator_enum;
|
self.options.indicator_type = indicator_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_tty() bool {
|
fn is_tty() bool {
|
||||||
|
@ -110,7 +109,7 @@ const Zpinner = struct {
|
||||||
|
|
||||||
fn run(self: *Self) !void {
|
fn run(self: *Self) !void {
|
||||||
while (true) {
|
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| {
|
while (splits.next()) |character| {
|
||||||
if (!self.mutex.tryLock()) {
|
if (!self.mutex.tryLock()) {
|
||||||
return;
|
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" {
|
test "empty options struct contains default delay value" {
|
||||||
const expectedValue = std.time.ns_per_s * 0.1;
|
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));
|
try std.testing.expectEqual(@as(u64, defaultOptions.delay), @as(u64, expectedValue));
|
||||||
}
|
}
|
||||||
test "empty options struct contains default snake charset" {
|
test "empty options struct contains default snake charset" {
|
||||||
const expectedValue = indicator.Snake;
|
const expectedValue = SnakeCharacters;
|
||||||
|
|
||||||
const opts = Options{};
|
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" {
|
test "empty options struct contains default empty prefix" {
|
||||||
const expectedValue = "";
|
const expectedValue = "";
|
||||||
|
|
Loading…
Reference in a new issue