Skip to content

Commit b46e69e

Browse files
authored
Merge pull request #2 from dcodesdev/dev
Dev
2 parents 2842031 + 21349e3 commit b46e69e

4 files changed

Lines changed: 30 additions & 8 deletions

File tree

src/cli.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ use clap::Parser;
66
pub struct Opts {
77
#[clap(default_value = "./")]
88
pub path: String,
9+
10+
#[clap(short, long)]
11+
pub token: bool,
912
}

src/fs.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,26 @@ use std::fs;
22
use std::io::Read;
33
use std::path::Path;
44

5-
pub fn read_directory_contents(dir: &str) -> anyhow::Result<String> {
5+
pub fn read_directory_contents(dir: &Path) -> anyhow::Result<String> {
66
let mut combined_content = String::new();
7-
let base_path = Path::new(dir);
8-
read_directory_contents_recursive(base_path, base_path, &mut combined_content)?;
7+
8+
if dir.is_file() {
9+
read_file(dir, &mut combined_content)?;
10+
} else {
11+
read_directory_contents_recursive(dir, dir, &mut combined_content)?;
12+
}
13+
914
Ok(combined_content)
1015
}
1116

17+
pub fn read_file(path: &Path, content: &mut String) -> anyhow::Result<()> {
18+
let file_content = std::fs::read_to_string(path)?;
19+
20+
*content = file_content;
21+
22+
Ok(())
23+
}
24+
1225
fn read_directory_contents_recursive(
1326
base_path: &Path,
1427
current_path: &Path,

src/run.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
use clap::Parser;
2+
use std::path::Path;
23

3-
use crate::{cli, clip::copy_to_clipboard, fs::read_directory_contents};
4+
use crate::{cli, clip::copy_to_clipboard, fs::read_directory_contents, tiktoken::count_tokens};
45

56
pub fn run() -> anyhow::Result<()> {
67
let opts = cli::Opts::parse();
7-
let path = opts.path;
8+
let (path, token) = (opts.path, opts.token);
9+
10+
let path = Path::new(&path);
11+
let contents = read_directory_contents(path)?;
812

9-
let contents = read_directory_contents(&path)?;
1013
copy_to_clipboard(&contents)?;
1114

15+
if token {
16+
let tokens = count_tokens(&contents)?;
17+
println!("{} GPT-4 tokens.", tokens);
18+
}
19+
1220
Ok(())
1321
}

src/tiktoken.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use tiktoken_rs::o200k_base;
22

3-
// TODO: remove this dead code
4-
#[allow(dead_code)]
53
pub fn count_tokens(string: &str) -> anyhow::Result<usize> {
64
let bpe = o200k_base()?;
75
let tokens = bpe.encode_with_special_tokens(string);

0 commit comments

Comments
 (0)