Skip to content

Commit

Permalink
Add custom Local Shell Profile path feature for Windows
Browse files Browse the repository at this point in the history
Signed-off-by: xiaoming <[email protected]>
  • Loading branch information
QQxiaoming committed Aug 22, 2024
1 parent 7fdc00d commit fbf3442
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ en-US:
- Add custom color feature to the highlight
- Allow editing the text to be pasted when confirming multiple lines of paste
- Allow the tab title to display a custom session name
- Add custom Local Shell Profile path feature for Windows
- Fix the issue that clicking the new tab button in split screen mode may not create the session correctly or be located under the wrong tab group
- Fix the issue that the SSH connection cannot be reconnected by tapping the Enter key in some cases
- Fix the issue that the target session object is inaccurate when locking/unlocking the session
Expand All @@ -25,6 +26,7 @@ zh-CN:
- 高亮功能增加自定义颜色功能
- 多行粘贴确认时允许编辑待粘贴文本
- 标签栏标题允许显示自定义会话名称
- Windows增加自定义Local Shell Profile路径功能
- 修复分屏模式下某些情况点击新标签按钮会话未正确创建或位于错误的标签页组下
- 修复ssh连接部分情况下无法通过敲击回车键发起重连的问题
- 修复锁定/解锁会话时目标会话对象不准确
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Add custom color feature to the highlight
- Allow editing the text to be pasted when confirming multiple lines of paste
- Allow the tab title to display a custom session name
- Add custom Local Shell Profile path feature for Windows
- Fix the issue that clicking the new tab button in split screen mode may not create the session correctly or be located under the wrong tab group
- Fix the issue that the SSH connection cannot be reconnected by tapping the Enter key in some cases
- Fix the issue that the target session object is inaccurate when locking/unlocking the session
Expand Down
11 changes: 11 additions & 0 deletions src/globaloptions/globaloptionsadvancedwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ GlobalOptionsAdvancedWidget::GlobalOptionsAdvancedWidget(QWidget *parent) :
#else
ui->checkBoxEnableCtrlC->setVisible(false);
#endif
#if defined(Q_OS_WIN)
ui->labelPowerShellProfile->setVisible(true);
ui->lineEditPowerShellProfile->setVisible(true);
ui->toolButtonPowerShellProfile->setVisible(true);
ui->pushButtonPowerShellProfile->setVisible(true);
#else
ui->labelPowerShellProfile->setVisible(false);
ui->lineEditPowerShellProfile->setVisible(false);
ui->toolButtonPowerShellProfile->setVisible(false);
ui->pushButtonPowerShellProfile->setVisible(false);
#endif
}

GlobalOptionsAdvancedWidget::~GlobalOptionsAdvancedWidget()
Expand Down
43 changes: 39 additions & 4 deletions src/globaloptions/globaloptionsadvancedwidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,41 @@
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_32">
<item>
<widget class="QLabel" name="labelPowerShellProfile">
<property name="text">
<string>PowerShell Profile</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEditPowerShellProfile">
<property name="toolTip">
<string>Set the PowerShell profile path</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="toolButtonPowerShellProfile">
<property name="toolTip">
<string>Choose the PowerShell profile path</string>
</property>
<property name="text">
<string notr="true">...</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButtonPowerShellProfile">
<property name="text">
<string>Open</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="spacing">
Expand Down Expand Up @@ -186,12 +221,12 @@
</item>
<item>
<widget class="QCheckBox" name="checkBoxEnableCtrlC">
<property name="text">
<string>Enable Ctrl+C for copy mode</string>
</property>
<property name="toolTip">
<string>Enable Ctrl+C for copy when characters are selected</string>
</property>
<property name="text">
<string>Enable Ctrl+C for copy mode</string>
</property>
</widget>
</item>
<item>
Expand All @@ -217,7 +252,7 @@
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
<enum>Qt::Orientation::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
Expand Down
34 changes: 34 additions & 0 deletions src/globaloptions/globaloptionswindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ GlobalOptionsWindow::GlobalOptionsWindow(QWidget *parent) :
globalOptionsTerminalWidget->ui->checkBoxEcho->setChecked(settings.value("Echo", false).toBool());
#if defined(Q_OS_WIN) || defined(Q_OS_LINUX)
globalOptionsAdvancedWidget->ui->checkBoxEnableCtrlC->setChecked(settings.value("EnableCtrlC", false).toBool());
#endif
#if defined(Q_OS_WIN)
globalOptionsAdvancedWidget->ui->lineEditPowerShellProfile->setText(settings.value("PowerShellProfile", QApplication::applicationDirPath() + "/Profile.ps1").toString());
#endif
QString defaultLocalShell = settings.value("DefaultLocalShell",
#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
Expand Down Expand Up @@ -348,6 +351,24 @@ GlobalOptionsWindow::GlobalOptionsWindow(QWidget *parent) :
#endif
}
});
#if defined(Q_OS_WIN)
connect(globalOptionsAdvancedWidget->ui->toolButtonPowerShellProfile, &QToolButton::clicked, this, [&](){
QString profile = FileDialog::getOpenFileName(this, tr("Select PowerShell Profile"), globalOptionsAdvancedWidget->ui->lineEditPowerShellProfile->text(), tr("PowerShell Files (*.ps1)"));
if (!profile.isEmpty()) {
QFileInfo profileInfo(profile);
if(!profileInfo.exists() || !profileInfo.isFile()) {
QMessageBox::warning(this, tr("Warning"), tr("The PowerShell Profile is not a valid file!"));
return;
}
globalOptionsAdvancedWidget->ui->lineEditPowerShellProfile->setText(profile);
} else {
globalOptionsAdvancedWidget->ui->lineEditPowerShellProfile->setText(QApplication::applicationDirPath() + "/Profile.ps1");
}
});
connect(globalOptionsAdvancedWidget->ui->pushButtonPowerShellProfile, &QPushButton::clicked, this, [&](){
QDesktopServices::openUrl(QUrl::fromLocalFile(globalOptionsAdvancedWidget->ui->lineEditPowerShellProfile->text()));
});
#endif
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &GlobalOptionsWindow::buttonBoxAccepted);
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &GlobalOptionsWindow::buttonBoxRejected);

