From e936c6cb1cb2308a970b4fe6a52db7c3cd96e3bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Javier=20Merino=20Mor=C3=A1n?= Date: Wed, 1 Jun 2022 18:07:46 +0200 Subject: [PATCH] Improvements to Session::getDynamicTitle() Walk over LocalTabTitle just once looking for percent signs, and doing the appropriate substitution based on the next character. This not only is slightly more performant, but also avoids trouble if one of the substitutions happens to contain a percent sign. --- src/session/Session.cpp | 120 ++++++++++++++++++++++++++++------------ 1 file changed, 86 insertions(+), 34 deletions(-) diff --git a/src/session/Session.cpp b/src/session/Session.cpp index 3d32f228b..271453b32 100644 --- a/src/session/Session.cpp +++ b/src/session/Session.cpp @@ -1127,48 +1127,100 @@ QString Session::getDynamicTitle() QString title = tabTitleFormat(Session::LocalTabTitle); // search for and replace known marker - int UID = process->userId(&ok); - if (!ok) { - title.replace(QLatin1String("%B"), QStringLiteral("-")); - } else { - // title.replace(QLatin1String("%I"), QString::number(UID)); - if (UID == 0) { - title.replace(QLatin1String("%B"), QStringLiteral("#")); - } else { - title.replace(QLatin1String("%B"), QStringLiteral("$")); - } - } - - title.replace(QLatin1String("%u"), process->userName()); - title.replace(QLatin1String("%h"), Konsole::ProcessInfo::localHost()); - title.replace(QLatin1String("%n"), process->name(&ok)); - - title.replace(QLatin1String("%w"), userTitle()); - title.replace(QLatin1String("%#"), QString::number(sessionId())); - QString dir = _reportedWorkingUrl.toLocalFile(); - ok = true; + bool dirOk = true; if (dir.isEmpty()) { // update current directory from process updateWorkingDirectory(); // Previous process may have been freed in updateSessionProcessInfo() process = getProcessInfo(); - dir = process->currentDir(&ok); + dir = process->currentDir(&dirOk); } - if (!ok) { - title.replace(QLatin1String("%d"), QStringLiteral("-")); - title.replace(QLatin1String("%D"), QStringLiteral("-")); - } else { - // allow for shortname to have the ~ as homeDir - const QString homeDir = process->userHomeDir(); - if (!homeDir.isEmpty()) { - if (dir.startsWith(homeDir)) { - dir.remove(0, homeDir.length()); - dir.prepend(QLatin1Char('~')); - } + + int pos = 0; + while ((pos = title.indexOf(QLatin1Char('%'), pos)) != -1) { + if (pos >= title.size() - 1) { + break; + } + + switch (title.at(pos + 1).toLatin1()) { + case 'B': { + int UID = process->userId(&ok); + if (!ok) { + title.replace(pos, 2, QStringLiteral("-")); + pos--; + } else { + // title.replace(QLatin1String("%I"), QString::number(UID)); + if (UID == 0) { + title.replace(pos, 2, QStringLiteral("#")); + pos--; + } else { + title.replace(pos, 2, QStringLiteral("$")); + pos--; + } + } + } break; + case 'u': { + QString replacement = process->userName(); + title.replace(pos, 2, replacement); + pos += replacement.size() - 2; + } break; + case 'h': { + QString replacement = Konsole::ProcessInfo::localHost(); + title.replace(pos, 2, replacement); + pos += replacement.size() - 2; + } break; + case 'n': { + QString replacement = process->name(&ok); + title.replace(pos, 2, replacement); + pos += replacement.size() - 2; + } break; + case 'w': { + QString replacement = userTitle(); + title.replace(pos, 2, replacement); + pos += replacement.size() - 2; + } break; + case '#': { + QString replacement = QString::number(sessionId()); + title.replace(pos, 2, replacement); + pos += replacement.size() - 2; + } break; + case 'd': + if (!dirOk) { + title.replace(pos, 2, QStringLiteral("-")); + pos--; + } else { + // allow for shortname to have the ~ as homeDir + const QString homeDir = process->userHomeDir(); + if (!homeDir.isEmpty()) { + if (dir.startsWith(homeDir)) { + dir.remove(0, homeDir.length()); + dir.prepend(QLatin1Char('~')); + } + } + const QString replacement = process->formatShortDir(dir); + title.replace(pos, 2, replacement); + pos += replacement.size() - 2; + } + break; + case 'D': + if (!dirOk) { + title.replace(pos, 2, QStringLiteral("-")); + pos--; + } else { + // allow for shortname to have the ~ as homeDir + const QString homeDir = process->userHomeDir(); + if (!homeDir.isEmpty()) { + if (dir.startsWith(homeDir)) { + dir.remove(0, homeDir.length()); + dir.prepend(QLatin1Char('~')); + } + } + title.replace(pos, 2, dir); + pos += dir.size() - 2; + } + break; } - title.replace(QLatin1String("%D"), dir); - title.replace(QLatin1String("%d"), process->formatShortDir(dir)); } return title;