From 7ce018f4392cb9aba5b8ad0067d7af8dd0da2494 Mon Sep 17 00:00:00 2001 From: Espen Jacobsson Date: Fri, 2 Jul 2021 23:30:11 +0200 Subject: [PATCH 1/2] Add support for file extension as custom string --- package.json | 8 ++++---- src/activity.ts | 4 +++- src/constants.ts | 1 + 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 4a84693382..d89eaddbac 100644 --- a/package.json +++ b/package.json @@ -70,12 +70,12 @@ "discord.detailsEditing": { "type": "string", "default": "Editing {file_name}", - "description": "Custom string for the details section of the rich presence\n\t- '{empty}' will be replaced with an empty space.\n\t- '{file_name}' will be replaced with the current file name.\n\t- '{dir_name}' will get replaced with the folder name that has the current file.\n\t- '{full_dir_name}' will get replaced with the full directory name without the current file name.\n\t- '{workspace}' will be replaced with the current workspace name, if any.\n\t- '{workspace_folder}' will be replaced with the currently accessed workspace folder, if any.\n\t- '{workspace_and_folder} will be replaced with the currently accessed workspace and workspace folder like this: 'Workspace - WorkspaceFolder'\n\t- '{current_column}' will get replaced with the current column of the current line.\n\t- '{current_line}' will get replaced with the current line number.\n\t- '{total_lines}' will get replaced with the total line number.\n\t- '{file_size}' will get replaced with the current file's size.\n\t- '{git_repo_name}' will be replaced with the active Git repository name (from the git URL)\n\t- '{git_branch}' will be replaced with the current active branch name." + "description": "Custom string for the details section of the rich presence\n\t- '{empty}' will be replaced with an empty space.\n\t- '{file_name}' will be replaced with the current file name.\n\t- '{file_extension}' will be replaced with the current file extension.\n\t- '{dir_name}' will get replaced with the folder name that has the current file.\n\t- '{full_dir_name}' will get replaced with the full directory name without the current file name.\n\t- '{workspace}' will be replaced with the current workspace name, if any.\n\t- '{workspace_folder}' will be replaced with the currently accessed workspace folder, if any.\n\t- '{workspace_and_folder} will be replaced with the currently accessed workspace and workspace folder like this: 'Workspace - WorkspaceFolder'\n\t- '{current_column}' will get replaced with the current column of the current line.\n\t- '{current_line}' will get replaced with the current line number.\n\t- '{total_lines}' will get replaced with the total line number.\n\t- '{file_size}' will get replaced with the current file's size.\n\t- '{git_repo_name}' will be replaced with the active Git repository name (from the git URL)\n\t- '{git_branch}' will be replaced with the current active branch name." }, "discord.detailsDebugging": { "type": "string", "default": "Debugging {file_name}", - "description": "Custom string for the details section of the rich presence when debugging\n\t- '{empty}' will be replaced with an empty space.\n\t- '{file_name}' will be replaced with the current file name.\n\t- '{dir_name}' will get replaced with the folder name that has the current file.\n\t- '{full_dir_name}' will get replaced with the full directory name without the current file name.\n\t- '{workspace}' will be replaced with the current workspace name, if any.\n\t- '{workspace_folder}' will be replaced with the currently accessed workspace folder, if any.\n\t- '{workspace_and_folder} will be replaced with the currently accessed workspace and workspace folder like this: 'Workspace - WorkspaceFolder'\n\t- '{current_column}' will get replaced with the current column of the current line.\n\t- '{current_line}' will get replaced with the current line number.\n\t- '{total_lines}' will get replaced with the total line number.\n\t- '{file_size}' will get replaced with the current file's size.\n\t- '{git_repo_name}' will be replaced with the active Git repository name (from the git URL)\n\t- '{git_branch}' will be replaced with the current active branch name." + "description": "Custom string for the details section of the rich presence when debugging\n\t- '{empty}' will be replaced with an empty space.\n\t- '{file_name}' will be replaced with the current file name.\n\t- '{file_extension}' will be replaced with the current file extension.\n\t- '{dir_name}' will get replaced with the folder name that has the current file.\n\t- '{full_dir_name}' will get replaced with the full directory name without the current file name.\n\t- '{workspace}' will be replaced with the current workspace name, if any.\n\t- '{workspace_folder}' will be replaced with the currently accessed workspace folder, if any.\n\t- '{workspace_and_folder} will be replaced with the currently accessed workspace and workspace folder like this: 'Workspace - WorkspaceFolder'\n\t- '{current_column}' will get replaced with the current column of the current line.\n\t- '{current_line}' will get replaced with the current line number.\n\t- '{total_lines}' will get replaced with the total line number.\n\t- '{file_size}' will get replaced with the current file's size.\n\t- '{git_repo_name}' will be replaced with the active Git repository name (from the git URL)\n\t- '{git_branch}' will be replaced with the current active branch name." }, "discord.lowerDetailsIdling": { "type": "string", @@ -85,12 +85,12 @@ "discord.lowerDetailsEditing": { "type": "string", "default": "Workspace: {workspace}", - "description": "Custom string for the state section of the rich presence\n\t- '{empty}' will be replaced with an empty space.\n\t- '{file_name}' will be replaced with the current file name.\n\t- '{dir_name}' will get replaced with the folder name that has the current file.\n\t- '{full_dir_name}' will get replaced with the full directory name without the current file name.\n\t- '{workspace}' will be replaced with the current workspace name, if any.\n\t- '{workspace_folder}' will be replaced with the currently accessed workspace folder, if any.\n\t- '{workspace_and_folder} will be replaced with the currently accessed workspace and workspace folder like this: 'Workspace - WorkspaceFolder'\n\t- '{current_column}' will get replaced with the current column of the current line.\n\t- '{current_line}' will get replaced with the current line number.\n\t- '{total_lines}' will get replaced with the total line number.\n\t- '{file_size}' will get replaced with the current file's size.\n\t- '{git_repo_name}' will be replaced with the active Git repository name (from the git URL)\n\t- '{git_branch}' will be replaced with the current active branch name." + "description": "Custom string for the state section of the rich presence\n\t- '{empty}' will be replaced with an empty space.\n\t- '{file_name}' will be replaced with the current file name.\n\t- '{file_extension}' will be replaced with the current file extension.\n\t- '{dir_name}' will get replaced with the folder name that has the current file.\n\t- '{full_dir_name}' will get replaced with the full directory name without the current file name.\n\t- '{workspace}' will be replaced with the current workspace name, if any.\n\t- '{workspace_folder}' will be replaced with the currently accessed workspace folder, if any.\n\t- '{workspace_and_folder} will be replaced with the currently accessed workspace and workspace folder like this: 'Workspace - WorkspaceFolder'\n\t- '{current_column}' will get replaced with the current column of the current line.\n\t- '{current_line}' will get replaced with the current line number.\n\t- '{total_lines}' will get replaced with the total line number.\n\t- '{file_size}' will get replaced with the current file's size.\n\t- '{git_repo_name}' will be replaced with the active Git repository name (from the git URL)\n\t- '{git_branch}' will be replaced with the current active branch name." }, "discord.lowerDetailsDebugging": { "type": "string", "default": "Debugging: {workspace}", - "description": "Custom string for the state section of the rich presence when debugging\n\t- '{empty}' will be replaced with an empty space.\n\t- '{file_name}' will be replaced with the current file name.\n\t- '{dir_name}' will get replaced with the folder name that has the current file.\n\t- '{full_dir_name}' will get replaced with the full directory name without the current file name.\n\t- '{workspace}' will be replaced with the current workspace name, if any.\n\t- '{workspace_folder}' will be replaced with the currently accessed workspace folder, if any.\n\t- '{workspace_and_folder} will be replaced with the currently accessed workspace and workspace folder like this: 'Workspace - WorkspaceFolder'\n\t- '{current_column}' will get replaced with the current column of the current line.\n\t- '{current_line}' will get replaced with the current line number.\n\t- '{total_lines}' will get replaced with the total line number.\n\t- '{file_size}' will get replaced with the current file's size.\n\t- '{git_repo_name}' will be replaced with the active Git repository name (from the git URL)\n\t- '{git_branch}' will be replaced with the current active branch name." + "description": "Custom string for the state section of the rich presence when debugging\n\t- '{empty}' will be replaced with an empty space.\n\t- '{file_name}' will be replaced with the current file name.\n\t- '{file_extension}' will be replaced with the current file extension.\n\t- '{dir_name}' will get replaced with the folder name that has the current file.\n\t- '{full_dir_name}' will get replaced with the full directory name without the current file name.\n\t- '{workspace}' will be replaced with the current workspace name, if any.\n\t- '{workspace_folder}' will be replaced with the currently accessed workspace folder, if any.\n\t- '{workspace_and_folder} will be replaced with the currently accessed workspace and workspace folder like this: 'Workspace - WorkspaceFolder'\n\t- '{current_column}' will get replaced with the current column of the current line.\n\t- '{current_line}' will get replaced with the current line number.\n\t- '{total_lines}' will get replaced with the total line number.\n\t- '{file_size}' will get replaced with the current file's size.\n\t- '{git_repo_name}' will be replaced with the active Git repository name (from the git URL)\n\t- '{git_branch}' will be replaced with the current active branch name." }, "discord.lowerDetailsNoWorkspaceFound": { "type": "string", diff --git a/src/activity.ts b/src/activity.ts index 05bb050126..213253940b 100644 --- a/src/activity.ts +++ b/src/activity.ts @@ -1,4 +1,4 @@ -import { basename, parse, sep } from 'path'; +import { basename, extname, parse, sep } from 'path'; import { debug, env, Selection, TextDocument, window, workspace } from 'vscode'; import { @@ -139,6 +139,7 @@ async function details(idling: CONFIG_KEYS, editing: CONFIG_KEYS, debugging: CON if (window.activeTextEditor) { const fileName = basename(window.activeTextEditor.document.fileName); + const fileExtension = extname(window.activeTextEditor.document.fileName).replace('.', ''); const { dir } = parse(window.activeTextEditor.document.fileName); const split = dir.split(sep); const dirName = split[split.length - 1]; @@ -173,6 +174,7 @@ async function details(idling: CONFIG_KEYS, editing: CONFIG_KEYS, debugging: CON } raw = raw .replace(REPLACE_KEYS.FileName, fileName) + .replace(REPLACE_KEYS.FileExtension, fileExtension) .replace(REPLACE_KEYS.DirName, dirName) .replace(REPLACE_KEYS.Workspace, workspaceName) .replace(REPLACE_KEYS.WorkspaceFolder, workspaceFolderName) diff --git a/src/constants.ts b/src/constants.ts index a98a8f895e..32d9b814ce 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -19,6 +19,7 @@ export const UNKNOWN_GIT_REPO_NAME = 'Unknown' as const; export const enum REPLACE_KEYS { Empty = '{empty}', + FileExtension = '{file_extension}', FileName = '{file_name}', DirName = '{dir_name}', FullDirName = '{full_dir_name}', From b078f225379d59b2a8029641b85c8b7d947fe29c Mon Sep 17 00:00:00 2001 From: Espen Jacobsson Date: Mon, 5 Sep 2022 18:24:00 +0200 Subject: [PATCH 2/2] Fix activity.ts --- src/activity.ts | 125 +----------------------------------------------- 1 file changed, 2 insertions(+), 123 deletions(-) diff --git a/src/activity.ts b/src/activity.ts index f513b32d97..2a7bb9ae6b 100644 --- a/src/activity.ts +++ b/src/activity.ts @@ -110,6 +110,7 @@ async function details(idling: CONFIG_KEYS, editing: CONFIG_KEYS, debugging: CON if (window.activeTextEditor) { const fileName = basename(window.activeTextEditor.document.fileName); + const fileExtension = extname(window.activeTextEditor.document.fileName).replace('.', ''); const { dir } = parse(window.activeTextEditor.document.fileName); const split = dir.split(sep); const dirName = split[split.length - 1]; @@ -144,6 +145,7 @@ async function details(idling: CONFIG_KEYS, editing: CONFIG_KEYS, debugging: CON } raw = raw .replace(REPLACE_KEYS.FileName, fileName) + .replace(REPLACE_KEYS.FileExtension, fileExtension) .replace(REPLACE_KEYS.DirName, dirName) .replace(REPLACE_KEYS.Workspace, workspaceName) .replace(REPLACE_KEYS.WorkspaceFolder, workspaceFolderName) @@ -253,126 +255,3 @@ export async function activity(previous: ActivityPayload = {}) { return state; } - -async function details(idling: CONFIG_KEYS, editing: CONFIG_KEYS, debugging: CONFIG_KEYS) { - const config = getConfig(); - let raw = (config[idling] as string).replace(REPLACE_KEYS.Empty, FAKE_EMPTY); - - if (window.activeTextEditor) { - const fileName = basename(window.activeTextEditor.document.fileName); - const fileExtension = extname(window.activeTextEditor.document.fileName).replace('.', ''); - const { dir } = parse(window.activeTextEditor.document.fileName); - const split = dir.split(sep); - const dirName = split[split.length - 1]; - - const noWorkspaceFound = config[CONFIG_KEYS.LowerDetailsNoWorkspaceFound].replace(REPLACE_KEYS.Empty, FAKE_EMPTY); - const workspaceFolder = workspace.getWorkspaceFolder(window.activeTextEditor.document.uri); - const workspaceFolderName = workspaceFolder?.name ?? noWorkspaceFound; - const workspaceName = workspace.name?.replace(REPLACE_KEYS.VSCodeWorkspace, EMPTY) ?? workspaceFolderName; - const workspaceAndFolder = `${workspaceName}${ - workspaceFolderName === FAKE_EMPTY ? '' : ` - ${workspaceFolderName}` - }`; - - const fileIcon = resolveFileIcon(window.activeTextEditor.document); - - if (debug.activeDebugSession) { - raw = config[debugging] as string; - } else { - raw = config[editing] as string; - } - - if (workspaceFolder) { - const { name } = workspaceFolder; - const relativePath = workspace.asRelativePath(window.activeTextEditor.document.fileName).split(sep); - relativePath.splice(-1, 1); - raw = raw.replace(REPLACE_KEYS.FullDirName, `${name}${sep}${relativePath.join(sep)}`); - } - - try { - raw = await fileDetails(raw, window.activeTextEditor.document, window.activeTextEditor.selection); - } catch (error) { - log(LogLevel.Error, `Failed to generate file details: ${error as string}`); - } - raw = raw - .replace(REPLACE_KEYS.FileName, fileName) - .replace(REPLACE_KEYS.FileExtension, fileExtension) - .replace(REPLACE_KEYS.DirName, dirName) - .replace(REPLACE_KEYS.Workspace, workspaceName) - .replace(REPLACE_KEYS.WorkspaceFolder, workspaceFolderName) - .replace(REPLACE_KEYS.WorkspaceAndFolder, workspaceAndFolder) - .replace(REPLACE_KEYS.LanguageLowerCase, toLower(fileIcon)) - .replace(REPLACE_KEYS.LanguageTitleCase, toTitle(fileIcon)) - .replace(REPLACE_KEYS.LanguageUpperCase, toUpper(fileIcon)); - } - - return raw; -} - -async function fileDetails(_raw: string, document: TextDocument, selection: Selection) { - let raw = _raw.slice(); - - if (raw.includes(REPLACE_KEYS.TotalLines)) { - raw = raw.replace(REPLACE_KEYS.TotalLines, document.lineCount.toLocaleString()); - } - - if (raw.includes(REPLACE_KEYS.CurrentLine)) { - raw = raw.replace(REPLACE_KEYS.CurrentLine, (selection.active.line + 1).toLocaleString()); - } - - if (raw.includes(REPLACE_KEYS.CurrentColumn)) { - raw = raw.replace(REPLACE_KEYS.CurrentColumn, (selection.active.character + 1).toLocaleString()); - } - - if (raw.includes(REPLACE_KEYS.FileSize)) { - let currentDivision = 0; - let size: number; - try { - ({ size } = await workspace.fs.stat(document.uri)); - } catch { - size = document.getText().length; - } - const originalSize = size; - if (originalSize > 1000) { - size /= 1000; - currentDivision++; - while (size > 1000) { - currentDivision++; - size /= 1000; - } - } - - raw = raw.replace( - REPLACE_KEYS.FileSize, - `${originalSize > 1000 ? size.toFixed(2) : size}${FILE_SIZES[currentDivision]}`, - ); - } - - const git = await getGit(); - - if (raw.includes(REPLACE_KEYS.GitBranch)) { - if (git?.repositories.length) { - raw = raw.replace( - REPLACE_KEYS.GitBranch, - git.repositories.find((repo) => repo.ui.selected)?.state.HEAD?.name ?? FAKE_EMPTY, - ); - } else { - raw = raw.replace(REPLACE_KEYS.GitBranch, UNKNOWN_GIT_BRANCH); - } - } - - if (raw.includes(REPLACE_KEYS.GitRepoName)) { - if (git?.repositories.length) { - raw = raw.replace( - REPLACE_KEYS.GitRepoName, - git.repositories - .find((repo) => repo.ui.selected) - ?.state.remotes[0].fetchUrl?.split('/')[1] - .replace('.git', '') ?? FAKE_EMPTY, - ); - } else { - raw = raw.replace(REPLACE_KEYS.GitRepoName, UNKNOWN_GIT_REPO_NAME); - } - } - - return raw; -}