Rework client name change

This commit is contained in:
Maxime Coste 2013-09-13 00:01:47 +02:00
parent 000af8e206
commit 27a1e70b01
4 changed files with 13 additions and 33 deletions

View File

@ -73,12 +73,12 @@ public:
Context& context() { return m_context; }
const String& name() const { return m_name; }
void set_name(String name) { m_name = std::move(name); }
UserInterface& ui() const { return *m_ui; }
private:
Context m_context;
friend class InputMode;
friend class ClientManager;
std::unique_ptr<UserInterface> m_ui;
std::unique_ptr<InputMode> m_mode;
std::vector<std::unique_ptr<InputMode>> m_mode_trash;

View File

@ -19,16 +19,7 @@ String ClientManager::generate_name() const
for (int i = 0; true; ++i)
{
String name = "unnamed" + to_string(i);
bool found = false;
for (auto& client : m_clients)
{
if (client->m_name == name)
{
found = true;
break;
}
}
if (not found)
if (validate_client_name(name))
return name;
}
}
@ -123,7 +114,7 @@ void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer)
continue;
if (client->context().editor().is_editing())
throw runtime_error("client '" + client->m_name + "' is inserting in '" +
throw runtime_error("client '" + client->name() + "' is inserting in '" +
buffer.display_name() + '\'');
// change client context to edit the first buffer which is not the
@ -145,30 +136,18 @@ void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer)
m_windows.erase(end, m_windows.end());
}
void ClientManager::set_client_name(Client& client, String name)
bool ClientManager::validate_client_name(const String& name) const
{
auto it = find_if(m_clients, [&name](std::unique_ptr<Client>& client)
{ return client->m_name == name; });
if (it != m_clients.end() and it->get() != &client)
throw runtime_error("name not unique: " + name);
client.m_name = std::move(name);
}
Client& ClientManager::get_client(const Context& context)
{
for (auto& client : m_clients)
{
if (&client->context() == &context)
return *client;
}
throw runtime_error("no client for current context");
auto it = find_if(m_clients, [&](const std::unique_ptr<Client>& client)
{ return client->name() == name; });
return it == m_clients.end();
}
Client& ClientManager::get_client(const String& name)
{
for (auto& client : m_clients)
{
if (client->m_name == name)
if (client->name() == name)
return *client;
}
throw runtime_error("no client named: " + name);

View File

@ -26,9 +26,8 @@ public:
void redraw_clients() const;
Client& get_client(const Context& context);
Client& get_client(const String& name);
void set_client_name(Client& client, String name);
bool validate_client_name(const String& name) const;
void remove_client(Client& client);
private:

View File

@ -777,8 +777,10 @@ void set_client_name(CommandParameters params, Context& context)
{
ParametersParser parser(params, OptionMap{},
ParametersParser::Flags::None, 1, 1);
auto& manager = ClientManager::instance();
manager.set_client_name(manager.get_client(context), params[0]);
if (ClientManager::instance().validate_client_name(params[0]))
context.client().set_name(params[0]);
else if (context.client().name() != params[0])
throw runtime_error("client name '" + params[0] + "' is not unique");
}
void set_register(CommandParameters params, Context& context)