[haiku-commits] haiku: hrev48816 - src/apps/terminal

  • From: axeld@xxxxxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Thu, 19 Feb 2015 22:41:02 +0100 (CET)

hrev48816 adds 1 changeset to branch 'master'
old head: caf2bf0181d1216576f9c06b6c6886194241c325
new head: dbf8c834a25bf2e138e2163ff110d62b663c9736
overview: 
http://cgit.haiku-os.org/haiku/log/?qt=range&q=dbf8c834a25b+%5Ecaf2bf0181d1

----------------------------------------------------------------------------

dbf8c834a25b: 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.

                                   [ Axel Dörfler <axeld@xxxxxxxxxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev48816
Commit:      dbf8c834a25bf2e138e2163ff110d62b663c9736
URL:         http://cgit.haiku-os.org/haiku/commit/?id=dbf8c834a25b
Author:      Axel Dörfler <axeld@xxxxxxxxxxxxxxxx>
Date:        Thu Feb 19 17:41:22 2015 UTC

----------------------------------------------------------------------------

5 files changed, 82 insertions(+), 17 deletions(-)
src/apps/terminal/AppearPrefView.cpp   |  8 ++--
src/apps/terminal/PatternEvaluator.cpp | 72 +++++++++++++++++++++++++-----
src/apps/terminal/PrefHandler.cpp      |  4 +-
src/apps/terminal/TermConst.cpp        | 14 +++++-
src/apps/terminal/TermConst.h          |  1 +

----------------------------------------------------------------------------

diff --git a/src/apps/terminal/AppearPrefView.cpp 
b/src/apps/terminal/AppearPrefView.cpp
index ceb1c05..67a5ab1 100644
--- a/src/apps/terminal/AppearPrefView.cpp
+++ b/src/apps/terminal/AppearPrefView.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright 2001-2010, Haiku, Inc.
+ * Copyright 2001-2015, Haiku, Inc.
  * Copyright 2003-2004 Kian Duffy, myob@xxxxxxxxxxxxxxxxxxxxx
  * 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)
diff --git a/src/apps/terminal/PatternEvaluator.cpp 
b/src/apps/terminal/PatternEvaluator.cpp
index cfe15f8..e5ded68 100644
--- a/src/apps/terminal/PatternEvaluator.cpp
+++ b/src/apps/terminal/PatternEvaluator.cpp
@@ -1,5 +1,6 @@
 /*
  * Copyright 2010, Ingo Weinhold, ingo_weinhold@xxxxxx.
+ * Copyright 2015, Axel Dörfler, axeld@xxxxxxxxxxxxxxxx.
  * 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
 
 
diff --git a/src/apps/terminal/PrefHandler.cpp 
b/src/apps/terminal/PrefHandler.cpp
index 7147f96..76508c9 100644
--- a/src/apps/terminal/PrefHandler.cpp
+++ b/src/apps/terminal/PrefHandler.cpp
@@ -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@xxxxxxxxxxxxxxxxxxxxx>
  * Copyright (c) 2003-4 Kian Duffy <myob@xxxxxxxxxxxxxxxxxxxxx>
  * 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 },
diff --git a/src/apps/terminal/TermConst.cpp b/src/apps/terminal/TermConst.cpp
index aeee241..ae4a608 100644
--- a/src/apps/terminal/TermConst.cpp
+++ b/src/apps/terminal/TermConst.cpp
@@ -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 = ":@-./_~";
diff --git a/src/apps/terminal/TermConst.h b/src/apps/terminal/TermConst.h
index 681c915..ef7d704 100644
--- a/src/apps/terminal/TermConst.h
+++ b/src/apps/terminal/TermConst.h
@@ -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;


Other related posts:

  • » [haiku-commits] haiku: hrev48816 - src/apps/terminal - axeld