Add colors to cli

This commit is contained in:
augustin64 2023-12-01 10:47:58 +01:00
parent b28322a448
commit 040b3d525c
3 changed files with 30 additions and 4 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
*/inputs */inputs
**/__pycache__

View File

@ -7,6 +7,7 @@ Suggéré: alias aoc="$CHEMIN_VERS_CE_REPO/utils/cli.py"
from optparse import OptionParser from optparse import OptionParser
from pathlib import Path from pathlib import Path
import datetime import datetime
import colors
import os import os
import requests import requests
@ -30,10 +31,23 @@ def download_input(year, day):
print("ok") print("ok")
def show_puzzle(year, day, part=None): def show_puzzle(year, day, part=None, colored=False):
"""Affiche l'énoncé du jour demandé""" """Affiche l'énoncé du jour demandé"""
request = session.get(f"https://adventofcode.com/{year}/day/{int(day)}") request = session.get(f"https://adventofcode.com/{year}/day/{int(day)}")
soup = BeautifulSoup(request.content, "html.parser")
content = request.content.decode("utf-8")
if colored:
content = content.replace("<h2", f"{colors.bold}<h2")
content = content.replace('<em class="star">', f'<em class="star">{colors.bold}{colors.yellow}')
content = content.replace('<em>', f'<em>{colors.bold}')
content = content.replace('<a href', f'{colors.green}<a href')
content = content.replace("</h2>", f"</h2>{colors.reset}\n")
content = content.replace("</em>", f"</em>{colors.reset}")
content = content.replace("</a>", f"</a>{colors.reset}")
soup = BeautifulSoup(content, "html.parser")
day_desc = soup.find_all("article", {"class": "day-desc"}) day_desc = soup.find_all("article", {"class": "day-desc"})
if part is None: if part is None:
for i in range(len(day_desc)): for i in range(len(day_desc)):
@ -67,7 +81,7 @@ def __main__(options, args):
options.level = None options.level = None
else: else:
options.level = int(options.level) options.level = int(options.level)
show_puzzle(options.year, options.day, options.level) show_puzzle(options.year, options.day, options.level, colored=True)
elif args[0] == "submit" or args[0] == "sb": elif args[0] == "submit" or args[0] == "sb":
if len(args) < 2: if len(args) < 2:
answer = input("Answer ?\n>> ") answer = input("Answer ?\n>> ")

11
utils/colors.py Normal file
View File

@ -0,0 +1,11 @@
reset = "\033[0m"
bold = "\033[1m"
magenta = "\033[35m"
yellow = "\033[33m"
white = "\033[37m"
green = "\033[32m"
black = "\033[30m"
cyan = "\033[36m"
blue = "\033[34m"
red = "\033[31m"