This repository has been archived on 2023-06-07. You can view files and clone it, but cannot push or open issues or pull requests.
errsy/gui.py
2023-06-01 12:49:10 +02:00

118 lines
3.9 KiB
Python

import sys
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.utils import analyze_args, exts_list
class GUI:
def __init__(self) -> None:
self.name = "Errsy"
self.parent = Tk()
self.parent.resizable(False, False)
self.f = Frame(self.parent)
def start(self) -> None:
"""Affiche la fenêtre"""
self._main_screen()
# self._change_pos(1280, 720)
self.parent.title(self.name)
self.parent.mainloop()
def _change_pos(self, x: int, y: int) -> None:
"""Change les dimensions de la fenêtre"""
width = self.parent.winfo_screenwidth()
height = self.parent.winfo_screenheight()
# Centre
x_i = (width // 2) - (x // 2)
y_i = (height // 2) - (y // 2)
self.parent.geometry(f"{x}x{y}+{x_i}+{y_i}")
def _open_file(self) -> None:
try:
chemin = askopenfile(
title=f"Choisir un fichier - {self.name}",
filetypes=[("Tous les fichiers", "*")]
+ [
(langage, f"*.{ext}")
for langage, exts in exts_list.items()
for ext in exts
],
)
except AttributeError:
print("Impossible de trouver le fichier", file=sys.stderr)
exit(1)
else:
if chemin:
return self._analyse(2, chemin.name)
def _open_directory(self) -> None:
try:
chemin = askdirectory()
except AttributeError:
print("Impossible de trouver le dossier", file=sys.stderr)
exit(1)
else:
return self._analyse(2, chemin)
def _main_screen(self) -> None:
"""Écran principal"""
Label(
self.parent,
text=f"{self.name} est une application qui permet d'utiliser "
"plus facilement clang-format",
).grid(column=0, row=0, rowspan=2)
Button(self.parent, text="Ouvrir un fichier", command=self._open_file).grid(
column=1, row=0
)
Button(
self.parent, text="Ouvrir un dossier", command=self._open_directory
).grid(column=1, row=1)
def _show_info(self, msg: str) -> None:
title = f"Contenu de l'avertissement - {self.name}"
info = Toplevel()
info.resizable(False, False)
info.title(title)
Label(info, text=msg).pack()
width = max([len(line) for line in [title] + msg.splitlines()]) * 12
height = (3 + msg.count("\n")) * 10
info.geometry(f"{width}x{height}")
def _analyse(self, start_row: int, path: str) -> None:
"""Analyse les données"""
self.f.destroy()
self.f = Frame(self.parent)
self.f.grid()
files = analyze_args([path])
idx = start_row
for file in files:
clang_format_output = clang_format(file)
parsed_output = parse_clang_format_output(clang_format_output)
if parsed_output:
for error in parsed_output:
Button(
self.parent,
text=f"Avertissement dans {error.filename} "
f"à la ligne {error.line_number}, "
f"caractère {error.column_number}.",
borderwidth=0,
justify="left",
anchor="w",
command=lambda: self._show_info(error.warning_content),
).grid(column=0, row=idx)
idx += 1
else:
Label(
self.parent,
text=f"Aucun avertissement trouvé dans {file}.",
).grid(column=0, row=idx)
idx += 1