support pages
This commit is contained in:
parent
a251690af1
commit
b7666327ca
3 changed files with 58 additions and 46 deletions
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
30
src/gui.py
30
src/gui.py
|
@ -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,16 +107,13 @@ 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[
|
|
||||||
elem_per_page * (num_page - 1) : elem_per_page * num_page
|
|
||||||
]:
|
|
||||||
Button(
|
Button(
|
||||||
self.f,
|
self.f,
|
||||||
text=f"Avertissement dans {error.filename} "
|
text=f"Avertissement dans {error.filename} "
|
||||||
|
@ -125,7 +123,7 @@ class GUI:
|
||||||
bg="SandyBrown",
|
bg="SandyBrown",
|
||||||
activebackground="Goldenrod",
|
activebackground="Goldenrod",
|
||||||
command=lambda: self._show_info(
|
command=lambda: self._show_info(
|
||||||
error.warning_message, error.warning_content
|
str(error.warning_message), str(error.warning_content)
|
||||||
),
|
),
|
||||||
).grid(column=0, row=idx, columnspan=self.columnspan)
|
).grid(column=0, row=idx, columnspan=self.columnspan)
|
||||||
idx += 1
|
idx += 1
|
||||||
|
@ -133,20 +131,22 @@ class GUI:
|
||||||
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
|
||||||
|
|
||||||
|
page_max = 1 + len(errors) // elem_per_page
|
||||||
|
if page_max > 1:
|
||||||
Button(
|
Button(
|
||||||
self.f,
|
self.f,
|
||||||
text="< Page précédente",
|
text="< Page précédente",
|
||||||
command=lambda: print("avant"),
|
state="disabled" if num_page <= 1 else "normal",
|
||||||
|
command=lambda: self._pager(num_page - 1, errors, start_row),
|
||||||
).grid(column=0, row=idx)
|
).grid(column=0, row=idx)
|
||||||
Label(self.f, text=f"Page {num_page}/{1 + len(errors) // elem_per_page}").grid(
|
Label(self.f, text=f"Page {num_page}/{page_max}").grid(column=1, row=idx)
|
||||||
column=1, row=idx
|
|
||||||
)
|
|
||||||
Button(
|
Button(
|
||||||
self.f,
|
self.f,
|
||||||
text="Page suivante >",
|
text="Page suivante >",
|
||||||
command=lambda: print("apres"),
|
state="disabled" if num_page >= page_max else "normal",
|
||||||
|
command=lambda: self._pager(num_page + 1, errors, start_row),
|
||||||
).grid(column=2, row=idx)
|
).grid(column=2, row=idx)
|
||||||
|
|
Reference in a new issue