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
2 changes: 1 addition & 1 deletion crates/jp_cli/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Commands {
Commands::Conversation(args) => args.run(ctx, handles).await,
Commands::Attachment(args) => {
debug_assert!(handles.is_empty(), "Attachment commands don't use handles");
args.run(ctx)
args.run(ctx).await
}
Commands::AttachmentAdd(args) => {
debug_assert!(handles.is_empty(), "Attachment commands don't use handles");
Expand Down
10 changes: 8 additions & 2 deletions crates/jp_cli/src/cmd/attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{

pub(super) mod add;
mod ls;
mod print;
mod rm;

#[derive(Debug, clap::Args)]
Expand All @@ -26,11 +27,12 @@ pub(crate) struct Attachment {
}

impl Attachment {
pub(crate) fn run(self, ctx: &mut Ctx) -> Output {
pub(crate) async fn run(self, ctx: &mut Ctx) -> Output {
match self.command {
Commands::Add(args) => args.run(ctx),
Commands::Remove(args) => args.run(ctx),
Commands::List(args) => args.run(ctx),
Commands::Print(args) => args.run(ctx).await,
}
}
}
Expand All @@ -45,7 +47,7 @@ impl IntoPartialAppConfig for Attachment {
match &self.command {
Commands::Add(args) => args.apply_cli_config(workspace, partial, merged_config),
Commands::Remove(args) => args.apply_cli_config(workspace, partial, merged_config),
Commands::List(_) => Ok(partial),
Commands::List(_) | Commands::Print(_) => Ok(partial),
}
}
}
Expand All @@ -63,6 +65,10 @@ enum Commands {
/// List attachments in context.
#[command(name = "ls", alias = "l")]
List(ls::Ls),

/// Preview how an attachment will render for the LLM.
#[command(name = "print", alias = "p")]
Print(print::Print),
}

pub(crate) fn validate_attachment(uri: &Url) -> Result<()> {
Expand Down
34 changes: 34 additions & 0 deletions crates/jp_cli/src/cmd/attachment/print.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use super::register_attachment;
use crate::{cmd::Output, ctx::Ctx, parser::AttachmentUrlOrPath};

#[derive(Debug, clap::Args)]
#[command(arg_required_else_help(true))]
pub(crate) struct Print {
/// The attachment URL to preview.
attachment: AttachmentUrlOrPath,
}

impl Print {
pub(crate) async fn run(self, ctx: &mut Ctx) -> Output {
let uri = self.attachment.parse(Some(ctx.workspace.root()))?;
let attachments = register_attachment(ctx, uri).await?;

for (idx, attachment) in attachments.iter().enumerate() {
if idx > 0 {
ctx.printer.println("");
}

match attachment.as_text() {
Some(text) => ctx.printer.println(text),
None => {
ctx.printer.eprintln(format!(
"Attachment '{}' is binary and cannot be previewed.",
attachment.source
));
}
}
}

Ok(())
}
}
Loading