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
10 changes: 7 additions & 3 deletions src/cli/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use clap::CommandFactory;
use clap_complete::generate;

use crate::cli::output::{
self, ByteLocation, HealOutput, HealUpdate, OutputMode, short_id, write_bookmark_markdown,
write_heal_output, write_json_success, write_success,
self, ByteLocation, CollectionWithCount, HealOutput, HealUpdate, OutputMode, short_id,
write_bookmark_markdown, write_heal_output, write_json_success, write_success,
};
use crate::cli::*;
use crate::config::Config;
Expand Down Expand Up @@ -2226,7 +2226,11 @@ fn handle_collection_list(cli: &Cli, mode: &OutputMode, args: &CollectionListArg
}

match mode {
OutputMode::Json => write_json_success(&collections)?,
OutputMode::Json => {
let with_counts: Vec<CollectionWithCount> =
collections.iter().map(CollectionWithCount::from).collect();
write_json_success(&with_counts)?
}
OutputMode::Table => {
let mut table = comfy_table::Table::new();
table.set_header(vec!["Name", "Bookmarks", "Description", "Created"]);
Expand Down
43 changes: 42 additions & 1 deletion src/cli/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,48 @@ use comfy_table::{Cell, Table};
use is_terminal::IsTerminal;
use serde::Serialize;

use crate::engine::bookmark::{Bookmark, Resolution};
use crate::engine::bookmark::{Bookmark, Collection, Resolution};

// --- Collection output formatting ---

/// A collection with its associated bookmark count for list output.
#[derive(Debug, Serialize)]
pub struct CollectionWithCount {
pub id: String,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub created_at: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub created_by: Option<String>,
pub bookmark_count: usize,
}

impl From<&(Collection, usize)> for CollectionWithCount {
fn from((collection, count): &(Collection, usize)) -> Self {
Self {
id: collection.id.clone(),
name: collection.name.clone(),
description: collection.description.clone(),
created_at: collection.created_at.clone(),
created_by: collection.created_by.clone(),
bookmark_count: *count,
}
}
}

impl From<(Collection, usize)> for CollectionWithCount {
fn from((collection, count): (Collection, usize)) -> Self {
Self {
id: collection.id,
name: collection.name,
description: collection.description,
created_at: collection.created_at,
created_by: collection.created_by,
bookmark_count: count,
}
}
}

/// Helper function to get the first annotation's notes from a bookmark.
fn get_first_note(bm: &Bookmark) -> &str {
Expand Down
Loading