Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
keith committed Jun 27, 2016
0 parents commit 164ff66
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.build
Packages
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DEVELOPMENT-SNAPSHOT-2016-02-08-a
18 changes: 18 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Copyright (c) 2016 Keith Smiley (http://keith.so)

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the 'Software'), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 changes: 8 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import PackageDescription

let package = Package(
name: "reminders",
dependencies: [
.Package(url: "https://github.com/kylef/Commander", majorVersion:0)
]
)
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# reminders-cli

A simple CLI for interacting with OS X reminders.

## Usage:

#### Show all lists

```
$ reminders show-lists
Soon
Eventually
```

#### Show reminders on a specific list

```
$ reminders show Soon
0 Write README
1 Ship reminders-cli
```

#### Complete an item on a list

```
$ reminders complete Soon 0
$ reminders show Soon
0 Ship reminders-cli
```

#### Add a reminder to a list

```
$ reminders add Soon Go to sleep
$ reminders show Soon
0 Ship reminders-cli
1 Go to sleep
```

## Installation:

#### With [Homebrew](http://brew.sh/)

```
$ brew install keith/formulae/reminders-cli
```

#### Manually

Download the latest release from
[here](https://github.com/keith/reminders-cli/releases)

```
$ tar -zxvf reminders.tar.gz
$ mv reminders /usr/local/bin
$ rm reminders.tar.gz
```

#### Building manually

Install [swiftenv](https://github.com/kylef/swiftenv/)

```
$ cd reminders-cli
$ swiftenv install
$ swift build --configuration release
$ cp .build/release/reminders /usr/local/bin/reminders
```
11 changes: 11 additions & 0 deletions Sources/CollectionType+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extension CollectionType {
func find(@noescape predicate: (Generator.Element) throws -> Bool) rethrows -> Generator.Element? {
return try self.indexOf(predicate).flatMap { self[$0] }
}
}

extension CollectionType where Index == Int {
subscript(safe index: Int) -> Generator.Element? {
return index < self.count && index >= 0 ? self[index] : nil
}
}
5 changes: 5 additions & 0 deletions Sources/GCD.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

func executeOnMainQueue(closure: () -> Void) {
dispatch_async(dispatch_get_main_queue(), closure)
}
11 changes: 11 additions & 0 deletions Sources/NSDate+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation

extension NSDate: Comparable {}

public func < (lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.compare(rhs) == NSComparisonResult.OrderedAscending
}

public func == (lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.isEqualToDate(rhs)
}
100 changes: 100 additions & 0 deletions Sources/Reminders.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import EventKit

private let Store = EKEventStore()

final class Reminders {
func requestAccess(completion: (granted: Bool) -> Void) {
Store.requestAccessToEntityType(.Reminder) { granted, _ in
executeOnMainQueue {
completion(granted: granted)
}
}
}

func showLists() {
let calendars = self.getCalendars()
for calendar in calendars {
print(calendar.title)
}
}

func showListItems(withName name: String) {
let calendar = self.calendar(withName: name)
let semaphore = dispatch_semaphore_create(0)

self.reminders(onCalendar: calendar) { reminders in
for (i, reminder) in reminders.enumerate() {
print(i, reminder.title)
}

dispatch_semaphore_signal(semaphore)
}

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
}

func complete(itemAtIndex index: Int, onListNamed name: String) {
let calendar = self.calendar(withName: name)
let semaphore = dispatch_semaphore_create(0)

self.reminders(onCalendar: calendar) { reminders in
guard let reminder = reminders[safe: index] else {
print("No reminder at index \(index) on \(name)")
exit(1)
}

do {
reminder.completed = true
try Store.saveReminder(reminder, commit: true)
} catch let error {
print("Failed to save reminder with error: \(error)")
}

dispatch_semaphore_signal(semaphore)
}

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
}

func addReminder(string string: String, toListNamed name: String) {
let calendar = self.calendar(withName: name)
let reminder = EKReminder(eventStore: Store)
reminder.calendar = calendar
reminder.title = string

do {
try Store.saveReminder(reminder, commit: true)
} catch let error {
print("Failed to save reminder with error: \(error)")
}
}

// MARK: - Private functions

private func reminders(onCalendar calendar: EKCalendar,
completion: (reminders: [EKReminder]) -> Void)
{
let predicate = Store.predicateForRemindersInCalendars([calendar])
Store.fetchRemindersMatchingPredicate(predicate) { reminders in
let reminders = reminders?.filter { !$0.completed }
.sort { $0.creationDate < $1.creationDate }
completion(reminders: reminders ?? [])
}
}

private func calendar(withName name: String) -> EKCalendar {
if let calendar = self.getCalendars()
.find({ $0.title.lowercaseString == name.lowercaseString })
{
return calendar
} else {
print("No reminders list matching \(name)")
exit(1)
}
}

private func getCalendars() -> [EKCalendar] {
return Store.calendarsForEntityType(.Reminder)
.filter { $0.allowsContentModifications }
}
}
32 changes: 32 additions & 0 deletions Sources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import AppKit
import Commander

private let reminders = Reminders()
private func createCLI() -> Group {
return Group {
$0.command("show-lists") {
reminders.showLists()
}
$0.command("show") { (listName: String) in
reminders.showListItems(withName: listName)
}
$0.command("complete") { (listName: String, index: Int) in
reminders.complete(itemAtIndex: index, onListNamed: listName)
}
$0.command("add") { (listName: String, parser: ArgumentParser) in
let string = parser.remainder.joinWithSeparator(" ")
reminders.addReminder(string: string, toListNamed: listName)
}
}
}

reminders.requestAccess { granted in
if granted {
createCLI().run()
} else {
print("Need to grant reminders access")
exit(1)
}
}

NSApplication.sharedApplication().run()

0 comments on commit 164ff66

Please sign in to comment.