post list subcommand

This commit is contained in:
Nathan McCarty 2025-02-08 19:19:17 -05:00
parent bbf7b4f6c8
commit b55a4f2b41

45
blog
View file

@ -6,6 +6,8 @@ use DB::BlogMeta;
use DB::MarkdownPost;
use DB::IdrisPost;
use Pretty::Table;
my %*SUB-MAIN-OPTS =
:named-anywhere,
:bundling,
@ -150,3 +152,46 @@ multi MAIN(
my $db = read-db $db-dir;
$db.render: $site-dir;
}
#| Provide a table of posts, in newest to oldest order
multi MAIN(
"post",
"list",
#| The path of the database directory
IO::Path(Str) :$db-dir = $default-db-dir,
#| The number of posts to show on a single page
Int :$per-page = 10;
#| The page number to show
Int :$page = 1;
) {
my $db = read-db $db-dir;
my @pages =
$db.sorted-posts.rotor($per-page, :partial);
my @page = @pages[$page - 1].flat;
my $table = Pretty::Table.new:
title => "Posts (page $page/{@pages.elems})",
field-names => ["ID", "Title", "Type"];
for @page -> $pair {
my $id = $pair.key;
my $post = $pair.value;
# TODO: Terminal aware truncation
my $title = do if $post.title.chars > 50 {
"{$post.title.substr(0, 50)}..."
} else {
$post.title
};
my $type = do given $post {
when MarkdownPost {
"md"
}
when IdrisPost {
"idr"
}
default {
""
}
}
$table.add-row: [$id, $title, $type];
}
say $table;
}