96 lines
3.4 KiB
Python
96 lines
3.4 KiB
Python
import sys
|
|
from colorama import init, Fore, Style
|
|
|
|
from shell.Handlers.GlobalHandler import GlobalHandler
|
|
from shell.Parser import Parser
|
|
|
|
# Инициализация colorama для кроссплатформенной поддержки цветов
|
|
init()
|
|
|
|
|
|
class Interactive:
|
|
def __init__(self):
|
|
self.prompt = Interactive._get_colored_prompt()
|
|
self.parser = Parser()
|
|
self.handler = GlobalHandler()
|
|
|
|
|
|
@classmethod
|
|
def _get_colored_prompt(cls, prompt = "Vaiola> "):
|
|
"""Создает градиентный цветной промпт 'Vaiola>'"""
|
|
colored_prompt = ""
|
|
|
|
# Цвета для градиента (от ярко-белого к фиолетовому и обратно к белому)
|
|
colors = [
|
|
Fore.LIGHTWHITE_EX, # V
|
|
Fore.MAGENTA, # a
|
|
Fore.MAGENTA, # i (немного менее яркий фиолетовый)
|
|
Fore.LIGHTMAGENTA_EX, # o (фиолетовый)
|
|
Fore.LIGHTMAGENTA_EX, # l (немного более яркий фиолетовый)
|
|
Fore.LIGHTWHITE_EX, # a (ярко-фиолетовый)
|
|
Fore.LIGHTWHITE_EX # > (ярко-белый)
|
|
]
|
|
|
|
# Применяем цвета к каждому символу промпта
|
|
for i, char in enumerate(prompt):
|
|
if i < len(colors):
|
|
colored_prompt += colors[i] + char
|
|
else:
|
|
colored_prompt += Fore.LIGHTWHITE_EX + char
|
|
|
|
colored_prompt += Style.RESET_ALL
|
|
return colored_prompt
|
|
|
|
def input(self):
|
|
args = ['']
|
|
while True:
|
|
new_args = self.parser.parse(input(self.prompt).strip())
|
|
if len(new_args) == 0:
|
|
continue
|
|
args[len(args) - 1] += '\n' + new_args[0] if args[len(args) - 1] != '' else new_args[0]
|
|
args.extend(new_args[1:])
|
|
if self.parser.in_quotes:
|
|
self.prompt = self._get_colored_prompt("\"____\"> ")
|
|
continue
|
|
else:
|
|
self.prompt = self._get_colored_prompt()
|
|
break
|
|
return args
|
|
|
|
|
|
|
|
def start(self):
|
|
"""Запускает интерактивную оболочку"""
|
|
while True:
|
|
try:
|
|
command = self.input()
|
|
try:
|
|
self.handler.handle(command)
|
|
except ValueError as e:
|
|
print(f"Error: {e}")
|
|
except RuntimeWarning:
|
|
print("Warning: last command failed")
|
|
|
|
# # Выход из цикла по команде exit или quit
|
|
# if command.lower() in ['exit', 'quit']:
|
|
# print("До свидания!")
|
|
# break
|
|
|
|
# # Парсим команду
|
|
# try:
|
|
# parsed = Parser.parse(command)
|
|
# print(f"Парсинг результата: {parsed}")
|
|
# # Здесь можно добавить обработку команд
|
|
# except ValueError as e:
|
|
# print(f"Ошибка: {e}")
|
|
|
|
except KeyboardInterrupt:
|
|
print("\nGoodbye!")
|
|
break
|
|
except EOFError:
|
|
print("\nGoodbye!")
|
|
break
|
|
|
|
# Запуск интерактивной оболочки
|
|
if __name__ == "__main__":
|
|
Interactive().start() |