Terminal: added basic conditional title patterns.

* You can now insert arbitrary text only if the following or previous
  placeholder does not resolve to an empty value using the %<, %> and
  %- placeholders.
* Additionally, any non-alpha numeric character between % and the
  placeholder character will only be displayed if the placeholder does
  not resolve to an empty value, too.
* All of this allows you to get rid of the extra space between
  "Terminal" and ":" before the current path -- which is now the
  default.
This commit is contained in:
Axel Dörfler 2015-02-19 17:41:22 +00:00
parent caf2bf0181
commit dbf8c834a2
5 changed files with 82 additions and 17 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2001-2010, Haiku, Inc.
* Copyright 2001-2015, Haiku, Inc.
* Copyright 2003-2004 Kian Duffy, myob@users.sourceforge.net
* Parts Copyright 1998-1999 Kazuho Okui and Takashi Murai.
* All rights reserved. Distributed under the terms of the MIT license.
@ -133,7 +133,8 @@ AppearancePrefView::AppearancePrefView(const char* name,
new BMessage(MSG_TAB_TITLE_SETTING_CHANGED));
fTabTitle->SetToolTip(BString(B_TRANSLATE(
"The pattern specifying the tab titles. The following placeholders\n"
"can be used:\n")) << kTooTipSetTabTitlePlaceholders);
"can be used:")) << "\n" << kTooTipSetTabTitlePlaceholders
<< "\n" << kToolTipCommonTitlePlaceholders);
fWindowTitle = new BTextControl("windowTitle", B_TRANSLATE("Window title:"),
"", NULL);
@ -141,7 +142,8 @@ AppearancePrefView::AppearancePrefView(const char* name,
new BMessage(MSG_WINDOW_TITLE_SETTING_CHANGED));
fWindowTitle->SetToolTip(BString(B_TRANSLATE(
"The pattern specifying the window titles. The following placeholders\n"
"can be used:\n")) << kTooTipSetWindowTitlePlaceholders);
"can be used:")) << "\n" << kTooTipSetWindowTitlePlaceholders
<< "\n" << kToolTipCommonTitlePlaceholders);
BLayoutBuilder::Group<>(this)
.SetInsets(5, 5, 5, 5)

View File

@ -1,5 +1,6 @@
/*
* Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
* Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
* Distributed under the terms of the MIT License.
*/
@ -19,24 +20,69 @@
PatternEvaluator::Evaluate(const char* pattern, PlaceholderMapper& mapper)
{
BString result;
BString before;
bool isBefore = false;
bool isAfter = false;
bool hadResult = false;
bool began = false;
while (*pattern != '\0') {
// find next placeholder
const char* placeholder = strchr(pattern, '%');
if (placeholder == NULL)
return result.Append(pattern);
size_t length = 0;
if (placeholder != NULL)
length = placeholder - pattern;
else
length = INT_MAX;
// append skipped chars
if (placeholder != pattern)
result.Append(pattern, placeholder - pattern);
if (placeholder != pattern) {
if (isBefore) {
before.SetTo(pattern, length);
isBefore = false;
} else if (!isAfter || hadResult) {
result.Append(pattern, length);
isBefore = false;
before.SetTo(NULL);
isAfter = false;
}
}
if (placeholder == NULL)
return result;
pattern = placeholder + 1;
// check for an escaped '%'
if (*pattern == '%') {
result += '%';
// check for special placeholders
switch (pattern[0]) {
case '%':
// An escaped '%'
result += '%';
pattern++;
continue;
case '<':
// An optional before string
isBefore = began = true;
hadResult = false;
pattern++;
continue;
case '>':
// An optional after string
isAfter = true;
began = false;
before.SetTo(NULL);
pattern++;
continue;
case '-':
// End of any other section; ignore
pattern++;
isBefore = false;
isAfter = false;
continue;
}
// Count non alpha numeric characters to the before section
while (pattern[0] != '\0' && !isalnum(pattern[0])) {
before.Append(pattern[0], 1);
pattern++;
continue;
}
// parse a number, if there is one
@ -53,12 +99,19 @@ PatternEvaluator::Evaluate(const char* pattern, PlaceholderMapper& mapper)
if (*pattern != '\0' && mapper.MapPlaceholder(*pattern,
number, hasNumber, mappedValue)) {
// mapped successfully -- append the replacement string
if (began && !mappedValue.IsEmpty())
hadResult = true;
if (!before.IsEmpty() && !mappedValue.IsEmpty()) {
result += before;
before.SetTo(NULL);
}
result += mappedValue;
pattern++;
} else {
// something went wrong -- just append the literal part of the
// pattern
result.Append(placeholder, pattern - placeholder);
result.Append(placeholder, length);
}
}
@ -66,7 +119,6 @@ PatternEvaluator::Evaluate(const char* pattern, PlaceholderMapper& mapper)
}
// #pragma mark - PlaceholderMapper

View File

@ -1,5 +1,5 @@
/*
* Copyright 2003-2013, Haiku, Inc. All Rights Reserved.
* Copyright 2003-2015, Haiku, Inc. All Rights Reserved.
* Copyright (c) 2004 Daniel Furrer <assimil8or@users.sourceforge.net>
* Copyright (c) 2003-4 Kian Duffy <myob@users.sourceforge.net>
* Copyright (c) 1998,99 Kazuho Okui and Takashi Murai.
@ -85,7 +85,7 @@ static const pref_defaults kTermDefaults[] = {
{ PREF_IM_AWARE, "0"},
{ PREF_TAB_TITLE, "%1d: %p%e" },
{ PREF_WINDOW_TITLE, "%T %i: %t" },
{ PREF_WINDOW_TITLE, "%T% i: %t" },
{ PREF_BLINK_CURSOR, PREF_TRUE },
{ PREF_WARN_ON_EXIT, PREF_TRUE },
{ PREF_CURSOR_STYLE, PREF_BLOCK_CURSOR },

View File

@ -30,8 +30,18 @@ const char* const kTooTipSetWindowTitlePlaceholders = B_TRANSLATE(
"\t%e\t-\tThe encoding of the current tab. Not shown for UTF-8.\n"
"\t%i\t-\tThe index of the window.\n"
"\t%p\t-\tThe name of the active process in the current tab.\n"
"\t%t\t-\tThe title of the current tab.\n"
"\t%%\t-\tThe character '%'.");
"\t%t\t-\tThe title of the current tab.");
const char* const kToolTipCommonTitlePlaceholders = B_TRANSLATE(
"\t%%\t-\tThe character '%'.\n"
"\t%<\t-\tStarts a section that will only be shown if a placeholder\n"
"\t\t\tafterwards is not empty.\n"
"\t%>\t-\tStarts a section that will only be shown if a placeholder\n"
"\t\t\tbetween a previous %< section and this one is not empty.\n"
"\t%-\t-\tEnds a %< or %> section.\n\n"
"Any non alpha numeric character between '%' and the format "
"letter will insert a space only\nif the placeholder value is not "
"empty. It will add to the %< section.");
const char* const kShellEscapeCharacters = " ~`#$&*()\\|[]{};'\"<>?!";
const char* const kDefaultAdditionalWordCharacters = ":@-./_~";

View File

@ -143,6 +143,7 @@ static const char* const PREF_WINDOW_TITLE = "Window title";
// shared strings
extern const char* const kTooTipSetTabTitlePlaceholders;
extern const char* const kTooTipSetWindowTitlePlaceholders;
extern const char* const kToolTipCommonTitlePlaceholders;
extern const char* const kShellEscapeCharacters;
extern const char* const kDefaultAdditionalWordCharacters;