feat: ability to remove details and state

This commit is contained in:
iCrawl 2021-02-11 01:18:04 +01:00
parent 2cb2283330
commit b55656a014
No known key found for this signature in database
GPG key ID: 1AB888B16355FBB2
6 changed files with 78 additions and 37 deletions

4
package-lock.json generated
View file

@ -1,11 +1,11 @@
{ {
"name": "discord-vscode", "name": "discord-vscode",
"version": "5.1.1", "version": "5.2.0",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"version": "5.1.1", "version": "5.2.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"bufferutil": "^4.0.3", "bufferutil": "^4.0.3",

View file

@ -1,7 +1,7 @@
{ {
"name": "discord-vscode", "name": "discord-vscode",
"displayName": "Discord Presence", "displayName": "Discord Presence",
"version": "5.1.1", "version": "5.2.0",
"description": "Update your discord status with a rich presence.", "description": "Update your discord status with a rich presence.",
"private": true, "private": true,
"author": { "author": {
@ -94,7 +94,7 @@
}, },
"discord.lowerDetailsNoWorkspaceFound": { "discord.lowerDetailsNoWorkspaceFound": {
"type": "string", "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." "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": { "discord.largeImageIdling": {
@ -129,6 +129,16 @@
"type": "boolean", "type": "boolean",
"default": false, "default": false,
"description": "Swaps the big and small image on the rich presence" "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"
} }
} }
} }

View file

@ -5,6 +5,7 @@ import {
CONFIG_KEYS, CONFIG_KEYS,
DEBUG_IMAGE_KEY, DEBUG_IMAGE_KEY,
EMPTY, EMPTY,
FAKE_EMPTY,
FILE_SIZES, FILE_SIZES,
IDLE_IMAGE_KEY, IDLE_IMAGE_KEY,
REPLACE_KEYS, REPLACE_KEYS,
@ -13,7 +14,7 @@ import {
VSCODE_IMAGE_KEY, VSCODE_IMAGE_KEY,
VSCODE_INSIDERS_IMAGE_KEY, VSCODE_INSIDERS_IMAGE_KEY,
} from './constants'; } from './constants';
import { GitExtension } from './git'; import { API, GitExtension } from './git';
import { log, LogLevel } from './logger'; import { log, LogLevel } from './logger';
import { getConfig, resolveFileIcon, toLower, toTitle, toUpper } from './util'; import { getConfig, resolveFileIcon, toLower, toTitle, toUpper } from './util';
@ -47,14 +48,13 @@ export async function activity(previous: ActivityPayload = {}) {
: VSCODE_IMAGE_KEY; : VSCODE_IMAGE_KEY;
const defaultSmallImageText = config[CONFIG_KEYS.SmallImage].replace(REPLACE_KEYS.AppName, appName); const defaultSmallImageText = config[CONFIG_KEYS.SmallImage].replace(REPLACE_KEYS.AppName, appName);
const defaultLargeImageText = config[CONFIG_KEYS.LargeImageIdling]; const defaultLargeImageText = config[CONFIG_KEYS.LargeImageIdling];
const removeDetails = config[CONFIG_KEYS.RemoveDetails];
const removeLowerDetails = config[CONFIG_KEYS.RemoveLowerDetails];
let state: ActivityPayload = { let state: ActivityPayload = {
details: await details(CONFIG_KEYS.DetailsIdling, CONFIG_KEYS.DetailsEditing, CONFIG_KEYS.DetailsDebugging), details: removeDetails
state: await details( ? undefined
CONFIG_KEYS.LowerDetailsIdling, : await details(CONFIG_KEYS.DetailsIdling, CONFIG_KEYS.DetailsEditing, CONFIG_KEYS.DetailsDebugging),
CONFIG_KEYS.LowerDetailsEditing,
CONFIG_KEYS.LowerDetailsDebugging,
),
startTimestamp: previous.startTimestamp ?? Date.now(), startTimestamp: previous.startTimestamp ?? Date.now(),
largeImageKey: IDLE_IMAGE_KEY, largeImageKey: IDLE_IMAGE_KEY,
largeImageText: defaultLargeImageText, largeImageText: defaultLargeImageText,
@ -78,12 +78,16 @@ export async function activity(previous: ActivityPayload = {}) {
.replace(REPLACE_KEYS.LanguageLowerCase, toLower(largeImageKey)) .replace(REPLACE_KEYS.LanguageLowerCase, toLower(largeImageKey))
.replace(REPLACE_KEYS.LanguageTitleCase, toTitle(largeImageKey)) .replace(REPLACE_KEYS.LanguageTitleCase, toTitle(largeImageKey))
.replace(REPLACE_KEYS.LanguageUpperCase, toUpper(largeImageKey)) .replace(REPLACE_KEYS.LanguageUpperCase, toUpper(largeImageKey))
.padEnd(2, EMPTY); .padEnd(2, FAKE_EMPTY);
state = { state = {
...state, ...state,
details: await details(CONFIG_KEYS.DetailsIdling, CONFIG_KEYS.DetailsEditing, CONFIG_KEYS.DetailsDebugging), details: removeDetails
state: await details( ? undefined
: await details(CONFIG_KEYS.DetailsIdling, CONFIG_KEYS.DetailsEditing, CONFIG_KEYS.DetailsDebugging),
state: removeLowerDetails
? undefined
: await details(
CONFIG_KEYS.LowerDetailsIdling, CONFIG_KEYS.LowerDetailsIdling,
CONFIG_KEYS.LowerDetailsEditing, CONFIG_KEYS.LowerDetailsEditing,
CONFIG_KEYS.LowerDetailsDebugging, CONFIG_KEYS.LowerDetailsDebugging,
@ -114,7 +118,7 @@ export async function activity(previous: ActivityPayload = {}) {
async function details(idling: CONFIG_KEYS, editing: CONFIG_KEYS, debugging: CONFIG_KEYS) { async function details(idling: CONFIG_KEYS, editing: CONFIG_KEYS, debugging: CONFIG_KEYS) {
const config = getConfig(); 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) { if (window.activeTextEditor) {
const fileName = basename(window.activeTextEditor.document.fileName); 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 split = dir.split(sep);
const dirName = split[split.length - 1]; 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 workspaceFolder = workspace.getWorkspaceFolder(window.activeTextEditor.document.uri);
const workspaceFolderName = workspaceFolder?.name ?? noWorkspaceFound; const workspaceFolderName = workspaceFolder?.name ?? noWorkspaceFound;
const workspaceName = workspace.name ?? workspaceFolderName; const workspaceName = workspace.name?.replace(REPLACE_KEYS.VSCodeWorkspace, EMPTY) ?? workspaceFolderName;
const workspaceAndFolder = `${workspaceName}${workspaceFolderName === EMPTY ? '' : ` - ${workspaceFolderName}`}`; const workspaceAndFolder = `${workspaceName}${
workspaceFolderName === FAKE_EMPTY ? '' : ` - ${workspaceFolderName}`
}`;
const fileIcon = resolveFileIcon(window.activeTextEditor.document); 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 = raw.replace(REPLACE_KEYS.FullDirName, `${name}${sep}${relativePath.join(sep)}`);
} }
try {
raw = await fileDetails(raw, window.activeTextEditor.document, window.activeTextEditor.selection); 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 raw = raw
.replace(REPLACE_KEYS.FileName, fileName) .replace(REPLACE_KEYS.FileName, fileName)
.replace(REPLACE_KEYS.DirName, dirName) .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) { async function fileDetails(_raw: string, document: TextDocument, selection: Selection) {
let raw = _raw.slice(); let raw = _raw.slice();
const gitExtension = extensions.getExtension<GitExtension>('vscode.git');
const git = gitExtension?.exports.getAPI(1);
if (raw.includes(REPLACE_KEYS.TotalLines)) { if (raw.includes(REPLACE_KEYS.TotalLines)) {
raw = raw.replace(REPLACE_KEYS.TotalLines, document.lineCount.toLocaleString()); 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<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}`);
}
if (raw.includes(REPLACE_KEYS.GitBranch)) { if (raw.includes(REPLACE_KEYS.GitBranch)) {
if (git?.repositories.length) { if (git?.repositories.length) {
raw = raw.replace( raw = raw.replace(
REPLACE_KEYS.GitBranch, 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 { } else {
raw = raw.replace(REPLACE_KEYS.GitBranch, UNKNOWN_GIT_BRANCH); raw = raw.replace(REPLACE_KEYS.GitBranch, UNKNOWN_GIT_BRANCH);
@ -212,7 +233,7 @@ async function fileDetails(_raw: string, document: TextDocument, selection: Sele
git.repositories git.repositories
.find((repo) => repo.ui.selected) .find((repo) => repo.ui.selected)
?.state.remotes[0].fetchUrl?.split('/')[1] ?.state.remotes[0].fetchUrl?.split('/')[1]
.replace('.git', '') ?? EMPTY, .replace('.git', '') ?? FAKE_EMPTY,
); );
} else { } else {
raw = raw.replace(REPLACE_KEYS.GitRepoName, UNKNOWN_GIT_REPO_NAME); raw = raw.replace(REPLACE_KEYS.GitRepoName, UNKNOWN_GIT_REPO_NAME);

View file

@ -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_EXTENSIONS: { [key: string]: { image: string } } = LANG.KNOWN_EXTENSIONS;
export const KNOWN_LANGUAGES: { language: string; image: string }[] = LANG.KNOWN_LANGUAGES; export const KNOWN_LANGUAGES: { language: string; image: string }[] = LANG.KNOWN_LANGUAGES;
export const EMPTY = '\u200b\u200b'; export const EMPTY = '' as const;
export const FILE_SIZES = [' bytes', 'kb', 'mb', 'gb', 'tb']; 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 IDLE_IMAGE_KEY = 'vscode-big' as const;
export const DEBUG_IMAGE_KEY = 'debug'; export const DEBUG_IMAGE_KEY = 'debug' as const;
export const VSCODE_IMAGE_KEY = 'vscode'; export const VSCODE_IMAGE_KEY = 'vscode' as const;
export const VSCODE_INSIDERS_IMAGE_KEY = 'vscode-insiders'; export const VSCODE_INSIDERS_IMAGE_KEY = 'vscode-insiders' as const;
export const UNKNOWN_GIT_BRANCH = 'Unknown.'; export const UNKNOWN_GIT_BRANCH = 'Unknown' as const;
export const UNKNOWN_GIT_REPO_NAME = 'Unknown.'; export const UNKNOWN_GIT_REPO_NAME = 'Unknown' as const;
export const enum REPLACE_KEYS { export const enum REPLACE_KEYS {
Empty = '{empty}', Empty = '{empty}',
@ -22,6 +23,7 @@ export const enum REPLACE_KEYS {
DirName = '{dir_name}', DirName = '{dir_name}',
FullDirName = '{full_dir_name}', FullDirName = '{full_dir_name}',
Workspace = '{workspace}', Workspace = '{workspace}',
VSCodeWorkspace = '(Workspace)',
WorkspaceFolder = '{workspace_folder}', WorkspaceFolder = '{workspace_folder}',
WorkspaceAndFolder = '{workspace_and_folder}', WorkspaceAndFolder = '{workspace_and_folder}',
LanguageLowerCase = '{lang}', LanguageLowerCase = '{lang}',
@ -51,4 +53,6 @@ export const enum CONFIG_KEYS {
SuppressNotifications = 'suppressNotifications', SuppressNotifications = 'suppressNotifications',
WorkspaceExcludePatterns = 'workspaceExcludePatterns', WorkspaceExcludePatterns = 'workspaceExcludePatterns',
SwapBigAndSmallImage = 'swapBigAndSmallImage', SwapBigAndSmallImage = 'swapBigAndSmallImage',
RemoveDetails = 'removeDetails',
RemoveLowerDetails = 'removeLowerDetails',
} }

View file

@ -134,9 +134,13 @@ export async function activate(context: ExtensionContext) {
await login(); await login();
} }
try {
const gitExtension = extensions.getExtension<GitExtension>('vscode.git'); const gitExtension = extensions.getExtension<GitExtension>('vscode.git');
if (!gitExtension?.isActive) {
await gitExtension?.activate(); await gitExtension?.activate();
} }
} catch {}
}
export async function deactivate() { export async function deactivate() {
cleanUp(); cleanUp();

View file

@ -18,6 +18,8 @@ type WorkspaceExtensionConfiguration = WorkspaceConfiguration & {
suppressNotifications: boolean; suppressNotifications: boolean;
workspaceExcludePatterns: string[]; workspaceExcludePatterns: string[];
swapBigAndSmallImage: boolean; swapBigAndSmallImage: boolean;
removeDetails: boolean;
removeLowerDetails: boolean;
}; };
export function getConfig() { export function getConfig() {