Skip to content

Commit

Permalink
✨ Colorize output (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex committed Jun 20, 2023
1 parent 4736868 commit f87a8e1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,5 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

project
41 changes: 31 additions & 10 deletions bump_pydantic/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import difflib
import os
import sys
import time
from pathlib import Path
from typing import Any, Dict
from typing import Any, Dict, Iterator

import libcst as cst
from libcst.codemod import CodemodContext
Expand All @@ -14,6 +13,7 @@
PositionProvider,
ScopeProvider,
)
from rich.console import Console
from typer import Argument, Exit, Option, Typer, echo

from bump_pydantic import __version__
Expand All @@ -36,6 +36,7 @@ def main(
diff: bool = Option(False, help="Show diff instead of applying changes."),
version: bool = Option(None, "--version", callback=version_callback, is_eager=True),
):
console = Console()
files_str = list(package.glob("**/*.py"))
files = [str(file.relative_to(".")) for file in files_str]

Expand Down Expand Up @@ -68,7 +69,13 @@ def main(
codemods = gather_codemods()

# TODO: We can run this in parallel - batch it into files / cores.
# We may need to run the resolve_cache() on each core - not sure.
# with ProcessPoolExecutor():
# cpu_count = multiprocessing.cpu_count()
# batch_size = len(files) // cpu_count + 1

# batches = [files[i : i + batch_size] for i in range(0, len(files), batch_size)]
# print(batches)

for codemod in codemods:
for filename in files:
module_and_package = calculate_module_and_package(str(package), filename)
Expand All @@ -91,18 +98,32 @@ def main(

if input_code != output_code:
if diff:
# TODO: Should be colored.
lines = difflib.unified_diff(
input_code.splitlines(keepends=True),
output_code.splitlines(keepends=True),
fromfile=filename,
tofile=filename,
color_diff(
console=console,
lines=difflib.unified_diff(
input_code.splitlines(keepends=True),
output_code.splitlines(keepends=True),
fromfile=filename,
tofile=filename,
),
)
sys.stdout.writelines(lines)
else:
with open(filename, "w") as fp:
fp.write(output_tree.code)

modified = [Path(f) for f in files if os.stat(f).st_mtime > start_time]
if modified:
print(f"Refactored {len(modified)} files.")


def color_diff(console: Console, lines: Iterator[str]) -> None:
for line in lines:
line = line.rstrip("\n")
if line.startswith("+"):
console.print(line, style="green")
elif line.startswith("-"):
console.print(line, style="red")
elif line.startswith("^"):
console.print(line, style="blue")
else:
console.print(line, style="white")

0 comments on commit f87a8e1

Please sign in to comment.