From b55656a0141f58c4b2476a05319227851d6db824 Mon Sep 17 00:00:00 2001 From: iCrawl Date: Thu, 11 Feb 2021 01:18:04 +0100 Subject: [PATCH] feat: ability to remove details and state --- package-lock.json | 4 +-- package.json | 14 ++++++++-- src/activity.ts | 67 +++++++++++++++++++++++++++++++---------------- src/constants.ts | 20 ++++++++------ src/extension.ts | 8 ++++-- src/util.ts | 2 ++ 6 files changed, 78 insertions(+), 37 deletions(-) diff --git a/package-lock.json b/package-lock.json index bdd056f..a693dc4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "discord-vscode", - "version": "5.1.1", + "version": "5.2.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "5.1.1", + "version": "5.2.0", "license": "MIT", "dependencies": { "bufferutil": "^4.0.3", diff --git a/package.json b/package.json index 122e782..f5de877 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "discord-vscode", "displayName": "Discord Presence", - "version": "5.1.1", + "version": "5.2.0", "description": "Update your discord status with a rich presence.", "private": true, "author": { @@ -94,7 +94,7 @@ }, "discord.lowerDetailsNoWorkspaceFound": { "type": "string", - "default": "No workspace.", + "default": "No workspace", "description": "Custom string for the state section of the rich presence when no workspace is found.\nIf set to '{empty}', this will be an empty space.\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." }, "discord.largeImageIdling": { @@ -129,6 +129,16 @@ "type": "boolean", "default": false, "description": "Swaps the big and small image on the rich presence" + }, + "discord.removeDetails": { + "type": "boolean", + "default": false, + "description": "Removes the details" + }, + "discord.removeLowerDetails": { + "type": "boolean", + "default": false, + "description": "Removes the lower details" } } } diff --git a/src/activity.ts b/src/activity.ts index dcfae46..172377c 100644 --- a/src/activity.ts +++ b/src/activity.ts @@ -5,6 +5,7 @@ import { CONFIG_KEYS, DEBUG_IMAGE_KEY, EMPTY, + FAKE_EMPTY, FILE_SIZES, IDLE_IMAGE_KEY, REPLACE_KEYS, @@ -13,7 +14,7 @@ import { VSCODE_IMAGE_KEY, VSCODE_INSIDERS_IMAGE_KEY, } from './constants'; -import { GitExtension } from './git'; +import { API, GitExtension } from './git'; import { log, LogLevel } from './logger'; import { getConfig, resolveFileIcon, toLower, toTitle, toUpper } from './util'; @@ -47,14 +48,13 @@ export async function activity(previous: ActivityPayload = {}) { : VSCODE_IMAGE_KEY; const defaultSmallImageText = config[CONFIG_KEYS.SmallImage].replace(REPLACE_KEYS.AppName, appName); const defaultLargeImageText = config[CONFIG_KEYS.LargeImageIdling]; + const removeDetails = config[CONFIG_KEYS.RemoveDetails]; + const removeLowerDetails = config[CONFIG_KEYS.RemoveLowerDetails]; let state: ActivityPayload = { - details: await details(CONFIG_KEYS.DetailsIdling, CONFIG_KEYS.DetailsEditing, CONFIG_KEYS.DetailsDebugging), - state: await details( - CONFIG_KEYS.LowerDetailsIdling, - CONFIG_KEYS.LowerDetailsEditing, - CONFIG_KEYS.LowerDetailsDebugging, - ), + details: removeDetails + ? undefined + : await details(CONFIG_KEYS.DetailsIdling, CONFIG_KEYS.DetailsEditing, CONFIG_KEYS.DetailsDebugging), startTimestamp: previous.startTimestamp ?? Date.now(), largeImageKey: IDLE_IMAGE_KEY, largeImageText: defaultLargeImageText, @@ -78,16 +78,20 @@ export async function activity(previous: ActivityPayload = {}) { .replace(REPLACE_KEYS.LanguageLowerCase, toLower(largeImageKey)) .replace(REPLACE_KEYS.LanguageTitleCase, toTitle(largeImageKey)) .replace(REPLACE_KEYS.LanguageUpperCase, toUpper(largeImageKey)) - .padEnd(2, EMPTY); + .padEnd(2, FAKE_EMPTY); state = { ...state, - details: await details(CONFIG_KEYS.DetailsIdling, CONFIG_KEYS.DetailsEditing, CONFIG_KEYS.DetailsDebugging), - state: await details( - CONFIG_KEYS.LowerDetailsIdling, - CONFIG_KEYS.LowerDetailsEditing, - CONFIG_KEYS.LowerDetailsDebugging, - ), + details: removeDetails + ? undefined + : await details(CONFIG_KEYS.DetailsIdling, CONFIG_KEYS.DetailsEditing, CONFIG_KEYS.DetailsDebugging), + state: removeLowerDetails + ? undefined + : await details( + CONFIG_KEYS.LowerDetailsIdling, + CONFIG_KEYS.LowerDetailsEditing, + CONFIG_KEYS.LowerDetailsDebugging, + ), }; if (swapBigAndSmallImage) { @@ -114,7 +118,7 @@ export async function activity(previous: ActivityPayload = {}) { 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, EMPTY); + let raw = (config[idling] as string).replace(REPLACE_KEYS.Empty, FAKE_EMPTY); if (window.activeTextEditor) { const fileName = basename(window.activeTextEditor.document.fileName); @@ -122,11 +126,13 @@ async function details(idling: CONFIG_KEYS, editing: CONFIG_KEYS, debugging: CON const split = dir.split(sep); const dirName = split[split.length - 1]; - const noWorkspaceFound = config[CONFIG_KEYS.LowerDetailsNoWorkspaceFound].replace(REPLACE_KEYS.Empty, EMPTY); + 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 ?? workspaceFolderName; - const workspaceAndFolder = `${workspaceName}${workspaceFolderName === EMPTY ? '' : ` - ${workspaceFolderName}`}`; + const workspaceName = workspace.name?.replace(REPLACE_KEYS.VSCodeWorkspace, EMPTY) ?? workspaceFolderName; + const workspaceAndFolder = `${workspaceName}${ + workspaceFolderName === FAKE_EMPTY ? '' : ` - ${workspaceFolderName}` + }`; const fileIcon = resolveFileIcon(window.activeTextEditor.document); @@ -143,7 +149,11 @@ async function details(idling: CONFIG_KEYS, editing: CONFIG_KEYS, debugging: CON raw = raw.replace(REPLACE_KEYS.FullDirName, `${name}${sep}${relativePath.join(sep)}`); } - raw = await fileDetails(raw, window.activeTextEditor.document, window.activeTextEditor.selection); + 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.DirName, dirName) @@ -160,8 +170,6 @@ async function details(idling: CONFIG_KEYS, editing: CONFIG_KEYS, debugging: CON async function fileDetails(_raw: string, document: TextDocument, selection: Selection) { let raw = _raw.slice(); - const gitExtension = extensions.getExtension('vscode.git'); - const git = gitExtension?.exports.getAPI(1); if (raw.includes(REPLACE_KEYS.TotalLines)) { raw = raw.replace(REPLACE_KEYS.TotalLines, document.lineCount.toLocaleString()); @@ -194,11 +202,24 @@ async function fileDetails(_raw: string, document: TextDocument, selection: Sele ); } + let git: API | undefined; + try { + log(LogLevel.Debug, 'Loading git extension'); + const gitExtension = extensions.getExtension('vscode.git'); + if (!gitExtension?.isActive) { + log(LogLevel.Trace, 'Git extension not activated, activating...'); + await gitExtension?.activate(); + } + git = gitExtension?.exports.getAPI(1); + } catch (error) { + log(LogLevel.Error, `Failed to load git extension, is git installed?; ${error as string}`); + } + 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 ?? EMPTY, + git.repositories.find((repo) => repo.ui.selected)?.state.HEAD?.name ?? FAKE_EMPTY, ); } else { raw = raw.replace(REPLACE_KEYS.GitBranch, UNKNOWN_GIT_BRANCH); @@ -212,7 +233,7 @@ async function fileDetails(_raw: string, document: TextDocument, selection: Sele git.repositories .find((repo) => repo.ui.selected) ?.state.remotes[0].fetchUrl?.split('/')[1] - .replace('.git', '') ?? EMPTY, + .replace('.git', '') ?? FAKE_EMPTY, ); } else { raw = raw.replace(REPLACE_KEYS.GitRepoName, UNKNOWN_GIT_REPO_NAME); diff --git a/src/constants.ts b/src/constants.ts index 51452a2..b2ac929 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -5,16 +5,17 @@ export const CLIENT_ID = '383226320970055681' as const; export const KNOWN_EXTENSIONS: { [key: string]: { image: string } } = LANG.KNOWN_EXTENSIONS; export const KNOWN_LANGUAGES: { language: string; image: string }[] = LANG.KNOWN_LANGUAGES; -export const EMPTY = '\u200b\u200b'; -export const FILE_SIZES = [' bytes', 'kb', 'mb', 'gb', 'tb']; +export const EMPTY = '' as const; +export const FAKE_EMPTY = '\u200b\u200b' as const; +export const FILE_SIZES = [' bytes', 'kb', 'mb', 'gb', 'tb'] as const; -export const IDLE_IMAGE_KEY = 'vscode-big'; -export const DEBUG_IMAGE_KEY = 'debug'; -export const VSCODE_IMAGE_KEY = 'vscode'; -export const VSCODE_INSIDERS_IMAGE_KEY = 'vscode-insiders'; +export const IDLE_IMAGE_KEY = 'vscode-big' as const; +export const DEBUG_IMAGE_KEY = 'debug' as const; +export const VSCODE_IMAGE_KEY = 'vscode' as const; +export const VSCODE_INSIDERS_IMAGE_KEY = 'vscode-insiders' as const; -export const UNKNOWN_GIT_BRANCH = 'Unknown.'; -export const UNKNOWN_GIT_REPO_NAME = 'Unknown.'; +export const UNKNOWN_GIT_BRANCH = 'Unknown' as const; +export const UNKNOWN_GIT_REPO_NAME = 'Unknown' as const; export const enum REPLACE_KEYS { Empty = '{empty}', @@ -22,6 +23,7 @@ export const enum REPLACE_KEYS { DirName = '{dir_name}', FullDirName = '{full_dir_name}', Workspace = '{workspace}', + VSCodeWorkspace = '(Workspace)', WorkspaceFolder = '{workspace_folder}', WorkspaceAndFolder = '{workspace_and_folder}', LanguageLowerCase = '{lang}', @@ -51,4 +53,6 @@ export const enum CONFIG_KEYS { SuppressNotifications = 'suppressNotifications', WorkspaceExcludePatterns = 'workspaceExcludePatterns', SwapBigAndSmallImage = 'swapBigAndSmallImage', + RemoveDetails = 'removeDetails', + RemoveLowerDetails = 'removeLowerDetails', } diff --git a/src/extension.ts b/src/extension.ts index 01fd1e7..0bcb180 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -134,8 +134,12 @@ export async function activate(context: ExtensionContext) { await login(); } - const gitExtension = extensions.getExtension('vscode.git'); - await gitExtension?.activate(); + try { + const gitExtension = extensions.getExtension('vscode.git'); + if (!gitExtension?.isActive) { + await gitExtension?.activate(); + } + } catch {} } export async function deactivate() { diff --git a/src/util.ts b/src/util.ts index 5a79223..0a6c987 100644 --- a/src/util.ts +++ b/src/util.ts @@ -18,6 +18,8 @@ type WorkspaceExtensionConfiguration = WorkspaceConfiguration & { suppressNotifications: boolean; workspaceExcludePatterns: string[]; swapBigAndSmallImage: boolean; + removeDetails: boolean; + removeLowerDetails: boolean; }; export function getConfig() {