fixed some spacing problems, properly handling cli arguments now

This commit is contained in:
Lumen Keyes 2024-07-15 10:10:08 -06:00
parent 5da40c6416
commit 72b8052f70
5 changed files with 861 additions and 854 deletions

View File

@ -21,6 +21,8 @@ pub fn build(b: *std.Build) void {
exe.linkLibC(); exe.linkLibC();
exe.root_module.addImport("grapheme", zg.module("grapheme")); exe.root_module.addImport("grapheme", zg.module("grapheme"));
exe.root_module.addImport("DisplayWidth", zg.module("DisplayWidth")); exe.root_module.addImport("DisplayWidth", zg.module("DisplayWidth"));
const clap = b.dependency("clap", .{});
exe.root_module.addImport("clap", clap.module("clap"));
b.installArtifact(exe); b.installArtifact(exe);

View File

@ -19,6 +19,10 @@
.url = "https://codeberg.org/dude_the_builder/zg/archive/v0.13.2.tar.gz", .url = "https://codeberg.org/dude_the_builder/zg/archive/v0.13.2.tar.gz",
.hash = "122055beff332830a391e9895c044d33b15ea21063779557024b46169fb1984c6e40", .hash = "122055beff332830a391e9895c044d33b15ea21063779557024b46169fb1984c6e40",
}, },
.clap = .{
.url = "git+https://github.com/Hejsil/zig-clap#c0193e9247335a6c1688b946325060289405de2a",
.hash = "12207ee987ce045596cb992cfb15b0d6d9456e50d4721c3061c69dabc2962053644d",
},
}, },
.paths = .{ .paths = .{
// This makes *all* files, recursively, included in this package. It is generally // This makes *all* files, recursively, included in this package. It is generally

1644
output.pdf

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,8 @@
//TODO: program is adding way more spacing than necessary at the bottom of pages, suspect the c lib
// is computing size strangely
//c library usage based on https://medium.com/@eddo2626/lets-learn-zig-4-using-c-libraries-in-zig-5fcc3206f0dc //c library usage based on https://medium.com/@eddo2626/lets-learn-zig-4-using-c-libraries-in-zig-5fcc3206f0dc
const std = @import("std"); const std = @import("std");
const clap = @import("clap"); const clap = @import("clap");
//using the zg lib: https://codeberg.org/dude_the_builder/zg
const DisplayWidth = @import("DisplayWidth"); const DisplayWidth = @import("DisplayWidth");
const c = @cImport({ const c = @cImport({
@cInclude("pdfgen.h"); @cInclude("pdfgen.h");
@ -17,14 +17,14 @@ const expect = std.testing.expect;
const assert = std.debug.assert; const assert = std.debug.assert;
const Card = struct { const Card = struct {
name: []const u8 = "", //string name: []const u8 = "", //string
mana_cost: []const u8 = "", //string mana_cost: []const u8 = "", //string
cmc: f32 = 0, //technically a float? but I think we can always cast safely cast. EDIT: NOPE cmc: f32 = 0, //float
type_line: []const u8 = "", //string type_line: []const u8 = "", //string
oracle_text: []const u8 = "", //string oracle_text: []const u8 = "", //string
power: []const u8 = "", //coerced to string power: []const u8 = "", //coerced to string
toughness: []const u8 = "", //coerced to string toughness: []const u8 = "", //coerced to string
card_faces: ?[]Card = null, //array of cards card_faces: ?[]Card = null, //array of cards
}; };
const TextCard = struct { const TextCard = struct {
@ -44,7 +44,7 @@ const cardWidth = 30;
const cardHeight = 32; const cardHeight = 32;
const minCardHeight = 5; const minCardHeight = 5;
const pageHeight = 66; const pageHeight = 66;
var heightMayVary = true; var heightMayVary = true; //whether or not cards must have a constant height
const formatString = "{s: <" ++ std.fmt.digits2(cardWidth) ++ "}"; const formatString = "{s: <" ++ std.fmt.digits2(cardWidth) ++ "}";
const lineFormatter = "|" ++ formatString; const lineFormatter = "|" ++ formatString;
@ -60,19 +60,26 @@ test "Check constants" {
} }
pub fn main() !void { pub fn main() !void {
var args = try std.process.argsWithAllocator(std.heap.page_allocator); // var args = try std.process.argsWithAllocator(std.heap.page_allocator);
//TODO: properly handle program arguments
_ = args.next(); //handle program name (args[0])
const listFileName: []const u8 = args.next() orelse {
return error.ExpectedArgument;
};
heightMayVary = stringToBool(args.next());
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit(); defer arena.deinit();
const allocator = arena.allocator(); const allocator = arena.allocator();
const params = comptime clap.parseParamsComptime(
\\-h, --help Display this help and exit.
\\-l, --listFile <str> The list of cards to print
\\
);
const res = clap.parse(clap.Help, &params, clap.parsers.default, .{
.allocator = allocator,
}) catch |err| {
return err;
};
if(res.args.listFile == null) return error.ExpectedArgument;
const listFileName = res.args.listFile.?;
const oracleFile = try cwd.openFile(oracleFileName, .{}); const oracleFile = try cwd.openFile(oracleFileName, .{});
var jsonReader = json.reader(allocator, oracleFile.reader()); var jsonReader = json.reader(allocator, oracleFile.reader());
const parsedJson = try json.parseFromTokenSource([]Card, allocator, &jsonReader, .{ .ignore_unknown_fields = true }); const parsedJson = try json.parseFromTokenSource([]Card, allocator, &jsonReader, .{ .ignore_unknown_fields = true });
@ -99,9 +106,9 @@ pub fn main() !void {
cards.sort(CardSortContext{ .list = cards.values() }); cards.sort(CardSortContext{ .list = cards.values() });
var allPrinted = std.ArrayList([]const u8).init(allocator); var allPrinted = std.ArrayList([]const u8).init(allocator);
const pdfHeight = c.PDF_LETTER_HEIGHT;
const pdf_doc: *c.pdf_doc = @ptrCast(c.pdf_create(c.PDF_A4_WIDTH, c.PDF_A4_HEIGHT, &c.pdf_info{ .creator = ("My Software" ++ " " ** 53).* }).?); const pdfWidth = c.PDF_LETTER_WIDTH;
_ = c.pdf_set_font(pdf_doc, "Times-Roman"); const pdf_doc: *c.pdf_doc = @ptrCast(c.pdf_create(pdfWidth, pdfHeight, &c.pdf_info{ .creator = ("My Software" ++ " " ** 53).* }).?);
var rowToPrint = std.ArrayList(TextCard).init(allocator); var rowToPrint = std.ArrayList(TextCard).init(allocator);
for (cards.values()) |cardText| { for (cards.values()) |cardText| {
@ -124,10 +131,10 @@ pub fn main() !void {
const pageRelative = rowNum % pageHeight; const pageRelative = rowNum % pageHeight;
const pageOffset: f32 = 12 * @as(f32, @floatFromInt(pageRelative)); const pageOffset: f32 = 12 * @as(f32, @floatFromInt(pageRelative));
if (pageRelative != 0) { if (pageRelative != 0) {
_ = c.pdf_add_text(pdf_doc, page, try std.mem.Allocator.dupeZ(allocator, u8, text), 8, 10, c.PDF_A4_HEIGHT - pageOffset, c.PDF_BLACK); _ = c.pdf_add_text(pdf_doc, page, try std.mem.Allocator.dupeZ(allocator, u8, text), 8, 10, pdfHeight - pageOffset, c.PDF_BLACK);
} else { } else {
page = c.pdf_append_page(pdf_doc); page = c.pdf_append_page(pdf_doc);
_ = c.pdf_add_text(pdf_doc, page, try std.mem.Allocator.dupeZ(allocator, u8, text), 8, 10, c.PDF_A4_HEIGHT - pageOffset, c.PDF_BLACK); _ = c.pdf_add_text(pdf_doc, page, try std.mem.Allocator.dupeZ(allocator, u8, text), 8, 10, pdfHeight - pageOffset, c.PDF_BLACK);
} }
} }
@ -206,7 +213,6 @@ const cardRow = struct {
third: []const u8 = spacer, third: []const u8 = spacer,
last: []const u8 = "\n", last: []const u8 = "\n",
fn print(allocator: std.mem.Allocator, cards: []TextCard, allPrinted: *std.ArrayList([]const u8)) !void { fn print(allocator: std.mem.Allocator, cards: []TextCard, allPrinted: *std.ArrayList([]const u8)) !void {
// _ = allocator;
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var lines = linesList{}; var lines = linesList{};
@ -215,15 +221,11 @@ const cardRow = struct {
const dw = DisplayWidth{ .data = &dwd }; const dw = DisplayWidth{ .data = &dwd };
for (cards, 0..) |cardObj, cardNo| { for (cards, 0..) |cardObj, cardNo| {
//const cardText = try card(cardObj);
const cardText = cardObj.lines; const cardText = cardObj.lines;
for (cardText, 0..) |line, idx| { for (cardText, 0..) |line, idx| {
//this step is probably unnecessary
const strippedLine = std.mem.trimRight(u8, line, " "); const strippedLine = std.mem.trimRight(u8, line, " ");
const paddedLine = try std.fmt.allocPrint(gpa.allocator(), lineFormatter, .{strippedLine}); const paddedLine = try std.fmt.allocPrint(gpa.allocator(), lineFormatter, .{strippedLine});
// const theoreticalLength = (paddedLine.len * 3) + 1;
assert(dw.strWidth(paddedLine) == dw.strWidth(spacer)); assert(dw.strWidth(paddedLine) == dw.strWidth(spacer));
// assert(theoreticalLength == pageWidth);
const placeholder = if (idx < lines.items(.first).len) lines.get(idx) else cardRow{}; const placeholder = if (idx < lines.items(.first).len) lines.get(idx) else cardRow{};
const new: cardRow = switch (cardNo) { const new: cardRow = switch (cardNo) {
0 => .{ .first = paddedLine }, 0 => .{ .first = paddedLine },
@ -244,9 +246,8 @@ const cardRow = struct {
} }
for (lines.items(.first), 0..) |_, idx| { for (lines.items(.first), 0..) |_, idx| {
const line = lines.get(idx); const line = lines.get(idx);
// const printedWidth = line + line.second.len + line.third.len + line.last.len;
// assert(printedWidth == pageWidth);
const fullLine = try std.mem.concat(gpa.allocator(), u8, &[_][]const u8{ line.first, line.second, line.third, line.last }); const fullLine = try std.mem.concat(gpa.allocator(), u8, &[_][]const u8{ line.first, line.second, line.third, line.last });
assert(dw.strWidth(fullLine) == dw.strWidth(fullWidthSpacer));
try allPrinted.append(fullLine); try allPrinted.append(fullLine);
} }
} }

Binary file not shown.