Skip to content

Commit 5dc826e

Browse files
committed
feat(tree): enable Rich formatting for both console and file output - Modify tree generator to save Rich formatted output to files instead of plain text - Update file output to use Rich console rendering with emojis and formatting - Bundle command continues to use plain text for clean LLM consumption - Update tests to expect Rich formatting in file output - Update POA.md to reflect new Rich file output capability - Maintain .llmignore exclusions for htmlcov/, site/, and coverage.xml
1 parent 7ca908f commit 5dc826e

3 files changed

Lines changed: 28 additions & 7 deletions

File tree

POA.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ To provide developers with a simple, extensible, and portable Python CLI toolkit
6464
3. **Tool 1: Directory Tree Generator (`tree` command) (Status: ✅ COMPLETE)**
6565
* Logic implemented in `src/contextcraft/tools/tree_generator.py`.
6666
* Handles `root_dir`, `--output`, and CLI `--ignore` options.
67-
* Uses `rich.tree.Tree` for console output, plain text for file.
67+
* Uses `rich.tree.Tree` for both console and file output with beautiful formatting.
6868
* Production-level docstrings and comments.
6969
***COMPLETED:** Comprehensive unit and integration tests (145 total tests passing).
7070
***COMPLETED:** Full integration with `.llmignore` system and config management.

src/contextcraft/tools/tree_generator.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,18 +304,36 @@ def generate_and_output_tree(
304304
current_tool_specific_exclusions = DEFAULT_EXCLUDED_ITEMS_TOOL_SPECIFIC.copy()
305305

306306
if output_file_path:
307-
tree_lines = _generate_tree_lines_recursive(
308-
current_dir=root_dir,
307+
# Create Rich tree for file output (same as console display)
308+
rich_tree_root_label = f"📁 [link file://{root_dir.resolve()}]{root_dir.name}"
309+
rich_tree_root = RichTree(
310+
rich_tree_root_label,
311+
guide_style="bold bright_blue",
312+
)
313+
_add_nodes_to_rich_tree_recursive(
314+
rich_tree_node=rich_tree_root,
315+
current_path_obj=root_dir,
309316
root_dir_for_ignores=root_dir,
310317
llmignore_spec=llmignore_spec,
311318
cli_ignores=effective_cli_ignores,
312-
config_global_excludes=config_global_excludes, # <--- PASS
319+
config_global_excludes=config_global_excludes,
313320
tool_specific_fallback_exclusions=current_tool_specific_exclusions,
314321
)
322+
323+
# Render Rich tree to string for file output
324+
from io import StringIO
325+
326+
from rich.console import Console as RichConsole
327+
328+
string_buffer = StringIO()
329+
file_console = RichConsole(file=string_buffer, width=120, legacy_windows=False)
330+
file_console.print(rich_tree_root)
331+
rich_output = string_buffer.getvalue()
332+
315333
try:
316334
output_file_path.parent.mkdir(parents=True, exist_ok=True)
317335
with output_file_path.open(mode="w", encoding="utf-8") as f:
318-
f.write("\n".join(tree_lines))
336+
f.write(rich_output)
319337
console.print(
320338
f"Directory tree saved to [cyan]{output_file_path.resolve()}[/cyan]"
321339
)

tests/tools/test_tree_generator.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ def test_tree_output_to_file_basic(create_project_structure_for_tree):
5454
assert "file3.txt" in generated_tree_content
5555
assert "empty_dir" in generated_tree_content
5656

57-
# Check for tree structure characters (basic tree formatting)
58-
assert "├──" in generated_tree_content or "└──" in generated_tree_content
57+
# Check for tree structure characters (Rich tree formatting)
58+
assert "┣━━" in generated_tree_content or "┗━━" in generated_tree_content
59+
# Check for Rich formatting elements
60+
assert "📁" in generated_tree_content # Directory icon
61+
assert "📄" in generated_tree_content # File icon
5962

6063

6164
def test_tree_with_llmignore(create_project_structure_for_tree):

0 commit comments

Comments
 (0)