Commit e936c6cb authored by Luis Javier Merino's avatar Luis Javier Merino Committed by Tomaz Canabrava
Browse files

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.
parent 69a8340d
Pipeline #199397 passed with stage
in 3 minutes and 46 seconds
......@@ -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;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment