From 3345b20f1ffd934a575dd0ad1540af8c2a6d907a Mon Sep 17 00:00:00 2001 From: mitteneer Date: Tue, 29 Apr 2025 00:38:20 -0400 Subject: [PATCH] Make an ordinal formatting funcrion I am hungry --- src/app/views/artists.zig | 7 ++++++- src/app/views/artists/get.zmpl | 2 +- src/ordinal_fmt.zig | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 src/ordinal_fmt.zig diff --git a/src/app/views/artists.zig b/src/app/views/artists.zig index 9dacc08..728ee2c 100644 --- a/src/app/views/artists.zig +++ b/src/app/views/artists.zig @@ -2,6 +2,7 @@ const std = @import("std"); const jetzig = @import("jetzig"); const jetquery = @import("jetzig").jetquery; const dateFmt = @import("../../date_fmt.zig").dateFmt; +const ordinalFmt = @import("../../ordinal_fmt.zig").ordinalFmt; pub fn index(request: *jetzig.Request) !jetzig.View { var root = try request.data(.object); @@ -57,7 +58,11 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View { if (try artist_jq_result.postgresql.result.next()) |artist_row| { const artist = try artist_row.to(ArtistResult, .{ .dupe = true, .allocator = request.allocator }); - try root.put("artist", artist); + var artist_view = try root.put("artist", .object); + try artist_view.put("name", artist.name); + try artist_view.put("id", artist.id); + try artist_view.put("scrobbles", artist.scrobbles); + try artist_view.put("rank", ordinalFmt(request.allocator, artist.rank)); } try artist_jq_result.drain(); diff --git a/src/app/views/artists/get.zmpl b/src/app/views/artists/get.zmpl index c272f79..adf6158 100644 --- a/src/app/views/artists/get.zmpl +++ b/src/app/views/artists/get.zmpl @@ -6,7 +6,7 @@ @partial partials/header

{{.artist.name}}

-{{.artist.scrobbles}} scrobbles ({{.artist.rank}}th place) +{{.artist.scrobbles}} scrobbles ({{.artist.rank}} place)
First listen: {{.first_song_name}} ({{.first_song_date}})
diff --git a/src/ordinal_fmt.zig b/src/ordinal_fmt.zig new file mode 100644 index 0000000..6b126ab --- /dev/null +++ b/src/ordinal_fmt.zig @@ -0,0 +1,16 @@ +const std = @import("std"); +//const log = std.math.log10; + +pub fn ordinalFmt(allocator: std.mem.Allocator, ord: isize) ![]const u8 { + const buff = try allocator.alloc(u8, 3 + @as(usize, @intFromFloat(@floor(@log10(@as(f64, @floatFromInt(ord))))))); + const ind: []const u8 = switch (@mod(ord, 100)) { + 11, 12, 13 => "th", + else => switch (@mod(ord, 10)) { + 1 => "st", + 2 => "nd", + 3 => "rd", + else => "th", + }, + }; + return std.fmt.bufPrint(buff, "{}{s}", .{ ord, ind }); +}