Skip to content

Commit b938a49

Browse files
committed
✨ Output events in markdown
1 parent d83ca48 commit b938a49

File tree

6 files changed

+65
-6
lines changed

6 files changed

+65
-6
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ A macOS tool to fetch (the next) calendar events as json
1616
Example commands (use `plan --help` for full usage)
1717

1818
- `plan today` Returns all events for today
19+
- `plan today --format=markdown` Returns all events for today in markdown
1920
- `plan next` Returns the current or next event within the next hour
2021
- `plan next --reject-tag somekeyword` Returns the current or next event within the next hour ignoring events which notes have text containing `tag:somekeyword`
2122
- `plan calendars` List available calendars
2223

23-
Example output
24+
Example output in json
2425

2526
```json
2627
[
@@ -42,6 +43,13 @@ Example output
4243
}
4344
]
4445
```
46+
47+
Example output in markdown
48+
49+
```markdown
50+
- 18:00 - 19:00 🏆 Release plan
51+
```
52+
4553
## Installation
4654

4755
**Via Github**

Sources/Cli.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,26 @@ struct Today: ParsableCommand {
2323
abstract: "List todays schedule"
2424
)
2525

26+
@Option(help: ArgumentHelp(
27+
"Reject events which notes contain the tag <t> eg. 'timeblock' ",
28+
discussion: "Events with tag will be ignored",
29+
valueName: "t"
30+
)) var rejectTag: String = ""
31+
32+
@Option(help: ArgumentHelp(
33+
"Outpt format <f>. Available: json or markdown ",
34+
discussion: "Output format",
35+
valueName: "f"
36+
)) var format: Format = .json
37+
2638
mutating func run() {
27-
Plan().today().printAsJson()
39+
let events = Plan().today(rejectTag: rejectTag)
40+
switch format {
41+
case .json:
42+
events.printAsMarkdown()
43+
case .markdown:
44+
events.printAsJson()
45+
}
2846
}
2947
}
3048

Sources/Event.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,19 @@ extension [Event] {
172172
print("fail")
173173
}
174174
}
175+
176+
//
177+
178+
// - 09:00 - 12:00 🚊 Travel Home
179+
// - 12:00 - 13:00 🥗 Lunch
180+
func printAsMarkdown() {
181+
let dateFormatter = DateFormatter()
182+
dateFormatter.dateFormat = "HH:mm"
183+
for event in self {
184+
let startHour = dateFormatter.string(from: event.startsAt)
185+
let endHour = dateFormatter.string(from: event.endsAt)
186+
let line = "- \(startHour) - \(endHour) \(event.label)"
187+
print(line)
188+
}
189+
}
175190
}

Sources/EventStore.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ struct EventStore {
2121
return eventStore
2222
}
2323

24-
func today() -> [Event] {
24+
func today(
25+
filterAfter: (Event) -> Bool
26+
) -> [Event] {
2527
let today = Calendar.current.startOfDay(for: Date())
2628
let start = Calendar.current.date(byAdding: .day, value: 0, to: today)!
2729
let end = Calendar.current.date(byAdding: .day, value: 1, to: today)!
2830

29-
return fetch(start: start, end: end, filterBefore: FiltersBefore.accept, filterAfter: FiltersAfter.accept)
31+
return fetch(start: start, end: end, filterBefore: FiltersBefore.accept, filterAfter: filterAfter)
3032
}
3133

3234
func calendars() -> [Cal] {

Sources/Format.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import ArgumentParser
2+
3+
enum Format: String, ExpressibleByArgument {
4+
case json, markdown
5+
}

Sources/Plan.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,19 @@ import Foundation
44
struct Plan {
55
let store = EventStore()
66

7-
func today() -> [Event] {
8-
EventStore().today()
7+
func today(
8+
rejectTag: String
9+
) -> [Event] {
10+
var filtersAfter: [(Event) -> Bool] = []
11+
if !rejectTag.isEmpty {
12+
let rtf: (Event) -> Bool = FiltersAfter.rejectTag(tag: rejectTag)
13+
filtersAfter.append(rtf)
14+
}
15+
let filterAfter = filtersAfter.count > 0 ? FiltersAfter.combined(filters: filtersAfter) : FiltersAfter.accept
16+
17+
return EventStore().today(
18+
filterAfter: filterAfter
19+
)
920
}
1021

1122
func calendars() -> [Cal] {

0 commit comments

Comments
 (0)