Skip to content
Open
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
14 changes: 13 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ moka = { version = "0.12", features = ["sync"] }
# Async stream utilities
futures-util = "0.3"

# Terminal tables — `buzz --format table` human-friendly list rendering (buzz-cli)
comfy-table = "7"

# WebSocket client (test client)
tokio-tungstenite = { version = "0.29", features = ["rustls-tls-webpki-roots"] }
url = "2"
Expand Down
3 changes: 3 additions & 0 deletions crates/buzz-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ rustls = { version = "0.23", default-features = false, features = ["ring", "std"
# Random number generation — full jitter for exponential backoff in with_retry
rand = { workspace = true }

# Terminal tables — renders list output for the global `--format table` flag
comfy-table = { workspace = true }

[dev-dependencies]
# Scratch files for channel-templates.json fixtures in tests
tempfile = "3"
Expand Down
27 changes: 17 additions & 10 deletions crates/buzz-cli/src/commands/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,30 @@ pub async fn cmd_list_channels(
.collect();
let output = match format {
crate::OutputFormat::Compact => {
let compact: Vec<serde_json::Value> = channels
.iter()
.map(|c| {
serde_json::json!({
"channel_id": c.get("channel_id").cloned().unwrap_or_default(),
"name": c.get("name").cloned().unwrap_or_default(),
})
})
.collect();
serde_json::to_string(&compact).unwrap_or_default()
serde_json::to_string(&compact_channels(&channels)).unwrap_or_default()
}
crate::OutputFormat::Table => {
crate::output::render_rows(&["channel_id", "name"], &compact_channels(&channels))
}
crate::OutputFormat::Json => serde_json::to_string(&channels).unwrap_or_default(),
};
println!("{output}");
Ok(())
}

/// Reduced channel fields shared by `--format compact` and `--format table`.
fn compact_channels(channels: &[serde_json::Value]) -> Vec<serde_json::Value> {
channels
.iter()
.map(|c| {
serde_json::json!({
"channel_id": c.get("channel_id").cloned().unwrap_or_default(),
"name": c.get("name").cloned().unwrap_or_default(),
})
})
.collect()
}

/// Search channels by human-readable name (kind:39000 group metadata).
///
/// The relay's access control already filters out channels the caller can't see
Expand Down
31 changes: 18 additions & 13 deletions crates/buzz-cli/src/commands/feed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,31 @@ pub async fn cmd_get_feed(
let normalized = normalize_events(&events);
let output = match format {
crate::OutputFormat::Compact => {
let evts: Vec<serde_json::Value> =
serde_json::from_str(&normalized).unwrap_or_default();
let compact: Vec<serde_json::Value> = evts
.iter()
.map(|e| {
serde_json::json!({
"id": e.get("id").cloned().unwrap_or_default(),
"content": e.get("content").cloned().unwrap_or_default(),
"created_at": e.get("created_at").cloned().unwrap_or_default(),
})
})
.collect();
serde_json::to_string(&compact).unwrap_or_default()
serde_json::to_string(&compact_feed(&normalized)).unwrap_or_default()
}
crate::OutputFormat::Table => {
crate::output::render_rows(&["id", "content", "created_at"], &compact_feed(&normalized))
}
crate::OutputFormat::Json => normalized,
};
println!("{output}");
Ok(())
}

/// Reduced feed fields shared by `--format compact` and `--format table`.
fn compact_feed(normalized: &str) -> Vec<serde_json::Value> {
let evts: Vec<serde_json::Value> = serde_json::from_str(normalized).unwrap_or_default();
evts.iter()
.map(|e| {
serde_json::json!({
"id": e.get("id").cloned().unwrap_or_default(),
"content": e.get("content").cloned().unwrap_or_default(),
"created_at": e.get("created_at").cloned().unwrap_or_default(),
})
})
.collect()
}

pub async fn dispatch(
cmd: crate::FeedCmd,
client: &BuzzClient,
Expand Down
33 changes: 20 additions & 13 deletions crates/buzz-cli/src/commands/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,24 +242,31 @@ fn parse_member_pubkeys(event: &serde_json::Value) -> Vec<String> {
fn format_events(normalized: &str, format: &crate::OutputFormat) -> String {
match format {
crate::OutputFormat::Compact => {
let events: Vec<serde_json::Value> =
serde_json::from_str(normalized).unwrap_or_default();
let compact: Vec<serde_json::Value> = events
.iter()
.map(|e| {
serde_json::json!({
"id": e.get("id").cloned().unwrap_or_default(),
"content": e.get("content").cloned().unwrap_or_default(),
"created_at": e.get("created_at").cloned().unwrap_or_default(),
})
})
.collect();
serde_json::to_string(&compact).unwrap_or_default()
serde_json::to_string(&compact_events(normalized)).unwrap_or_default()
}
crate::OutputFormat::Table => crate::output::render_rows(
&["id", "content", "created_at"],
&compact_events(normalized),
),
crate::OutputFormat::Json => normalized.to_string(),
}
}

/// Reduced message fields shared by `--format compact` and `--format table`.
fn compact_events(normalized: &str) -> Vec<serde_json::Value> {
let events: Vec<serde_json::Value> = serde_json::from_str(normalized).unwrap_or_default();
events
.iter()
.map(|e| {
serde_json::json!({
"id": e.get("id").cloned().unwrap_or_default(),
"content": e.get("content").cloned().unwrap_or_default(),
"created_at": e.get("created_at").cloned().unwrap_or_default(),
})
})
.collect()
}

pub async fn cmd_get_messages(
client: &BuzzClient,
channel_id: &str,
Expand Down
41 changes: 25 additions & 16 deletions crates/buzz-cli/src/commands/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,34 @@ pub async fn cmd_get_users(
.collect();
let output = match format {
crate::OutputFormat::Compact => {
let compact: Vec<serde_json::Value> = profiles
.iter()
.map(|p| serde_json::json!({
"pubkey": p.get("pubkey").cloned().unwrap_or_default(),
"display_name": p.get("display_name").or_else(|| p.get("name")).cloned().unwrap_or_default(),
}))
.collect();
serde_json::to_string(&compact).unwrap_or_default()
serde_json::to_string(&compact_profiles(&profiles)).unwrap_or_default()
}
crate::OutputFormat::Table => {
crate::output::render_rows(&["pubkey", "display_name"], &compact_profiles(&profiles))
}
crate::OutputFormat::Json => serde_json::to_string(&profiles).unwrap_or_default(),
};
println!("{output}");
Ok(())
}

/// Reduced user fields shared by `--format compact` and `--format table`.
fn compact_profiles(profiles: &[serde_json::Value]) -> Vec<serde_json::Value> {
profiles
.iter()
.map(|p| {
serde_json::json!({
"pubkey": p.get("pubkey").cloned().unwrap_or_default(),
"display_name": p
.get("display_name")
.or_else(|| p.get("name"))
.cloned()
.unwrap_or_default(),
})
})
.collect()
}

/// Search for users by display name via NIP-50 full-text search on kind:0 profiles.
/// Returns [] if the relay does not implement NIP-50 search.
async fn search_by_name(
Expand Down Expand Up @@ -132,14 +145,10 @@ async fn search_by_name(
.collect();
let output = match format {
crate::OutputFormat::Compact => {
let compact: Vec<serde_json::Value> = profiles
.iter()
.map(|p| serde_json::json!({
"pubkey": p.get("pubkey").cloned().unwrap_or_default(),
"display_name": p.get("display_name").or_else(|| p.get("name")).cloned().unwrap_or_default(),
}))
.collect();
serde_json::to_string(&compact).unwrap_or_default()
serde_json::to_string(&compact_profiles(&profiles)).unwrap_or_default()
}
crate::OutputFormat::Table => {
crate::output::render_rows(&["pubkey", "display_name"], &compact_profiles(&profiles))
}
crate::OutputFormat::Json => serde_json::to_string(&profiles).unwrap_or_default(),
};
Expand Down
8 changes: 7 additions & 1 deletion crates/buzz-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod agent_management;
mod client;
mod commands;
mod error;
mod output;
mod validate;

use clap::{Parser, Subcommand};
Expand Down Expand Up @@ -89,7 +90,9 @@ struct Cli {
#[arg(long, env = "BUZZ_AUTH_TAG")]
auth_tag: Option<String>,

/// Output format: 'json' (default, full fields) or 'compact' (reduced fields).
/// Output format: 'json' (default, full fields), 'compact' (reduced
/// fields), or 'table' (human-friendly terminal table of the reduced
/// fields).
#[arg(long, value_enum, default_value = "json")]
format: OutputFormat,

Expand Down Expand Up @@ -169,6 +172,9 @@ pub enum OutputFormat {
/// Reduced fields for agent scanning
#[value(name = "compact")]
Compact,
/// Human-friendly terminal table of the reduced (compact) fields
#[value(name = "table")]
Table,
}

#[derive(Subcommand)]
Expand Down
Loading