Add 'session' command to change remote session name

This commit is contained in:
Enrico Lumetti 2016-07-20 16:52:53 +02:00
parent 88a9607552
commit 1a7cd707aa
3 changed files with 37 additions and 3 deletions

View File

@ -1895,6 +1895,25 @@ const CommandDesc change_working_directory_cmd = {
}
};
const CommandDesc change_session_name = {
"session",
nullptr,
"session <name>: change remote session name",
ParameterDesc{{}, ParameterDesc::Flags::None, 1, 1},
CommandFlags::None,
CommandHelper{},
CommandCompleter{},
[](const ParametersParser& parser, Context&, const ShellContext&)
{
if (not Server::instance().rename_session(parser[0]))
throw runtime_error(format("Cannot rename current session: '{}' may be already in use", parser[0]));
// update modeline
for (auto& client : ClientManager::instance())
client->redraw_ifn();
}
};
}
void register_commands()
@ -1952,6 +1971,7 @@ void register_commands()
register_command(set_register_cmd);
register_command(select_cmd);
register_command(change_working_directory_cmd);
register_command(change_session_name);
}
}

View File

@ -651,13 +651,26 @@ Server::Server(String session_name)
m_listener.reset(new FDWatcher{listen_sock, accepter});
}
bool Server::rename_session(const String& name)
{
String old_socket_file = format("/tmp/kakoune/{}/{}", getpwuid(geteuid())->pw_name, m_session);
String new_socket_file = format("/tmp/kakoune/{}/{}", getpwuid(geteuid())->pw_name, name);
if (link(old_socket_file.c_str(), new_socket_file.c_str()) != 0)
return false;
unlink(old_socket_file.c_str());
m_session = name;
return true;
}
void Server::close_session(bool do_unlink)
{
if (do_unlink)
{
char socket_file[128];
format_to(socket_file, "/tmp/kakoune/{}/{}", getpwuid(geteuid())->pw_name, m_session);
unlink(socket_file);
String socket_file = format("/tmp/kakoune/{}/{}", getpwuid(geteuid())->pw_name, m_session);
unlink(socket_file.c_str());
}
m_listener->close_fd();
m_listener.reset();

View File

@ -48,6 +48,7 @@ struct Server : public Singleton<Server>
~Server();
const String& session() const { return m_session; }
bool rename_session(const String& name);
void close_session(bool do_unlink = true);
private: