mirror of
https://github.com/xHyroM/zed-discord-presence.git
synced 2024-11-21 21:51:05 +01:00
feat: customizable application id (#24)
This commit is contained in:
parent
fd9bd4204d
commit
bce54aed6f
3 changed files with 30 additions and 11 deletions
|
@ -54,6 +54,7 @@ impl Rules {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct Configuration {
|
||||
pub application_id: String,
|
||||
pub base_icons_url: String,
|
||||
|
||||
pub state: Option<String>,
|
||||
|
@ -92,6 +93,7 @@ macro_rules! set_string {
|
|||
impl Configuration {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
application_id: String::from("1263505205522337886"),
|
||||
base_icons_url: String::from(
|
||||
"https://raw.githubusercontent.com/xhyrom/zed-discord-presence/main/assets/icons/",
|
||||
),
|
||||
|
@ -108,6 +110,7 @@ impl Configuration {
|
|||
|
||||
pub fn set(&mut self, initialization_options: Option<Value>) {
|
||||
if let Some(options) = initialization_options {
|
||||
set_string!(self, options, application_id, "application_id");
|
||||
set_string!(self, options, base_icons_url, "base_icons_url");
|
||||
set_option!(self, options, state, "state");
|
||||
set_option!(self, options, details, "details");
|
||||
|
|
|
@ -31,25 +31,30 @@ use crate::util;
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct Discord {
|
||||
client: Mutex<DiscordIpcClient>,
|
||||
client: Option<Mutex<DiscordIpcClient>>,
|
||||
start_timestamp: Duration,
|
||||
}
|
||||
|
||||
impl Discord {
|
||||
pub fn new() -> Self {
|
||||
let discord_client = DiscordIpcClient::new("1263505205522337886")
|
||||
.expect("Failed to initialize Discord Ipc Client");
|
||||
let start_timestamp = SystemTime::now();
|
||||
let since_epoch = start_timestamp
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("Failed to get duration since UNIX_EPOCH");
|
||||
|
||||
Self {
|
||||
client: Mutex::new(discord_client),
|
||||
client: None,
|
||||
start_timestamp: since_epoch,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_client(&mut self, application_id: String) {
|
||||
let discord_client = DiscordIpcClient::new(application_id.as_str())
|
||||
.expect("Failed to initialize Discord Ipc Client");
|
||||
|
||||
self.client = Some(Mutex::new(discord_client));
|
||||
}
|
||||
|
||||
pub fn connect(&self) {
|
||||
let mut client = self.get_client();
|
||||
let result = client.connect();
|
||||
|
@ -62,8 +67,12 @@ impl Discord {
|
|||
result.unwrap();
|
||||
}
|
||||
|
||||
pub fn get_client(&self) -> MutexGuard<DiscordIpcClient> {
|
||||
return self.client.lock().expect("Failed to lock discord client");
|
||||
pub fn get_client(&self) -> MutexGuard<'_, DiscordIpcClient> {
|
||||
self.client
|
||||
.as_ref()
|
||||
.expect("Discord client not initialized")
|
||||
.lock()
|
||||
.expect("Failed to lock discord client")
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
|
|
|
@ -45,8 +45,8 @@ struct Document {
|
|||
|
||||
#[derive(Debug)]
|
||||
struct Backend {
|
||||
discord: discord::Discord,
|
||||
client: Client,
|
||||
discord: Mutex<Discord>,
|
||||
workspace_file_name: Mutex<String>,
|
||||
git_remote_url: Mutex<Option<String>>,
|
||||
config: Mutex<Configuration>,
|
||||
|
@ -82,7 +82,7 @@ impl Backend {
|
|||
fn new(client: Client) -> Self {
|
||||
Self {
|
||||
client,
|
||||
discord: Discord::new(),
|
||||
discord: Mutex::new(Discord::new()),
|
||||
workspace_file_name: Mutex::new(String::new()),
|
||||
git_remote_url: Mutex::new(None),
|
||||
config: Mutex::new(Configuration::new()),
|
||||
|
@ -120,7 +120,7 @@ impl Backend {
|
|||
.as_ref()
|
||||
.map(|text| placeholders.replace(text));
|
||||
|
||||
self.discord.change_activity(
|
||||
self.get_discord().change_activity(
|
||||
state,
|
||||
details,
|
||||
large_image,
|
||||
|
@ -154,6 +154,10 @@ impl Backend {
|
|||
fn get_config(&self) -> MutexGuard<Configuration> {
|
||||
return self.config.lock().expect("Failed to lock config");
|
||||
}
|
||||
|
||||
fn get_discord(&self) -> MutexGuard<Discord> {
|
||||
return self.discord.lock().expect("Failed to lock discord");
|
||||
}
|
||||
}
|
||||
|
||||
#[tower_lsp::async_trait]
|
||||
|
@ -179,13 +183,16 @@ impl LanguageServer for Backend {
|
|||
let mut config = self.config.lock().unwrap();
|
||||
config.set(params.initialization_options);
|
||||
|
||||
let mut discord = self.get_discord();
|
||||
discord.create_client(config.application_id.to_string());
|
||||
|
||||
if config.rules.suitable(
|
||||
workspace_path
|
||||
.to_str()
|
||||
.expect("Failed to transform workspace path to str"),
|
||||
) {
|
||||
// Connect discord client
|
||||
self.discord.connect();
|
||||
discord.connect();
|
||||
} else {
|
||||
// Exit LSP
|
||||
exit(0);
|
||||
|
@ -215,7 +222,7 @@ impl LanguageServer for Backend {
|
|||
}
|
||||
|
||||
async fn shutdown(&self) -> Result<()> {
|
||||
self.discord.kill();
|
||||
self.get_discord().kill();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue