-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
108 lines (91 loc) · 2.75 KB
/
extension.js
File metadata and controls
108 lines (91 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const vscode = require('vscode');
const fs = require('fs');
const path = require('path');
let startTime = null;
let accumulatedTimeMs = 0;
let interval = null;
let statusBarItem = null;
let storageFilePath = '';
function activate(context) {
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders) return;
const workspacePath = workspaceFolders[0].uri.fsPath;
storageFilePath = path.join(workspacePath, '.devtime.json');
loadStoredTime();
const startCmd = vscode.commands.registerCommand('devtime.startTracking', () => {
if (!startTime) {
startTime = new Date();
startInterval();
vscode.window.showInformationMessage('DevTime tracking started.');
} else {
vscode.window.showInformationMessage('Tracking is already running.');
}
});
const stopCmd = vscode.commands.registerCommand('devtime.stopTracking', () => {
if (startTime) {
const now = new Date();
accumulatedTimeMs += now.getTime() - startTime.getTime();
startTime = null;
clearInterval(interval);
saveTime();
updateStatusBar();
vscode.window.showInformationMessage('DevTime tracking stopped.');
} else {
vscode.window.showInformationMessage('Tracking is not running.');
}
});
context.subscriptions.push(startCmd, stopCmd);
statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
statusBarItem.show();
// ✅ Inicia automaticamente o tracking ao abrir o projeto
startTime = new Date();
startInterval();
updateStatusBar();
}
function startInterval() {
interval = setInterval(() => {
updateStatusBar();
}, 1000);
}
function updateStatusBar() {
let totalMs = accumulatedTimeMs;
if (startTime) {
const now = new Date();
totalMs += now.getTime() - startTime.getTime();
}
const totalHours = totalMs / (1000 * 60 * 60);
const displayTime = totalHours.toFixed(2);
if (statusBarItem) {
statusBarItem.text = `$(clock) DevTime: ${displayTime} hrs`;
}
}
function loadStoredTime() {
try {
if (fs.existsSync(storageFilePath)) {
const data = fs.readFileSync(storageFilePath, 'utf8');
const parsed = JSON.parse(data);
accumulatedTimeMs = typeof parsed.accumulatedTimeMs === 'number' ? parsed.accumulatedTimeMs : 0;
}
} catch (err) {
console.error('Failed to load stored DevTime:', err);
}
}
function saveTime() {
try {
const data = { accumulatedTimeMs };
fs.writeFileSync(storageFilePath, JSON.stringify(data), 'utf8');
} catch (err) {
console.error('Failed to save DevTime:', err);
}
}
function deactivate() {
if (startTime) {
const now = new Date();
accumulatedTimeMs += now.getTime() - startTime.getTime();
saveTime();
}
}
module.exports = {
activate,
deactivate
};