update: 2026-02-16 00:29:47

This commit is contained in:
2026-02-16 00:29:47 +08:00
parent 19384439f2
commit b75dee4536
6 changed files with 385 additions and 20 deletions

View File

@@ -11,13 +11,36 @@ from .config import Config
class GitOperations:
"""Git操作类"""
def __init__(self):
def __init__(self, work_dir: str = ""):
"""
初始化Git操作类
Args:
work_dir: 工作目录(默认为当前目录)
"""
self.executor = CommandExecutor()
self.current_dir = os.getcwd()
if work_dir:
self.current_dir = work_dir
else:
self.current_dir = os.getcwd()
def change_directory(self, new_dir: str):
"""
切换工作目录
Args:
new_dir: 新的工作目录
"""
if os.path.isdir(new_dir):
self.current_dir = os.path.abspath(new_dir)
os.chdir(self.current_dir)
else:
raise ValueError(f"目录不存在: {new_dir}")
def is_git_repo(self) -> bool:
"""检查当前目录是否是Git仓库"""
return os.path.isdir('.git')
git_dir = os.path.join(self.current_dir, '.git')
return os.path.isdir(git_dir)
def init_repo(self) -> bool:
"""

View File

@@ -2,18 +2,71 @@
UI交互模块 - 处理用户界面和交互逻辑
"""
import os
from .git_operations import GitOperations
from .remote_manager import RemoteManager
from .utils import OutputFormatter, InputValidator, Colors
from .utils import OutputFormatter, InputValidator, Colors, PlatformUtils
class GitManagerUI:
"""Git管理器UI"""
def __init__(self):
self.git_ops = GitOperations()
def __init__(self, work_dir: str = ""):
"""
初始化UI
Args:
work_dir: 工作目录(默认为当前目录)
"""
self.git_ops = GitOperations(work_dir)
self.remote_mgr = RemoteManager()
def select_work_directory(self):
"""让用户选择工作目录"""
from .utils import Colors
print(f"\n{Colors.BRIGHT_CYAN}{'=' * 60}")
print(f"{Colors.BRIGHT_MAGENTA}{Colors.BOLD} QuickGit - 萌芽一键Git管理工具 v1.0{Colors.ENDC}")
print(f"{Colors.BRIGHT_CYAN}{'=' * 60}{Colors.ENDC}")
# 获取当前目录
current_dir = os.getcwd()
# 显示平台信息和路径示例
platform = PlatformUtils.get_platform_name()
print(f"{Colors.BRIGHT_YELLOW}当前平台:{Colors.ENDC} {Colors.WHITE}{platform}{Colors.ENDC}")
print(f"{Colors.BRIGHT_YELLOW}脚本目录:{Colors.ENDC} {Colors.WHITE}{current_dir}{Colors.ENDC}")
print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}")
# 显示路径输入提示
OutputFormatter.info("请输入要管理的Git仓库目录")
# 根据平台显示不同的示例
if PlatformUtils.is_windows():
OutputFormatter.tip("Windows路径示例: C:\\Users\\YourName\\project")
OutputFormatter.tip("或使用相对路径: .\\myproject")
else:
OutputFormatter.tip("Linux/macOS路径示例: /home/yourname/project")
OutputFormatter.tip("或使用相对路径: ./myproject 或 ~/project")
print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}")
# 获取用户输入的目录
work_dir = InputValidator.get_directory(
f"{Colors.BRIGHT_CYAN}>> 请输入目录路径{Colors.ENDC}",
default=current_dir
)
# 切换到工作目录
try:
self.git_ops.change_directory(work_dir)
OutputFormatter.success(f"已切换到工作目录: {work_dir}")
except Exception as e:
OutputFormatter.error(f"切换目录失败: {str(e)}")
return False
return True
def show_welcome(self):
"""显示欢迎信息"""
from .utils import Colors
@@ -43,6 +96,11 @@ class GitManagerUI:
OutputFormatter.menu_item(5, "管理远程仓库")
OutputFormatter.menu_item(6, "退出程序")
print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}")
# 显示永久提示
OutputFormatter.tip("提交代码前建议先拉取最新代码,减少代码冲突")
OutputFormatter.tip("使用SSH进行Git提交更方便快捷和安全")
print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}")
def handle_init_repo(self):
"""处理初始化仓库"""
@@ -60,10 +118,6 @@ class GitManagerUI:
OutputFormatter.error("当前目录不是Git仓库请先初始化")
return
# 提示:先拉取再提交
OutputFormatter.tip("建议先拉取最新代码再提交,减少代码冲突")
print(f"{Colors.CYAN}{'-' * 60}{Colors.ENDC}")
# 检查是否有更改
OutputFormatter.status('running', "检查文件更改中...")
@@ -158,6 +212,11 @@ class GitManagerUI:
def run(self):
"""运行主程序"""
# 首先选择工作目录
if not self.select_work_directory():
return
# 显示欢迎信息
self.show_welcome()
while True:

View File

@@ -206,6 +206,42 @@ class PlatformUtils:
return "macOS"
else:
return "Unknown"
@staticmethod
def normalize_path(path: str) -> str:
"""
标准化路径 - 跨平台兼容
Args:
path: 输入路径
Returns:
标准化后的绝对路径
"""
# 展开用户目录 (~)
path = os.path.expanduser(path)
# 转换为绝对路径
path = os.path.abspath(path)
# 标准化路径分隔符
path = os.path.normpath(path)
return path
@staticmethod
def is_valid_directory(path: str) -> bool:
"""
检查路径是否为有效目录
Args:
path: 要检查的路径
Returns:
是否为有效目录
"""
try:
normalized_path = PlatformUtils.normalize_path(path)
return os.path.isdir(normalized_path)
except Exception:
return False
class InputValidator:
@@ -272,3 +308,40 @@ class InputValidator:
return default
return user_input in ['y', 'yes']
@staticmethod
def get_directory(prompt: str, default: str = "") -> str:
"""
获取用户输入的目录路径
Args:
prompt: 提示信息
default: 默认目录(如果用户直接回车)
Returns:
标准化后的目录路径
"""
while True:
# 显示提示
if default:
user_input = input(f"{prompt} (回车使用当前目录: {default}): ").strip()
else:
user_input = input(f"{prompt}: ").strip()
# 如果为空,使用默认值
if not user_input:
if default:
return PlatformUtils.normalize_path(default)
else:
OutputFormatter.error("目录路径不能为空")
continue
# 标准化路径
normalized_path = PlatformUtils.normalize_path(user_input)
# 检查目录是否存在
if PlatformUtils.is_valid_directory(normalized_path):
return normalized_path
else:
OutputFormatter.error(f"目录不存在: {normalized_path}")
OutputFormatter.info("请输入有效的目录路径")