diff --git a/README.md b/README.md index 58fc504..dcf6d83 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,7 @@ Examples: | `--include-git` | Include .git directory in the analysis | | `--max-size` | Maximum allowed text content size in KB (default: 10240 KB) | | `--copy-to-clipboard` | Copy the output to clipboard | +| `--no-input` | Surpress all user prompts and default answers to "y" (yes). Useful if you want to use this app as script in your pipelines, e.g. as Github Actions | ## Ignore Functionality diff --git a/codebase_digest/app.py b/codebase_digest/app.py index 4d7ef51..3f801e7 100644 --- a/codebase_digest/app.py +++ b/codebase_digest/app.py @@ -12,6 +12,7 @@ import pyperclip import xml.etree.ElementTree as ET import html +from input_handler import InputHandler # Initialize colorama for colorful console output. init() @@ -371,6 +372,7 @@ def main(): help="Maximum allowed text content size in KB (default: 10240 KB)") parser.add_argument("--copy-to-clipboard", action="store_true", help="Copy the output to clipboard after analysis") + parser.add_argument("--no-input", action="store_true", help="Run the script without any user input") if len(sys.argv) == 1: parser.print_help(sys.stderr) @@ -378,6 +380,8 @@ def main(): args = parser.parse_args() + input_handler = InputHandler(no_input=args.no_input) + if not args.path: print(Fore.RED + "Error: Path argument is required." + Style.RESET_ALL) parser.print_help(sys.stderr) @@ -400,7 +404,7 @@ def main(): if estimated_size / 1024 > args.max_size: print(Fore.YELLOW + f"\nWarning: The estimated output size ({estimated_size / 1024:.2f} KB) exceeds the maximum allowed size ({args.max_size} KB)." + Style.RESET_ALL) - proceed = input("Do you want to proceed? (y/n): ").lower().strip() + proceed = input_handler.get_input("Do you want to proceed? (y/n): ") if proceed != 'y': print(Fore.YELLOW + "Analysis aborted." + Style.RESET_ALL) sys.exit(0) @@ -459,7 +463,7 @@ def main(): except Exception as e: print(Fore.RED + f"Failed to copy to clipboard: {str(e)}" + Style.RESET_ALL) else: - copy_to_clipboard = input("Do you want to copy the output to clipboard? (y/n): ").lower().strip() + copy_to_clipboard = input_handler.get_input("Do you want to copy the output to clipboard? (y/n): ") if copy_to_clipboard == 'y': try: pyperclip.copy(output) @@ -473,7 +477,7 @@ def main(): if data['text_content_size'] / 1024 > args.max_size: print(Fore.RED + f"\nWarning: The text content size ({data['text_content_size'] / 1024:.2f} KB) exceeds the maximum allowed size ({args.max_size} KB)." + Style.RESET_ALL) - proceed = input("Do you want to proceed? (y/n): ").lower().strip() + proceed = input_handler.get_input("Do you want to proceed? (y/n): ") if proceed != 'y': print(Fore.YELLOW + "Analysis aborted." + Style.RESET_ALL) sys.exit(0) diff --git a/codebase_digest/input_handler.py b/codebase_digest/input_handler.py new file mode 100644 index 0000000..62957e3 --- /dev/null +++ b/codebase_digest/input_handler.py @@ -0,0 +1,27 @@ +class InputHandler: + """ + InputHandler class to manage user input with optional default responses. + + Attributes: + no_input (bool): Flag to determine if user input should be bypassed. + default_response (str): Default response to use when no_input is True. + + Methods: + __init__(no_input=False, default_response='y'): + Initializes the InputHandler with optional no_input flag and default response. + + get_input(prompt): + Prompts the user for input unless no_input is True, in which case it returns the default response. + Args: + prompt (str): The prompt message to display to the user. + Returns: + str: The user's input or the default response. + """ + def __init__(self, no_input=False, default_response='y'): + self.no_input = no_input + self.default_response = default_response + + def get_input(self, prompt): + if not self.no_input: + return input(prompt).lower().strip() + return self.default_response diff --git a/codebase_digest/tests/test_input_handler.py b/codebase_digest/tests/test_input_handler.py new file mode 100644 index 0000000..12207cd --- /dev/null +++ b/codebase_digest/tests/test_input_handler.py @@ -0,0 +1,21 @@ +import unittest +from unittest.mock import patch +from input_handler import InputHandler + +class TestInputHandler(unittest.TestCase): + + def test_get_regular_input(self): + handler = InputHandler(no_input=False) + with patch('builtins.input', return_value='Yes'): + self.assertEqual(handler.get_input('Enter something: '), 'yes') + + def test_surpassed_input_should_return_assigned_detault(self): + handler = InputHandler(no_input=True, default_response='n') + self.assertEqual(handler.get_input('Enter something: '), 'n') + + def test_surpassed_input_should_return_yes_by_default(self): + handler = InputHandler(no_input=True) + self.assertEqual(handler.get_input('Enter something: '), 'y') + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/requirements-dev.txt b/requirements-dev.txt index 87f8557..5ca806a 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,4 +1,5 @@ PyGithub twine packaging -setuptools \ No newline at end of file +setuptools +unittest \ No newline at end of file