Skip to content

Commit

Permalink
Add GANTT_CHART as option, create Settings class
Browse files Browse the repository at this point in the history
  • Loading branch information
cfurrow committed Mar 16, 2022
1 parent 3895d2c commit b4f9e85
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
4 changes: 4 additions & 0 deletions alfred/info.plist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@
<key>WIKI_LINK</key>
<string>false</string>
</dict>
<dict>
<key>GANTT_CHART</key>
<string>false</string>
</dict>
<key>version</key>
<string>{{version}}</string>
<key>webaddress</key>
Expand Down
33 changes: 26 additions & 7 deletions cal_to_md.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,22 @@
import EventKit
import Foundation

class Settings {
var useWikiLinks: Bool
var addGanttChart: Bool

init(useWikiLinks: Bool, addGanttChart: Bool) {
self.useWikiLinks = useWikiLinks
self.addGanttChart = addGanttChart
}
}
class CalendarPermissionError: Error { }

let semaphore = DispatchSemaphore(value: 1)
let surroundEventNameWithWikiLink = ProcessInfo.processInfo.environment["WIKI_LINK"]?.lowercased() == "true"
let addGanttChart = ProcessInfo.processInfo.environment["GANTT_CHART"]?.lowercased() == "true"
let settings = Settings(useWikiLinks: surroundEventNameWithWikiLink, addGanttChart: addGanttChart)
let defaultSettings = Settings(useWikiLinks: false, addGanttChart: false)

defer {
semaphore.wait()
Expand Down Expand Up @@ -69,17 +82,21 @@ class EventHelper {
}

class BaseFormatter {
var settings: Settings
var events: [EKEvent] = []
var output : String

init(events: [EKEvent]) {
init(events: [EKEvent], settings: Settings?) {
// value of optional type 'Settings?' must be unwrapped to a value of type 'Settings'
// if let ... is a way to safely unwrap the optional value.
// Or you can use the nil-coalescing operator (??) to provide a default value if the optional value is nil.
// self.settings = settings ?? defaultSettings
self.settings = settings ?? defaultSettings
self.events = events
self.output = ""
}

func buildEventDescription(event:EKEvent) -> String {
let surroundEventNameWithWikiLink = ProcessInfo.processInfo.environment["WIKI_LINK"]

let dateFormatter = DateTimeHelper.buildDateFormatter()
let timeFormatter = DateTimeHelper.buildTimeFormatter()
let date = dateFormatter.string(from: event.startDate)
Expand All @@ -88,7 +105,7 @@ class BaseFormatter {
let title = EventHelper.sanitizeEventTitle(event: event)

var eventName = "\(date) - \(title)"
if(surroundEventNameWithWikiLink?.lowercased() == "true") {
if(self.settings.useWikiLinks) {
eventName = "[[\(eventName)]]"
}

Expand Down Expand Up @@ -190,9 +207,11 @@ func handleAuthorized(store:EKEventStore, semaphore:DispatchSemaphore) {
eventsArray.sort { (event1, event2) -> Bool in
return event1.startDate < event2.startDate
}
let ganttFormatter = GanttFormatter(events: eventsArray)
print(ganttFormatter.build())
let listFormatter = ListFormatter(events: eventsArray)
if (settings.addGanttChart) {
let ganttFormatter = GanttFormatter(events: eventsArray, settings: settings)
print(ganttFormatter.build())
}
let listFormatter = ListFormatter(events: eventsArray, settings: settings)
print(listFormatter.build())
}

Expand Down

0 comments on commit b4f9e85

Please sign in to comment.