Skip to content

Commit

Permalink
feat: idle action
Browse files Browse the repository at this point in the history
  • Loading branch information
xhyrom committed Dec 30, 2024
1 parent 4ea5390 commit ba6b748
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
19 changes: 18 additions & 1 deletion lsp/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,16 @@ impl Rules {
}
}

#[derive(Debug, PartialEq)]
pub enum IdleAction {
ClearActivity, // Clear the activity
ChangeActivity, // Change the activity
}

#[derive(Debug)]
pub struct Idle {
pub timeout: u64, // in seconds
pub timeout: u64, // in seconds
pub action: IdleAction, // what to do when idle

pub state: Option<String>,
pub details: Option<String>,
Expand All @@ -69,6 +76,7 @@ impl Default for Idle {
fn default() -> Self {
Idle {
timeout: 300,
action: IdleAction::ChangeActivity,

state: Some("Idling".to_string()),
details: Some("In Zed".to_string()),
Expand Down Expand Up @@ -175,6 +183,15 @@ impl Configuration {

if let Some(idle) = options.get("idle") {
self.idle.timeout = idle.get("timeout").and_then(|t| t.as_u64()).unwrap_or(300);
self.idle.action = idle.get("action").and_then(|a| a.as_str()).map_or(
IdleAction::ChangeActivity,
|action| match action {
"clear_activity" => IdleAction::ClearActivity,
"change_activity" => IdleAction::ChangeActivity,
_ => IdleAction::ChangeActivity,
},
);

set_option!(self, idle, state, "state");
set_option!(self, idle, details, "details");
set_option!(self, idle, large_image, "large_image");
Expand Down
7 changes: 7 additions & 0 deletions lsp/src/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ impl Discord {
.await
}

pub async fn clear_activity(&self) {
let mut client = self.get_client().await;
client
.clear_activity()
.unwrap_or_else(|_| println!("Failed to clear activity"));
}

#[allow(clippy::too_many_arguments)]
pub async fn change_activity(
&self,
Expand Down
8 changes: 7 additions & 1 deletion lsp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ impl Backend {
let config_guard = config_clone.lock().await;
let placeholders = Placeholders::new(None, &config_guard, "");

let discord_guard = discord_clone.lock().await;

if config_guard.idle.action == configuration::IdleAction::ClearActivity {
discord_guard.clear_activity().await;
return;
}

let (state, details, large_image, large_text, small_image, small_text) =
Backend::process_fields(
&placeholders,
Expand All @@ -152,7 +159,6 @@ impl Backend {
&config_guard.idle.small_text,
);

let discord_guard = discord_clone.lock().await;
discord_guard
.change_activity(
state,
Expand Down

0 comments on commit ba6b748

Please sign in to comment.