完成远程命令的标签

This commit is contained in:
2025-08-31 11:07:11 +08:00
parent b7cdaa02c8
commit 12a3457fe9
4 changed files with 177 additions and 1 deletions

View File

@@ -161,6 +161,51 @@ class RemoteCommandsTab(QWidget):
git_group.setLayout(git_layout)
main_layout.addWidget(git_group)
# 目录管理组
dir_group = QGroupBox("目录管理")
dir_layout = QVBoxLayout()
# 当前目录显示
current_dir_layout = QHBoxLayout()
current_dir_layout.addWidget(QLabel("当前目录:"))
self.current_dir_display = QLineEdit()
# 允许用户输入当前目录路径
# self.current_dir_display.setReadOnly(True)
# 添加回车键刷新目录功能
self.current_dir_display.returnPressed.connect(self.refresh_directory)
current_dir_layout.addWidget(self.current_dir_display)
# 刷新目录按钮
self.refresh_dir_button = QPushButton("刷新目录")
self.refresh_dir_button.clicked.connect(self.refresh_directory)
current_dir_layout.addWidget(self.refresh_dir_button)
dir_layout.addLayout(current_dir_layout)
# 目录列表
dir_list_layout = QHBoxLayout()
dir_list_layout.addWidget(QLabel("目录内容:"))
self.dir_list_text = QTextEdit()
self.dir_list_text.setReadOnly(True)
self.dir_list_text.setMaximumHeight(150)
dir_list_layout.addWidget(self.dir_list_text)
dir_layout.addLayout(dir_list_layout)
# 删除目录
delete_dir_layout = QHBoxLayout()
delete_dir_layout.addWidget(QLabel("删除目录:"))
self.delete_dir_input = QLineEdit()
delete_dir_layout.addWidget(self.delete_dir_input)
self.delete_dir_button = QPushButton("删除目录")
self.delete_dir_button.clicked.connect(self.delete_directory)
delete_dir_layout.addWidget(self.delete_dir_button)
dir_layout.addLayout(delete_dir_layout)
dir_group.setLayout(dir_layout)
main_layout.addWidget(dir_group)
# 命令输出区域
output_group = QGroupBox("命令输出")
output_layout = QVBoxLayout()
@@ -198,6 +243,11 @@ class RemoteCommandsTab(QWidget):
logger.info(f"设置服务器配置: git_url={git_url}, remote_dir={remote_dir}")
self.repo_url_input.setText(git_url)
self.remote_dir_display.setText(remote_dir)
# 如果设置了远程目录,也设置为当前目录
if remote_dir:
self.current_dir_display.setText(remote_dir)
self.refresh_directory()
def install_git(self):
logger.info("安装Git")
@@ -269,7 +319,81 @@ class RemoteCommandsTab(QWidget):
self.status_label.setText(message)
self.status_label.setStyleSheet("color: green;")
QMessageBox.information(self, "成功", message)
# 如果是目录相关操作,刷新目录列表
if "删除" in message or "目录" in message:
self.refresh_directory()
else:
self.status_label.setText(message)
self.status_label.setStyleSheet("color: red;")
QMessageBox.warning(self, "错误", message)
QMessageBox.warning(self, "错误", message)
def refresh_directory(self):
logger.info("刷新目录列表")
if not self.ssh_client:
QMessageBox.warning(self, "警告", "请先连接到服务器")
return
current_dir = self.current_dir_display.text().strip()
if not current_dir:
current_dir = "~" # 默认用户主目录
self.current_dir_display.setText(current_dir)
logger.info(f"使用默认目录: {current_dir}")
else:
logger.info(f"使用用户输入目录: {current_dir}")
self.status_label.setText("正在刷新目录列表...")
# 创建并启动线程执行命令
command = f"cd {current_dir} && pwd && ls -la"
self.command_thread = RemoteCommandThread(self.ssh_client, command)
self.command_thread.output_signal.connect(self.append_dir_output)
self.command_thread.finished_signal.connect(self.on_dir_refresh_finished)
self.command_thread.start()
def append_dir_output(self, text):
self.dir_list_text.append(text)
def on_dir_refresh_finished(self, success, message):
if success:
self.status_label.setText("目录列表已刷新")
self.status_label.setStyleSheet("color: green;")
else:
self.status_label.setText("刷新目录列表失败")
self.status_label.setStyleSheet("color: red;")
QMessageBox.warning(self, "错误", f"刷新目录列表失败: {message}")
def delete_directory(self):
logger.info("删除目录")
if not self.ssh_client:
QMessageBox.warning(self, "警告", "请先连接到服务器")
return
dir_to_delete = self.delete_dir_input.text().strip()
if not dir_to_delete:
QMessageBox.warning(self, "警告", "请输入要删除的目录名")
return
current_dir = self.current_dir_display.text().strip()
if not current_dir:
current_dir = "~"
# 确认删除
reply = QMessageBox.question(self, "确认删除",
f"确定要删除目录 '{current_dir}/{dir_to_delete}' 及其所有内容吗?\n此操作不可撤销!",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.No:
return
self.output_text.clear()
self.status_label.setText("正在删除目录...")
# 创建并启动线程执行命令
command = f"cd {current_dir} && rm -rf {dir_to_delete}"
self.command_thread = RemoteCommandThread(self.ssh_client, command)
self.command_thread.output_signal.connect(self.append_output)
self.command_thread.finished_signal.connect(self.on_command_finished)
self.command_thread.start()