Skip to content

Commit

Permalink
feat: make state & details option
Browse files Browse the repository at this point in the history
  • Loading branch information
xhyrom committed Aug 4, 2024
1 parent e8f72a0 commit 69c0ec2
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 34 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ You can configure state, details and git integration by changing Discord Presenc
"details": "In {workspace}",
// URL for large image
"large_image": "{base_icons_url}/{language}.png",
"large_text": "{language:u}", // :u makes first letter upper-case
// URL for small image
"small_image": "{base_icons_url}/zed.png",
"small_text": "Zed",
"git_integration": true
}
}
}
}
```

You can also use `null` to unset the option. Possible for everything except `base_icons_url` and `git_integration`
14 changes: 7 additions & 7 deletions lsp/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use serde_json::Value;
pub struct Configuration {
pub base_icons_url: String,

pub state: String,
pub details: String,
pub state: Option<String>,
pub details: Option<String>,

pub large_image: Option<String>,
pub large_text: Option<String>,
Expand Down Expand Up @@ -58,10 +58,10 @@ impl Configuration {
pub fn new() -> Self {
Self {
base_icons_url: String::from("https://raw.githubusercontent.com/xhyrom/zed-discord-presence/feat/recognize-languages/assets/icons/"),
state: String::from("Working on {filename}"),
details: String::from("In {workspace}"),
state: Some(String::from("Working on {filename}")),
details: Some(String::from("In {workspace}")),
large_image: Some(String::from("{base_icons_url}/{language}.png")),
large_text: Some(String::from("{language}")),
large_text: Some(String::from("{language:u}")),
small_image: Some(String::from("{base_icons_url}/zed.png")),
small_text: Some(String::from("Zed")),
git_integration: true,
Expand All @@ -71,8 +71,8 @@ impl Configuration {
pub fn set(&mut self, initialization_options: Option<Value>) {
if let Some(options) = initialization_options {
set_string!(self, options, base_icons_url, "base_icons_url");
set_string!(self, options, state, "state");
set_string!(self, options, details, "details");
set_option!(self, options, state, "state");
set_option!(self, options, details, "details");
set_option!(self, options, large_image, "large_image");
set_option!(self, options, large_text, "large_text");
set_option!(self, options, small_image, "small_image");
Expand Down
40 changes: 19 additions & 21 deletions lsp/src/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::{
};

use discord_rich_presence::{
activity::{self, Assets, Button, Timestamps},
activity::{Activity, Assets, Button, Timestamps},
DiscordIpc, DiscordIpcClient,
};

Expand Down Expand Up @@ -66,10 +66,11 @@ impl Discord {
return self.client.lock().expect("Failed to lock discord client");
}

#[allow(clippy::too_many_arguments)]
pub fn change_activity(
&self,
state: String,
details: String,
state: Option<String>,
details: Option<String>,
large_image: Option<String>,
large_text: Option<String>,
small_image: Option<String>,
Expand All @@ -79,31 +80,28 @@ impl Discord {
let mut client = self.get_client();
let timestamp: i64 = self.start_timestamp.as_millis() as i64;

let activity = Activity::new()
.timestamps(Timestamps::new().start(timestamp))
.buttons(
git_remote_url
.as_ref()
.map(|url| vec![Button::new("View Repository", url)])
.unwrap_or_default(),
);

let activity = util::set_optional_field(activity, state.as_deref(), Activity::state);
let activity = util::set_optional_field(activity, details.as_deref(), Activity::details);

let assets = Assets::new();
let assets = util::set_optional_field(assets, large_image.as_deref(), Assets::large_image);
let assets = util::set_optional_field(assets, large_text.as_deref(), Assets::large_text);
let assets = util::set_optional_field(assets, small_image.as_deref(), Assets::small_image);
let assets = util::set_optional_field(assets, small_text.as_deref(), Assets::small_text);

let buttons = git_remote_url
.as_ref()
.map(|url| vec![Button::new("View Repository", url)])
.unwrap_or_default();
let activity = activity.assets(assets);

client
.set_activity(
activity::Activity::new()
.assets(assets)
.state(state.as_str())
.details(details.as_str())
.timestamps(Timestamps::new().start(timestamp))
.buttons(buttons),
)
.unwrap_or_else(|_| {
println!(
"Failed to set activity with state {} and details {}",
state, details
)
});
.set_activity(activity)
.unwrap_or_else(|_| println!("Failed to set activity with activity"));
}
}
10 changes: 8 additions & 2 deletions lsp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,14 @@ impl Backend {
let workspace = self.get_workspace_file_name();
let placeholders = Placeholders::new(&doc, &config, workspace.deref());

let state = placeholders.replace(&config.state);
let details = placeholders.replace(&config.details);
let state = config
.state
.as_ref()
.map(|state| placeholders.replace(state));
let details = config
.details
.as_ref()
.map(|details| placeholders.replace(details));

let large_image = config
.large_image
Expand Down
31 changes: 27 additions & 4 deletions lsp/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
use crate::{configuration::Configuration, languages::get_language, Document};

macro_rules! replace_with_capitalization {
($text:expr, $($placeholder:expr => $value:expr),*) => {{
let mut result = $text.to_string();
$(
let capitalized = capitalize_first_letter($value);
result = result.replace(concat!("{", $placeholder, "}"), $value)
.replace(concat!("{", $placeholder, ":u}"), &capitalized);
)*
result
}};
}

pub struct Placeholders<'a> {
filename: &'a str,
workspace: &'a str,
Expand All @@ -18,10 +30,13 @@ impl<'a> Placeholders<'a> {
}

pub fn replace(&self, text: &str) -> String {
text.replace("{filename}", self.filename)
.replace("{workspace}", self.workspace)
.replace("{language}", self.language.as_str())
.replace("{base_icons_url}", self.base_icons_url)
replace_with_capitalization!(
text,
"filename" => self.filename,
"workspace" => self.workspace,
"language" => self.language.as_str(),
"base_icons_url" => self.base_icons_url
)
}
}

Expand All @@ -34,3 +49,11 @@ where
}
obj
}

fn capitalize_first_letter(s: &str) -> String {
let mut c = s.chars();
match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
}
}

0 comments on commit 69c0ec2

Please sign in to comment.