Skip to content

Latest commit

History

46 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

uni

Static BadgeStatic Badge

uni image

uni is a Zig library that lets you easily colorize your strings and outputs on your code. It uses ANSI escape codes to put color and styles in your strings and outputs.

It was built on Linux, with ❤️, as part of my ongoing journey of learning Zig.

I'm so grateful for all the help I got from the Ziggit.dev community!

Roadmap:

  • Normal colors
  • Styles
  • RGB
  • Hexadecimal
  • Detects terminal capacity
  • Use colors only when the terminal supports it
  • Supports major OSes
    • Linux
    • FreeBSD
    • Windows
    • MacOS

How to use Uni in your project?

  1. Add Uni as a dependency in your build.zig.zon file, running the following command: zig fetch --save git+https://github.com/menosbits/uni
.{
.name="YourProject",
.version="0.0.0",
.dependencies= .{
.uni= .{
.url="git+https://github.com/menosbits/uni#2b4013623a29c41904a03d662a813760045aec16",
.hash="uni-0.9.0-tz1MSq2bAADWZhGuUZ8Ot-5k0ALjlsFMZNIO0HyIFmIZ",
},
},
.paths= .{
"...",
},
}
  1. Import uni in your build.zig file:
pubfnbuild(b: *std.Build) void {
constexe=b.addExecutable(.{ ... });
constuni=b.dependency("uni", .{
.target=target,
.optimize=optimize,
});
exe.root_module.addImport("uni", uni.module("uni"));
}
  1. See the examples below for usage.

Examples:

Print with custom foreground, background color and style
conststd=@import("std");
constuni=@import("uni");
constColor=uni.Color;
constStyle=uni.Style;
pubfnmain(init: std.process.Init) !void {
varstdout_buff: [1024]u8=undefined;
varstdout_file=std.Io.File.stdout();
varstdout_writer=stdout_file.writer(init.io, &stdout_buff);
conststdout=&stdout_writer.interface;
constwarn=uni.init().add(.{ Color.Foreground.red, Color.Background.black, Style.bold });
trystdout.print("{s}: This is a warning!\n", .{ warn.format("WARNING") });
trystdout.flush();
}
Print with main color aliases
conststd=@import("std");
constuni=@import("uni");
pubfnmain(init: std.process.Init) !void {
varstdout_buff: [1024]u8=undefined;
varstdout_file=std.Io.File.stdout();
varstdout_writer=stdout_file.writer(init.io, &stdout_buff);
conststdout=&stdout_writer.interface;
constgreen=uni.init().green(.foreground).bold();
trystdout.print("{s}: success!\n", .{ green.format("GREAT") });
constbg_green=uni.init().green(.background).bold();
trystdout.print("{s}: success!\n", .{ bg_green.format("GREAT") });
trystdout.flush();
}
Colorize a string
conststd=@import("std");
constuni=@import("uni");
pubfnmain(init: std.process.Init) !void {
varstdout_buff: [1024]u8=undefined;
varstdout_file=std.Io.File.stdout();
varstdout_writer=stdout_file.writer(init.io, &stdout_buff);
conststdout=&stdout_writer.interface;
constbright_yellow_string=uni.init().brightYellow(.foreground).format("This is a bright yellow string");
trystdout.print("{s}\n", .{ bright_yellow_string });
trystdout.flush();
}
Reuse your setted colors
conststd=@import("std");
constuni=@import("uni");
pubfnmain(init: std.process.Init) !void {
varstdout_buff: [1024]u8=undefined;
varstdout_file=std.Io.File.stdout();
varstdout_writer=stdout_file.writer(init.io, &stdout_buff);
conststdout=&stdout_writer.interface;
constmagenta=uni.init().magenta(.foreground);
trystdout.print("This is {s}. This is also a magenta word: {s}.\n", .{ magenta.format("magenta"), magenta.format("Uni") });
trystdout.flush();
}
Print with RGB color
conststd=@import("std");
constuni=@import("uni");
constColor=uni.Color;
pubfnmain(init: std.process.Init) !void {
varstdout_buff: [1024]u8=undefined;
varstdout_file=std.Io.File.stdout();
varstdout_writer=stdout_file.writer(init.io, &stdout_buff);
conststdout=&stdout_writer.interface;
constred=uni.init().rgb(102, 0, 0, .foreground); // First wayconstgreen=uni.init().add(.{ Color.RGB.fg(0, 102, 0) }); // Second wayconstblue=uni.init().add(.{ Color.RGB.fg(0, 0, 102) });
trystdout.print("{s}{s}{s}\n", .{ red.format("R"), green.format("G"), blue.format("B") });
constbg_red=uni.init().rgb(102, 0, 0, .background); // First wayconstbg_green=uni.init().add(.{ Color.RGB.bg(0, 102, 0) }); // Second wayconstbg_blue=uni.init().add(.{ Color.RGB.bg(0, 0, 102) });
trystdout.print("{s}{s}{s}\n", .{ bg_red.format("R"), bg_green.format("G"), bg_blue.format("B") });
}
Use uni on your existing code
conststd=@import("std");
constuni=@import("uni");
pubfnmain(init: std.process.Init) !void {
varstdout_buff: [1024]u8=undefined;
varstdout_file=std.Io.File.stdout();
varstdout_writer=stdout_file.writer(init.io, &stdout_buff);
conststdout=&stdout_writer.interface;
constuni=uni.init().cyan(.foreground);
tryuni.on(stdout);
trystdout.print("This is a cyan string\n", .{});
tryuni.off(stdout);
trystdout.print("This is a default colored string\n", .{});
trystdout.flush();
}
Print with Hexadecimal code
conststd=@import("std");
constuni=@import("uni");
constColor=uni.Color;
pubfnmain(init: std.process.Init) !void {
constfirst=uni.init().add(.{ Color.Hex.fg("#a7c080"), Color.Hex.bg("2d353b") }); // First wayconstsecond=uni.init().hex("A7C080", .foreground).hex("#2D353B", .background); // Second waytrystdout.print("{s}\n", .{ first.format("We accept hexadecimal too! Yay!") });
trystdout.print("{s}\n", .{ second.format("Everforest theme is great!") });
trystdout.flush();
}

About

A Zig library that lets you easily colorize your strings and outputs on your code.

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages