Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 7 additions & 3 deletions codebase_digest/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -371,13 +372,16 @@ 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)
sys.exit(1)

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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions codebase_digest/input_handler.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions codebase_digest/tests/test_input_handler.py
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
PyGithub
twine
packaging
setuptools
setuptools
unittest