Skip to content

Commit 4e5cdf7

Browse files
authored
handle no clipboard more gracefully, prints to stdout (#30)
1 parent 89f30d2 commit 4e5cdf7

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

src/main.rs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
use anyhow::{Context, Result};
77
use clap::Parser;
88
use code2prompt::{
9-
copy_to_clipboard, get_model_info, get_tokenizer, get_git_diff, get_git_diff_between_branches, get_git_log,
10-
handle_undefined_variables, handlebars_setup, label, render_template, traverse_directory, write_to_file,
9+
copy_to_clipboard, get_git_diff, get_git_diff_between_branches, get_git_log, get_model_info,
10+
get_tokenizer, handle_undefined_variables, handlebars_setup, label, render_template,
11+
traverse_directory, write_to_file,
1112
};
1213
use colored::*;
1314
use indicatif::{ProgressBar, ProgressStyle};
@@ -41,7 +42,7 @@ struct Cli {
4142

4243
/// Exclude files/folders from the source tree based on exclude patterns
4344
#[clap(long)]
44-
exclude_from_tree: bool,
45+
exclude_from_tree: bool,
4546

4647
/// Display the token count of the generated prompt
4748
#[clap(long)]
@@ -91,7 +92,7 @@ struct Cli {
9192

9293
/// Print output as JSON
9394
#[clap(long)]
94-
json: bool,
95+
json: bool,
9596
}
9697

9798
fn main() -> Result<()> {
@@ -153,7 +154,8 @@ fn main() -> Result<()> {
153154
error!("Please provide exactly two branches separated by a comma.");
154155
std::process::exit(1);
155156
}
156-
git_diff_branch = get_git_diff_between_branches(&args.path, &branches[0], &branches[1]).unwrap_or_default()
157+
git_diff_branch = get_git_diff_between_branches(&args.path, &branches[0], &branches[1])
158+
.unwrap_or_default()
157159
}
158160

159161
// git diff two get_git_diff_between_branches
@@ -169,7 +171,7 @@ fn main() -> Result<()> {
169171
}
170172

171173
spinner.finish_with_message("Done!".green().to_string());
172-
174+
173175
// Prepare JSON Data
174176
let mut data = json!({
175177
"absolute_code_path": label(&args.path),
@@ -199,8 +201,13 @@ fn main() -> Result<()> {
199201
0
200202
};
201203

202-
let paths: Vec<String> = files.iter()
203-
.filter_map(|file| file.get("path").and_then(|p| p.as_str()).map(|s| s.to_string()))
204+
let paths: Vec<String> = files
205+
.iter()
206+
.filter_map(|file| {
207+
file.get("path")
208+
.and_then(|p| p.as_str())
209+
.map(|s| s.to_string())
210+
})
204211
.collect();
205212

206213
let model_info = get_model_info(&args.encoding);
@@ -230,7 +237,27 @@ fn main() -> Result<()> {
230237

231238
// Copy to Clipboard
232239
if !args.no_clipboard {
233-
copy_to_clipboard(&rendered)?;
240+
match copy_to_clipboard(&rendered) {
241+
Ok(_) => {
242+
println!(
243+
"{}{}{} {}",
244+
"[".bold().white(),
245+
"✓".bold().green(),
246+
"]".bold().white(),
247+
"Copied to clipboard successfully.".green()
248+
);
249+
}
250+
Err(e) => {
251+
eprintln!(
252+
"{}{}{} {}",
253+
"[".bold().white(),
254+
"!".bold().red(),
255+
"]".bold().white(),
256+
format!("Failed to copy to clipboard: {}", e).red()
257+
);
258+
println!("{}", &rendered);
259+
}
260+
}
234261
}
235262

236263
// Output File

src/template.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,15 @@ pub fn handle_undefined_variables(
116116
///
117117
/// * `Result<()>` - An empty result indicating success or an error.
118118
pub fn copy_to_clipboard(rendered: &str) -> Result<()> {
119-
let mut clipboard = Clipboard::new().expect("Failed to initialize clipboard");
120-
clipboard
121-
.set_text(rendered.to_string())
122-
.context("Failed to copy to clipboard")?;
123-
println!(
124-
"{}{}{} {}",
125-
"[".bold().white(),
126-
"✓".bold().green(),
127-
"]".bold().white(),
128-
"Prompt copied to clipboard!".green()
129-
);
130-
Ok(())
119+
match Clipboard::new() {
120+
Ok(mut clipboard) => {
121+
clipboard
122+
.set_text(rendered.to_string())
123+
.context("Failed to copy to clipboard")?;
124+
Ok(())
125+
}
126+
Err(e) => Err(anyhow::anyhow!("Failed to initialize clipboard: {}", e)),
127+
}
131128
}
132129

133130
/// Writes the rendered template to a specified output file.

0 commit comments

Comments
 (0)