From 9a7f45cc460e178d983287bd1cb3c45efe1d8c3c Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Mon, 3 Jul 2023 12:21:30 +0300 Subject: [PATCH] Terminal: Allow the underlying application to set title Change-Id: I3530d645f16047df2546902d900e5e2fee8d071c Reviewed-by: Marcus Tillmanns --- src/plugins/terminal/terminalpane.cpp | 14 +++----------- src/plugins/terminal/terminalsurface.cpp | 1 + src/plugins/terminal/terminalsurface.h | 1 + src/plugins/terminal/terminalwidget.cpp | 23 +++++++++++++++++++++++ src/plugins/terminal/terminalwidget.h | 3 +++ src/plugins/terminal/tests/title | 4 ++++ 6 files changed, 35 insertions(+), 11 deletions(-) create mode 100755 src/plugins/terminal/tests/title diff --git a/src/plugins/terminal/terminalpane.cpp b/src/plugins/terminal/terminalpane.cpp index d30d7f79357..196a9acd3da 100644 --- a/src/plugins/terminal/terminalpane.cpp +++ b/src/plugins/terminal/terminalpane.cpp @@ -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(); diff --git a/src/plugins/terminal/terminalsurface.cpp b/src/plugins/terminal/terminalsurface.cpp index ea994302318..3bd355fa552 100644 --- a/src/plugins/terminal/terminalsurface.cpp +++ b/src/plugins/terminal/terminalsurface.cpp @@ -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; diff --git a/src/plugins/terminal/terminalsurface.h b/src/plugins/terminal/terminalsurface.h index a6fc7425d48..f6e463ac8dd 100644 --- a/src/plugins/terminal/terminalsurface.h +++ b/src/plugins/terminal/terminalsurface.h @@ -103,6 +103,7 @@ signals: void altscreenChanged(bool altScreen); void unscroll(); void bell(); + void titleChanged(const QString &title); private: std::unique_ptr d; diff --git a/src/plugins/terminal/terminalwidget.cpp b/src/plugins/terminal/terminalwidget.cpp index de464b326cb..d232bfee277 100644 --- a/src/plugins/terminal/terminalwidget.cpp +++ b/src/plugins/terminal/terminalwidget.cpp @@ -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 color) const return std::get(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()) diff --git a/src/plugins/terminal/terminalwidget.h b/src/plugins/terminal/terminalwidget.h index 11cd9335cc9..66f721df5da 100644 --- a/src/plugins/terminal/terminalwidget.h +++ b/src/plugins/terminal/terminalwidget.h @@ -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 m_selection; std::optional m_linkSelection; diff --git a/src/plugins/terminal/tests/title b/src/plugins/terminal/tests/title new file mode 100755 index 00000000000..d6dc5193125 --- /dev/null +++ b/src/plugins/terminal/tests/title @@ -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"