forked from raphaelmansuy/code2prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix/interractive_4 (raphaelmansuy#16)
* first_phase_refactoring * add nice print help * update * fix(code2prompt): r(code2prompt): improve token price display * update * fix(.): r: remove unused imports and variables * fix: Improve README.md * docs: Improve documentation for code2prompt/main.py * feat: Add improvements.md file with suggested enhancements * feat: Implement logging in critical areas of the code * fix: Add log_error function to logging_utils * fix: Add log_output_created function to logging_utils.py * feat: Add log_clipboard_copy and log_token_count functions to logging_utils * feat: Add colored logging to the terminal * feat: Add log level configuration in command line * feat: Add logging level configuration to .code2promptrc and analyze-code.j2 * feat: Improve log message formatting * fix: Add stderr logging for clipboard copy success and failure * fix: Set default log level to WARNING * feat: Add color and emoji to clipboard copy success message * feat: Add header to token price estimation output * fix: Correct token price output formatting * fix: Correct the order of input and output prices in log_token_prices function * fix: Correct the order of "In" and "Out" tokens in the log_token_prices function * fix: Correct the formatting of the token price table * fix/aider(.gitignore): add .aider* to ignore file * fix(code2prompt): update main.py file * fix(gitignore): add .ruff_cache to gitignore * first version * better version * fix(code2prompt): enhance interactive command with improved cursor navigation and visible line handling * fix(code2prompt): add terminal resize handling and improve instructions * fix(code2prompt): move interactive_command.py to commands directory * fix(include_loader): import jinja2 to resolve undefined name error fix(logging_utils): remove unused logger variable chore(deps): add jinja2 dependency to pyproject.toml test(analyzer): remove unused imports test(create_template_directory): remove unused imports test(include_loader): remove unused imports test(is_filtered): improve readability and consistency test(template_include): remove unused imports * fix(code2prompt/utils): Improve logging and file processing criteria * fix(code2prompt/commands): improve `get_terminal_height`, `get_directory_tree`, and `format_tree` functions * fix(code2prompt/commands): improve interactive command page up and down functionality * fix(code2prompt): add interactive file selector for generate command * fix(code2prompt/commands): Refactored interactive file selector, added support for multiple paths * fix(code2prompt): Refactor InteractiveFileSelector to handle multiple paths * fix(code2prompt/commands): improve interactive file selector responsiveness to terminal resize events * fix(code2prompt): Introduce file_path_retriever module to handle file path filtering and processing * fix(code2prompt/commands): improve code organization and documentation in the GenerateCommand class * fix(code2prompt): Improve performance of `_get_directory_tree` method and remove unnecessary validation for `retrieve_file_paths` * fix(code2prompt/commands): handle invalid or missing paths in interactive selector * fix(code2prompt/commands): create and use private methods to handle key bindings and application creation * fix(code2prompt/commands): update interactive file selector to use Path objects * fix(code2prompt): improve interactive file selector behavior * fix(code2prompt/commands): improve directory tree generation and formatting * fix(code2prompt/commands): improve interactive file selector
- Loading branch information
1 parent
7a5343d
commit c8647d4
Showing
42 changed files
with
2,176 additions
and
710 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
{ | ||
"suppress_comments": false, | ||
"line_number": false | ||
} | ||
"line_number": false, | ||
"log_level": "INFO", | ||
"encoding": "cl100k_base", | ||
"filter": "*.py,*.js", | ||
"exclude": "tests/*,docs/*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
# Role Overview | ||
|
||
You are an elite software developer with extensive expertise in Python, command-line tools, and file system operations. Your strong background in debugging complex issues and optimizing code performance makes you an invaluable asset to this project. | ||
|
||
## Key Attributes | ||
|
||
- **Pragmatic Approach**: You prioritize delivering high-quality, maintainable code that meets project requirements. | ||
- **Modular Design**: You embrace composability and modularity, ensuring that your code is easy to extend and maintain. | ||
- **Principled Coding**: You adhere to the KISS (Keep It Simple, Stupid) and DRY (Don't Repeat Yourself) principles, promoting simplicity and efficiency. | ||
- **Documentation & Testing**: You recognize the importance of clear documentation and thorough testing to guarantee the reliability of your work. | ||
- **Functional Preference**: You prefer using functions and modules over classes, focusing on functional programming paradigms. | ||
|
||
## Technological Stack | ||
|
||
This project utilizes the following technologies: | ||
|
||
- **Python Version**: 3.6+ | ||
- **Dependencies**: | ||
- `python = "^3.8,<4.0"` | ||
- `rich = "^13.7.1"` # For rich text and beautiful formatting | ||
- `click = "^8.1.7"` # For creating elegant command-line interfaces | ||
- `jinja2 = "^3.1.4"` # For template rendering | ||
- `prompt-toolkit = "^3.0.47"` # For building powerful interactive command-line applications | ||
- `tiktoken = "^0.7.0"` # For tokenization tasks | ||
- `pyperclip = "^1.9.0"` # For clipboard operations | ||
- `colorama = "^0.4.6"` # For colored terminal text output | ||
- `tqdm = "^4.66.4"` # For progress bars | ||
- `tabulate = "^0.9.0"` # For tabular data formatting | ||
- `pydantic` # For data validation and type checking | ||
- `poetry` # For dependency management |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,6 @@ __pycache__ | |
*.pyc | ||
.DS_Store | ||
.tasks | ||
cli.log | ||
cli.log | ||
.aider* | ||
.ruff_cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# code2prompt/commands/analyze.py | ||
|
||
from pathlib import Path | ||
from typing import Dict | ||
|
||
from code2prompt.commands.base_command import BaseCommand | ||
from code2prompt.utils.analyzer import ( | ||
analyze_codebase, | ||
format_flat_output, | ||
format_tree_output, | ||
get_extension_list, | ||
) | ||
|
||
|
||
class AnalyzeCommand(BaseCommand): | ||
"""Command for analyzing the codebase structure.""" | ||
|
||
def execute(self) -> None: | ||
"""Execute the analyze command.""" | ||
self.logger.info("Analyzing codebase...") | ||
|
||
for path in self.config.path: | ||
self._analyze_path(Path(path)) | ||
|
||
self.logger.info("Analysis complete.") | ||
|
||
def _analyze_path(self, path: Path) -> None: | ||
""" | ||
Analyze a single path and output the results. | ||
Args: | ||
path (Path): The path to analyze. | ||
""" | ||
extension_counts, extension_dirs = analyze_codebase(path) | ||
|
||
if not extension_counts: | ||
self.logger.warning(f"No files found in {path}") | ||
return | ||
|
||
if self.config.format == "flat": | ||
output = format_flat_output(extension_counts) | ||
else: | ||
output = format_tree_output(extension_dirs) | ||
|
||
print(output) | ||
|
||
print("\nComma-separated list of extensions:") | ||
print(get_extension_list(extension_counts)) | ||
|
||
if self.config.tokens: | ||
total_tokens = self._count_tokens(extension_counts) | ||
self.logger.info(f"Total tokens in codebase: {total_tokens}") | ||
|
||
def _count_tokens(self, extension_counts: Dict[str, int]) -> int: | ||
""" | ||
Count the total number of tokens in the codebase. | ||
Args: | ||
extension_counts (Dict[str, int]): A dictionary of file extensions and their counts. | ||
Returns: | ||
int: The total number of tokens. | ||
""" | ||
total_tokens = 0 | ||
for _ext, count in extension_counts.items(): | ||
# This is a simplified token count. You might want to implement a more | ||
# sophisticated counting method based on the file type. | ||
total_tokens += count * 100 # Assuming an average of 100 tokens per file | ||
|
||
return total_tokens |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# code2prompt/commands/base_command.py | ||
|
||
from abc import ABC, abstractmethod | ||
import logging | ||
from code2prompt.config import Configuration | ||
|
||
class BaseCommand(ABC): | ||
""" | ||
Abstract base class for all commands in the code2prompt tool. | ||
This class defines the basic structure and common functionality | ||
for all command classes. It ensures that each command has access | ||
to the configuration and a logger, and defines an abstract execute | ||
method that must be implemented by all subclasses. | ||
Attributes: | ||
config (Configuration): The configuration object for the command. | ||
logger (logging.Logger): The logger instance for the command. | ||
""" | ||
|
||
def __init__(self, config: Configuration, logger: logging.Logger): | ||
""" | ||
Initialize the BaseCommand with configuration and logger. | ||
Args: | ||
config (Configuration): The configuration object for the command. | ||
logger (logging.Logger): The logger instance for the command. | ||
""" | ||
self.config = config | ||
self.logger = logger | ||
|
||
@abstractmethod | ||
def execute(self) -> None: | ||
""" | ||
Execute the command. | ||
This method must be implemented by all subclasses to define | ||
the specific behavior of each command. | ||
Raises: | ||
NotImplementedError: If the subclass does not implement this method. | ||
""" | ||
raise NotImplementedError("Subclasses must implement execute method") | ||
|
||
def log_start(self) -> None: | ||
""" | ||
Log the start of the command execution. | ||
""" | ||
self.logger.info(f"Starting execution of {self.__class__.__name__}") | ||
|
||
def log_end(self) -> None: | ||
""" | ||
Log the end of the command execution. | ||
""" | ||
self.logger.info(f"Finished execution of {self.__class__.__name__}") | ||
|
||
def handle_error(self, error: Exception) -> None: | ||
""" | ||
Handle and log any errors that occur during command execution. | ||
Args: | ||
error (Exception): The exception that was raised. | ||
""" | ||
self.logger.error(f"Error in {self.__class__.__name__}: {str(error)}", exc_info=True) | ||
|
||
def validate_config(self) -> bool: | ||
""" | ||
Validate the configuration for the command. | ||
This method should be overridden by subclasses to perform | ||
command-specific configuration validation. | ||
Returns: | ||
bool: True if the configuration is valid, False otherwise. | ||
""" | ||
return True |
Oops, something went wrong.