Skip to content

Commit

Permalink
fix formatting with rich
Browse files Browse the repository at this point in the history
  • Loading branch information
noklam committed Feb 2, 2025
1 parent fe637e0 commit 00be4ca
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/ruff_in_python/message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from dataclasses import dataclass
from pathlib import Path
from typing import Union
from abc import ABC, abstractmethod

from rich.text import Text

@dataclass
class Location:
row: int
column: int

@dataclass
class Message(ABC):
filename: Path
location: Location

@property
@abstractmethod
def code(self) -> str:
pass

@property
@abstractmethod
def body(self) -> str:
pass

def richify(self) -> Text:
text = Text()
text.append(f"{self.filename}", style="bold white")
text.append(":", style="cyan")
text.append(f"{self.location.column}", style="white")
text.append(":", style="cyan")
text.append(f"{self.location.row}", style="qhirw")
text.append(f"\t{self.code}", style="bold red")
text.append(f"\t{self.body}")
return text
@dataclass
class ImportStarUsage(Message):
@property
def code(self) -> str:
return "F403"

@property
def body(self) -> str:
return "Unable to detect undefined names"

@dataclass
class IfTuple(Message):
@property
def code(self) -> str:
return "F634"

@property
def body(self) -> str:
return "If test is a tuple, which is always `True`"

MessageType = Union[ImportStarUsage, IfTuple]

# # For serialization (if needed):
# def message_to_dict(message: Message) -> dict:
# return {
# "type": message.__class__.__name__,
# "filename": str(message.filename),
# "location": {
# "row": message.location.row,
# "column": message.location.column
# }
# }

# def dict_to_message(data: dict) -> MessageType:
# cls = globals()[data["type"]]
# return cls(
# filename=Path(data["filename"]),
# location=Location(**data["location"])
# )

if __name__ == "__main__":
from rich import print
m1 = IfTuple(Path("some_path"),Location(1,2))
print(m1.richify())

0 comments on commit 00be4ca

Please sign in to comment.