Обновлен консольный вывод команд

This commit is contained in:
2026-02-12 18:37:30 +08:00
parent c9e9423d2c
commit 676cd86b3b
5 changed files with 160 additions and 35 deletions

View File

@@ -3,32 +3,42 @@ import os
import subprocess
from ..config import flags, profile, c_comp
from ..utils.fs import check_dir
from ..utils.text import colored, BRIGHT_GREEN, BOLD, BRIGHT_RED
def build(args: list[str] | None = None) -> int:
active_profile: str = profile
project_name = os.path.basename(os.getcwd())
if "dcr.toml" not in check_dir():
print("Ошибка: не найден файл dcr.toml")
print(colored("error", BRIGHT_RED + BOLD) + ": не найден файл dcr.toml")
return 1
if "target" not in check_dir():
os.mkdir("./target")
if args:
if len(args) >= 1 and args[0].startswith("--"):
candidate: str = args[0][2:]
if candidate in flags:
active_profile: str = candidate
else:
print("Ошибка: неверный флаг сборки")
print(colored("error", BRIGHT_RED + BOLD) + ": неизвестный флаг сборки")
return 1
else:
print("Ошибка: флаг сборки не найден")
print(colored("error", BRIGHT_RED + BOLD) + ": неизвестный аргумент")
return 1
print(
" Сборка проекта `"
+ colored(project_name, BRIGHT_GREEN + BOLD)
+ "`\n Профиль: "
+ colored(active_profile, BRIGHT_GREEN + BOLD)
+ "\n Компилятор: "
+ colored(c_comp, BRIGHT_GREEN + BOLD)
+ "\n"
)
if "target" not in check_dir():
os.mkdir("./target")
if active_profile not in check_dir("target"):
os.mkdir(f"./target/{active_profile}")
if "main.c" in check_dir("src"):
print(f"Запуск сборки с профилем {active_profile}")
compile_flags = flags[active_profile]
start_time: float = time.time()
try:
@@ -45,10 +55,16 @@ def build(args: list[str] | None = None) -> int:
end_time: float = time.time()
times: float = end_time - start_time
times: float = int(times * 100) / 100
print(f"Сборка завершена, за {times} секунд")
print(
" "
+ colored("✔", BRIGHT_GREEN + BOLD)
+ " Сборка завершена успешно, за "
+ colored(str(times), BRIGHT_GREEN + BOLD)
+ " секунд"
)
return 0
except:
print("Ошибка сборки")
print(colored("error", BRIGHT_RED + BOLD) + ": сборка не удалась")
return 1
return 0

View File

@@ -1,19 +1,53 @@
from ..utils.fs import check_dir
import os
import shutil
from ..utils.fs import check_dir
from ..utils.text import colored, BRIGHT_RED, BOLD, BRIGHT_GREEN, BRIGHT_YELLOW
from ..config import flags
def clean(args: list[str]) -> int:
def clean(args: list[str] | None = None) -> int:
project_name = os.path.basename(os.getcwd())
items: list[str] = check_dir()
if len(args) >= 1:
print("Ошибка: команда не поддежривает аргументы")
if args and len(args) > 1:
print(colored("error", BRIGHT_RED + BOLD) + ": неизвестный аргумент")
return 1
if "dcr.toml" not in items:
print("Ошибка: вы находитесь не в корне проекта")
print(colored("error", BRIGHT_RED + BOLD) + ": не найден файл dcr.toml")
return 1
print(" Очистка проекта `" + colored(project_name, BRIGHT_GREEN + BOLD) + "`")
if "target" not in items:
print("Вы уже очистили сборки")
print(colored("warn", BRIGHT_YELLOW + BOLD) + ": директория target не найдена")
return 1
if args:
profile = args[0]
if profile.startswith("--"):
profile = profile[2:]
if profile not in flags:
print(colored("error", BRIGHT_RED + BOLD) + ": неизвестный профиль")
return 1
if profile not in check_dir("target"):
print(
colored("warn", BRIGHT_YELLOW + BOLD)
+ f": директория target/{profile} не найдена"
)
return 1
print(" Профиль: " + colored(profile, BRIGHT_GREEN + BOLD))
shutil.rmtree(os.path.join("target", profile))
print(
colored("\n ✔", BRIGHT_GREEN + BOLD)
+ f" Удалена директория target/{profile}"
)
return 0
shutil.rmtree("target")
print("Сборки очишены")
print(colored("\n ✔", BRIGHT_GREEN + BOLD) + " Удалена директория target")
return 0

View File

