-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
227 lines (192 loc) Β· 6.03 KB
/
extension.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const MODES = require('./mode').MODES;
const DataManager = require('./dataManager');
const ViewManager = require('./viewManager');
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
let dataManager = null;
let viewManager = null;
let interval = null;
const playButtonIcon = '| βΆοΈ';
const pauseButtonIcon = '| βΈ';
const workingIcon = 'π οΈ';
const breakIcon = 'β';
const longBreakIcon = 'π';
let numTomatoes = 0;
let elapsedTime = 0;
let lastTime = new Date().getTime();
let timerRunning = true;
let mode = MODES.working;
let modeIcon = workingIcon;
let pomodorosPerLongBreak = 4;
let timerSpan = null;
let remainingTime = null;
let playButton = null;
let pauseButton = null;
const pad = (n) => {
return (n < 10) ? ("0" + n) : n;
}
function formatTime(time) {
return Math.floor(time/60000) + ':' + pad(Math.floor((time%60000)/1000));
}
function changeMode() {
if (mode === MODES.working) {
if (numTomatoes % pomodorosPerLongBreak === 0) {
mode = MODES.longBreak;
modeIcon = longBreakIcon;
timerSpan = getTimerSpan();
} else {
mode = MODES.break;
modeIcon = breakIcon;
}
} else {
mode = MODES.working;
modeIcon = workingIcon;
}
remainingTime = timerSpan;
elapsedTime = 0;
}
function getTomatoDisplayString() {
const isCondensed = dataManager.getIsCondensed(vscode);
if (isCondensed) {
return numTomatoes + ' π
';
}
if (numTomatoes === 0) {
return 'None yet!';
}
let tomatoes = '';
for (let i = 0; i < numTomatoes; i++) {
tomatoes += 'π
';
}
return tomatoes;
}
function getStatusDisplayString() {
const tomatoes = getTomatoDisplayString();
if (timerRunning) {
return pauseButtonIcon + ' | ' + tomatoes + ' | ' + modeIcon + ' | ' + formatTime(remainingTime) + ' |';
}
return playButtonIcon + ' | ' + tomatoes + ' | ' + modeIcon + ' | ' + formatTime(remainingTime) + ' |';
}
function advance() {
const now = new Date().getTime();
if (timerRunning) {
const delta = (new Date().getTime() - lastTime);
elapsedTime += delta;
remainingTime = timerSpan - elapsedTime;
if (remainingTime <= 0) {
if (mode === MODES.working) {
numTomatoes += 1;
vscode.window.showInformationMessage('You earned a π
! Now take a break.');
} else {
vscode.window.showInformationMessage('Back to work!');
}
changeMode();
initializeTime();
timerRunning = false;
}
playButton.text = '';
pauseButton.text = getStatusDisplayString();
playButton.hide();
pauseButton.show();
} else {
playButton.text = getStatusDisplayString();
pauseButton.text = '';
pauseButton.hide();
playButton.show();
}
lastTime = now;
}
function getTimerSpan() {
if (mode === MODES.working) {
return dataManager.getPomodoroLenghtMilliseconds(vscode);
}
if (mode === MODES.break) {
return dataManager.getShortBreakLengthMilliseconds(vscode);
}
if (mode === MODES.longBreak) {
return dataManager.getLongBreakLengthMilliseconds(vscode);
}
throw(new Error('Invalid mode: ' + mode));
}
function initializeTime() {
lastTime = new Date().getTime();
timerSpan = getTimerSpan();
remainingTime = timerSpan;
elapsedTime = 0;
timerRunning = false;
}
function playTimer() {
timerRunning = true;
}
function pauseTimer() {
timerRunning = false;
}
function showStatusDisplay() {
if (!playButton) {
playButton = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Left, 1
);
}
playButton.text = getStatusDisplayString();
playButton.command = 'pomodoro-timer-vscode.playPomodoro';
if (!timerRunning) {
playButton.show();
}
if (!pauseButton) {
pauseButton = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Left, 2
);
}
pauseButton.text = getStatusDisplayString();
pauseButton.command = 'pomodoro-timer-vscode.pausePomodoro';
if (timerRunning) {
pauseButton.hide();
}
}
function startExtension() {
timerRunning = false;
initializeTime();
showStatusDisplay();
pauseButton = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Left, 2
);
// messageDisplay = vscode.window.createStatusBarItem(
// vscode.StatusBarAlignment.Left, 3
// );
// vscode.window.setStatusBarMessage('π Start earning tomatoes! |');
}
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
dataManager = new DataManager(context.workspaceState);
viewManager = new ViewManager(); // context
numTomatoes = dataManager.getTodaysTomatoes();
pomodorosPerLongBreak = dataManager.getPomodorosPerLongBreak(vscode);
startExtension();
console.log('"pomodoro-timer" is now active');
vscode.window.showInformationMessage('π
timer ready! Hit play to start work session');
let pausePom = vscode.commands.registerCommand('pomodoro-timer-vscode.pausePomodoro', function () {
vscode.window.showInformationMessage('π
timer paused');
pauseTimer();
playButton.show();
pauseButton.hide();
});
let playPom = vscode.commands.registerCommand('pomodoro-timer-vscode.playPomodoro', function () {
showStatusDisplay();
vscode.window.showInformationMessage('π
timer started');
playTimer();
playButton.hide();
pauseButton.show();
interval = this.setInterval(advance, 10);
});
// context.subscriptions.push(pomReady);
context.subscriptions.push(pausePom);
context.subscriptions.push(playPom);
}
// This method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate
}