Skip to content

Commit

Permalink
fixed weird timing issue due to init, updated gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
mralexgray committed Sep 7, 2015
1 parent ad0e16d commit db502e1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ profile
# osx noise
.DS_Store
profile
build
73 changes: 29 additions & 44 deletions App/BitBar/Plugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand All @@ -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 {
Expand Down Expand Up @@ -323,6 +307,7 @@ - (void) setErrorContent:(NSString *)errorContent {
}

- (NSString *)allContent {

if (_allContent == nil) {

if ([self.errorContent length] > 0) {
Expand Down
16 changes: 2 additions & 14 deletions App/BitBarTests/PluginManagerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

Expand All @@ -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");

}
Expand All @@ -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];
Expand Down

0 comments on commit db502e1

Please sign in to comment.