ggcat/
lib.rs

1use std::{fs, process};
2
3use arboard::Clipboard;
4
5fn copy_to_clipboard(contents: &str) {
6    let mut clipboard = Clipboard::new().unwrap();
7    clipboard.set_text(contents).unwrap();
8}
9
10pub fn run(mut args: impl Iterator<Item = String>) {
11    args.next();
12
13    let filepath = args.next().unwrap_or_else(|| {
14        println!("ggcat; A simple tool to read text file contents to clipboard\n\n Usage: ggcat <filepath>");
15        process::exit(1);
16    });
17
18    let contents = fs::read_to_string(filepath)
19        .expect("Unable to read file!! Make sure the filepath is valid");
20
21    copy_to_clipboard(&contents);
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27
28    #[test]
29    fn test_run() {
30        let mut clipboard = Clipboard::new().unwrap();
31        clipboard.clear().unwrap();
32
33        let args = vec!["ggcat".to_string(), "test/1.txt".to_string()].into_iter();
34        run(args);
35        let clipboard_contents = clipboard.get_text().unwrap();
36        assert_eq!(clipboard_contents, "This is one");
37
38        let args = vec!["ggcat".to_string(), "test/2.txt".to_string()].into_iter();
39        run(args);
40        let clipboard_contents = clipboard.get_text().unwrap();
41        assert_eq!(clipboard_contents, "This is two");
42    }
43}