From f8af0fad7f206f029869cda7b4a1846b19aee423 Mon Sep 17 00:00:00 2001 From: Dipam Vasani Date: Sun, 7 Apr 2024 22:27:24 -0700 Subject: [PATCH] Add ability to ignore patterns directly from cli (#4) * Add ability to ignore patterns directly from cli * Updated for latest changes to main --------- Co-authored-by: Simon Willison --- files_to_prompt/cli.py | 23 ++++++++++++++++++++--- tests/test_files_to_prompt.py | 25 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/files_to_prompt/cli.py b/files_to_prompt/cli.py index 2e84f83..38c8712 100644 --- a/files_to_prompt/cli.py +++ b/files_to_prompt/cli.py @@ -23,7 +23,9 @@ def read_gitignore(path): return [] -def process_path(path, include_hidden, ignore_gitignore, gitignore_rules): +def process_path( + path, include_hidden, ignore_gitignore, gitignore_rules, ignore_patterns +): if os.path.isfile(path): with open(path, "r") as f: file_contents = f.read() @@ -51,6 +53,13 @@ def process_path(path, include_hidden, ignore_gitignore, gitignore_rules): if not should_ignore(os.path.join(root, f), gitignore_rules) ] + if ignore_patterns: + files = [ + f + for f in files + if not any(fnmatch(f, pattern) for pattern in ignore_patterns) + ] + for file in files: file_path = os.path.join(root, file) with open(file_path, "r") as f: @@ -75,8 +84,14 @@ def process_path(path, include_hidden, ignore_gitignore, gitignore_rules): is_flag=True, help="Ignore .gitignore files and include all files", ) +@click.option( + "--ignore-patterns", + multiple=True, + default=[], + help="List of patterns to ignore", +) @click.version_option() -def cli(paths, include_hidden, ignore_gitignore): +def cli(paths, include_hidden, ignore_gitignore, ignore_patterns): """ Takes one or more paths to files or directories and outputs every file, recursively, each one preceded with its filename like this: @@ -96,4 +111,6 @@ def cli(paths, include_hidden, ignore_gitignore): raise click.BadArgumentUsage(f"Path does not exist: {path}") if not ignore_gitignore: gitignore_rules.extend(read_gitignore(os.path.dirname(path))) - process_path(path, include_hidden, ignore_gitignore, gitignore_rules) + process_path( + path, include_hidden, ignore_gitignore, gitignore_rules, ignore_patterns + ) diff --git a/tests/test_files_to_prompt.py b/tests/test_files_to_prompt.py index d0e200f..3bf8521 100644 --- a/tests/test_files_to_prompt.py +++ b/tests/test_files_to_prompt.py @@ -82,6 +82,31 @@ def test_multiple_paths(tmpdir): assert "Contents of single file" in result.output +def test_ignore_patterns(tmpdir): + runner = CliRunner() + with tmpdir.as_cwd(): + os.makedirs("test_dir") + with open("test_dir/file_to_ignore.txt", "w") as f: + f.write("This file should be ignored due to ignore patterns") + with open("test_dir/file_to_include.txt", "w") as f: + f.write("This file should be included") + + result = runner.invoke(cli, ["test_dir", "--ignore-patterns", "*.txt"]) + assert result.exit_code == 0 + assert "test_dir/file_to_ignore.txt" not in result.output + assert "This file should be ignored due to ignore patterns" not in result.output + assert "test_dir/file_to_include.txt" not in result.output + + result = runner.invoke( + cli, ["test_dir", "--ignore-patterns", "file_to_ignore.*"] + ) + assert result.exit_code == 0 + assert "test_dir/file_to_ignore.txt" not in result.output + assert "This file should be ignored due to ignore patterns" not in result.output + assert "test_dir/file_to_include.txt" in result.output + assert "This file should be included" in result.output + + def test_mixed_paths_with_options(tmpdir): runner = CliRunner() with tmpdir.as_cwd():