Skip to content

Commit c812091

Browse files
feat: timer countdown
1 parent 0e08287 commit c812091

2 files changed

Lines changed: 44 additions & 45 deletions

File tree

src/commands.ts

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function commandFactory(statusBarItems: MobStatusBarItem[]) {
1919
}),
2020
vscode.commands.registerCommand("mob-vscode-gui.start", async () => {
2121
const timeInput = await vscode.window.showInputBox({
22-
title: "How much time?",
22+
title: "How many minutes?",
2323
placeHolder: "Enter to ignore",
2424
validateInput: (input) => timerInputValidator(input),
2525
});
@@ -49,10 +49,14 @@ export function commandFactory(statusBarItems: MobStatusBarItem[]) {
4949

5050
if (timer > 0) {
5151
const timerCountdown = new TimerCountdown();
52-
timerCountdown.startTimer(Number(timeInput));
52+
timerCountdown.startTimer(timer);
5353
}
5454
} finally {
5555
startItem?.stopLoading();
56+
57+
if (timer > 0) {
58+
startItem?.startCountDown(timer)
59+
}
5660
}
5761
}),
5862
vscode.commands.registerCommand("mob-vscode-gui.next", async () => {
@@ -66,6 +70,9 @@ export function commandFactory(statusBarItems: MobStatusBarItem[]) {
6670
await asyncExec(command, expectedMessage);
6771
} finally {
6872
nextItem?.stopLoading();
73+
74+
const startItem = statusBarItems.find((item) => item.id === "start");
75+
startItem?.stopLoading();
6976
}
7077
}),
7178
vscode.commands.registerCommand("mob-vscode-gui.done", async () => {
@@ -79,48 +86,14 @@ export function commandFactory(statusBarItems: MobStatusBarItem[]) {
7986
await asyncExec(command, expectedMessage);
8087
} finally {
8188
utilsItem?.stopLoading();
82-
}
83-
}),
84-
vscode.commands.registerCommand("mob-vscode-gui.reset", () => {
85-
const validInputs = {
86-
yes: ["y", "yes"],
87-
no: ["n", "no"],
88-
};
89-
90-
const input = vscode.window.showInputBox({
91-
title:
92-
"Are you sure? This will delete your commits in the WIP branch (but preserves uncommitted work)",
93-
placeHolder: "yes(y) / no(n)",
94-
validateInput: (input) => {
95-
if (
96-
!validInputs.yes.includes(input.toLowerCase()) &&
97-
!validInputs.no.includes(input.toLowerCase())
98-
) {
99-
return "Please enter yes(y) or no(n)";
100-
}
101-
102-
return null;
103-
},
104-
});
10589

106-
input.then(async (input) => {
107-
if (input && validInputs.yes.includes(input)) {
108-
const command = "mob reset";
109-
const expectedMessage = ["Branches", "deleted"];
110-
111-
const utilsItem = statusBarItems.find((item) => item.id === "utils");
112-
utilsItem?.startLoading("Reseting session...");
113-
try {
114-
await asyncExec(command, expectedMessage);
115-
} finally {
116-
utilsItem?.stopLoading();
117-
}
118-
}
119-
});
90+
const startItem = statusBarItems.find((item) => item.id === "start");
91+
startItem?.stopLoading();
92+
}
12093
}),
12194
vscode.commands.registerCommand("mob-vscode-gui.timer", () => {
12295
const timeInput = vscode.window.showInputBox({
123-
title: "How much time?",
96+
title: "How many minutes?",
12497
placeHolder: "Enter to ignore",
12598
validateInput: (input) => timerInputValidator(input),
12699
});
@@ -131,6 +104,9 @@ export function commandFactory(statusBarItems: MobStatusBarItem[]) {
131104

132105
if (timer > 0) {
133106
command += ` ${timer}`;
107+
108+
const startItem = statusBarItems.find((item) => item.id === "start");
109+
startItem?.startCountDown(timer);
134110
}
135111

136112
const expectedMessage = ["Happy collaborating!"];
@@ -148,11 +124,6 @@ export function commandFactory(statusBarItems: MobStatusBarItem[]) {
148124
description: "Commit mob session",
149125
command: "mob-vscode-gui.done",
150126
},
151-
// {
152-
// label: "Reset",
153-
// description: "Delete local and remote WIP branch",
154-
// command: "mob-vscode-gui.reset",
155-
// },
156127
{
157128
label: "Timer",
158129
description: "Set timer (in minutes)",

src/status-bar-items/mob-status-bar-item.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ interface Props {
1111

1212
export class MobStatusBarItem {
1313
private _statusBarItem: StatusBarItem;
14+
private _lastSetInterval: NodeJS.Timeout | null = null;
1415

1516
constructor(private readonly _props: Props) {
1617
this._statusBarItem = window.createStatusBarItem(
@@ -37,10 +38,37 @@ export class MobStatusBarItem {
3738
}
3839

3940
public stopLoading() {
41+
if(this._lastSetInterval !== null) {
42+
clearInterval(this._lastSetInterval);
43+
this._lastSetInterval = null;
44+
}
45+
4046
this._statusBarItem.text = `$(${this._props.icon}) ${this._props.name}`;
4147
this._statusBarItem.show();
4248
}
4349

50+
public startCountDown(minutes: number) {
51+
const self = this;
52+
let seconds = minutes * 60;
53+
54+
if(this._lastSetInterval !== null) {
55+
clearInterval(this._lastSetInterval);
56+
this._lastSetInterval = null;
57+
}
58+
59+
this._lastSetInterval = setInterval(function () {
60+
var date = new Date(0);
61+
date.setSeconds(seconds);
62+
var timeString = date.toISOString().substr(11, 8);
63+
64+
self._statusBarItem.text = `${timeString}`;
65+
66+
if (--seconds < 0) {
67+
return;
68+
}
69+
}, 1000);
70+
}
71+
4472
public dispose() {
4573
this._statusBarItem.dispose();
4674
}

0 commit comments

Comments
 (0)