Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewCaseres committed Apr 4, 2021
1 parent 26ef35c commit 90a219b
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 29 deletions.
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,27 @@
"author": "Matthew Caseres <matthewcaseres@outlook.com>",
"license": "MIT",
"devDependencies": {
"@types/google-spreadsheet": "^3.0.2",
"@types/hh-mm-ss": "^1.2.0",
"@types/json2csv": "^5.0.1",
"@types/luxon": "^1.26.1",
"@types/node": "^14.14.31",
"@types/uuid": "^8.3.0",
"google-spreadsheet": "^3.1.15",
"luxon": "^1.26.0",
"ts-node": "^9.1.1",
"typescript": "^4.2.2"
},
"dependencies": {
"@types/hh-mm-ss": "^1.2.0",
"colors": "^1.4.0",
"hh-mm-ss": "^1.2.0",
"log-update": "^4.0.0"
"log-update": "^4.0.0",
"yaml": "^1.10.2"
},
"scripts": {
"timer": "npx ts-node usage/scripts/run.ts",
"timer": "npx ts-node usage/run.ts",
"habit": "npx ts-node usage/habits.ts",
"CSV": "node usage/scripts/CSV.js",
"build": "tsc"
}
}
31 changes: 5 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,16 @@ export type TimeInput = {
s?: number;
};
export type TimeBlock<T extends string = string> = {
activity: T | "pomodoroBreak";
activity: T;
finishUnder?: boolean;
start?: Date;
end?: Date;
focus?: number;
} & TimeInput;
export type LoggedBlock = {
goalSeconds: number;
loggedSeconds: number;
activity: string;
finishUnder?: boolean | undefined;
start?: Date | undefined;
end?: Date | undefined;
focus?: number | undefined;
};
type PomodoroMakerInputs<T extends string> = {activity: T | "pomodoroBreak", blocks: number, blockLength: TimeInput, breakLength: TimeInput}
export function pomodoroMaker<T extends string = string>({activity, blocks, blockLength, breakLength}: PomodoroMakerInputs<T>) {
let onWork: TimeBlock<T> = {activity, ...blockLength}
let onBreak: TimeBlock<T> = {activity: "pomodoroBreak", ...breakLength, finishUnder: true}
let pom: TimeBlock<T>[] = [{...onWork}]
for (let i = 1; i < blocks; i++) {
pom = [...pom, {...onBreak}, {...onWork}]
}
return pom
}

export async function pomodoro<T extends string = string>(
timeBlocks: TimeBlock<T>[],
bindings?: Record<string, (timeBlocks: TimeBlock<T>[], index: number) => TimeBlock<T>[]>
): Promise<LoggedBlock[]> {
): Promise<(TimeBlock & {goalSeconds: number, loggedSeconds: number})[]> {
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
return new Promise((resolve, reject) => {
Expand All @@ -48,12 +28,11 @@ export async function pomodoro<T extends string = string>(
logUpdate(displayTimeBlocks(timeBlocks));
}, 200);
//Keeps track of current timeblock.
process.stdin.on("keypress", (str) => {
process.stdin.on("keypress", (str, key) => {
if (bindings && str in bindings) {
timeBlocks = bindings[str](timeBlocks, i)
} else if (str.match(/[0-9]/)) {
} else if (key && (key.name == 'enter' || key.name == 'return')) {
timeBlocks[i].end = new Date();
timeBlocks[i].focus = parseInt(str);
if (i < timeBlocks.length - 1) {
i++;
timeBlocks[i].start = new Date();
Expand Down Expand Up @@ -86,7 +65,7 @@ function displayTimeBlock(timeBlock: TimeBlock) {
return getTimeBlockColors(timeBlock)(
`${timeBlock.activity} ${TimeFormat.fromS(
secondsPassed
)}/${TimeFormat.fromS(totalSeconds)} ${timeBlock.focus ?? ""}`
)}/${TimeFormat.fromS(totalSeconds)}`
);
}

Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"resolveJsonModule": true,
"target": "es5",
"module": "commonjs",
"declaration": true,
Expand Down
Loading

0 comments on commit 90a219b

Please sign in to comment.