Skip to content

Commit dd95493

Browse files
chore: improve terminal output formatting
Co-Authored-By: [email protected] <[email protected]>
1 parent 9db2c43 commit dd95493

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

examples/dict_to_schema/run.py

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
import codegen
22
from codegen.sdk.enums import ProgrammingLanguage
33
from codegen import Codebase
4+
import sys
5+
import time
6+
7+
def print_progress(current: int, total: int, width: int = 40) -> None:
8+
"""Print a progress bar showing current/total progress."""
9+
filled = int(width * current / total)
10+
bar = "█" * filled + "░" * (width - filled)
11+
percent = int(100 * current / total)
12+
print(f"\r[{bar}] {percent}% ({current}/{total})", end="", file=sys.stderr)
13+
if current == total:
14+
print(file=sys.stderr)
415

516

617
@codegen.function("dict-to-pydantic-schema")
@@ -17,13 +28,15 @@ def run(codebase: Codebase):
1728
models_created = 0
1829

1930
total_files = len(codebase.files)
20-
print(f"\n📁 Scanning {total_files} files for dictionary literals...")
31+
print("\n\033[1;36m📁 Scanning files for dictionary literals...\033[0m")
32+
print(f"Found {total_files} Python files to process")
2133

2234
for i, file in enumerate(codebase.files, 1):
2335
needs_imports = False
2436
file_modified = False
2537

26-
print(f"\n🔍 Checking file {i}/{total_files}: {file.path}")
38+
print_progress(i, total_files)
39+
print(f"\n\033[1;34m🔍 Processing: {file.path}\033[0m")
2740

2841
for global_var in file.global_vars:
2942
try:
@@ -36,16 +49,16 @@ def run(codebase: Codebase):
3649
model_def = f"""class {class_name}(BaseModel):
3750
{dict_content.replace(",", "\n ")}"""
3851

39-
print("\n" + "=" * 60)
40-
print(f"🔄 Converting global variable '{global_var.name}' to schema")
41-
print("=" * 60)
42-
print("📝 Original code:")
52+
print("\n" + "" * 60)
53+
print(f"\033[1;32m🔄 Converting global variable '{global_var.name}' to schema\033[0m")
54+
print("" * 60)
55+
print("\033[1;34m📝 Original code:\033[0m")
4356
print(f" {global_var.name} = {global_var.value.source}")
44-
print("\n✨ Generated schema:")
57+
print("\n\033[1;35m✨ Generated schema:\033[0m")
4558
print(" " + model_def.replace("\n", "\n "))
46-
print("\n✅ Updated code:")
59+
print("\n\033[1;32m✅ Updated code:\033[0m")
4760
print(f" {global_var.name} = {class_name}(**{global_var.value.source})")
48-
print("=" * 60)
61+
print("" * 60)
4962

5063
global_var.insert_before(model_def + "\n\n")
5164
global_var.set_value(f"{class_name}(**{global_var.value.source})")
@@ -69,18 +82,18 @@ def run(codebase: Codebase):
6982
model_def = f"""class {class_name}(BaseModel):
7083
{dict_content.replace(",", "\n ")}"""
7184

72-
print("\n" + "=" * 60)
73-
print(f"🔄 Converting class attribute '{cls.name}.{attr.name}' to schema")
74-
print("=" * 60)
75-
print("📝 Original code:")
85+
print("\n" + "" * 60)
86+
print(f"\033[1;32m🔄 Converting class attribute '{cls.name}.{attr.name}' to schema\033[0m")
87+
print("" * 60)
88+
print("\033[1;34m📝 Original code:\033[0m")
7689
print(f" class {cls.name}:")
7790
print(f" {attr.name} = {attr.value.source}")
78-
print("\n✨ Generated schema:")
91+
print("\n\033[1;35m✨ Generated schema:\033[0m")
7992
print(" " + model_def.replace("\n", "\n "))
80-
print("\n✅ Updated code:")
93+
print("\n\033[1;32m✅ Updated code:\033[0m")
8194
print(f" class {cls.name}:")
8295
print(f" {attr.name} = {class_name}(**{attr.value.source})")
83-
print("=" * 60)
96+
print("" * 60)
8497

8598
cls.insert_before(model_def + "\n\n")
8699
attr.set_value(f"{class_name}(**{attr.value.source})")
@@ -100,18 +113,20 @@ def run(codebase: Codebase):
100113
print(f" ✅ Successfully modified {file.path}")
101114
files_modified += 1
102115

103-
print("\n" + "=" * 60)
104-
print("📊 Summary of Changes")
105-
print("=" * 60)
106-
print(f"✨ Files modified: {files_modified}")
107-
print(f"🔄 Schemas created: {models_created}")
108-
print("=" * 60)
116+
print("\n" + "" * 60)
117+
print("\033[1;35m📊 Summary of Changes\033[0m")
118+
print("" * 60)
119+
print(f"\033[1;32m✨ Files modified: {files_modified}\033[0m")
120+
print(f"\033[1;32m🔄 Schemas created: {models_created}\033[0m")
121+
print("" * 60)
109122

110123
if __name__ == "__main__":
111-
print("\n🔍 Initializing codebase...")
124+
print("\n\033[1;36m🔍 Initializing codebase...\033[0m")
125+
print("Cloning repository, this may take a moment...")
112126
codebase = Codebase.from_repo("fastapi/fastapi", programming_language=ProgrammingLanguage.PYTHON)
113-
print("\n🚀 Running dict-to-pydantic-schema codemod...")
114-
print("\nℹ️ This codemod will:")
127+
128+
print("\n\033[1;35m🚀 Running dict-to-pydantic-schema codemod\033[0m")
129+
print("\n\033[1;34mℹ️ This codemod will:\033[0m")
115130
print(" 1. Find dictionary literals in your code")
116131
print(" 2. Convert them to Pydantic models")
117132
print(" 3. Update assignments to use the new models")

0 commit comments

Comments
 (0)