feat: implement buttons

Co-authored-by: Jack C <30955604+jacany@users.noreply.github.com>
This commit is contained in:
iCrawl 2021-04-10 14:42:28 +02:00
parent 03c988ba47
commit fbd799f371
No known key found for this signature in database
GPG key ID: 1AB888B16355FBB2
4 changed files with 46 additions and 16 deletions

View file

@ -144,6 +144,11 @@
"type": "boolean",
"default": false,
"description": "Removes the timestamp"
},
"discord.removeRemoteRepository": {
"type": "boolean",
"default": false,
"description": "Removes the View Repository button"
}
}
}

View file

@ -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<GitExtension>('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) {

View file

@ -56,4 +56,5 @@ export const enum CONFIG_KEYS {
RemoveDetails = 'removeDetails',
RemoveLowerDetails = 'removeLowerDetails',
RemoveTimestamp = 'removeTimestamp',
removeRemotRepository = 'removeRemoteRepository',
}

View file

@ -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<GitExtension>('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;
}