Skip to content
/ cowsay Public

๐Ÿฎ ๐Ÿฎ ๐Ÿฎ A simple library generates an ascii cow says a message. ๐Ÿฎ ๐Ÿฎ ๐Ÿฎ

Notifications You must be signed in to change notification settings

shaozi/cowsay

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

23 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Cowsay

This is a simple zig library that mimic the ascii art cowsay .

What's New

[x] Support UTF-8

Install

  1. Use the zig fetch command to fetch and save the library:

    • Fetch the latest: zig fetch --save git+https://github.com/shaozi/cowsay
    • or, fetch a specific version: zig fetch --save https://github.com/shaozi/cowsay/archive/refs/tags/v3.0.0.tar.gz
  2. 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"));
  3. Import it in your zig file:

    const Cowsay = @import("Cowsay");

Usage

Basic usage

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.

Eyes

cow.eyes = [_]u8{ '$', '$' };

Output:

+--------------+
| Hello world! |
+--------------+
        \ ^__^
         \($$)\_______
          (__)\       )\/\
              ||----w |
              ||     ||

Note

The first two o in the file are eyes.

Load an ASCII cow file

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.

Cow think

try cow.think("Hmm... Hello ... world ...", .{});

Output

+----------------------------+
| Hmm... Hello ... world ... |
+----------------------------+
               o  ^__^
                o (oo)\_______
                  (__)\       )\/\
                      ||----w |
                      ||     ||

Write to an ArrayList, out as a string

var 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);

About

๐Ÿฎ ๐Ÿฎ ๐Ÿฎ A simple library generates an ascii cow says a message. ๐Ÿฎ ๐Ÿฎ ๐Ÿฎ

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages