support pages

This commit is contained in:
Mylloon 2023-06-01 16:44:00 +02:00
parent a251690af1
commit b7666327ca
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 58 additions and 46 deletions

View file

@ -5,7 +5,7 @@ import sys
from src.errors import ClangError
def clang_format(file_path) -> str:
def clang_format(file_path) -> tuple[str, str]:
command = ["clang-format", "--dry-run", file_path]
try:
output = subprocess.check_output(command, stderr=subprocess.STDOUT, text=True)
@ -20,10 +20,11 @@ def clang_format(file_path) -> str:
)
exit(1)
return output
return file_path, output
def parse_clang_format_output(output) -> list[ClangError]:
def parse_clang_format_output(data) -> list[ClangError]:
filename, output = data
error_pattern = (
r"(?P<filename>.+):(?P<line_number>\d+):(?P<column_number>\d+)"
r": warning: code should be clang-formatted \[(?P<warning_message>.+)\]"
@ -49,4 +50,7 @@ def parse_clang_format_output(output) -> list[ClangError]:
)
)
if not errors:
return [ClangError(filename)]
return errors

View file

@ -1,11 +1,14 @@
from typing import Union
class ClangError:
def __init__(
self,
filename: str,
line_number: int,
column_number: int,
warning_message: str,
warning_content: str,
line_number: Union[int, None] = None,
column_number: Union[int, None] = None,
warning_message: Union[str, None] = None,
warning_content: Union[str, None] = None,
) -> None:
self._filename = filename
self._line_number = line_number
@ -19,21 +22,26 @@ class ClangError:
return self._filename
@property
def line_number(self) -> int:
def line_number(self) -> Union[int, None]:
"""Sur quelle ligne se situe l'erreur"""
return self._line_number
@property
def column_number(self) -> int:
def column_number(self) -> Union[int, None]:
"""Où se situe l'erreur sur la ligne"""
return self._column_number
@property
def warning_message(self) -> str:
def warning_message(self) -> Union[str, None]:
"""Quelle est le nom de l'erreur"""
return self._warning_message
@property
def warning_content(self) -> str:
def warning_content(self) -> Union[str, None]:
"""Quelle est le contenu de l'erreur"""
return self._warning_content
@property
def clean(self) -> bool:
"""Vérifie s'il y a des erreurs"""
return self._warning_message is None

View file

@ -3,6 +3,7 @@ from tkinter import Button, Frame, Label, Tk, Toplevel
from tkinter.filedialog import askdirectory, askopenfile
from src.clangformat import clang_format, parse_clang_format_output
from src.errors import ClangError
from src.utils import analyze_args, exts_list
@ -97,7 +98,7 @@ class GUI:
data = []
for file in files:
clang_format_output = clang_format(file)
data.append((file, parse_clang_format_output(clang_format_output)))
data += parse_clang_format_output(clang_format_output)
self._pager(1, data, start_row)
def _reset_frame(self) -> None:
@ -106,47 +107,46 @@ class GUI:
self.f = Frame(self.parent)
self.f.grid(columnspan=self.columnspan)
def _pager(self, num_page: int, errors: list, start_row: int) -> None:
def _pager(self, num_page: int, errors: list[ClangError], start_row: int) -> None:
self._reset_frame()
idx = start_row
elem_per_page = 5
for file, error in errors:
if error:
for error in error[
elem_per_page * (num_page - 1) : elem_per_page * num_page
]:
Button(
self.f,
text=f"Avertissement dans {error.filename} "
f"à la ligne {error.line_number}, "
f"caractère {error.column_number}.",
borderwidth=0,
bg="SandyBrown",
activebackground="Goldenrod",
command=lambda: self._show_info(
error.warning_message, error.warning_content
),
).grid(column=0, row=idx, columnspan=self.columnspan)
idx += 1
elem_per_page = 10
for error in errors[elem_per_page * (num_page - 1) : elem_per_page * num_page]:
if not error.clean:
Button(
self.f,
text=f"Avertissement dans {error.filename} "
f"à la ligne {error.line_number}, "
f"caractère {error.column_number}.",
borderwidth=0,
bg="SandyBrown",
activebackground="Goldenrod",
command=lambda: self._show_info(
str(error.warning_message), str(error.warning_content)
),
).grid(column=0, row=idx, columnspan=self.columnspan)
idx += 1
else:
Label(
self.f,
bg="MediumSpringGreen",
text=f"Aucun avertissement trouvé dans {file}.",
text=f"Aucun avertissement trouvé dans {error.filename}.",
).grid(column=0, row=idx, columnspan=self.columnspan)
idx += 1
Button(
self.f,
text="< Page précédente",
command=lambda: print("avant"),
).grid(column=0, row=idx)
Label(self.f, text=f"Page {num_page}/{1 + len(errors) // elem_per_page}").grid(
column=1, row=idx
)
Button(
self.f,
text="Page suivante >",
command=lambda: print("apres"),
).grid(column=2, row=idx)
page_max = 1 + len(errors) // elem_per_page
if page_max > 1:
Button(
self.f,
text="< Page précédente",
state="disabled" if num_page <= 1 else "normal",
command=lambda: self._pager(num_page - 1, errors, start_row),
).grid(column=0, row=idx)
Label(self.f, text=f"Page {num_page}/{page_max}").grid(column=1, row=idx)
Button(
self.f,
text="Page suivante >",
state="disabled" if num_page >= page_max else "normal",
command=lambda: self._pager(num_page + 1, errors, start_row),
).grid(column=2, row=idx)