Skip to content

Commit f9cf16f

Browse files
committed
+added configurable hours per day setting
1 parent 5e6db1b commit f9cf16f

7 files changed

+129
-7
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ node_modules
55
.vscode-test/
66

77
.vsix
8-
.images
8+
.images
9+
/config.json

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ Add logo
2424

2525
## 0.5.3
2626

27-
Fix day calculation
27+
Fix day calculation
28+
29+
## 0.5.4
30+
31+
Add config file (now you can set hour per day)

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ If you want to know what time was spent for projects.
77
* Auto activate (no needed hotkey)
88
* Spent time is write into csak-timelog.json file (under vscode folder)
99
* Show current spent time on statusbar
10-
* The day is workday (8 hour per day)
1110
* Timer is paused automatic after 10min inactivity
1211
* Save every 60sec or when saved a document
1312

1413
![](https://github.com/csakaszamok/csak-ext-timer/blob/master/screen1.PNG?raw=true)
1514

15+
### Set workday
1616

17+
The default "workhours per day" value is 0.
18+
In this case show only time is displayed (hour,minute,second)
19+
20+
If you want to see workdays then you can set "workhours per day" value for display.
21+
Press F1, type "csak-ext-timer config" then select saveConfig menu and type the new value between 1-24. New value will be saved.
1722

gif1.gif

273 KB
Loading

package.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "csak-ext-timer",
33
"displayName": "csak-ext-timer",
44
"description": "If you want to know what time was spent for projects",
5-
"version": "0.5.3",
5+
"version": "0.5.4",
66
"publisher": "csakaszamok",
77
"engines": {
88
"vscode": "^1.18.0"
@@ -13,6 +13,15 @@
1313
"activationEvents": [
1414
"*"
1515
],
16+
"contributes": {
17+
"commands": [
18+
{
19+
"command": "extension.csakexttimerFunctions",
20+
"title": "csak-ext-timer config",
21+
"description": "Save/load hours setting for csak-ext-timer"
22+
}
23+
]
24+
},
1625
"icon": "csak-ext-timer-logo.png",
1726
"repository": {
1827
"type": "git",

screen1.PNG

-9.32 KB
Loading

src/extension.ts

+106-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { window, commands, Disposable, ExtensionContext, StatusBarAlignment, Sta
44
import { setInterval, clearInterval } from 'timers';
55
import * as fs from 'fs';
66
import * as vscode from 'vscode';
7+
import Window = vscode.window;
8+
import QuickPickItem = vscode.QuickPickItem;
9+
import QuickPickOptions = vscode.QuickPickOptions;
710

811
const path = require('path');
912
/*const remote = require('electron').remote
@@ -30,8 +33,49 @@ export function activate(context: ExtensionContext) {
3033
// Add to a list of disposables which are disposed when this extension is deactivated.
3134
context.subscriptions.push(controller);
3235
context.subscriptions.push(wordCounter);
36+
37+
vscode.commands.registerCommand('extension.csakexttimerFunctions', csakexttimerFunctions);
3338
}
3439

40+
class Config {
41+
42+
private _hoursperday: number;
43+
44+
constructor() {
45+
this.loadConfig();
46+
}
47+
48+
loadConfig(path?) {
49+
let obj = {};
50+
let _path = path ? path : __dirname + '/../config.json';
51+
if (fs.existsSync(_path)) {
52+
obj = JSON.parse(fs.readFileSync(_path, 'utf8'));
53+
this._hoursperday = (<any>obj).hoursperday;
54+
} else {
55+
this._hoursperday = 0;
56+
}
57+
}
58+
59+
saveConfig(path?) {
60+
let _path = path ? path : __dirname + '/../config.json';
61+
let obj = {
62+
hoursperday: this._hoursperday
63+
}
64+
fs.writeFileSync(_path, JSON.stringify(obj), 'utf8');
65+
}
66+
67+
get hoursperday() {
68+
return this._hoursperday;
69+
}
70+
71+
set hoursperday(value) {
72+
this._hoursperday = value;
73+
}
74+
75+
}
76+
77+
var globalConfig: Config;
78+
3579
class WordCounter {
3680

3781
private _statusBarItem: StatusBarItem;
@@ -45,13 +89,21 @@ class WordCounter {
4589
hours = hours - (days * 24);
4690
minutes = minutes - (days * 24 * 60) - (hours * 60);
4791
seconds = seconds - (days * 24 * 60 * 60) - (hours * 60 * 60) - (minutes * 60);
48-
days = Math.floor((days * 24 + hours) / 8); //8 hour per workday
4992

5093
function padding(num) {
51-
return num < 10 ? '0' + num : num;
94+
let result = '';
95+
result = num < 1000 ? '000' + num : num;
96+
result = num < 100 ? '00' + num : num;
97+
result = num < 10 ? '0' + num : num;
98+
return result;
99+
}
100+
101+
if (globalConfig.hoursperday && globalConfig.hoursperday > 0) {
102+
days = Math.floor((days * 24 + hours) / globalConfig.hoursperday); //8 hour per workday
103+
return `${days} day + ${padding(hours)}:${padding(minutes)}:${padding(seconds)}`;
52104
}
53105

54-
return `${days} day ${padding(hours)}:${padding(minutes)}:${padding(seconds)}`;
106+
return `${padding(hours + (days * 24))}:${padding(minutes)}:${padding(seconds)}`;
55107
}
56108

57109
public updateWordCount(time, inactive = '') {
@@ -125,6 +177,8 @@ class WordCounterController {
125177

126178
public logfile: string = '';
127179

180+
private config: object;
181+
128182
private heartbeat() {
129183

130184
}
@@ -178,6 +232,10 @@ class WordCounterController {
178232
this.starttime = new Date();
179233
this.lasttime = this.starttime;
180234

235+
globalConfig = new Config();
236+
globalConfig.loadConfig();
237+
238+
181239
/* setTimeout(() => {
182240
this._wordCounter.updateWordCount(this.getElapsedTime());
183241
}, 1);*/
@@ -262,3 +320,48 @@ class WordCounterController {
262320
}
263321
}
264322

323+
324+
// Main menu /////////////////////////////////////
325+
function csakexttimerFunctions() {
326+
327+
/*if (!vscode.window.activeTextEditor) {
328+
vscode.window.showInformationMessage('Open a file first to manipulate text selections');
329+
return;
330+
}*/
331+
332+
var opts: QuickPickOptions = { matchOnDescription: true, placeHolder: "Spent timer" };
333+
var items: QuickPickItem[] = [];
334+
335+
items.push({ label: "saveConfig", description: "save hour setting to config file" });
336+
items.push({ label: "loadConfig", description: "load hour setting from config file" });
337+
338+
Window.showQuickPick(items).then((selection) => {
339+
if (!selection) {
340+
return;
341+
}
342+
let e = Window.activeTextEditor;
343+
let d = e.document;
344+
let sel = e.selections;
345+
346+
switch (selection.label) {
347+
case "saveConfig":
348+
vscode.window.showInputBox({ prompt: 'Hours per day' }).then(
349+
val => {
350+
let i = parseInt(val);
351+
globalConfig.hoursperday = i;
352+
globalConfig.saveConfig();
353+
vscode.window.showInformationMessage('Current hours per day is: ' + i);
354+
}
355+
);
356+
break;
357+
case "loadConfig":
358+
globalConfig.loadConfig();
359+
vscode.window.showInformationMessage('Current hours per day is: ' + globalConfig.hoursperday)
360+
break;
361+
default:
362+
console.log("?")
363+
break;
364+
}
365+
});
366+
367+
}

0 commit comments

Comments
 (0)