From c626969744bca0efb8805c26f7cd52f40d09236f Mon Sep 17 00:00:00 2001 From: Kalle Carlbark Date: Thu, 30 May 2024 22:28:38 +0200 Subject: [PATCH] feat: Simplify setting indicator by using enum --- src/indicator.zig | 22 ------------------ src/main.zig | 21 +++++++++++------ src/zpinner.zig | 57 ++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 63 insertions(+), 37 deletions(-) delete mode 100644 src/indicator.zig diff --git a/src/indicator.zig b/src/indicator.zig deleted file mode 100644 index 0b564a8..0000000 --- a/src/indicator.zig +++ /dev/null @@ -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)]; - } -}; diff --git a/src/main.zig b/src/main.zig index 4a281ba..b20073a 100644 --- a/src/main.zig +++ b/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", .{}); } diff --git a/src/zpinner.zig b/src/zpinner.zig index 253ce2e..885b032 100644 --- a/src/zpinner.zig +++ b/src/zpinner.zig @@ -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 = "";