Created
January 30, 2025 20:10
-
-
Save ddrscott/fbb0b2c0d973a1d373c91d530472b862 to your computer and use it in GitHub Desktop.
Simple bash and Rust functions to help creating LLM prompts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
. functions.sh fence foo.txt bar.txtor
Should output:
Then you can do stuff like:
fence foo.txt bar.txt | pbcopyWhich fills in the copy/paste buffer so you're ready to paste into your favorite GPT Chatbox.