forked from qt-creator/qt-creator
Terminal: Allow the underlying application to set title
Change-Id: I3530d645f16047df2546902d900e5e2fee8d071c Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
committed by
Orgad Shaneh
parent
1bd2e84f9a
commit
9a7f45cc46
@@ -219,23 +219,15 @@ void TerminalPane::setupTerminalWidget(TerminalWidget *terminal)
|
||||
if (!terminal)
|
||||
return;
|
||||
|
||||
const auto setTabText = [this, terminal]() {
|
||||
const auto setTabText = [this, terminal] {
|
||||
const int index = m_tabWidget.indexOf(terminal);
|
||||
const FilePath cwd = terminal->cwd();
|
||||
|
||||
const QString exe = terminal->currentCommand().isEmpty()
|
||||
? terminal->shellName()
|
||||
: terminal->currentCommand().executable().fileName();
|
||||
|
||||
if (cwd.isEmpty())
|
||||
m_tabWidget.setTabText(index, exe);
|
||||
else
|
||||
m_tabWidget.setTabText(index, exe + " - " + cwd.fileName());
|
||||
m_tabWidget.setTabText(index, terminal->title());
|
||||
};
|
||||
|
||||
connect(terminal, &TerminalWidget::started, this, setTabText);
|
||||
connect(terminal, &TerminalWidget::cwdChanged, this, setTabText);
|
||||
connect(terminal, &TerminalWidget::commandChanged, this, setTabText);
|
||||
connect(terminal, &TerminalWidget::titleChanged, this, setTabText);
|
||||
|
||||
if (!terminal->shellName().isEmpty())
|
||||
setTabText();
|
||||
|
||||
@@ -249,6 +249,7 @@ struct TerminalSurfacePrivate
|
||||
case VTERM_PROP_ICONNAME:
|
||||
break;
|
||||
case VTERM_PROP_TITLE:
|
||||
emit q->titleChanged(QString::fromUtf8(val->string.str, val->string.len));
|
||||
break;
|
||||
case VTERM_PROP_ALTSCREEN:
|
||||
m_altscreen = val->boolean;
|
||||
|
||||
@@ -103,6 +103,7 @@ signals:
|
||||
void altscreenChanged(bool altScreen);
|
||||
void unscroll();
|
||||
void bell();
|
||||
void titleChanged(const QString &title);
|
||||
|
||||
private:
|
||||
std::unique_ptr<TerminalSurfacePrivate> d;
|
||||
|
||||
@@ -399,6 +399,18 @@ void TerminalWidget::setupSurface()
|
||||
if (TerminalSettings::instance().audibleBell.value())
|
||||
QApplication::beep();
|
||||
});
|
||||
connect(m_surface.get(),
|
||||
&Internal::TerminalSurface::titleChanged,
|
||||
this,
|
||||
[this](const QString &title) {
|
||||
const FilePath titleFile = FilePath::fromUserInput(title);
|
||||
if (!m_title.isEmpty()
|
||||
|| m_openParameters.shellCommand.value_or(CommandLine{}).executable()
|
||||
!= titleFile) {
|
||||
m_title = titleFile.isFile() ? titleFile.baseName() : title;
|
||||
}
|
||||
emit titleChanged();
|
||||
});
|
||||
|
||||
if (m_shellIntegration) {
|
||||
connect(m_shellIntegration.get(),
|
||||
@@ -442,6 +454,17 @@ QColor TerminalWidget::toQColor(std::variant<int, QColor> color) const
|
||||
return std::get<QColor>(color);
|
||||
}
|
||||
|
||||
QString TerminalWidget::title() const
|
||||
{
|
||||
const FilePath dir = cwd();
|
||||
QString title = m_title;
|
||||
if (title.isEmpty())
|
||||
title = currentCommand().isEmpty() ? shellName() : currentCommand().executable().fileName();
|
||||
if (dir.isEmpty())
|
||||
return title;
|
||||
return title + " - " + dir.fileName();
|
||||
}
|
||||
|
||||
void TerminalWidget::updateCopyState()
|
||||
{
|
||||
if (!hasFocus())
|
||||
|
||||
@@ -82,6 +82,7 @@ public:
|
||||
|
||||
void setShellName(const QString &shellName);
|
||||
QString shellName() const;
|
||||
QString title() const;
|
||||
|
||||
Utils::FilePath cwd() const;
|
||||
Utils::CommandLine currentCommand() const;
|
||||
@@ -98,6 +99,7 @@ signals:
|
||||
void started(qint64 pid);
|
||||
void cwdChanged(const Utils::FilePath &cwd);
|
||||
void commandChanged(const Utils::CommandLine &cmd);
|
||||
void titleChanged();
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
@@ -207,6 +209,7 @@ private:
|
||||
bool m_ignoreScroll{false};
|
||||
|
||||
QString m_preEditString;
|
||||
QString m_title;
|
||||
|
||||
std::optional<Selection> m_selection;
|
||||
std::optional<LinkSelection> m_linkSelection;
|
||||
|
||||
4
src/plugins/terminal/tests/title
Executable file
4
src/plugins/terminal/tests/title
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo -e "\033[1m ⎆ The terminal title should have changed to 'Test Title'\033[0m"
|
||||
echo -e "\033]0;Test Title\007"
|
||||
Reference in New Issue
Block a user