Skip to content

Commit

Permalink
CSV output added
Browse files Browse the repository at this point in the history
  • Loading branch information
olfuerniss committed Dec 5, 2017
1 parent ec5eed5 commit e1e2d88
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 8 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ Command line tool for macOS to to list people with birthdays from your contacts.
Byrthdays is a tool to list people with birthdays from your macOS contacts.
Usage:
byrthdays [-d <days>] [-o <pretty|json|xml>]
byrthdays [-d <days>] [-o <pretty|json|xml|csv>]
Options:
-d extract only birthdays within the given number of days (default 14; -1 for all)
-o to set the output format. Can be either 'pretty', 'json' or 'xml' (default 'pretty')
-o to set the output format. Can be either 'pretty', 'json', 'xml' or 'csv' (default 'pretty')
-h prints this help
byrthdays v1.0.2
Oliver Fürniß, 04/12/2017
byrthdays v1.0.3
Oliver Fürniß, 05/12/2017
Website: https://github.com/olfuerniss/byrthdays
```

I use the JSON output in my byrthdays widget for [Übersicht](http://tracesof.net/uebersicht/) (not released yet). Could also be used in [Alfred](https://www.alfredapp.com/) workflows or menu bar apps like [BitBar](https://getbitbar.com/).
2 changes: 2 additions & 0 deletions byrthdays.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
AE6C41DE1FCDD5A900D60356 /* BirthdayPeople.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BirthdayPeople.m; sourceTree = "<group>"; };
AEC2DEB51FD2955000547018 /* NSMutableString+Extension.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSMutableString+Extension.h"; sourceTree = "<group>"; };
AEC2DEB61FD2955000547018 /* NSMutableString+Extension.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSMutableString+Extension.m"; sourceTree = "<group>"; };
AECC57BA1FD7030300E58B0B /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -52,6 +53,7 @@
children = (
AE38CBC81FCD667A005D1445 /* src */,
AE38CBC71FCD667A005D1445 /* Products */,
AECC57BA1FD7030300E58B0B /* README.md */,
);
sourceTree = "<group>";
};
Expand Down
4 changes: 4 additions & 0 deletions src/NSMutableString+Extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@
- (void) appendXmlElement:(NSString *)name booleanValue:(Boolean)value;
- (void) appendXmlElement:(NSString *)name integerValue:(NSInteger)value;

- (void) appendCsvStringValue:(NSString *)value;
- (void) appendCsvBooleanValue:(Boolean)value;
- (void) appendCsvIntegerValue:(NSInteger)value;

@end
14 changes: 14 additions & 0 deletions src/NSMutableString+Extension.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,18 @@ - (void) appendXmlElement:(NSString *)name integerValue:(NSInteger)value {
[self appendFormat:@"<%@>%ld</%@>", name, value, name];
}

#pragma mark - CSV

- (void) appendCsvStringValue:(NSString *)value {
[self appendFormat:@"\"%@\"", [value stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]];
}

- (void) appendCsvBooleanValue:(Boolean)value {
[self appendFormat:@"%@", value ? @"true" : @"false"];
}

- (void) appendCsvIntegerValue:(NSInteger)value {
[self appendFormat:@"%ld", value];
}

@end
60 changes: 56 additions & 4 deletions src/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
void nsprintf(NSString *format, ...);
void printJSON(NSArray *byrthdays, NSDateFormatter *dateFormatter);
void printXML(NSArray *birthdayPeople, NSDateFormatter *dateFormatter);
void printCSV(NSArray *birthdayPeople, NSDateFormatter *dateFormatter);
void printPretty(NSArray *byrthdays, NSDateFormatter *dateFormatter);
void help(void);

Expand Down Expand Up @@ -65,6 +66,8 @@ int main(int argc, const char * argv[]) {
printJSON(byrthdayPeople, dateFormatter);
} else if([output isEqualToString:@"xml"]) {
printXML(byrthdayPeople, dateFormatter);
} else if([output isEqualToString:@"csv"]) {
printCSV(byrthdayPeople, dateFormatter);
} else {
printPretty(byrthdayPeople, dateFormatter);
}
Expand Down Expand Up @@ -145,6 +148,55 @@ void printXML(NSArray *birthdayPeople, NSDateFormatter *dateFormatter) {
nsprintf(@"%@", resp);
}

void printCSV(NSArray *birthdayPeople, NSDateFormatter *dateFormatter) {
NSMutableString *resp = [[NSMutableString alloc] init];

// header
NSMutableArray *headerEntries = [NSMutableArray arrayWithObjects:
@"uid",
@"me",
@"first_name",
@"last_name",
@"nick_name",
@"birthday_date",
@"next_birthday_date",
@"age",
@"days_to_birthday",
nil];
[headerEntries enumerateObjectsUsingBlock:^(NSString *headerEntry, NSUInteger idx, BOOL *stop) {
[resp appendFormat:@"\"%@\"", headerEntry];

if(idx < [headerEntries count]-1) {
[resp appendString:@", "];
}
}];
[resp appendString:@"\n"];

// content
[birthdayPeople enumerateObjectsUsingBlock:^(BirthdayPerson *person, NSUInteger idx, BOOL *stop) {
[resp appendCsvStringValue:[person uniqueId]];
[resp appendString:@", "];
[resp appendCsvBooleanValue:[person me]];
[resp appendString:@", "];
[resp appendCsvStringValue:[person firstName]];
[resp appendString:@", "];
[resp appendCsvStringValue:[person lastName]];
[resp appendString:@", "];
[resp appendCsvStringValue:[person nickName]];
[resp appendString:@", "];
[resp appendCsvStringValue:[dateFormatter stringFromDate:[person birthdayDate]]];
[resp appendString:@", "];
[resp appendCsvStringValue:[dateFormatter stringFromDate:[person nextBirthdayDate]]];
[resp appendString:@", "];
[resp appendCsvIntegerValue:[person age]];
[resp appendString:@", "];
[resp appendCsvIntegerValue:[person daysToBirthday]];
[resp appendString:@"\n"];
}];

nsprintf(@"%@", resp);
}

void printPretty(NSArray *birthdayPeople, NSDateFormatter *dateFormatter) {
for(BirthdayPerson *person in birthdayPeople) {
if(person.daysToBirthday == 0) {
Expand All @@ -169,14 +221,14 @@ void help() {
NSString *help = @"\n"
"Byrthdays is a tool to list people with birthdays from your macOS contacts. \n\n"
"Usage: \n"
" byrthdays [-d <days>] [-o <pretty|json|xml>] \n\n"
" byrthdays [-d <days>] [-o <pretty|json|xml|csv>] \n\n"
"Options: \n"
" -d extract only birthdays within the given number of days (default 14; -1 for all) \n"
" -o to set the output format. Can be either 'pretty', 'json' or 'xml' (default 'pretty') \n"
" -o to set the output format. Can be either 'pretty', 'json', 'xml' or 'csv' (default 'pretty') \n"
" -h prints this help \n"
"\n"
"byrthdays v1.0.2 \n"
"Oliver Fürniß, 04/12/2017 \n"
"byrthdays v1.0.3 \n"
"Oliver Fürniß, 05/12/2017 \n"
"Website: https://github.com/olfuerniss/byrthdays \n";

nsprintf(help);
Expand Down

0 comments on commit e1e2d88

Please sign in to comment.