Skip to content

Instantly share code, notes, and snippets.

@ddrscott
Created January 30, 2025 20:10
Show Gist options
  • Save ddrscott/fbb0b2c0d973a1d373c91d530472b862 to your computer and use it in GitHub Desktop.
Save ddrscott/fbb0b2c0d973a1d373c91d530472b862 to your computer and use it in GitHub Desktop.
Simple bash and Rust functions to help creating LLM prompts
use std::env;
use std::fs::{self, File};
use std::io::{BufRead, BufReader};
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
eprintln!("Usage: fence FILE...");
return;
}
println!("Help me with the following files:");
for file in &args[1..] {
match fs::metadata(file) {
Ok(metadata) => {
if metadata.is_file() {
let full_path = env::current_dir().expect("Failed to get current directory").join(file);
// Extract filename here and store it
let filename = full_path.file_name().unwrap_or_default();
println!(
"
File: `{}`:
```
{}
```",
filename.to_string_lossy(),
file_contents(&full_path).unwrap_or_else(|_| "".to_string())
);
}
},
Err(_) => continue,
}
}
}
fn file_contents(path: &std::path::Path) -> Result<String, std::io::Error> {
let file = File::open(path)?;
let reader = BufReader::new(file);
let mut content = String::new();
for line in reader.lines() {
if let Ok(l) = line {
content.push_str(&l);
content.push('\n'); // add newline to mimic cat's behavior
}
}
Ok(content.trim().to_string())
}
fence () {
echo "Help me with the following files:"
for file in "$@"
do
if [ -f "$file" ]
then
echo -e "
File: \`$(basename "$file")\`:
\`\`\`
$(cat "$file")
\`\`\`
"
fi
done
}
@ddrscott
Copy link
Author

Usage:

. functions.sh

fence foo.txt bar.txt

or

./target/release/fence foo.txt bar.txt

Should output:

Help me with the following files:

File: `foo.txt`:
```
Contents of foo.txt
```

File: `bar.txt`:
```
Contents of bar.txt
```

Then you can do stuff like:

fence foo.txt bar.txt | pbcopy

Which fills in the copy/paste buffer so you're ready to paste into your favorite GPT Chatbox.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment