Skip to content

Commit

Permalink
fix: Fallback to default icons if no client ID was provided
Browse files Browse the repository at this point in the history
fix: Fallback to default icons if no client ID was provided
  • Loading branch information
vyfor authored May 23, 2024
2 parents 1f3e08c + dde71a8 commit 7cea2c3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
Binary file modified assets/language/shell.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 31 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ static mut START_TIME: Option<u128> = None;
static mut CONFIG: Option<Config> = None;

struct Config {
is_custom_client: bool,
rich_client: RichClient,
editor_image: String,
editor_tooltip: String,
Expand Down Expand Up @@ -89,16 +90,19 @@ pub unsafe extern "C" fn init(

let args = &*args_ptr;

let mut is_custom_client = false;
let (client_id, editor_image) = match ptr_to_string(args.client).as_str() {
"vim" => (1219918645770059796, get_asset("editor", "vim")),
"neovim" => (1219918880005165137, get_asset("editor", "neovim")),
"lunarvim" => (1220295374087000104, get_asset("editor", "lunarvim")),
"nvchad" => (1220296082861326378, get_asset("editor", "nvchad")),
"astronvim" => (1230866983977746532, get_asset("editor", "astronvim")),
id => (
id.parse::<u64>().expect("Invalid client ID"),
ptr_to_string(args.image),
),
id => {
let id = id.parse::<u64>().expect("Invalid client ID");
is_custom_client = true;

(id, ptr_to_string(args.image))
}
};

let editor_tooltip = ptr_to_string(args.editor_tooltip);
Expand Down Expand Up @@ -135,7 +139,9 @@ pub unsafe extern "C" fn init(

let workspace =
workspace.file_name().unwrap().to_string_lossy().to_string();

CONFIG = Some(Config {
is_custom_client,
rich_client,
editor_image,
editor_tooltip,
Expand Down Expand Up @@ -241,26 +247,27 @@ pub unsafe extern "C" fn update_presence_with_assets(
match AssetType::from(asset_type) {
Some(AssetType::Language) => {
let filename = if !filename.is_empty() {
&filename
filename
} else if !name.is_empty() && name != "Cord.new" {
&name
name.clone()
} else {
"a new file"
"a new file".to_owned()
};
let details = if args.is_read_only {
config.viewing_text.replace("{}", filename)
config.viewing_text.replace("{}", &filename)
} else {
config.editing_text.replace("{}", filename)
config.editing_text.replace("{}", &filename)
};
let details = cursor_position
.map_or(details.clone(), |pos| {
format!("{details}:{pos}")
});

if icon.is_empty() || tooltip.is_empty() {
if let Some((default_icon, default_tooltip)) =
mappings::language::get(&filetype, filename)
{
let option =
mappings::language::get(&filetype, &filename);

if let Some((default_icon, default_tooltip)) = option {
if icon.is_empty() {
icon = get_asset("language", default_icon);
}
Expand All @@ -275,6 +282,18 @@ pub unsafe extern "C" fn update_presence_with_assets(
tooltip = name;
}
}

if !(config.is_custom_client
|| icon.is_empty()
|| icon.starts_with("http://")
|| icon.starts_with("https://"))
{
icon = mappings::language::get(&icon, &filename)
.map(|(icon, _)| icon.to_owned())
.unwrap_or_else(|| {
get_asset("language", &icon)
});
}
}

(details, icon, tooltip)
Expand Down
1 change: 1 addition & 0 deletions src/mappings/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub fn get<'a>(
"xml" => ("xml", "XML"),
"yaml" => ("yaml", "YAML"),
"zig" => ("zig", "Zig"),
"zsh" => ("shell", "Zsh"),
_ => match filename.to_lowercase().rsplit_once('.') {
Some((_, extension)) => match extension {
"gml" => ("gml", "Game Maker Language"),
Expand Down
2 changes: 1 addition & 1 deletion src/util/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{

pub const GITHUB_ASSETS_URL: &str =
"http://raw.githubusercontent.com/vyfor/cord.nvim/master/assets";
const ASSETS_VERSION: &str = "9";
const ASSETS_VERSION: &str = "10";
const VCS_MARKERS: [&str; 3] = [".git", ".svn", ".hg"];

#[inline(always)]
Expand Down

0 comments on commit 7cea2c3

Please sign in to comment.