Skip to content

Commit 39981b3

Browse files
committed
Add simple destination shortening for status bar #7
1 parent ba03dfc commit 39981b3

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

TranzportMac.xcodeproj/project.pbxproj

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
AA95FAF51B0273EA00CED5A4 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AA95FAF41B0273EA00CED5A4 /* Images.xcassets */; };
2222
AA95FAF81B0273EA00CED5A4 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = AA95FAF61B0273EA00CED5A4 /* MainMenu.xib */; };
2323
AA95FB041B0273EA00CED5A4 /* TranzportMacTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA95FB031B0273EA00CED5A4 /* TranzportMacTests.swift */; };
24+
AAAC8DEA1B04E54100D4D5E9 /* StationFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAAC8DE91B04E54100D4D5E9 /* StationFormatter.swift */; };
2425
AAC0FF231B029B5E00B320EA /* StartupLaunch.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAC0FF221B029B5E00B320EA /* StartupLaunch.swift */; };
2526
/* End PBXBuildFile section */
2627

@@ -53,6 +54,7 @@
5354
AA95FAFD1B0273EA00CED5A4 /* TranzportMacTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TranzportMacTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
5455
AA95FB021B0273EA00CED5A4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
5556
AA95FB031B0273EA00CED5A4 /* TranzportMacTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TranzportMacTests.swift; sourceTree = "<group>"; };
57+
AAAC8DE91B04E54100D4D5E9 /* StationFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StationFormatter.swift; sourceTree = "<group>"; };
5658
AAC0FF221B029B5E00B320EA /* StartupLaunch.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StartupLaunch.swift; sourceTree = "<group>"; };
5759
/* End PBXFileReference section */
5860

@@ -94,7 +96,8 @@
9496
isa = PBXGroup;
9597
children = (
9698
AAC0FF221B029B5E00B320EA /* StartupLaunch.swift */,
97-
AA5C45F21B02839D00923BC3 /* Alamofire */,
99+
AAAC8DE61B04E44600D4D5E9 /* External */,
100+
AAAC8DE91B04E54100D4D5E9 /* StationFormatter.swift */,
98101
);
99102
name = Helpers;
100103
sourceTree = "<group>";
@@ -179,6 +182,14 @@
179182
name = "Supporting Files";
180183
sourceTree = "<group>";
181184
};
185+
AAAC8DE61B04E44600D4D5E9 /* External */ = {
186+
isa = PBXGroup;
187+
children = (
188+
AA5C45F21B02839D00923BC3 /* Alamofire */,
189+
);
190+
name = External;
191+
sourceTree = "<group>";
192+
};
182193
/* End PBXGroup section */
183194

184195
/* Begin PBXNativeTarget section */
@@ -278,6 +289,7 @@
278289
isa = PBXSourcesBuildPhase;
279290
buildActionMask = 2147483647;
280291
files = (
292+
AAAC8DEA1B04E54100D4D5E9 /* StationFormatter.swift in Sources */,
281293
AA5C45EB1B02839000923BC3 /* Download.swift in Sources */,
282294
AA5C45F61B02841400923BC3 /* Departure.swift in Sources */,
283295
AA5C45EE1B02839000923BC3 /* Request.swift in Sources */,

TranzportMac/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
9999
menu.removeAllItems()
100100

101101
if departures.count != 0 {
102-
statusBarItem.title = departures.first?.description
102+
statusBarItem.title = departures.first?.shortenedDescription
103103

104104
for dep in departures {
105105
let depItem = NSMenuItem()

TranzportMac/Departure.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class Departure: NSObject, Printable, NSCoding {
1919
return line + "" + destination + " in " + minutes + " min"
2020
}
2121

22+
var shortenedDescription: String {
23+
return line + "" + StationFormatter.shortenStationName(destination) + " (" + minutes + " min)"
24+
}
25+
2226
override init() {
2327
}
2428

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// StationFormatter.swift
3+
// TranzportMac
4+
//
5+
// Created by Stephan Rabanser on 14/05/15.
6+
// Copyright (c) 2015 Stephan Rabanser. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
extension String {
12+
public func indexOfCharacter(char: Character) -> Int? {
13+
if let idx = find(self, char) {
14+
return distance(self.startIndex, idx)
15+
}
16+
return nil
17+
}
18+
}
19+
20+
class StationFormatter {
21+
22+
class func shortenStationName(sName: String) -> String {
23+
var needle: Character = "("
24+
var shortenedString = sName
25+
if find(sName, needle) == nil {
26+
needle = " "
27+
if let idx = find(sName, needle) {
28+
let pos = distance(sName.startIndex, idx)
29+
shortenedString = sName.substringWithRange(Range<String.Index>(start: sName.startIndex, end: advance(sName.startIndex, pos + 2)))
30+
shortenedString = shortenedString + ["."]
31+
} else {
32+
needle = "-"
33+
if let idx = find(sName, needle) {
34+
let pos = distance(sName.startIndex, idx)
35+
shortenedString = sName.substringWithRange(Range<String.Index>(start: sName.startIndex, end: advance(sName.startIndex, pos + 2)))
36+
shortenedString = shortenedString + ["."]
37+
}
38+
}
39+
}
40+
return shortenedString
41+
}
42+
43+
}

0 commit comments

Comments
 (0)