Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@
"default": true,
"description": "Should elapsed time be shown?"
},
"vscord.status.RestoreElapsedTime": {
"type": "boolean",
"default": true,
"description": "Should elapsed time be saved and restored on different sessions?"
},
"vscord.status.resetElapsedTimePerFile": {
"type": "boolean",
"default": false,
Expand Down
24 changes: 19 additions & 5 deletions src/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
type TextDocument
} from "vscode";

import { sessionStart, previousTotal } from "./extension"

export enum CURRENT_STATUS {
IDLE = "idle",
NOT_IN_FILE = "notInFile",
Expand Down Expand Up @@ -98,11 +100,23 @@ export const activity = async (

if (isIdling && !config.get(CONFIG_KEYS.Status.Idle.Enabled)) return {};

if (config.get(CONFIG_KEYS.Status.ShowElapsedTime)) {
presence.startTimestamp = config.get(CONFIG_KEYS.Status.ResetElapsedTimePerFile)
? Date.now()
: (previous.startTimestamp ?? Date.now());
} else {
if( config.get( CONFIG_KEYS.Status.ShowElapsedTime ) )
{
if( config.get( CONFIG_KEYS.Status.RestoreElapsedTime ) )
{
presence.startTimestamp = sessionStart - previousTotal;
}
else if( config.get( CONFIG_KEYS.Status.ResetElapsedTimePerFile ) || !previous.startTimestamp )
{
presence.startTimestamp = Date.now();
}
else
{
presence.startTimestamp = previous.startTimestamp;
}
}
else
{
delete presence.startTimestamp;
}

Expand Down
23 changes: 12 additions & 11 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export interface ExtensionConfigurationType {
"status.idle.resetElapsedTime": boolean;
"status.idle.timeout": number;
"status.showElapsedTime": boolean;
"status.RestoreElapsedTime": boolean;
"status.resetElapsedTimePerFile": boolean;
"ignore.workspaces": Array<string>;
"ignore.workspacesText": string | Record<string, string>;
Expand Down Expand Up @@ -267,19 +268,19 @@ export type WorkspaceConfigurationWithType<Configuration extends Record<string,
* @param section Configuration name, supports _dotted_ names.
* @param value The new value.
* @param configurationTarget The {@link ConfigurationTarget configuration target} or a boolean value.
* - If `true` updates {@link ConfigurationTarget.Global Global settings}.
* - If `false` updates {@link ConfigurationTarget.Workspace Workspace settings}.
* - If `undefined` or `null` updates to {@link ConfigurationTarget.WorkspaceFolder Workspace folder settings} if configuration is resource specific,
* otherwise to {@link ConfigurationTarget.Workspace Workspace settings}.
* - If `true` updates {@link ConfigurationTarget.Global Global settings}.
* - If `false` updates {@link ConfigurationTarget.Workspace Workspace settings}.
* - If `undefined` or `null` updates to {@link ConfigurationTarget.WorkspaceFolder Workspace folder settings} if configuration is resource specific,
* otherwise to {@link ConfigurationTarget.Workspace Workspace settings}.
* @param overrideInLanguage Whether to update the value in the scope of requested languageId or not.
* - If `true` updates the value under the requested languageId.
* - If `undefined` updates the value under the requested languageId only if the configuration is defined for the language.
* - If `true` updates the value under the requested languageId.
* - If `undefined` updates the value under the requested languageId only if the configuration is defined for the language.
* @throws error while updating
* - configuration which is not registered.
* - window configuration to workspace folder
* - configuration to workspace or workspace folder when no workspace is opened.
* - configuration to workspace folder when there is no workspace folder settings.
* - configuration to workspace folder when {@link WorkspaceConfiguration} is not scoped to a resource.
* - configuration which is not registered.
* - window configuration to workspace folder
* - configuration to workspace or workspace folder when no workspace is opened.
* - configuration to workspace folder when there is no workspace folder settings.
* - configuration to workspace folder when {@link WorkspaceConfiguration} is not scoped to a resource.
*/
update<S extends keyof Configuration | (string & Record<never, never>)>(
section: S,
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export const CONFIG_KEYS = {
Timeout: "status.idle.timeout" as const
} as const,
ShowElapsedTime: "status.showElapsedTime" as const,
RestoreElapsedTime: "status.RestoreElapsedTime" as const,
ResetElapsedTimePerFile: "status.resetElapsedTimePerFile" as const
} as const,
Ignore: {
Expand Down
8 changes: 8 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { logInfo } from "./logger";
import { dataClass } from "./data";
import { StatusBarMode, editor } from "./editor";

import { loadTotalTime, saveTotalTime } from './session';

export let sessionStart = Math.floor( Date.now() / 1000 );
export let previousTotal = loadTotalTime();

const controller = new RPCController(
getApplicationId(getConfig()).clientId,
getConfig().get(CONFIG_KEYS.Behaviour.Debug)
Expand Down Expand Up @@ -192,6 +197,9 @@ export async function activate(ctx: ExtensionContext) {

export async function deactivate() {
logInfo("Discord Rich Presence for VS Code deactivated.");
const now = Math.floor( Date.now() / 1000 );
const elapsed = now - sessionStart;
saveTotalTime( previousTotal + elapsed );
await controller.destroy();
logInfo("[004] Destroyed Discord RPC client");
}
26 changes: 26 additions & 0 deletions src/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';

const FILE = path.join( os.homedir(), '.vscord-session.json' );

export function loadTotalTime(): number
{
if( !fs.existsSync( FILE ) )
return 0;

try
{
const { totalTime } = JSON.parse( fs.readFileSync( FILE, 'utf8' ) );
return typeof totalTime === 'number' ? totalTime : 0;
}
catch
{
return 0;
}
}

export function saveTotalTime( totalSeconds: number )
{
fs.writeFileSync( FILE, JSON.stringify( { totalTime: totalSeconds } ), 'utf8' );
}