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 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] command = ["clang-format", "--dry-run", file_path]
try: try:
output = subprocess.check_output(command, stderr=subprocess.STDOUT, text=True) output = subprocess.check_output(command, stderr=subprocess.STDOUT, text=True)
@ -20,10 +20,11 @@ def clang_format(file_path) -> str:
) )
exit(1) 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 = ( error_pattern = (
r"(?P<filename>.+):(?P<line_number>\d+):(?P<column_number>\d+)" r"(?P<filename>.+):(?P<line_number>\d+):(?P<column_number>\d+)"
r": warning: code should be clang-formatted \[(?P<warning_message>.+)\]" 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 return errors

View file

@ -1,11 +1,14 @@
from typing import Union
class ClangError: class ClangError:
def __init__( def __init__(
self, self,
filename: str, filename: str,
line_number: int, line_number: Union[int, None] = None,
column_number: int, column_number: Union[int, None] = None,
warning_message: str, warning_message: Union[str, None] = None,
warning_content: str, warning_content: Union[str, None] = None,
) -> None: ) -> None:
self._filename = filename self._filename = filename
self._line_number = line_number self._line_number = line_number
@ -19,21 +22,26 @@ class ClangError:
return self._filename return self._filename
@property @property
def line_number(self) -> int: def line_number(self) -> Union[int, None]:
"""Sur quelle ligne se situe l'erreur""" """Sur quelle ligne se situe l'erreur"""
return self._line_number return self._line_number
@property @property
def column_number(self) -> int: def column_number(self) -> Union[int, None]:
"""Où se situe l'erreur sur la ligne""" """Où se situe l'erreur sur la ligne"""
return self._column_number return self._column_number
@property @property
def warning_message(self) -> str: def warning_message(self) -> Union[str, None]:
"""Quelle est le nom de l'erreur""" """Quelle est le nom de l'erreur"""
return self._warning_message return self._warning_message
@property @property
def warning_content(self) -> str: def warning_content(self) -> Union[str, None]:
"""Quelle est le contenu de l'erreur""" """Quelle est le contenu de l'erreur"""
return self._warning_content 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 tkinter.filedialog import askdirectory, askopenfile
from src.clangformat import clang_format, parse_clang_format_output from src.clangformat import clang_format, parse_clang_format_output
from src.errors import ClangError
from src.utils import analyze_args, exts_list from src.utils import analyze_args, exts_list
@ -97,7 +98,7 @@ class GUI:
data = [] data = []
for file in files: for file in files:
clang_format_output = clang_format(file) 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) self._pager(1, data, start_row)
def _reset_frame(self) -> None: def _reset_frame(self) -> None:
@ -106,47 +107,46 @@ class GUI:
self.f = Frame(self.parent) self.f = Frame(self.parent)
self.f.grid(columnspan=self.columnspan) 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() self._reset_frame()
idx = start_row idx = start_row
elem_per_page = 5 elem_per_page = 10
for file, error in errors: for error in errors[elem_per_page * (num_page - 1) : elem_per_page * num_page]:
if error: if not error.clean:
for error in error[ Button(
elem_per_page * (num_page - 1) : elem_per_page * num_page self.f,
]: text=f"Avertissement dans {error.filename} "
Button( f"à la ligne {error.line_number}, "
self.f, f"caractère {error.column_number}.",
text=f"Avertissement dans {error.filename} " borderwidth=0,
f"à la ligne {error.line_number}, " bg="SandyBrown",
f"caractère {error.column_number}.", activebackground="Goldenrod",
borderwidth=0, command=lambda: self._show_info(
bg="SandyBrown", str(error.warning_message), str(error.warning_content)
activebackground="Goldenrod", ),
command=lambda: self._show_info( ).grid(column=0, row=idx, columnspan=self.columnspan)
error.warning_message, error.warning_content idx += 1
),
).grid(column=0, row=idx, columnspan=self.columnspan)
idx += 1
else: else:
Label( Label(
self.f, self.f,
bg="MediumSpringGreen", 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) ).grid(column=0, row=idx, columnspan=self.columnspan)
idx += 1 idx += 1
Button( page_max = 1 + len(errors) // elem_per_page
self.f, if page_max > 1:
text="< Page précédente", Button(
command=lambda: print("avant"), self.f,
).grid(column=0, row=idx) text="< Page précédente",
Label(self.f, text=f"Page {num_page}/{1 + len(errors) // elem_per_page}").grid( state="disabled" if num_page <= 1 else "normal",
column=1, row=idx command=lambda: self._pager(num_page - 1, errors, start_row),
) ).grid(column=0, row=idx)
Button( Label(self.f, text=f"Page {num_page}/{page_max}").grid(column=1, row=idx)
self.f, Button(
text="Page suivante >", self.f,
command=lambda: print("apres"), text="Page suivante >",
).grid(column=2, row=idx) state="disabled" if num_page >= page_max else "normal",
command=lambda: self._pager(num_page + 1, errors, start_row),
).grid(column=2, row=idx)