orgdo
Version:
Command-line tool to manage the Todo lists
114 lines (113 loc) • 4.36 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const utits_1 = require("./utits");
class Clock {
constructor(client) {
this.client = client;
}
start(id) {
return __awaiter(this, void 0, void 0, function* () {
if (id) {
yield this.client.getTask(id);
}
const state = yield this.client.getClockState();
if (state.type === "running") {
throw new Error(`Clock is running and will break in ${secondToTimeString(state.time)}`);
}
const args = yield this.getClockStartArtgs(id);
yield this.client.startClock(args);
});
}
stop() {
return __awaiter(this, void 0, void 0, function* () {
yield this.client.stopClock();
});
}
status() {
return __awaiter(this, void 0, void 0, function* () {
const state = yield this.client.getClockState();
const clocks = yield this.client.listClocks(todayFilter);
return { state, index: clocks.length };
});
}
update(options) {
return __awaiter(this, void 0, void 0, function* () {
yield this.client.updateClockSettings(options);
return this.client.getClockSettings();
});
}
list(options) {
return __awaiter(this, void 0, void 0, function* () {
if (!options.after) {
const date = utits_1.clearTimeInfo(new Date());
options.after = date.toString();
}
if (!options.before) {
const date = new Date();
date.setSeconds(date.getSeconds() + 1);
options.before = date.toString();
}
const afterFilter = newDateFilter(options.after, "after", v => v > 0);
const beforeFilter = newDateFilter(options.before, "before", v => v <= 0);
const filter = (clock) => {
return afterFilter(clock) && beforeFilter(clock);
};
return this.client.listClocks(filter);
});
}
getClockStartArtgs(id) {
return __awaiter(this, void 0, void 0, function* () {
const clocks = yield this.client.listClocks(todayFilter);
const settings = yield this.client.getClockSettings();
const isLong = (clocks.length + 1) % settings["long-break-count"] === 0;
const args = isLong
? {
workTime: settings["work-time"],
next: "long",
breakTime: settings["long-break-time"]
}
: {
workTime: settings["work-time"],
next: "short",
breakTime: settings["short-break-time"]
};
if (id) {
args.taskId = id;
}
return args;
});
}
}
exports.default = Clock;
function secondToTimeString(time) {
const minutes = Math.floor(time / 60);
const seconds = time % 60;
return `${minutes}:${utits_1.prependZero("" + seconds, 2)}`;
}
exports.secondToTimeString = secondToTimeString;
function todayFilter(clock) {
const date = utits_1.clearTimeInfo(new Date());
return clock.createdAt.getTime() - date.getTime() > 0;
}
function newDateFilter(datestr, field, compare) {
const date = new Date(datestr);
if (isNaN(date)) {
throw new Error(`Invalid ${field} datestr`);
}
return (clock) => {
return compare(clock.createdAt.getTime() - date.getTime());
};
}
function newClockModel(data) {
data.createdAt = new Date(data.createdAt);
return data;
}
exports.newClockModel = newClockModel;