@@ -4,6 +4,9 @@ import { window, commands, Disposable, ExtensionContext, StatusBarAlignment, Sta
4
4
import { setInterval , clearInterval } from 'timers' ;
5
5
import * as fs from 'fs' ;
6
6
import * as vscode from 'vscode' ;
7
+ import Window = vscode . window ;
8
+ import QuickPickItem = vscode . QuickPickItem ;
9
+ import QuickPickOptions = vscode . QuickPickOptions ;
7
10
8
11
const path = require ( 'path' ) ;
9
12
/*const remote = require('electron').remote
@@ -30,8 +33,49 @@ export function activate(context: ExtensionContext) {
30
33
// Add to a list of disposables which are disposed when this extension is deactivated.
31
34
context . subscriptions . push ( controller ) ;
32
35
context . subscriptions . push ( wordCounter ) ;
36
+
37
+ vscode . commands . registerCommand ( 'extension.csakexttimerFunctions' , csakexttimerFunctions ) ;
33
38
}
34
39
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
+
35
79
class WordCounter {
36
80
37
81
private _statusBarItem : StatusBarItem ;
@@ -45,13 +89,21 @@ class WordCounter {
45
89
hours = hours - ( days * 24 ) ;
46
90
minutes = minutes - ( days * 24 * 60 ) - ( hours * 60 ) ;
47
91
seconds = seconds - ( days * 24 * 60 * 60 ) - ( hours * 60 * 60 ) - ( minutes * 60 ) ;
48
- days = Math . floor ( ( days * 24 + hours ) / 8 ) ; //8 hour per workday
49
92
50
93
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 ) } ` ;
52
104
}
53
105
54
- return `${ days } day ${ padding ( hours ) } :${ padding ( minutes ) } :${ padding ( seconds ) } ` ;
106
+ return `${ padding ( hours + ( days * 24 ) ) } :${ padding ( minutes ) } :${ padding ( seconds ) } ` ;
55
107
}
56
108
57
109
public updateWordCount ( time , inactive = '' ) {
@@ -125,6 +177,8 @@ class WordCounterController {
125
177
126
178
public logfile : string = '' ;
127
179
180
+ private config : object ;
181
+
128
182
private heartbeat ( ) {
129
183
130
184
}
@@ -178,6 +232,10 @@ class WordCounterController {
178
232
this . starttime = new Date ( ) ;
179
233
this . lasttime = this . starttime ;
180
234
235
+ globalConfig = new Config ( ) ;
236
+ globalConfig . loadConfig ( ) ;
237
+
238
+
181
239
/* setTimeout(() => {
182
240
this._wordCounter.updateWordCount(this.getElapsedTime());
183
241
}, 1);*/
@@ -262,3 +320,48 @@ class WordCounterController {
262
320
}
263
321
}
264
322
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