add style selection support

This commit is contained in:
Mylloon 2023-06-02 00:02:30 +02:00
parent 9981d75651
commit 9910b0232f
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 63 additions and 5 deletions

View file

@ -5,8 +5,23 @@ import sys
from src.errors import ClangError
def clang_format(file_path) -> tuple[str, str]:
command = ["clang-format", "--dry-run", file_path]
class Style:
"""Preset of coding style"""
FILE = "file" # .clang-format
LLVM = "LLVM"
GNU = "GNU"
Google = "Google"
Chromium = "Chromium"
Microsoft = "Microsoft"
Mozilla = "Mozilla"
WebKit = "WebKit"
PRESETS_LIST = [k for k in locals().keys() if not k.startswith("_")]
def clang_format(file_path: str, style: str) -> tuple[str, str]:
command = ["clang-format", "--dry-run", file_path, f"--style={style}"]
try:
output = subprocess.check_output(command, stderr=subprocess.STDOUT, text=True)
except subprocess.CalledProcessError as e:

View file

@ -1,8 +1,8 @@
import sys
from tkinter import Button, Frame, Label, Tk, Toplevel
from tkinter import Button, Frame, Label, Menu, Tk, Toplevel
from tkinter.filedialog import askdirectory, askopenfile
from src.clangformat import clang_format, parse_clang_format_output
from src.clangformat import Style, clang_format, parse_clang_format_output
from src.errors import ClangError
from src.utils import analyze_args, exts_list
@ -15,6 +15,8 @@ class GUI:
self.f = Frame(self.parent)
self.columnspan = 3
self.style = Style.FILE
self.current_location = ""
def start(self) -> None:
"""Affiche la fenêtre"""
@ -50,6 +52,7 @@ class GUI:
exit(1)
else:
if chemin:
self.current_location = chemin.name
return self._analyse(start, chemin.name)
def _open_directory(self, start: int) -> None:
@ -59,8 +62,31 @@ class GUI:
print("Impossible de trouver le dossier", file=sys.stderr)
exit(1)
else:
self.current_location = chemin
return self._analyse(start, chemin)
def _add_styles_menu_bar(self, start: int) -> None:
def ev(s: str) -> str:
"""Récupère la valeur du style"""
return eval(compile(f"Style.{s}", "<string>", "eval"))
for style in sorted(Style.PRESETS_LIST):
self.style_menu.add_command(
label=f"{style} (en cours)" if ev(style) == self.style else style,
command=lambda s=ev(style): self._update_style(s, start),
)
def _update_style(self, new_style: str, start: int) -> None:
if new_style != self.style:
self.style_menu.delete(0, len(Style.PRESETS_LIST))
self.style = new_style
self._add_styles_menu_bar(start)
if len(self.current_location):
self._analyse(start, self.current_location)
def _main_screen(self) -> None:
"""Écran principal"""
Label(
@ -81,6 +107,23 @@ class GUI:
command=lambda: self._open_directory(start),
).grid(column=self.columnspan - 1, row=1)
menu_bar = Menu(self.parent)
self.parent.config(menu=menu_bar)
file_menu = Menu(menu_bar)
self.style_menu = Menu(file_menu)
self._update_style(self.style, start)
file_menu.add_cascade(label="Selection style", menu=self.style_menu)
self._add_styles_menu_bar(start)
file_menu.add_separator()
file_menu.add_command(
label="Fermer",
command=self.parent.destroy,
)
menu_bar.add_cascade(label="Fichier", menu=file_menu)
def _show_info(self, name_err: str, msg: str) -> None:
title = f"{name_err} - {self.name}"
info = Toplevel()
@ -97,7 +140,7 @@ class GUI:
files = analyze_args([path])
data = []
for file in files:
clang_format_output = clang_format(file)
clang_format_output = clang_format(file, self.style)
data += parse_clang_format_output(clang_format_output)
self._pager(1, data, start_row)