Skip to content

Commit

Permalink
add support for verbose option
Browse files Browse the repository at this point in the history
  • Loading branch information
seankim658 committed Jul 19, 2024
1 parent 93a51cb commit e6d6cbb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
46 changes: 40 additions & 6 deletions src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub fn parse_comma_delim_patterns(patterns: &Option<String>) -> Vec<String> {
/// tree.
/// - `no_codeblock`: Whether to wrap the code in markdown code blocks.
/// - `gitignore`: Whether or not to respect the gitignore file.
/// - `verbose`: Whether to print the glob pattern matching for investigation.
///
/// ### Returns
///
Expand All @@ -59,6 +60,7 @@ pub fn traverse_directory(
exclude_from_tree: bool,
no_codeblock: bool,
gitignore: bool,
verbose: bool,
) -> Result<(String, Vec<serde_json::Value>)> {
// Will hold the files found in the traversal.
let mut files = Vec::new();
Expand All @@ -68,6 +70,13 @@ pub fn traverse_directory(
let canonical_root_path = root.canonicalize()?;
let parent_dir = basename(&canonical_root_path);

if verbose {
println!(
"Include patterns: {:?}\nExclude patterns: {:?}",
include, exclude
);
}

// Walk through the directory tree.
// Initialize a WalkBuilder with the canonical root path (by default, respects .gitignore).
let tree = WalkBuilder::new(&canonical_root_path)
Expand All @@ -89,7 +98,7 @@ pub fn traverse_directory(
// Check if the file should be excluded from the tree based on the
// exclude patterns and exclude_from_tree arguments. Break the path component
// loop if it any part of the path should be excluded.
if exclude_from_tree && !include_file(path, include, exclude, include_priority)
if exclude_from_tree && !include_file(path, include, exclude, include_priority, verbose)
{
break;
}
Expand All @@ -111,7 +120,7 @@ pub fn traverse_directory(
};
}

if path.is_file() && include_file(path, include, exclude, include_priority) {
if path.is_file() && include_file(path, include, exclude, include_priority, verbose) {
// Read in the file contents into bytes.
if let Ok(file_bytes) = fs::read(path) {
let code_string = String::from_utf8_lossy(&file_bytes);
Expand Down Expand Up @@ -208,6 +217,7 @@ fn include_file(
include: &[String],
exclude: &[String],
include_priority: bool,
verbose: bool,
) -> bool {
let canonical_root_path = match fs::canonicalize(path) {
Ok(path) => path,
Expand All @@ -218,6 +228,10 @@ fn include_file(
};
let path_string = canonical_root_path.to_str().unwrap();

if verbose {
println!("=> Target path: {}", path_string);
}

// Check the glob patterns.
let include_bool = include
.iter()
Expand All @@ -228,10 +242,30 @@ fn include_file(

// Determine if the file should be included.
let result = match (include_bool, exclude_bool) {
(true, true) => include_priority,
(true, false) => true,
(false, true) => false,
(false, false) => include.is_empty(),
(true, true) => {
if verbose {
println!("\tInclude due to include priority: {}", include_priority);
}
include_priority
}
(true, false) => {
if verbose {
println!("\tInclude: true");
}
true
}
(false, true) => {
if verbose {
println!("\tInclude: false");
}
false
}
(false, false) => {
if verbose {
println!("\tNot in either condition, fallback: {}", include.is_empty());
}
include.is_empty()
}
};

result
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ struct Args {
/// Fetch a specific Github issue for the repository.
#[arg(long)]
issue: Option<u32>,

/// Run in verbose mode to investigate glob pattern matching.
#[arg(long, action(ArgAction::SetTrue))]
verbose: bool
}

/// Main entry point for the codeprompt application.
Expand Down Expand Up @@ -125,6 +129,7 @@ async fn main() -> Result<(), Error> {
args.exclude_from_tree,
args.no_codeblock,
args.gitignore,
args.verbose,
);

let (tree, files) = match tree_data {
Expand Down

0 comments on commit e6d6cbb

Please sign in to comment.