fix: leftover bugs n quirks (#106)

* fix: Loads of bugs n quirks

* misc: Requested Changes

* misc: Remove _ from property name
This commit is contained in:
Frangu Vlad 2018-11-10 18:39:01 +02:00 committed by Crawl
parent b75b8ad1ca
commit 9827df74fb
4 changed files with 85 additions and 68 deletions

View file

@ -1,22 +1,28 @@
const { Client } = require('discord-rpc'); const { Client } = require('discord-rpc');
import { import {
Disposable, Disposable,
StatusBarItem,
window,
workspace workspace
} from 'vscode'; } from 'vscode';
import Acivity from '../structures/Activity'; import Activity from '../structures/Activity';
import Logger from '../structures/Logger'; import Logger from '../structures/Logger';
let activityTimer: NodeJS.Timer;
export default class RPCClient implements Disposable { export default class RPCClient implements Disposable {
private _rpc: any = new Client({ transport: 'ipc' }); public statusBarIcon: StatusBarItem;
public config = workspace.getConfiguration('discord');
private _activity = new Acivity(); private _rpc: any;
private _config = workspace.getConfiguration('discord'); private _activity = new Activity();
private _clientId: string; private _clientId: string;
public constructor(clientId: string) { public constructor(clientId: string, statusBarIcon: StatusBarItem) {
this._clientId = clientId; this._clientId = clientId;
this.statusBarIcon = statusBarIcon;
} }
public get client() { public get client() {
@ -31,12 +37,41 @@ export default class RPCClient implements Disposable {
} }
public async login() { public async login() {
if (this._rpc) return;
this._rpc = new Client({ transport: 'ipc' });
Logger.log('Logging into RPC.'); Logger.log('Logging into RPC.');
return this._rpc.login({ clientId: this._clientId }); this._rpc.once('ready', () => {
Logger.log('Successfully connected to Discord.');
if (!this.config.get<boolean>('silent')) window.showInformationMessage('Successfully connected to Discord RPC');
this.statusBarIcon.hide();
this.statusBarIcon.text = '$(plug) Reconnect to Discord';
this.statusBarIcon.command = 'discord.reconnect';
if (activityTimer) clearInterval(activityTimer);
this.setActivity();
this._rpc.transport.once('close', async () => {
if (!this.config.get<boolean>('enabled')) return;
await this.dispose();
this.statusBarIcon.show();
});
activityTimer = setInterval(() => {
this.config = workspace.getConfiguration('discord');
this.setActivity(this.config.get<boolean>('workspaceElapsedTime'));
}, 10000);
});
await this._rpc.login({ clientId: this._clientId });
} }
public async dispose() { public async dispose() {
this._activity.dispose(); this._activity.dispose();
if (this._rpc) {
await this._rpc.destroy(); await this._rpc.destroy();
this._rpc = null;
}
clearInterval(activityTimer);
} }
} }

View file

@ -5,4 +5,4 @@ export const LIVE_SHARE_COMMANDS = {
START: 'liveshare.start', START: 'liveshare.start',
END: 'liveshare.end', END: 'liveshare.end',
JOIN: 'liveshare.join' JOIN: 'liveshare.join'
} };

View file

@ -1,50 +1,26 @@
import RPCClient from './client/RPCClient';
import Logger from './structures/Logger';
import { import {
commands, commands,
ExtensionContext, ExtensionContext,
StatusBarItem,
StatusBarAlignment, StatusBarAlignment,
StatusBarItem,
window, window,
workspace workspace
} from 'vscode'; } from 'vscode';
import { setInterval, clearInterval } from 'timers'; import RPCClient from './client/RPCClient';
import Logger from './structures/Logger';
let activityTimer: NodeJS.Timer; const statusBarIcon: StatusBarItem = window.createStatusBarItem(StatusBarAlignment.Left);
let statusBarIcon: StatusBarItem; statusBarIcon.text = '$(pulse) Connecting...';
statusBarIcon.command = 'discord.reconnect';
const config = workspace.getConfiguration('discord'); const config = workspace.getConfiguration('discord');
const rpc = new RPCClient(config.get<string>('clientID')!); const rpc = new RPCClient(config.get<string>('clientID')!, statusBarIcon);
export async function activate(context: ExtensionContext) { export async function activate(context: ExtensionContext) {
Logger.log('Discord Presence activated!'); Logger.log('Discord Presence activated!');
rpc.client.once('ready', () => {
Logger.log('Successfully connected to Discord.');
if (!config.get<boolean>('silent')) window.showInformationMessage('Successfully reconnected to Discord RPC');
if (statusBarIcon) statusBarIcon.dispose();
if (activityTimer) clearInterval(activityTimer);
rpc.setActivity();
rpc.client.transport.once('close', async () => {
if (!config.get<boolean>('enabled')) return;
await rpc.dispose();
await rpc.login();
if (!statusBarIcon) {
statusBarIcon = window.createStatusBarItem(StatusBarAlignment.Left);
statusBarIcon.text = '$(plug) Reconnect to Discord';
statusBarIcon.command = 'discord.reconnect';
statusBarIcon.show();
}
});
activityTimer = setInterval(() => {
rpc.setActivity(config.get<boolean>('workspaceElapsedTime'));
}, 10000);
})
if (config.get<boolean>('enabled')) { if (config.get<boolean>('enabled')) {
statusBarIcon.show();
try { try {
await rpc.login(); await rpc.login();
} catch (error) { } catch (error) {
@ -53,24 +29,25 @@ export async function activate(context: ExtensionContext) {
if (error.message.includes('ENOENT')) window.showErrorMessage('No Discord Client detected!'); if (error.message.includes('ENOENT')) window.showErrorMessage('No Discord Client detected!');
else window.showErrorMessage(`Couldn't connect to Discord via RPC: ${error.toString()}`); else window.showErrorMessage(`Couldn't connect to Discord via RPC: ${error.toString()}`);
} }
if (!statusBarIcon) { rpc.statusBarIcon.text = '$(pulse) Reconnect';
statusBarIcon = window.createStatusBarItem(StatusBarAlignment.Left); rpc.statusBarIcon.command = 'discord.reconnect';
statusBarIcon.text = '$(plug) Reconnect to Discord'; rpc.statusBarIcon.show();
statusBarIcon.command = 'discord.reconnect';
statusBarIcon.show();
}
} }
} }
const enabler = commands.registerCommand('discord.enable', async () => { const enabler = commands.registerCommand('discord.enable', async () => {
await rpc.dispose(); await rpc.dispose();
await config.update('enabled', true); await config.update('enabled', true);
rpc._config = workspace.getConfiguration('discord');
rpc.statusBarIcon.text = '$(pulse) Connecting...';
rpc.statusBarIcon.show();
await rpc.login(); await rpc.login();
window.showInformationMessage('Enabled Discord Rich Presence for this workspace.'); window.showInformationMessage('Enabled Discord Rich Presence for this workspace.');
}); });
const disabler = commands.registerCommand('discord.disable', async () => { const disabler = commands.registerCommand('discord.disable', async () => {
await config.update('enabled', false); await config.update('enabled', false);
rpc.config = workspace.getConfiguration('discord');
await rpc.dispose(); await rpc.dispose();
window.showInformationMessage('Disabled Discord Rich Presence for this workspace.'); window.showInformationMessage('Disabled Discord Rich Presence for this workspace.');
}); });
@ -79,14 +56,14 @@ export async function activate(context: ExtensionContext) {
await rpc.dispose(); await rpc.dispose();
await rpc.login(); await rpc.login();
if (!config.get('silent')) window.showInformationMessage('Reconnecting to Discord RPC...'); if (!config.get('silent')) window.showInformationMessage('Reconnecting to Discord RPC...');
if (statusBarIcon) statusBarIcon.text = '$(pulse) reconnecting...'; rpc.statusBarIcon.text = '$(pulse) Reconnecting...';
rpc.statusBarIcon.command = undefined;
}); });
context.subscriptions.push(enabler, disabler, reconnecter); context.subscriptions.push(enabler, disabler, reconnecter);
} }
export async function deactivate() { export async function deactivate() {
clearInterval(activityTimer);
await rpc.dispose(); await rpc.dispose();
} }

