Skip to content

Commit

Permalink
feat: add custom buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
vyfor committed Apr 13, 2024
1 parent b300334 commit 9f00000
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 19 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ 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)
workspace = 'In {}', -- Text to display when in a workspace (Empty string to disable)
},
buttons = {
{
label = 'View repository', -- Text displayed on the button
url = 'git', -- URL where the button leads to ('git' = Git repository URL)
},
-- {
-- label = 'View plugin',
-- url = 'https://github.com/vyfor/cord.nvim',
-- }
}
})
```
Expand Down
8 changes: 7 additions & 1 deletion lua/cord.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ cord.config = {
plugin_manager = 'Managing plugins in {}',
workspace = 'In {}',
},
buttons = {
{
label = 'View Repository',
url = 'git',
}
}
}

local discord
Expand Down Expand Up @@ -159,7 +165,7 @@ function cord.setup(userConfig)
end

function cord.setup_autocmds(config)
vim.api.nvim_create_autocmd('DirChanged', { callback = function() utils.update_workspace_and_repo(config) end })
vim.api.nvim_create_autocmd('DirChanged', { callback = function() utils.update_cwd(config, discord) end })
vim.api.nvim_create_autocmd('FocusGained', { callback = function() is_focused = true; last_presence = nil end })
vim.api.nvim_create_autocmd('FocusLost', { callback = function() is_focused = false end })
end
Expand Down
44 changes: 38 additions & 6 deletions lua/cord/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ local function init_discord(ffi)
void clear_presence();
void disconnect();
void set_cwd(const char* directory);
void set_repository_url(const char* url);
void set_buttons(
const char* first_label,
const char* first_url,
const char* second_label,
const char* second_url
);
void update_time();
]]

Expand Down Expand Up @@ -100,13 +105,40 @@ local function validate_severity(config)
return true
end

local function update_cwd(config, discord)
discord.set_cwd(find_workspace())
local function validate_buttons(config)
if config.display.show_repository then
local repo = fetch_repository()
if repo and repo ~= '' then
discord.set_repository_url(repo)
local buttons = {}
local repo
for i, button in ipairs(config.buttons) do
if i > 2 then
vim.notify('[cord.nvim] Detected more than two buttons in the config. Only the first two will be displayed', vim.log.levels.WARN)
return buttons
end
if button.url == 'git' then
if not repo then
repo = fetch_repository()
end
if repo and repo ~= '' then
table.insert(buttons, { label = button.label, url = repo })
end
else
table.insert(buttons, button)
end
end
return buttons
end
end

local function update_cwd(config, discord)
discord.set_cwd(find_workspace())

local buttons = validate_buttons(config)
if not buttons then return end

if #buttons == 1 then
discord.set_buttons(buttons[1].label, buttons[1].url, nil, nil)
elseif #buttons >= 2 then
discord.set_buttons(buttons[1].label, buttons[1].url, buttons[2].label, buttons[2].url)
end
end

Expand Down
42 changes: 30 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ static mut RICH_CLIENT: Option<Arc<Mutex<RichClient>>> = None;
static mut CLIENT_IMAGE: String = String::new();
static mut CWD: Option<String> = None;
static mut START_TIME: Option<u128> = None;
static mut REPOSITORY_URL: Option<String> = None;
static mut EDITOR_TOOLTIP: String = String::new();
static mut IDLE_TEXT: String = String::new();
static mut IDLE_TOOLTIP: String = String::new();
Expand All @@ -36,6 +35,7 @@ static mut EDITING_TEXT: String = String::new();
static mut FILE_BROWSER_TEXT: String = String::new();
static mut PLUGIN_MANAGER_TEXT: String = String::new();
static mut WORKSPACE_TEXT: String = String::new();
static mut BUTTONS: Vec<ActivityButton> = Vec::new();
static mut INITIALIZED: bool = false;

#[no_mangle]
Expand Down Expand Up @@ -131,7 +131,8 @@ pub extern "C" fn update_presence(
format!("{}/editor/idle.png?v=1", GITHUB_ASSETS_URL);
presence_large_text = IDLE_TOOLTIP.to_string();
}
"netrw" | "dirvish" | "TelescopePrompt" | "neo-tree" | "oil" | "NvimTree" | "minifiles" => {
"netrw" | "dirvish" | "TelescopePrompt" | "neo-tree"
| "oil" | "NvimTree" | "minifiles" => {
if FILE_BROWSER_TEXT.is_empty() {
return false;
}
Expand Down Expand Up @@ -239,21 +240,17 @@ pub extern "C" fn update_presence(
if let Some(presence_start_time) = START_TIME {
activity.timestamp = Some(presence_start_time);
}
if let Some(repository_url) = REPOSITORY_URL.clone() {
activity.buttons = Some(vec![ActivityButton {
label: "View Repository".to_string(),
url: repository_url,
}]);
if !BUTTONS.is_empty() {
activity.buttons = Some(BUTTONS.to_vec());
}
let mut client = client.lock().unwrap();
let val = match client.update(&Packet {
match client.update(&Packet {
pid: std::process::id(),
activity: Some(activity),
}) {
Ok(_) => true,
Err(_) => false,
};
val
}
} else {
false
}
Expand Down Expand Up @@ -300,9 +297,30 @@ pub extern "C" fn set_cwd(value: *const c_char) {
}

#[no_mangle]
pub extern "C" fn set_repository_url(value: *const c_char) {
pub extern "C" fn set_buttons(
first_label: *const c_char,
first_url: *const c_char,
second_label: *const c_char,
second_url: *const c_char,
) {
unsafe {
REPOSITORY_URL = Some(ptr_to_string(value));
BUTTONS.clear();
let first_label = ptr_to_string(first_label);
let first_url = ptr_to_string(first_url);
if !first_label.is_empty() && !first_url.is_empty() {
BUTTONS.push(ActivityButton {
label: first_label,
url: first_url,
});
}
let second_label = ptr_to_string(second_label);
let second_url = ptr_to_string(second_url);
if !second_label.is_empty() && !second_url.is_empty() {
BUTTONS.push(ActivityButton {
label: second_label,
url: second_url,
})
}
}
}

Expand Down

0 comments on commit 9f00000

Please sign in to comment.