diff --git a/.gitignore b/.gitignore index f519b9927..37d6309c7 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ profile # osx noise .DS_Store profile +build \ No newline at end of file diff --git a/App/BitBar/Plugin.m b/App/BitBar/Plugin.m index 5f35bf1ef..61214aa38 100644 --- a/App/BitBar/Plugin.m +++ b/App/BitBar/Plugin.m @@ -11,54 +11,42 @@ #import "NSDate+TimeAgo.h" #import "NSColor+Hex.h" -#define DEFAULT_TIME_INTERVAL_SECONDS 60 +#define DEFAULT_TIME_INTERVAL_SECONDS ((double)60.) @implementation Plugin - init { - if (self = [super init]) { - - self.currentLine = -1; - self.cycleLinesIntervalSeconds = 5; - - } - return self; + + return self = super.init ? _currentLine = -1, _cycleLinesIntervalSeconds = 5, self : nil; } - initWithManager:(PluginManager*)manager { - if (self = [self init]) { - _manager = manager; - } - return self; + + return self = self.init ? _manager = manager, self : nil; } -- (NSStatusItem *)statusItem { - - if (_statusItem == nil) { +- (NSStatusItem *)statusItem { return _statusItem = _statusItem ?: ({ // make the status item _statusItem = [self.manager.statusBar statusItemWithLength:NSVariableStatusItemLength]; // build the menu [self rebuildMenuForStatusItem:_statusItem]; - - } - - return _statusItem; + + _statusItem; + + }); } - (NSMenuItem*) buildMenuItemWithParams:(NSDictionary *)params { + NSString * title = [params objectForKey:@"title"]; - SEL sel = nil; - if ([params objectForKey:@"href"] != nil) { - sel = @selector(performMenuItemHREFAction:); - } - if ([params objectForKey:@"bash"] != nil) { - sel = @selector(performMenuItemOpenTerminalAction:); - } + SEL sel = params[@"href"] ? @selector(performMenuItemHREFAction:) + : params[@"bash"] ? @selector(performMenuItemOpenTerminalAction:) : nil; + NSMenuItem * item = [NSMenuItem.alloc initWithTitle:title action:sel keyEquivalent:@""]; - if (sel != nil) { + if (sel) { item.representedObject = params; [item setTarget:self]; } @@ -83,39 +71,35 @@ - (NSAttributedString*) attributedTitleWithParams:(NSDictionary *)params { } - (NSMenuItem*) buildMenuItemForLine:(NSString *)line { - NSDictionary * params = [self dictionaryForLine:line]; - return [self buildMenuItemWithParams:params]; + + return [self buildMenuItemWithParams:[self dictionaryForLine:line]]; } - (NSDictionary*) dictionaryForLine:(NSString *)line { + NSRange found = [line rangeOfString:@"|"]; - if (found.location == NSNotFound) { - return @{ @"title": line }; - } + if (found.location == NSNotFound) return @{ @"title": line }; + NSString * title = [[line substringToIndex:found.location] - stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; - NSMutableDictionary * params = NSMutableDictionary.new; - [params setObject:title forKey:@"title"]; + stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet]; + NSMutableDictionary * params = @{@"title":title}.mutableCopy; NSString * paramsStr = [line substringFromIndex:found.location + found.length]; - NSArray * paramsArr = [[paramsStr - stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] - componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; + NSArray * paramsArr = [[paramsStr stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet] + componentsSeparatedByCharactersInSet:NSCharacterSet.whitespaceCharacterSet]; for (NSString * paramStr in paramsArr) { NSRange found = [paramStr rangeOfString:@"="]; if (found.location != NSNotFound) { - NSString * key = [[paramStr substringToIndex:found.location] lowercaseString]; + NSString * key = [paramStr substringToIndex:found.location].lowercaseString; NSString * value = [paramStr substringFromIndex:found.location + found.length]; - [params setObject:value forKey:key]; + params[key] = value; } } return params; } - (void) performMenuItemHREFAction:(NSMenuItem *)menuItem { - NSMutableDictionary * params = menuItem.representedObject; - NSString * href = [params objectForKey:@"href"]; - NSURL * url = [NSURL URLWithString:href]; - [[NSWorkspace sharedWorkspace] openURL:url]; + + [NSWorkspace.sharedWorkspace openURL:[NSURL URLWithString:menuItem.representedObject[@"href"]]]; } - (void) performMenuItemOpenTerminalAction:(NSMenuItem *)menuItem { @@ -323,6 +307,7 @@ - (void) setErrorContent:(NSString *)errorContent { } - (NSString *)allContent { + if (_allContent == nil) { if ([self.errorContent length] > 0) { diff --git a/App/BitBarTests/PluginManagerTest.m b/App/BitBarTests/PluginManagerTest.m index 988a81d48..83323ac5e 100644 --- a/App/BitBarTests/PluginManagerTest.m +++ b/App/BitBarTests/PluginManagerTest.m @@ -16,18 +16,6 @@ @interface PluginManagerTest : XCTestCase @implementation PluginManagerTest -- (void)setUp -{ - [super setUp]; - // Put setup code here. This method is called before the invocation of each test method in the class. -} - -- (void)tearDown -{ - // Put teardown code here. This method is called after the invocation of each test method in the class. - [super tearDown]; -} - - (void)testInit { @@ -40,7 +28,7 @@ - (void)testPluginFiles { PluginManager *manager = [PluginManager.alloc initWithPluginPath:@"~/Work/bitbar/BitBar/BitBarTests/TestPlugins"]; - NSArray *pluginFiles = [manager pluginFiles]; + NSArray *pluginFiles = manager.plugins; XCTAssertEqual((NSUInteger)3, [pluginFiles count], @"pluginFiles count"); } @@ -49,7 +37,7 @@ - (void)testPlugins { PluginManager *manager = [PluginManager.alloc initWithPluginPath:@"~/Work/bitbar/BitBar/BitBarTests/TestPlugins"]; - NSArray *plugins = [manager plugins]; + NSArray *plugins = manager.plugins; XCTAssertEqual((NSUInteger)3, [plugins count], @"plugins count"); Plugin *one = [plugins objectAtIndex:0];