View file

@ -1,3 +1,5 @@
import { statSync } from 'fs';
import { basename, parse, sep } from 'path';
import { import {
debug, debug,
Disposable, Disposable,
@ -5,13 +7,14 @@ import {
window, window,
workspace workspace
} from 'vscode'; } from 'vscode';
import { basename, sep, parse } from 'path';
import { statSync } from 'fs';
const lang = require('../data/languages.json'); const lang = require('../data/languages.json');
const knownExtentions: { [key: string]: { image: string } } = lang.knownExtentions; const knownExtentions: { [key: string]: { image: string } } = lang.knownExtentions;
const knownLanguages: string[] = lang.knownLanguages; const knownLanguages: string[] = lang.knownLanguages;
interface Activity { const empty = '\u200b\u200b';
const sizes = [' bytes', 'kb', 'mb', 'gb', 'tb'];
interface State {
details?: string; details?: string;
state?: string; state?: string;
startTimestamp?: number | null; startTimestamp?: number | null;
@ -29,8 +32,8 @@ interface FileDetail {
currentColumn?: string; currentColumn?: string;
} }
export default class Acivity implements Disposable { export default class Activity implements Disposable {
private _state: Activity | null = null; private _state: State | null = null;
private _config = workspace.getConfiguration('discord'); private _config = workspace.getConfiguration('discord');
@ -47,8 +50,8 @@ export default class Acivity implements Disposable {
return this._state = { return this._state = {
...this._state, ...this._state,
details: this._generateDetails('detailsDebugging', 'detailsEditing', 'detailsIdle', this._state!.largeImageKey), details: this._generateDetails('detailsDebugging', 'detailsEditing', 'detailsIdle', this._state!.largeImageKey),
state: this._generateDetails('lowerDetailsDebugging', 'lowerDetailsEditing', 'lowerDetailsIdle', this._state!.largeImageKey), smallImageKey: debug.activeDebugSession ? 'debug' : env.appName.includes('Insiders') ? 'vscode-insiders' : 'vscode',
smallImageKey: debug.activeDebugSession ? 'debug' : env.appName.includes('Insiders') ? 'vscode-insiders' : 'vscode' state: this._generateDetails('lowerDetailsDebugging', 'lowerDetailsEditing', 'lowerDetailsIdle', this._state!.largeImageKey)
}; };
} }
this._lastKnownFile = window.activeTextEditor.document.fileName; this._lastKnownFile = window.activeTextEditor.document.fileName;
@ -59,7 +62,7 @@ export default class Acivity implements Disposable {
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(filename); return regex.test(filename);
})!] || (knownLanguages.includes(window.activeTextEditor.document.languageId) ? window.activeTextEditor.document.languageId : null) })!] || (knownLanguages.includes(window.activeTextEditor.document.languageId) ? window.activeTextEditor.document.languageId : null);
} }
let previousTimestamp = null; let previousTimestamp = null;
@ -67,8 +70,8 @@ export default class Acivity implements Disposable {
this._state = { this._state = {
details: this._generateDetails('detailsDebugging', 'detailsEditing', 'detailsIdle', largeImageKey), details: this._generateDetails('detailsDebugging', 'detailsEditing', 'detailsIdle', largeImageKey),
state: this._generateDetails('lowerDetailsDebugging', 'lowerDetailsEditing', 'lowerDetailsIdle', largeImageKey),
startTimestamp: window.activeTextEditor && previousTimestamp && workspaceElapsedTime ? previousTimestamp : window.activeTextEditor ? new Date().getTime() : null, startTimestamp: window.activeTextEditor && previousTimestamp && workspaceElapsedTime ? previousTimestamp : window.activeTextEditor ? new Date().getTime() : null,
state: this._generateDetails('lowerDetailsDebugging', 'lowerDetailsEditing', 'lowerDetailsIdle', largeImageKey),
largeImageKey: largeImageKey ? largeImageKey.image || largeImageKey : 'txt', largeImageKey: largeImageKey ? largeImageKey.image || largeImageKey : 'txt',
largeImageText: window.activeTextEditor largeImageText: window.activeTextEditor
? this._config.get<string>('largeImage')! ? this._config.get<string>('largeImage')!
@ -85,9 +88,13 @@ export default class Acivity implements Disposable {
return this._state; return this._state;
} }
public dispose() {
this._state = null;
this._lastKnownFile = '';
}
private _generateDetails(debugging: string, editing: string, idling: string, largeImageKey: any) { private _generateDetails(debugging: string, editing: string, idling: string, largeImageKey: any) {
const empty = '\u200b\u200b'; let raw: string = this._config.get<string>(idling)!.replace('{null}', empty);
let raw = this._config.get<string>(idling);
let filename = null; let filename = null;
let dirname = null; let dirname = null;
let checkState = false; let checkState = false;
@ -112,10 +119,11 @@ export default class Acivity implements Disposable {
} }
if (debug.activeDebugSession) { if (debug.activeDebugSession) {
raw = this._config.get<string>(debugging); raw = this._config.get<string>(debugging)!;
} else { } else {
raw = this._config.get<string>(editing); raw = this._config.get<string>(editing)!;
} }
const { totalLines, size, currentLine, currentColumn } = this._generateFileDetails(raw); const { totalLines, size, currentLine, currentColumn } = this._generateFileDetails(raw);
raw = raw! raw = raw!
.replace('{null}', empty) .replace('{null}', empty)
@ -144,14 +152,16 @@ export default class Acivity implements Disposable {
if (str.includes('{totallines}')) { if (str.includes('{totallines}')) {
fileDetail.totalLines = window.activeTextEditor.document.lineCount.toLocaleString(); fileDetail.totalLines = window.activeTextEditor.document.lineCount.toLocaleString();
} }
if (str.includes('{currentline}')) { if (str.includes('{currentline}')) {
fileDetail.currentLine = (window.activeTextEditor.selection.active.line + 1).toLocaleString(); fileDetail.currentLine = (window.activeTextEditor.selection.active.line + 1).toLocaleString();
} }
if (str.includes('{currentcolumn}')) { if (str.includes('{currentcolumn}')) {
fileDetail.currentColumn = (window.activeTextEditor.selection.active.character + 1).toLocaleString(); fileDetail.currentColumn = (window.activeTextEditor.selection.active.character + 1).toLocaleString();
} }
if (str.includes('{filesize}')) { if (str.includes('{filesize}')) {
const sizes = [' bytes', 'kb', 'mb', 'gb', 'tb'];
let currentDivision = 0; let currentDivision = 0;
let { size } = statSync(window.activeTextEditor.document.fileName); let { size } = statSync(window.activeTextEditor.document.fileName);
const originalSize = size; const originalSize = size;
@ -169,9 +179,4 @@ export default class Acivity implements Disposable {
return fileDetail; return fileDetail;
} }
public dispose() {
this._state = null;
this._lastKnownFile = '';
}
} }