Minor modifications #1959

This commit is contained in:
Alexey Milovidov 2018-03-23 19:55:35 +03:00
parent d298f9af5c
commit 046346645f
2 changed files with 15 additions and 13 deletions

View File

@ -52,6 +52,8 @@ inline char parseEscapeSequence(char c)
return '\a'; return '\a';
case 'b': case 'b':
return '\b'; return '\b';
case 'e':
return '\x1B'; /// \e escape sequence is non standard for C and C++ but supported by gcc and clang.
case 'f': case 'f':
return '\f'; return '\f';
case 'n': case 'n':

View File

@ -33,6 +33,7 @@
#include <IO/WriteBufferFromFileDescriptor.h> #include <IO/WriteBufferFromFileDescriptor.h>
#include <IO/WriteBufferFromFile.h> #include <IO/WriteBufferFromFile.h>
#include <IO/ReadBufferFromMemory.h> #include <IO/ReadBufferFromMemory.h>
#include <IO/ReadBufferFromString.h>
#include <IO/ReadHelpers.h> #include <IO/ReadHelpers.h>
#include <IO/WriteHelpers.h> #include <IO/WriteHelpers.h>
#include <DataStreams/AsynchronousBlockInputStream.h> #include <DataStreams/AsynchronousBlockInputStream.h>
@ -340,38 +341,37 @@ private:
Strings keys; Strings keys;
prompt_by_server_display_name = config().getString("prompt_by_server_display_name.default", "{display_name} :) "); prompt_by_server_display_name = config().getRawString("prompt_by_server_display_name.default", "{display_name} :) ");
config().keys("prompt_by_server_display_name", keys); config().keys("prompt_by_server_display_name", keys);
for (const String & key : keys) for (const String & key : keys)
{ {
if (key != "default" && server_display_name.find(key) != std::string::npos) if (key != "default" && server_display_name.find(key) != std::string::npos)
{ {
prompt_by_server_display_name = config().getString("prompt_by_server_display_name." + key); prompt_by_server_display_name = config().getRawString("prompt_by_server_display_name." + key);
break; break;
} }
} }
std::map<String, String> terminalCharacters = { /// Prompt may contain escape sequences including \e[ or \x1b[ sequences to set terminal color.
{ "\\e[", "\e[" },
{ "\\33[", "\33[" },
{ "\\033[", "\033["},
{ "\\x1B[", "\x1B["},
};
for (const auto & [key, value]: terminalCharacters)
{ {
boost::replace_all(prompt_by_server_display_name, key, value); String unescaped_prompt_by_server_display_name;
ReadBufferFromString in(prompt_by_server_display_name);
readEscapedString(unescaped_prompt_by_server_display_name, in);
prompt_by_server_display_name = std::move(unescaped_prompt_by_server_display_name);
} }
std::map<String, String> environment = { /// Prompt may contain the following substitutions in a form of {name}.
std::map<String, String> environment =
{
{"host", config().getString("host", "localhost")}, {"host", config().getString("host", "localhost")},
{"port", config().getString("port", "9000")}, {"port", config().getString("port", "9000")},
{"user", config().getString("user", "default")}, {"user", config().getString("user", "default")},
{"display_name", server_display_name}, {"display_name", server_display_name},
}; };
/// Quite suboptimal.
for (const auto & [key, value]: environment) for (const auto & [key, value]: environment)
{ {
boost::replace_all(prompt_by_server_display_name, "{" + key + "}", value); boost::replace_all(prompt_by_server_display_name, "{" + key + "}", value);