Skip to content

Commit

Permalink
merge #56(feat: Support Git-related filetypes and plugins)
Browse files Browse the repository at this point in the history
feat: Support Git-related filetypes and plugins
  • Loading branch information
vyfor authored May 30, 2024
2 parents c366ac0 + a6aff26 commit 7aec251
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ require('cord').setup({
file_browser = 'Browsing files in {}', -- Text to display when browsing files (Empty string to disable)
plugin_manager = 'Managing plugins in {}', -- Text to display when managing plugins (Empty string to disable)
lsp_manager = 'Configuring LSP in {}', -- Text to display when managing LSP servers (Empty string to disable)
vcs = 'Committing changes in {}', -- Text to display when using Git or Git-related plugin (Empty string to disable)
workspace = 'In {}', -- Text to display when in a workspace (Empty string to disable)
},
buttons = {
Expand Down
Binary file added assets/vcs/default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions lua/cord.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ cord.config = {
file_browser = 'Browsing files in {}',
plugin_manager = 'Managing plugins in {}',
lsp_manager = 'Configuring LSP in {}',
vcs = 'Committing changes in {}',
workspace = 'In {}',
},
buttons = {
Expand Down Expand Up @@ -78,6 +79,7 @@ local function connect(config)
config.text.file_browser,
config.text.plugin_manager,
config.text.lsp_manager,
config.text.vcs,
config.text.workspace,
vim.fn.getcwd(),
config.display.swap_fields
Expand Down
1 change: 1 addition & 0 deletions lua/cord/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ local function init_discord(ffi)
const char* file_browser_text;
const char* plugin_manager_text;
const char* lsp_manager_text;
const char* vcs_text;
const char* workspace_text;
const char* initial_path;
const bool swap;
Expand Down
29 changes: 29 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct Config {
file_browser_text: String,
plugin_manager_text: String,
lsp_manager_text: String,
vcs_text: String,
workspace_text: String,
workspace: String,
buttons: Vec<ActivityButton>,
Expand All @@ -65,6 +66,7 @@ pub struct InitArgs {
pub file_browser_text: *const c_char,
pub plugin_manager_text: *const c_char,
pub lsp_manager_text: *const c_char,
pub vcs_text: *const c_char,
pub workspace_text: *const c_char,
pub initial_path: *const c_char,
pub swap_fields: bool,
Expand Down Expand Up @@ -113,6 +115,7 @@ pub unsafe extern "C" fn init(
let file_browser_text = ptr_to_string(args.file_browser_text);
let plugin_manager_text = ptr_to_string(args.plugin_manager_text);
let lsp_manager_text = ptr_to_string(args.lsp_manager_text);
let vcs_text = ptr_to_string(args.vcs_text);
let workspace_text = ptr_to_string(args.workspace_text);
let swap_fields = args.swap_fields;
let workspace = find_workspace(&ptr_to_string(args.initial_path));
Expand Down Expand Up @@ -152,6 +155,7 @@ pub unsafe extern "C" fn init(
file_browser_text,
plugin_manager_text,
lsp_manager_text,
vcs_text,
workspace_text,
workspace,
buttons,
Expand Down Expand Up @@ -375,6 +379,31 @@ pub unsafe extern "C" fn update_presence_with_assets(

(details, icon, tooltip)
}
Some(AssetType::Vcs) => {
let details = config.vcs_text.replace("{}", &name);

if icon.is_empty() || tooltip.is_empty() {
if let Some((default_icon, default_tooltip)) =
mappings::vcs::get(&filetype)
{
if icon.is_empty() {
icon = get_asset("vcs", default_icon);
}
if tooltip.is_empty() {
tooltip = default_tooltip.to_string();
}
} else {
if icon.is_empty() {
return false;
}
if tooltip.is_empty() {
tooltip = name;
}
}
}

(details, icon, tooltip)
}
None => return false,
};

Expand Down
3 changes: 2 additions & 1 deletion src/mappings/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ pub fn get<'a>(
"elixir" => ("elixir", "Elixir"),
"erlang" => ("erlang", "Erlang"),
"fsharp" => ("fsharp", "F#"),
"git" | "gitignore" => ("git", "Git"),
"git" | "gitattributes" | "gitconfig" | "gitignore"
| "gitsendemail" => ("git", "Git"),
"go" => ("go", "Go"),
"groovy" => {
if filename == "build.gradle" {
Expand Down
5 changes: 5 additions & 0 deletions src/mappings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod file_browser;
pub mod language;
pub mod lsp_manager;
pub mod plugin_manager;
pub mod vcs;

pub fn get_by_filetype<'a>(filetype: &'a str, filename: &str) -> Filetype<'a> {
if let Some(language) = language::get(filetype, filename) {
Expand All @@ -16,6 +17,9 @@ pub fn get_by_filetype<'a>(filetype: &'a str, filename: &str) -> Filetype<'a> {
if let Some(lsp_manager) = lsp_manager::get(filetype) {
return Filetype::Lsp(lsp_manager.0, lsp_manager.1);
}
if let Some(vcs) = vcs::get(filetype) {
return Filetype::Vcs(vcs.0, vcs.1);
}
Filetype::Language("text", filetype)
}

Expand All @@ -24,4 +28,5 @@ pub enum Filetype<'a> {
FileBrowser(&'a str, &'a str),
PluginManager(&'a str, &'a str),
Lsp(&'a str, &'a str),
Vcs(&'a str, &'a str),
}
18 changes: 18 additions & 0 deletions src/mappings/vcs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub fn get(filetype: &str) -> Option<(&str, &str)> {
let vcs = match filetype {
"gitcommit" | "gitrebase" => ("default", "Git"),
"fugitive" | "fugitiveblame" => ("default", "Fugitive"),
"magit" => ("default", "Magit"),
"git.nvim" => ("default", "Git.nvim"),
"lazygit" => ("default", "Lazygit"),
_ => {
if filetype.starts_with("Neogit") {
("default", "Neogit")
} else {
return None;
}
}
};

Some(vcs)
}
2 changes: 2 additions & 0 deletions src/util/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub enum AssetType {
FileBrowser,
PluginManager,
Lsp,
Vcs,
}

impl AssetType {
Expand All @@ -13,6 +14,7 @@ impl AssetType {
1 => Some(AssetType::FileBrowser),
2 => Some(AssetType::PluginManager),
3 => Some(AssetType::Lsp),
4 => Some(AssetType::Vcs),
_ => None,
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/util/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ pub fn build_presence(
lsp_manager_presence(config, tooltip, icon);
(details, Some(icon), tooltip)
}
Filetype::Vcs(icon, tooltip) => {
let (details, icon, tooltip) = vcs_presence(config, tooltip, icon);
(details, Some(icon), tooltip)
}
}
}

Expand Down Expand Up @@ -282,6 +286,19 @@ fn lsp_manager_presence(
(presence_details, presence_large_image, presence_large_text)
}

#[inline(always)]
fn vcs_presence(
config: &Config,
tooltip: &str,
icon: &str,
) -> (String, String, String) {
let presence_details = config.vcs_text.replace("{}", tooltip);
let presence_large_image = get_asset("vcs", icon);
let presence_large_text = tooltip.to_string();

(presence_details, presence_large_image, presence_large_text)
}

#[inline(always)]
fn find_git_repository(workspace_path: &str) -> Option<String> {
let config_path = format!("{workspace_path}/.git/config");
Expand Down

0 comments on commit 7aec251

Please sign in to comment.