Skip to content

Commit

Permalink
feat: support custom icons
Browse files Browse the repository at this point in the history
  • Loading branch information
vyfor committed Apr 25, 2024
1 parent 2c31ffa commit 9627657
Show file tree
Hide file tree
Showing 5 changed files with 327 additions and 74 deletions.
29 changes: 26 additions & 3 deletions lua/cord.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ cord.config = {
{
label = 'View Repository',
url = 'git',
}
}
},
},
assets = {},
}

local discord
Expand Down Expand Up @@ -146,7 +147,29 @@ local function update_presence(config, initial)
discord.update_time()
end
local cursor_pos = config.display.show_cursor_position and (current_presence.cursor_line .. ':' .. current_presence.cursor_col) or nil
local success = discord.update_presence(current_presence.name, current_presence.type, current_presence.readonly, cursor_pos, problem_count)
local icon, name = utils.get_icon(config, current_presence.name, current_presence.type)
local success
if icon then
success = discord.update_presence_with_assets(
current_presence.name,
current_presence.type,
icon.name or name,
icon.icon,
icon.tooltip,
icon.asset_type or 0,
current_presence.readonly,
cursor_pos,
problem_count
)
else
success = discord.update_presence(
current_presence.name,
current_presence.type,
current_presence.readonly,
cursor_pos,
problem_count
)
end
if success then
last_presence = current_presence
if is_blacklisted == nil then
Expand Down
73 changes: 59 additions & 14 deletions lua/cord/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,35 @@ local function init_discord(ffi)
void init(
const char* client,
const char* image,
const char* editorTooltip,
const char* idleText,
const char* idleTooltip,
const char* viewingText,
const char* editingText,
const char* fileBrowserText,
const char* pluginManagerText,
const char* workspaceText,
const char* initialPath,
const Buttons* buttons,
const char* editor_tooltip,
const char* idle_text,
const char* idle_tooltip,
const char* viewing_text,
const char* editing_text,
const char* file_browser_text,
const char* plugin_manager_text,
const char* workspace_text,
const char* initial_path,
Buttons* buttons,
const bool swap
);
const bool update_presence(
const char* filename,
const char* filetype,
bool isReadOnly,
const char* cursorPosition,
int problemCount
bool is_read_only,
const char* cursor_position,
int problem_count
);
const bool update_presence_with_assets(
const char* filename,
const char* filetype,
const char* name,
const char* icon,
const char* tooltip,
int asset_type,
bool is_read_only,
const char* cursor_position,
int problem_count
);
void clear_presence();
void disconnect();
Expand Down Expand Up @@ -103,9 +114,43 @@ local function array_contains(arr, val)
return false
end

local function get_file_extension(filename)
for i = #filename, 1, -1 do
if filename:sub(i, i) == '.' then
return filename:sub(i)
end
end

return filename
end

local function get_icon(config, filename, filetype)
if not config.assets then
return
end

local icon = config.assets[filetype]
if icon then
return icon, filetype
end

icon = config.assets[filename]
if icon then
return icon, filename
end

local extension = get_file_extension(filename)
icon = config.assets[extension]
if icon then
return icon, extension
end
end


return {
init_discord = init_discord,
validate_severity = validate_severity,
get_problem_count = get_problem_count,
array_contains = array_contains
array_contains = array_contains,
get_icon = get_icon
}
Loading

0 comments on commit 9627657

Please sign in to comment.