Expand Down Expand Up @@ -627,6 +648,13 @@ bool GlobalOptionsWindow::getEnableCtrlC(void)
}
#endif

#if defined(Q_OS_WIN)
QString GlobalOptionsWindow::getPowerShellProfile(void)
{
return globalOptionsAdvancedWidget->ui->lineEditPowerShellProfile->text();
}
#endif

void GlobalOptionsWindow::buttonBoxAccepted(void)
{
GlobalSetting settings;
Expand Down Expand Up @@ -684,6 +712,9 @@ void GlobalOptionsWindow::buttonBoxAccepted(void)
settings.setValue("Echo", globalOptionsTerminalWidget->ui->checkBoxEcho->isChecked());
#if defined(Q_OS_WIN) || defined(Q_OS_LINUX)
settings.setValue("EnableCtrlC", globalOptionsAdvancedWidget->ui->checkBoxEnableCtrlC->isChecked());
#endif
#if defined(Q_OS_WIN)
settings.setValue("PowerShellProfile", globalOptionsAdvancedWidget->ui->lineEditPowerShellProfile->text());
#endif
QString defaultLocalShell = globalOptionsAdvancedWidget->ui->lineEditDefaultLocalShell->text();
#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
Expand Down Expand Up @@ -768,6 +799,9 @@ void GlobalOptionsWindow::buttonBoxRejected(void)
globalOptionsTerminalWidget->ui->checkBoxEcho->setChecked(settings.value("Echo", false).toBool());
#if defined(Q_OS_WIN) || defined(Q_OS_LINUX)
globalOptionsAdvancedWidget->ui->checkBoxEnableCtrlC->setChecked(settings.value("EnableCtrlC", false).toBool());
#endif
#if defined(Q_OS_WIN)
globalOptionsAdvancedWidget->ui->lineEditPowerShellProfile->setText(settings.value("PowerShellProfile", QApplication::applicationDirPath() + "/Profile.ps1").toString());
#endif
globalOptionsAdvancedWidget->ui->lineEditDefaultLocalShell->setText(settings.value("DefaultLocalShell",
#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
Expand Down
3 changes: 3 additions & 0 deletions src/globaloptions/globaloptionswindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class GlobalOptionsWindow : public QDialog
QColor getCursorColor(void);
#if defined(Q_OS_WIN) || defined(Q_OS_LINUX)
bool getEnableCtrlC(void);
#endif
#if defined(Q_OS_WIN)
QString getPowerShellProfile(void);
#endif
bool updateColorButtons(const QString &text);
void switchTheme(void);
Expand Down
10 changes: 9 additions & 1 deletion src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4190,7 +4190,11 @@ QString CentralWidget::startLocalShellSession(MainWidgetGroup *group, int groupI
sessionsWindow->setName(name);
QFileInfo workingDirectoryInfo(workingDirectory);
sessionsWindow->setWorkingDirectory(workingDirectoryInfo.isDir()?workingDirectory:QDir::homePath());
#if defined(Q_OS_WIN)
sessionsWindow->startLocalShellSession(command,globalOptionsWindow->getPowerShellProfile());
#else
sessionsWindow->startLocalShellSession(command);
#endif
sessionList.push_back(sessionsWindow);
connect(sessionsWindow, &SessionsWindow::titleChanged, this, [=](int title,const QString& newTitle){
if(title == 0 || title == 2) {
Expand Down Expand Up @@ -4241,7 +4245,7 @@ QString CentralWidget::startWslSession(MainWidgetGroup *group, int groupIndex, c
sessionsWindow->setName(name);
QFileInfo workingDirectoryInfo(workingDirectory);
sessionsWindow->setWorkingDirectory(workingDirectoryInfo.isDir()?workingDirectory:QDir::homePath());
sessionsWindow->startLocalShellSession(command,SessionsWindow::WSL);
sessionsWindow->startLocalShellSession(command,globalOptionsWindow->getPowerShellProfile(),SessionsWindow::WSL);
sessionList.push_back(sessionsWindow);
connect(sessionsWindow, &SessionsWindow::titleChanged, this, [=](int title,const QString& newTitle){
if(title == 0 || title == 2) {
Expand Down Expand Up @@ -4539,7 +4543,11 @@ int CentralWidget::cloneTargetSession(MainWidgetGroup *group, QString name,Sessi
checkSessionName(name);
}
sessionsWindowClone->setName(name);
#if defined(Q_OS_WIN)
sessionsWindowClone->cloneSession(sessionsWindow,globalOptionsWindow->getPowerShellProfile());
#else
sessionsWindowClone->cloneSession(sessionsWindow);
#endif
sessionList.push_back(sessionsWindowClone);
connect(sessionsWindowClone, &SessionsWindow::titleChanged, this, [=](int title,const QString& newTitle){
if(title == 0 || title == 2) {
Expand Down
16 changes: 11 additions & 5 deletions src/sessionswindow/sessionswindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,11 +756,11 @@ void SessionsWindow::preprocesseData(QByteArray &data) {
}
}

void SessionsWindow::cloneSession(SessionsWindow *src) {
void SessionsWindow::cloneSession(SessionsWindow *src, QString profile) {
switch(src->getSessionType()) {
case LocalShell: {
setWorkingDirectory(src->getWorkingDirectory());
startLocalShellSession(src->m_command, src->getShellType());
startLocalShellSession(src->m_command, profile, src->getShellType());
break;
case Telnet:
startTelnetSession(src->m_hostname, src->m_port, src->m_type);
Expand Down Expand Up @@ -788,19 +788,20 @@ void SessionsWindow::cloneSession(SessionsWindow *src) {
}
}

int SessionsWindow::startLocalShellSession(const QString &command, ShellType sTp) {
int SessionsWindow::startLocalShellSession(const QString &command,QString profile, ShellType sTp) {
QString shellPath;
QStringList args;
shellType = sTp;
if(command.isEmpty()) {
#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
Q_UNUSED(profile);
GlobalSetting settings;
QString defaultLocalShell = settings.value("Global/Options/DefaultLocalShell", "ENV:SHELL").toString();
if(defaultLocalShell != "ENV:SHELL") {
QFileInfo shellInfo(defaultLocalShell);
if(shellInfo.isExecutable()) {
shellPath = defaultLocalShell;
}
}
}
if(shellPath.isEmpty()) shellPath = qEnvironmentVariable("SHELL");
if(shellPath.isEmpty()) shellPath = "/bin/sh";
Expand All @@ -825,8 +826,13 @@ int SessionsWindow::startLocalShellSession(const QString &command, ShellType sTp
"-NoProfile",
"-NoExit",
"-File",
"\"" + QApplication::applicationDirPath() + "/Profile.ps1\""
};
if(profile.isEmpty()) {
profile = QString("\"") + QApplication::applicationDirPath() + "/Profile.ps1\"";
} else {
profile = QString("\"") + profile + "\"";
}
args.append(profile);
break;
}
case WSL:
Expand Down
6 changes: 3 additions & 3 deletions src/sessionswindow/sessionswindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ class SessionsWindow : public QObject
SessionsWindow(SessionType tp, QWidget *parent = nullptr);
~SessionsWindow();

void cloneSession(SessionsWindow *src);
void cloneSession(SessionsWindow *src, QString profile = QString());
#if defined(Q_OS_WIN)
int startLocalShellSession(const QString &command, ShellType sTp = PowerShell);
int startLocalShellSession(const QString &command, QString profile = QString(), ShellType sTp = PowerShell);
#else
int startLocalShellSession(const QString &command, ShellType sTp = UnixShell);
int startLocalShellSession(const QString &command, QString profile = QString(), ShellType sTp = UnixShell);
#endif
int startTelnetSession(const QString &hostname, quint16 port, QTelnet::SocketType type);
int startSerialSession(const QString &portName, uint32_t baudRate,
Expand Down

0 comments on commit fbf3442

Please sign in to comment.