@@ -1,3 +1,4 @@
from ..utils.text import colored, BRIGHT_RED, BOLD, printc, BRIGHT_GREEN, BRIGHT_CYAN
from ..config import (
file_dcr_toml,
file_main_c,
@@ -11,21 +12,21 @@ from ..utils.fs import check_dir
def init(args: list[str] | None = None) -> int:
if args:
print("Ошибка: команда init не поддерживает аргументы")
print(
colored("error", BRIGHT_RED + BOLD)
+ ": команда не поддерживает доп. аргументы"
)
return 1
items: list[str] = check_dir()
project_name = os.path.basename(os.getcwd())
if items:
print("Ошибка: директория не пустая")
print(colored("error", BRIGHT_RED + BOLD) + ": директория не пуста")
return 1
os.mkdir("src")
print("Инициализация проекта в " + os.getcwd())
with open("./src/main.c", "w") as file:
file.write(file_main_c)
with open("./dcr.toml", "w") as file:
with open(f"./dcr.toml", "w") as file:
file.write(
file_dcr_toml.format(
project_name=project_name,
@@ -34,5 +35,27 @@ def init(args: list[str] | None = None) -> int:
project_compiler=project_compiler,
)
)
print(
" "
+ colored("✔", BRIGHT_GREEN + BOLD)
+ " Создан файл "
+ colored("dcr.toml", BRIGHT_CYAN + BOLD)
)
os.mkdir("src")
with open("./src/main.c", "w") as file:
file.write(file_main_c)
print(
" "
+ colored("✔", BRIGHT_GREEN + BOLD)
+ " Создан файл "
+ colored("src/main.c\n", BRIGHT_CYAN + BOLD)
)
print(
"Проект `" + colored(project_name, BRIGHT_GREEN + BOLD) + "` успешно создан\n"
)
printc("Следуюший шаг:", BRIGHT_GREEN + BOLD)
printc(" dcr run", BRIGHT_CYAN + BOLD)
return 0

View File

@@ -1,3 +1,11 @@
from ..utils.text import (
colored,
printc,
BRIGHT_GREEN,
BOLD,
BRIGHT_CYAN,
BRIGHT_RED,
)
from ..config import (
file_dcr_toml,
file_main_c,
@@ -12,20 +20,39 @@ from ..utils.fs import check_dir
def new(args: list[str]) -> int:
items: list[str] = check_dir()
if len(args) == 0:
print("Ошибка: не указано название проекта")
print(colored("error", BRIGHT_RED + BOLD) + ": не указанно название проекта")
return 1
if len(args) > 1:
print("Ошибка: команда не поддежривает аргументы")
print(
colored("error", BRIGHT_RED + BOLD)
+ ": команда не поддерживает доп. аргументы"
)
return 1
project_name: str = args[0]
print("Создание проекта `" + colored(project_name, BRIGHT_GREEN + BOLD) + "`...")
if project_name in items:
print(f"Ошибка: директория {project_name} занята")
print(
colored("error", BRIGHT_RED + BOLD)
+ ": директория `"
+ colored(project_name, BRIGHT_GREEN + BOLD)
+ "` уже существует\n"
)
printc("Подсказка:", BRIGHT_GREEN + BOLD)
print(
" Используй `"
+ colored("dcr init", BRIGHT_CYAN + BOLD)
+ "` для инициализации существующего проекта\n или задай другое имя проекта"
)
return 1
os.makedirs(f"./{project_name}/src")
with open(f"./{project_name}/src/main.c", "w") as file:
file.write(file_main_c)
os.mkdir(project_name)
print(
" "
+ colored("✔", BRIGHT_GREEN + BOLD)
+ " Создана директория "
+ project_name
)
with open(f"./{project_name}/dcr.toml", "w") as file:
file.write(
@@ -36,5 +63,27 @@ def new(args: list[str]) -> int:
project_compiler=project_compiler,
)
)
print(
" "
+ colored("✔", BRIGHT_GREEN + BOLD)
+ " Создан файл "
+ colored("dcr.toml", BRIGHT_CYAN + BOLD)
)
os.makedirs(f"./{project_name}/src")
with open(f"./{project_name}/src/main.c", "w") as file:
file.write(file_main_c)
print(
" "
+ colored("✔", BRIGHT_GREEN + BOLD)
+ " Создан файл "
+ colored("src/main.c\n", BRIGHT_CYAN + BOLD)
)
print(
"Проект `" + colored(project_name, BRIGHT_GREEN + BOLD) + "` успешно создан\n"
)
printc("Следуюший шаг:", BRIGHT_GREEN + BOLD)
printc(" cd " + project_name + "\n dcr run", BRIGHT_CYAN + BOLD)
return 0

View File

@@ -1,31 +1,34 @@
import subprocess
import os
from ..utils.fs import check_dir
from ..utils.text import colored, BRIGHT_RED, BOLD
from ..config import flags, profile
from .build import build
def run(args: list[str] | None = None) -> int:
project_name = os.path.basename(os.getcwd())
active_profile: str = profile
if "dcr.toml" not in check_dir():
print("Ошибка: не найден файл dcr.toml")
print(colored("error", BRIGHT_RED + BOLD) + ": не найден файл dcr.toml")
return 1
if args:
if len(args) >= 1 and args[0].startswith("--"):
candidate: str = args[0][2:]
if candidate in flags:
active_profile: str = candidate
else:
print("Ошибка: неверный флаг сборки")
print(colored("error", BRIGHT_RED + BOLD) + ": неизвестный флаг сборки")
return 1
else:
print("Ошибка: флаг сборки не найден")
print(colored("error", BRIGHT_RED + BOLD) + ": неизвестный аргумент")
return 1
build_status: int = build(args)
if build_status == 0:
print("Запуск проекта")
print("\n Запуск " + "target/" + active_profile + "/main")
print("--------------------------------")
return subprocess.run([f"./target/{active_profile}/main"]).returncode
else:
if active_profile in check_dir("target/" + active_profile):