discord-vscode/src/activity.ts

227 lines
6.9 KiB
TypeScript
Raw Normal View History

2021-02-10 03:24:42 +00:00
import { basename, parse, sep } from 'path';
import { debug, env, extensions, Selection, TextDocument, window, workspace } from 'vscode';
import {
CONFIG_KEYS,
DEBUG_IMAGE_KEY,
EMPTY,
FILE_SIZES,
IDLE_IMAGE_KEY,
REPLACE_KEYS,
2021-02-10 15:54:44 +00:00
UNKNOWN_GIT_BRANCH,
UNKNOWN_GIT_REPO_NAME,
2021-02-10 03:24:42 +00:00
VSCODE_IMAGE_KEY,
VSCODE_INSIDERS_IMAGE_KEY,
} from './constants';
import { GitExtension } from './git';
import { log, LogLevel } from './logger';
import { getConfig, resolveFileIcon, toLower, toTitle, toUpper } from './util';
interface ActivityPayload {
2021-02-10 15:54:44 +00:00
details?: string;
2021-02-10 03:24:42 +00:00
state?: string;
startTimestamp?: number | null;
largeImageKey?: string;
largeImageText?: string;
smallImageKey?: string;
smallImageText?: string;
partyId?: string;
partySize?: number;
partyMax?: number;
matchSecret?: string;
joinSecret?: string;
spectateSecret?: string;
instance?: boolean;
}
2021-02-10 15:54:44 +00:00
export async function activity(previous: ActivityPayload = {}) {
2021-02-10 03:24:42 +00:00
const config = getConfig();
2021-02-10 17:32:13 +00:00
const swapBigAndSmallImage = config[CONFIG_KEYS.SwapBigAndSmallImage];
2021-02-10 03:24:42 +00:00
const appName = env.appName;
const defaultSmallImageKey = debug.activeDebugSession
? DEBUG_IMAGE_KEY
: appName.includes('Insiders')
? VSCODE_INSIDERS_IMAGE_KEY
: VSCODE_IMAGE_KEY;
2021-02-10 17:32:13 +00:00
const defaultSmallImageText = config[CONFIG_KEYS.SmallImage].replace(REPLACE_KEYS.AppName, appName);
const defaultLargeImageText = config[CONFIG_KEYS.LargeImageIdling];
2021-02-10 03:24:42 +00:00
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,
),
2021-02-10 15:54:44 +00:00
startTimestamp: previous.startTimestamp ?? Date.now(),
2021-02-10 03:24:42 +00:00
largeImageKey: IDLE_IMAGE_KEY,
2021-02-10 17:32:13 +00:00
largeImageText: defaultLargeImageText,
2021-02-10 03:24:42 +00:00
smallImageKey: defaultSmallImageKey,
2021-02-10 17:32:13 +00:00
smallImageText: defaultSmallImageText,
2021-02-10 03:24:42 +00:00
};
2021-02-10 17:32:13 +00:00
if (swapBigAndSmallImage) {
state = {
...state,
largeImageKey: defaultSmallImageKey,
largeImageText: defaultSmallImageText,
smallImageKey: IDLE_IMAGE_KEY,
smallImageText: defaultLargeImageText,
};
}
2021-02-10 03:24:42 +00:00
if (window.activeTextEditor) {
2021-02-10 15:54:44 +00:00
if (window.activeTextEditor.document.languageId === 'Log') {
return state;
}
2021-02-10 03:24:42 +00:00
const largeImageKey = resolveFileIcon(window.activeTextEditor.document);
const largeImageText = config[CONFIG_KEYS.LargeImage]
.replace(REPLACE_KEYS.LanguageLowerCase, toLower(largeImageKey))
.replace(REPLACE_KEYS.LanguageTitleCase, toTitle(largeImageKey))
.replace(REPLACE_KEYS.LanguageUpperCase, toUpper(largeImageKey))
.padEnd(2, 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,
),
};
2021-02-10 15:54:44 +00:00
2021-02-10 17:32:13 +00:00
if (swapBigAndSmallImage) {
state = {
...state,
smallImageKey: largeImageKey,
smallImageText: largeImageText,
};
} else {
state = {
...state,
largeImageKey,
largeImageText,
};
}
2021-02-10 15:54:44 +00:00
log(LogLevel.Trace, `VSCode language id: ${window.activeTextEditor.document.languageId}`);
2021-02-10 03:24:42 +00:00
}
2021-02-10 15:54:44 +00:00
log(LogLevel.Debug, `Discord Presence being sent to discord:\n${JSON.stringify(state, null, 2)}`);
2021-02-10 03:24:42 +00:00
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, EMPTY);
if (window.activeTextEditor) {
const fileName = basename(window.activeTextEditor.document.fileName);
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, 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 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)}`);
}
raw = await fileDetails(raw, window.activeTextEditor.document, window.activeTextEditor.selection);
raw = raw
.replace(REPLACE_KEYS.FileName, fileName)
.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();
const gitExtension = extensions.getExtension<GitExtension>('vscode.git');
const git = gitExtension?.exports.getAPI(1);
if (raw.includes(REPLACE_KEYS.TotalLines)) {
2021-02-10 17:32:13 +00:00
raw = raw.replace(REPLACE_KEYS.TotalLines, document.lineCount.toLocaleString());
2021-02-10 03:24:42 +00:00
}
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 } = await workspace.fs.stat(document.uri);
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]}`,
);
}
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,
);
} else {
2021-02-10 15:54:44 +00:00
raw = raw.replace(REPLACE_KEYS.GitBranch, UNKNOWN_GIT_BRANCH);
2021-02-10 03:24:42 +00:00
}
}
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', '') ?? EMPTY,
);
} else {
2021-02-10 15:54:44 +00:00
raw = raw.replace(REPLACE_KEYS.GitRepoName, UNKNOWN_GIT_REPO_NAME);
2021-02-10 03:24:42 +00:00
}
}
return raw;
}