shaozi/cowsay
🐮 🐮 🐮 A simple library generates an ascii cow says a message. 🐮 🐮 🐮
v0.13.2.tar.gz
This is a simple zig library that mimic the ascii art cowsay .
[x] Support UTF-8
Use the zig fetch
command to fetch and save the library:
zig fetch --save git+https://github.com/shaozi/cowsay
zig fetch --save https://github.com/shaozi/cowsay/archive/refs/tags/v3.0.0.tar.gz
Add the module to you own build.zig
file:
Add these lines right before the line b.installArtifact(exe);
:
const Cowsay = b.dependency("Cowsay", .{});
exe.root_module.addImport("Cowsay", Cowsay.module("Cowsay"));
Import it in your zig file:
const Cowsay = @import("Cowsay");
const stdout = std.io.getStdOut().writer();
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
const deinit_status = gpa.deinit();
//fail test; can't try in defer as defer is executed after we return
if (deinit_status == .leak) @panic("TEST FAIL");
}
var cow = Cowsay.init(gpa.allocator, stdout.any());
defer cow.deinit();
try cow.say("Hello {s}", .{"world!"});
Output:
+--------------+
| Hello world! |
+--------------+
\ ^__^
\(oo)\_______
(__)\ )\/\
||----w |
|| ||
NOTE
Cowsay takes type
std.io.AnyWriter
. Therefore, you must use the.any()
to convert a writer before pass it in. This allows you to use an ArrayList(u8) to let cowsay write output to a string.
cow.eyes = [_]u8{ '$', '$' };
Output:
+--------------+
| Hello world! |
+--------------+
\ ^__^
\($$)\_______
(__)\ )\/\
||----w |
|| ||
NOTE
The first two
o
in the file are eyes.
try cow.useCowFile("cat");
Output
+-------------+
| Hello meow! |
+-------------+
\ /\___/\
\(= ^.^ =)
(") (")__/
and use the default cow:
cow.useDefaultCow();
NOTE
- Cow file is simple text file of the ascii image, without the
\
bubble pointer.- Max length of the file is 1000 bytes.
try cow.think("Hmm... Hello ... world ...", .{});
Output
+----------------------------+
| Hmm... Hello ... world ... |
+----------------------------+
o ^__^
o (oo)\_______
(__)\ )\/\
||----w |
|| ||
ArrayList
, out as a stringvar buffer = std.ArrayList(u8).init(std.heap.page_allocator);
defer buffer.deinit();
const w = buffer.writer().any();
var cow = Cowsay.init(allocator, w);
defer cow.deinit();
try cow.say("Hello world!", .{});
try stdout.print("{s}", buffer.items);