From fbd799f37111e5c8185c7617cbdaa9854368ff82 Mon Sep 17 00:00:00 2001 From: iCrawl Date: Sat, 10 Apr 2021 14:42:28 +0200 Subject: [PATCH] feat: implement buttons Co-authored-by: Jack C <30955604+jacany@users.noreply.github.com> --- package.json | 5 +++++ src/activity.ts | 34 +++++++++++++++++++--------------- src/constants.ts | 1 + src/util.ts | 22 +++++++++++++++++++++- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index d944eb6..4229da3 100644 --- a/package.json +++ b/package.json @@ -144,6 +144,11 @@ "type": "boolean", "default": false, "description": "Removes the timestamp" + }, + "discord.removeRemoteRepository": { + "type": "boolean", + "default": false, + "description": "Removes the View Repository button" } } } diff --git a/src/activity.ts b/src/activity.ts index 3c458db..7bb8f6d 100644 --- a/src/activity.ts +++ b/src/activity.ts @@ -1,5 +1,5 @@ import { basename, parse, sep } from 'path'; -import { debug, env, extensions, Selection, TextDocument, window, workspace } from 'vscode'; +import { debug, env, Selection, TextDocument, window, workspace } from 'vscode'; import { CONFIG_KEYS, @@ -14,9 +14,8 @@ import { VSCODE_IMAGE_KEY, VSCODE_INSIDERS_IMAGE_KEY, } from './constants'; -import { API, GitExtension } from './git'; import { log, LogLevel } from './logger'; -import { getConfig, resolveFileIcon, toLower, toTitle, toUpper } from './util'; +import { getConfig, getGit, resolveFileIcon, toLower, toTitle, toUpper } from './util'; interface ActivityPayload { details?: string; @@ -50,6 +49,9 @@ export async function activity(previous: ActivityPayload = {}) { const defaultLargeImageText = config[CONFIG_KEYS.LargeImageIdling]; const removeDetails = config[CONFIG_KEYS.RemoveDetails]; const removeLowerDetails = config[CONFIG_KEYS.RemoveLowerDetails]; + const removeRemotRepository = config[CONFIG_KEYS.removeRemotRepository]; + + const git = await getGit(); let state: ActivityPayload = { details: removeDetails @@ -72,6 +74,19 @@ export async function activity(previous: ActivityPayload = {}) { }; } + if (!removeRemotRepository && git?.repositories.length) { + let repo = git.repositories.find((repo) => repo.ui.selected)?.state.remotes[0].fetchUrl; + + if (repo) { + repo = repo.replace(':', '/').replace('git@', 'https://').replace('.git', ''); + + state = { + ...state, + buttons: [{ label: 'View Repository', url: repo }], + }; + } + } + if (window.activeTextEditor) { const largeImageKey = resolveFileIcon(window.activeTextEditor.document); const largeImageText = config[CONFIG_KEYS.LargeImage] @@ -207,18 +222,7 @@ 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}`); - } + const git = await getGit(); if (raw.includes(REPLACE_KEYS.GitBranch)) { if (git?.repositories.length) { diff --git a/src/constants.ts b/src/constants.ts index 1064b23..32effdd 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -56,4 +56,5 @@ export const enum CONFIG_KEYS { RemoveDetails = 'removeDetails', RemoveLowerDetails = 'removeLowerDetails', RemoveTimestamp = 'removeTimestamp', + removeRemotRepository = 'removeRemoteRepository', } diff --git a/src/util.ts b/src/util.ts index 4e55b7d..f96ae00 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,7 +1,9 @@ import { basename } from 'path'; -import { TextDocument, workspace, WorkspaceConfiguration } from 'vscode'; +import { TextDocument, workspace, extensions, WorkspaceConfiguration } from 'vscode'; import { KNOWN_EXTENSIONS, KNOWN_LANGUAGES } from './constants'; +import { API, GitExtension } from './git'; +import { log, LogLevel } from './logger'; type WorkspaceExtensionConfiguration = WorkspaceConfiguration & { enabled: boolean; @@ -21,6 +23,7 @@ type WorkspaceExtensionConfiguration = WorkspaceConfiguration & { removeDetails: boolean; removeLowerDetails: boolean; removeTimestamp: boolean; + removeRemoteRepository: boolean; }; export function getConfig() { @@ -57,3 +60,20 @@ export function resolveFileIcon(document: TextDocument) { return typeof fileIcon === 'string' ? fileIcon : fileIcon?.image ?? 'text'; } + +export async function getGit() { + 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}`); + } + + return git; +}