-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.ts
123 lines (105 loc) · 3.22 KB
/
cli.ts
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
import { DAYS, Lesson } from "./parser.ts";
import { genCalendar } from "./mod.ts";
const lessons: { [title: string]: Lesson } = {};
let day = DAYS[0];
const help = `
new - Create a new lesson entry
show - Show the timetable
help - Show this message
day <num> - Set day of the week, 1 for monday (Default), and 7 for sunday
finish - Generate timetable.ics file and exit
delete <title> - Delete the lesson entry of specified title
`
.split("\n")
.filter((l) => l.includes(" - "))
.map((l) =>
l
.split(" - ")
.map((v, i) => {
if (i === 0) return v.padEnd(16, " ");
else return v;
})
.join("")
)
.join("\n");
const nextTime = (t: string) => {
const time = t.split(":").map((v) => parseInt(v));
const mapTime = (t: number[]) =>
t.map((v) => v.toString().padStart(2, "0")).join(":");
if (time[1] === 0) {
return mapTime([time[0], time[1] + 30]);
} else if (time[1] === 30) {
return mapTime([time[0] + 1, 0]);
} else {
throw new Error("Invalid time");
}
};
console.log("Welcome to the interactive CLI of e-bridge Timetable parser");
console.log("\n" + help + "\n");
while (true) {
const command = prompt(">", "")?.trim();
if (!command) continue;
// Process command
if (command === "new") {
// Command: new
console.log("Current day of week:", day);
const startInput = prompt("This class starts from: (eg. 9:00 or 9)", "")!;
const duration = prompt("This class lasts for: (hours)", "")!;
const start = startInput.includes(":") ? startInput : `${startInput}:00`;
const time = [start];
let i = parseFloat(duration) * 2;
while (--i) {
time.push(nextTime(time[time.length - 1]));
}
const paste = [];
console.log("You can paste the class info now.");
while (paste.length < 4) {
const input = prompt("", "")!.trim();
if (!input) continue;
else paste.push(input);
}
const lesson: Lesson = {
teachers: paste[1],
location: paste[2],
weeks: paste[3],
day,
time,
};
lessons[paste[0]] = lesson;
} else if (command === "show") {
// Command: show
console.table(lessons);
} else if (command === "help") {
// Command: help
console.log(help);
} else if (command.startsWith("day ") || command === "day") {
// Command: day
if (command === "day") {
console.log('Invalid usage of "day"');
continue;
}
const newDay = command.split(/\s+/);
const newDayId = parseInt(newDay[1]);
if (1 <= newDayId && newDayId <= 7) {
day = DAYS[newDayId - 1];
console.log("Current day of the week set to", day);
} else {
console.log("Invalid day number, must be between 1 to 7");
}
} else if (command === "finish") {
// Command: finish
const calendar = genCalendar(lessons);
Deno.writeTextFileSync("./timetable.ics", calendar.toString());
} else if (command.startsWith("delete ") || command === "delete") {
// Command: delete
if (command === "delete") {
console.log('Invalid usage of "delete"');
continue;
}
const title = command.slice(7).trim();
if (lessons[title]) delete lessons[title];
else console.log("Title not found");
} else {
console.log("Unknown command");
}
}