Skip to content

Commit 8069cc0

Browse files
committed
accepts a list of paths
1 parent bb6b4c2 commit 8069cc0

File tree

5 files changed

+27
-27
lines changed

5 files changed

+27
-27
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "clipcat"
3-
version = "0.1.4"
3+
version = "0.1.5"
44
edition = "2021"
55
description = "A command line tool for copying the contents to clipboard of multiple files in one go."
66
repository = "https://github.com/dcodesdev/clipcat"

src/cli.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clap::Parser;
55
#[command(version, about, long_about = None)]
66
pub struct Opts {
77
#[clap(default_value = "./")]
8-
pub path: String,
8+
pub path: Vec<String>,
99

1010
#[clap(short, long)]
1111
pub token: bool,

src/fs.rs

+16-19
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,30 @@ use std::fs;
22
use std::io::Read;
33
use std::path::Path;
44

5-
pub fn read_directory_contents(dir: &Path) -> anyhow::Result<String> {
5+
pub fn read_contents(path: &Path) -> anyhow::Result<String> {
66
let mut combined_content = String::new();
77

8-
if dir.is_file() {
9-
read_file(dir, &mut combined_content)?;
8+
if path.is_file() {
9+
read_file(path, &mut combined_content)?;
1010
} else {
11-
read_directory_contents_recursive(dir, dir, &mut combined_content)?;
11+
read_directory_contents_recursive(path, path, &mut combined_content)?;
1212
}
1313

1414
Ok(combined_content)
1515
}
1616

17-
pub fn read_file(path: &Path, content: &mut String) -> anyhow::Result<()> {
18-
let file_content = std::fs::read_to_string(path)?;
17+
pub fn read_file(path: &Path, combined_content: &mut String) -> anyhow::Result<()> {
18+
combined_content.push_str(&format!("File: {}\n", path.display()));
1919

20-
*content = file_content;
20+
let mut file_content = String::new();
21+
let mut file = fs::File::open(&path)?;
22+
23+
if let Err(_) = file.read_to_string(&mut file_content) {
24+
return Ok(());
25+
}
26+
27+
combined_content.push_str(&file_content);
28+
combined_content.push('\n');
2129

2230
Ok(())
2331
}
@@ -32,18 +40,7 @@ fn read_directory_contents_recursive(
3240
let path = entry.path();
3341

3442
if path.is_file() {
35-
let relative_path = path.strip_prefix(base_path).unwrap().to_string_lossy();
36-
combined_content.push_str(&format!("File: {}\n", relative_path));
37-
38-
let mut file_content = String::new();
39-
let mut file = fs::File::open(&path)?;
40-
41-
if let Err(_) = file.read_to_string(&mut file_content) {
42-
return Ok(());
43-
}
44-
45-
combined_content.push_str(&file_content);
46-
combined_content.push('\n');
43+
read_file(&path, combined_content)?;
4744
} else if path.is_dir() {
4845
read_directory_contents_recursive(base_path, &path, combined_content)?;
4946
}

src/run.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ use clap::Parser;
22
use std::path::Path;
33

44
use crate::{
5-
cli, clip::copy_to_clipboard, fs::read_directory_contents, num::format_number,
6-
tiktoken::count_tokens,
5+
cli, clip::copy_to_clipboard, fs::read_contents, num::format_number, tiktoken::count_tokens,
76
};
87

98
pub fn run() -> anyhow::Result<()> {
109
let opts = cli::Opts::parse();
11-
let (path, token) = (opts.path, opts.token);
10+
let (files, token) = (opts.path, opts.token);
1211

13-
let path = Path::new(&path);
14-
let contents = read_directory_contents(path)?;
12+
let mut contents = String::new();
13+
for path in files.iter() {
14+
let path = Path::new(&path);
15+
let content = read_contents(&path)?;
16+
contents.push_str(&content);
17+
}
1518

1619
copy_to_clipboard(&contents)?;
1720

0 commit comments

Comments
 (0)