Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
with:
version: 0.15.2
version: 0.16.0
- name: Install uv
uses: astral-sh/setup-uv@v6
with:
Expand All @@ -33,7 +33,7 @@ jobs:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v1
with:
version: 0.15.2
version: 0.16.0
- run: zig fmt --check .
- name: Install uv
uses: astral-sh/setup-uv@v6
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
![CI](https://github.com/tammoippen/zig-plotille/actions/workflows/main.yml/badge.svg)
[![zig 0.15.2](https://img.shields.io/badge/Zig-0.15.2-color?logo=zig&color=%23f3ab20)](https://ziglang.org/download/0.15.1/release-notes.html)
[![zig 0.16.0](https://img.shields.io/badge/Zig-0.16.0-color?logo=zig&color=%23f3ab20)](https://ziglang.org/download/0.16.0/release-notes.html)

# zig-plotille

Expand Down Expand Up @@ -81,10 +81,13 @@ Display all available named colors in a grid:
```zig
const plt = @import("plotille");

// Zig 0.16 hands I/O and the environment to `main` via `std.process.Init`:
// pub fn main(init: std.process.Init) !void { ... }

// Detect terminal capabilities
// ALWAYS required, before printing with color support
// (either set or detect)
try plt.terminfo.TermInfo.detect(allocator);
try plt.terminfo.TermInfo.detect(init.io, init.minimal.environ, allocator);

// Display color grid
for (std.enums.values(plt.color.ColorName)) |bg_value| {
Expand Down
5 changes: 2 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn build(b: *std.Build) !void {

const name = "plotille";
const entry = b.path("src/main.zig");
const version = try std.SemanticVersion.parse("0.0.1");
const version = try std.SemanticVersion.parse("0.1.0");
const module = b.addModule(name, .{
.root_source_file = entry,
.target = target,
Expand All @@ -32,14 +32,14 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = mode,
.strip = strip,
.link_libc = true,
});
const static_lib = b.addLibrary(.{
.name = name,
.root_module = c_module,
.version = version,
.linkage = .static,
});
static_lib.linkLibC();
const installed_static_lib = b.addInstallArtifact(static_lib, .{});
b.getInstallStep().dependOn(&installed_static_lib.step);

Expand All @@ -50,7 +50,6 @@ pub fn build(b: *std.Build) !void {
.version = version,
.linkage = .dynamic,
});
shared_lib.linkLibC();
const installed_shared_lib = b.addInstallArtifact(shared_lib, .{});
b.getInstallStep().dependOn(&installed_shared_lib.step);

Expand Down
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.{
.name = .plotille,
.version = "0.0.1",
.minimum_zig_version = "0.15.2",
.version = "0.1.0",
.minimum_zig_version = "0.16.0",
.dependencies = .{},
.paths = .{
"src",
Expand Down
50 changes: 30 additions & 20 deletions src/c-api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export fn dots_init() dots.Dots {
return dots.Dots{};
}
export fn dots_str(self: dots.Dots, buf: [*]u8, len: usize) usize {
var fbs = std.io.fixedBufferStream(buf[0..len]);
std.fmt.format(fbs.writer(), "{f}", .{self}) catch |err| switch (err) {
error.NoSpaceLeft => return 0,
var w: std.Io.Writer = .fixed(buf[0..len]);
w.print("{f}", .{self}) catch |err| switch (err) {
error.WriteFailed => return 0,
};
return fbs.pos;
return w.end;
}
export fn dots_fill(self: *dots.Dots) void {
return self.fill();
Expand Down Expand Up @@ -57,23 +57,33 @@ export fn color_by_hsl(h: f64, s: f64, l: f64) color.Color {
}

export fn color_str(buf: [*]u8, len: usize, text: [*:0]const u8, options: color.ColorOptions) usize {
var fbs = std.io.fixedBufferStream(buf[0..len]);
color.colorPrint(fbs.writer(), "{s}", .{text}, options) catch return 0;
return fbs.pos;
var w: std.Io.Writer = .fixed(buf[0..len]);
color.colorPrint(&w, "{s}", .{text}, options) catch return 0;
return w.end;
}

// terminfo

/// The environment block of the process. A C caller does not hand us a
/// `std.process.Init`, so pick it up from libc (which this library links).
fn processEnviron() std.process.Environ {
if (@import("builtin").os.tag == .windows) return .{ .block = .global };
const c_environ = std.c.environ;
var count: usize = 0;
while (c_environ[count] != null) : (count += 1) {}
return .{ .block = .{ .slice = c_environ[0..count :null] } };
}

export fn get_terminfo(out: *terminfo.TermInfo) bool {
if (terminfo.TermInfo.is_set()) {
out.* = terminfo.TermInfo.get();
return true;
}
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const allocator = std.heap.c_allocator;
// no `std.process.Init` available from C, so use the hardcoded instance
const io = std.Io.Threaded.global_single_threaded.io();

terminfo.TermInfo.detect(allocator) catch return false;
terminfo.TermInfo.detect(io, processEnviron(), allocator) catch return false;
out.* = terminfo.TermInfo.get();
return true;
}
Expand Down Expand Up @@ -127,9 +137,9 @@ export fn hist_free(h: *CHistogram) void {
export fn hist_str(h: CHistogram, buf: [*]u8, len: usize) usize {
if (h._internal) |internal| {
const hist_ptr: *const hist.Histogram = @ptrCast(@alignCast(internal));
var fbs = std.io.fixedBufferStream(buf[0..len]);
std.fmt.format(fbs.writer(), "{f}", .{hist_ptr.*}) catch return 0;
return fbs.pos;
var w: std.Io.Writer = .fixed(buf[0..len]);
w.print("{f}", .{hist_ptr.*}) catch return 0;
return w.end;
}
return 0;
}
Expand Down Expand Up @@ -246,9 +256,9 @@ export fn canvas_text(c: *CCanvas, p: canvas.Point, text: [*:0]const u8, fg_colo
export fn canvas_str(c: CCanvas, buf: [*]u8, len: usize) usize {
if (c._internal) |internal| {
const canvas_ptr: *const canvas.Canvas = @ptrCast(@alignCast(internal));
var fbs = std.io.fixedBufferStream(buf[0..len]);
std.fmt.format(fbs.writer(), "{f}", .{canvas_ptr.*}) catch return 0;
return fbs.pos;
var w: std.Io.Writer = .fixed(buf[0..len]);
w.print("{f}", .{canvas_ptr.*}) catch return 0;
return w.end;
}
return 0;
}
Expand Down Expand Up @@ -460,9 +470,9 @@ export fn figure_prepare(f: *CFigure) bool {
export fn figure_str(f: CFigure, buf: [*]u8, len: usize) usize {
if (f._internal) |internal| {
const figure_ptr: *const figure.Figure = @ptrCast(@alignCast(internal));
var fbs = std.io.fixedBufferStream(buf[0..len]);
std.fmt.format(fbs.writer(), "{f}", .{figure_ptr.*}) catch return 0;
return fbs.pos;
var w: std.Io.Writer = .fixed(buf[0..len]);
w.print("{f}", .{figure_ptr.*}) catch return 0;
return w.end;
}
return 0;
}
Loading
Loading