diff --git a/src/client_manager.cc b/src/client_manager.cc index 89fb7d7c..76623b91 100644 --- a/src/client_manager.cc +++ b/src/client_manager.cc @@ -95,6 +95,33 @@ void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer) m_windows.erase(end, m_windows.end()); } +void ClientManager::set_client_name(Context& context, String name) +{ + auto it = find_if(m_clients, [&name](std::unique_ptr& client) + { return client->name == name; }); + if (it != m_clients.end() and &(*it)->context != &context) + throw runtime_error("name not unique: " + name); + + for (auto& client : m_clients) + { + if (&client->context == &context) + { + client->name = std::move(name); + return; + } + } + throw runtime_error("no client for current context"); +} + +Context& ClientManager::get_client_context(const String& name) +{ + auto it = find_if(m_clients, [&name](std::unique_ptr& client) + { return client->name == name; }); + if (it != m_clients.end()) + return (*it)->context; + throw runtime_error("no client named: " + name); +} + void ClientManager::redraw_clients() const { for (auto& client : m_clients) diff --git a/src/client_manager.hh b/src/client_manager.hh index 92f558b2..d75a8bb1 100644 --- a/src/client_manager.hh +++ b/src/client_manager.hh @@ -22,6 +22,9 @@ public: void ensure_no_client_uses_buffer(Buffer& buffer); void redraw_clients() const; + + void set_client_name(Context& context, String name); + Context& get_client_context(const String& name); private: void remove_client_by_context(Context& context); @@ -36,6 +39,7 @@ private: std::unique_ptr user_interface; InputHandler input_handler; Context context; + String name; }; std::vector> m_clients; diff --git a/src/utils.hh b/src/utils.hh index b2c1847c..3c68f1cd 100644 --- a/src/utils.hh +++ b/src/utils.hh @@ -148,6 +148,13 @@ auto find(Container& container, const T& value) -> decltype(container.begin()) return std::find(container.begin(), container.end(), value); } +template +auto find_if(Container& container, T op) -> decltype(container.begin()) +{ + return std::find_if(container.begin(), container.end(), op); +} + + template bool contains(const Container& container, const T& value) {