Permalink
Browse files

fixed weird timing issue due to init, updated gitignore

  • Loading branch information...
1 parent ad0e16d commit db502e1ed70afd06375272765def94029d903759 @mralexgray mralexgray committed Sep 7, 2015
Showing with 32 additions and 58 deletions.
  1. +1 −0 .gitignore
  2. +29 −44 App/BitBar/Plugin.m
  3. +2 −14 App/BitBarTests/PluginManagerTest.m
View
@@ -16,3 +16,4 @@ profile
# osx noise
.DS_Store
profile
+build
View
@@ -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) {
@@ -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];

0 comments on commit db502e1

Please sign in to comment.