refactor: extract generating state and details into its own function
This commit is contained in:
parent
d3d02b9b02
commit
07eb608857
1 changed files with 34 additions and 23 deletions
|
@ -3,16 +3,17 @@ import { Client } from 'discord-rpc';
|
||||||
import { basename, extname } from 'path';
|
import { basename, extname } from 'path';
|
||||||
import { setInterval, clearInterval } from 'timers';
|
import { setInterval, clearInterval } from 'timers';
|
||||||
import {
|
import {
|
||||||
|
commands,
|
||||||
|
debug,
|
||||||
|
DebugSession,
|
||||||
|
Disposable,
|
||||||
env,
|
env,
|
||||||
ExtensionContext,
|
ExtensionContext,
|
||||||
commands,
|
|
||||||
window,
|
|
||||||
workspace,
|
|
||||||
TextDocument,
|
TextDocument,
|
||||||
TextDocumentChangeEvent,
|
TextDocumentChangeEvent,
|
||||||
Disposable,
|
window,
|
||||||
debug,
|
workspace,
|
||||||
DebugSession
|
WorkspaceFolder
|
||||||
} from 'vscode';
|
} from 'vscode';
|
||||||
const languages = require('./data/languages.json');
|
const languages = require('./data/languages.json');
|
||||||
|
|
||||||
|
@ -159,33 +160,21 @@ function setActivity(): void {
|
||||||
if (window.activeTextEditor && window.activeTextEditor.document.fileName === lastKnownFileName) return;
|
if (window.activeTextEditor && window.activeTextEditor.document.fileName === lastKnownFileName) return;
|
||||||
lastKnownFileName = window.activeTextEditor ? window.activeTextEditor.document.fileName : null;
|
lastKnownFileName = window.activeTextEditor ? window.activeTextEditor.document.fileName : null;
|
||||||
|
|
||||||
const details = window.activeTextEditor
|
const fileName: string = window.activeTextEditor ? basename(window.activeTextEditor.document.fileName) : null;
|
||||||
? debug.activeDebugSession
|
|
||||||
? `${config.get('details').replace('{filename}', basename(window.activeTextEditor.document.fileName))} (Debug)`
|
|
||||||
: config.get('details').replace('{filename}', basename(window.activeTextEditor.document.fileName))
|
|
||||||
: config.get('detailsIdle');
|
|
||||||
const checkState = window.activeTextEditor
|
|
||||||
? Boolean(workspace.getWorkspaceFolder(window.activeTextEditor.document.uri))
|
|
||||||
: false;
|
|
||||||
const state = window.activeTextEditor
|
|
||||||
? checkState
|
|
||||||
? config.get('workspace').replace('{workspace}', workspace.getWorkspaceFolder(window.activeTextEditor.document.uri).name)
|
|
||||||
: config.get('workspaceNotFound')
|
|
||||||
: config.get('workspaceIdle');
|
|
||||||
const largeImageKey = window.activeTextEditor
|
const largeImageKey = window.activeTextEditor
|
||||||
? languages[Object.keys(languages).find(key => {
|
? languages[Object.keys(languages).find(key => {
|
||||||
if (key.startsWith('.') && basename(window.activeTextEditor.document.fileName).endsWith(key)) return true;
|
if (key.startsWith('.') && fileName.endsWith(key)) return true;
|
||||||
const match = key.match(/^\/(.*)\/([mgiy]+)$/);
|
const match = key.match(/^\/(.*)\/([mgiy]+)$/);
|
||||||
if (!match) return false;
|
if (!match) return false;
|
||||||
const regex = new RegExp(match[1], match[2]);
|
const regex = new RegExp(match[1], match[2]);
|
||||||
return regex.test(basename(window.activeTextEditor.document.fileName));
|
return regex.test(fileName);
|
||||||
})]
|
})]
|
||||||
: 'vscode-big';
|
: 'vscode-big';
|
||||||
|
|
||||||
// Create a JSON Object with the user's activity information.
|
// Create a JSON Object with the user's activity information.
|
||||||
activity = {
|
activity = {
|
||||||
details,
|
details: generateDetails('detailsDebugging', 'detailsEditing', 'detailsIdle'),
|
||||||
state,
|
state: generateDetails('lowerDetailsDebugging', 'lowerDetailsEditing', 'lowerDetailsIdle'),
|
||||||
startTimestamp: new Date().getTime() / 1000,
|
startTimestamp: new Date().getTime() / 1000,
|
||||||
largeImageKey: largeImageKey
|
largeImageKey: largeImageKey
|
||||||
? largeImageKey.image
|
? largeImageKey.image
|
||||||
|
@ -204,3 +193,25 @@ function setActivity(): void {
|
||||||
instance: false
|
instance: false
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function generateDetails(debugging, editing, idling): string {
|
||||||
|
const fileName: string = window.activeTextEditor ? basename(window.activeTextEditor.document.fileName) : null;
|
||||||
|
const checkState: boolean = window.activeTextEditor
|
||||||
|
? Boolean(workspace.getWorkspaceFolder(window.activeTextEditor.document.uri))
|
||||||
|
: false;
|
||||||
|
const workspaceFolder: WorkspaceFolder = checkState ? workspace.getWorkspaceFolder(window.activeTextEditor.document.uri) : null;
|
||||||
|
|
||||||
|
return window.activeTextEditor
|
||||||
|
? debug.activeDebugSession
|
||||||
|
? config.get(debugging)
|
||||||
|
.replace('{filename}', fileName)
|
||||||
|
.replace('{workspace}', checkState
|
||||||
|
? workspaceFolder.name
|
||||||
|
: config.get('lowerDetailsNotFound'))
|
||||||
|
: config.get(editing)
|
||||||
|
.replace('{filename}', fileName)
|
||||||
|
.replace('{workspace}', checkState
|
||||||
|
? workspaceFolder.name
|
||||||
|
: config.get('lowerDetailsNotFound'))
|
||||||
|
: config.get(idling);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue