@@ -19,7 +19,7 @@ @interface AppDelegate ()
1919
2020@property (nonatomic , strong , readwrite ) NSStatusItem *statusItem;
2121@property (nonatomic , strong , readwrite ) SMCSensors *smcSensors;
22- @property (nonatomic , strong , readwrite ) NSDictionary *profile;
22+ @property (nonatomic , strong , readwrite ) NSMutableDictionary *profile;
2323@property (atomic , readwrite ) BOOL isMenuOpen;
2424
2525@end
@@ -34,13 +34,13 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification
3434 self.smcSensors = [[SMCSensors alloc ] init ];
3535 self.profile = [self loadProfile ];
3636
37+ NSMenu *menu = [[NSMenu alloc ] init ];
3738 self.statusItem = [[NSStatusBar systemStatusBar ] statusItemWithLength: NSVariableStatusItemLength ];
38- self.statusItem .menu = [[ NSMenu alloc ] init ] ;
39- [ self .statusItem. menu setDelegate: self ] ;
39+ self.statusItem .menu = menu ;
40+ menu. delegate = self;
4041#ifdef NON_SELECTABLE
41- [self .statusItem. menu setAutoenablesItems: NO ];
42+ [menu setAutoenablesItems: NO ];
4243#endif
43- [self .statusItem.menu addItemWithTitle: @" Quit" action: @selector (terminate: ) keyEquivalent: @" Q" ];
4444 self.statusItem .button .image = [NSImage imageNamed: @" StatusItem-Image" ];
4545
4646 [self updateStatusItemMenu: [NSApplication sharedApplication ]];
@@ -79,6 +79,9 @@ - (void)updateStatusItemMenu:(id)sender
7979 continue ;
8080 }
8181 NSString *humanReadableName = [self getHumanReadableString: key];
82+ if (![humanReadableName isKindOfClass: [NSString class ]]) {
83+ continue ;
84+ }
8285// NSLog(@"%@: %.1f C\n", humanReadableName, temperature);
8386 if (self.isMenuOpen && temperature >= 20 ) {
8487 color = [self getTempColor: temperature];
@@ -190,27 +193,74 @@ - (NSString *)sysctlByName:(const char *)name
190193 return str;
191194}
192195
193- - (NSDictionary *)loadProfile
196+ - (NSMutableDictionary *)loadProfile
194197{
195198 NSString *config;
196199 NSString *machine = [self sysctlByName: " hw.model" ];
197200 if (machine) {
198201 config = [[NSBundle mainBundle ] pathForResource: machine ofType: @" plist" inDirectory: @" Profiles" ];
199202 if (config) {
200- NSDictionary *profile = [NSDictionary dictionaryWithContentsOfFile: config];
203+ NSMutableDictionary *profile = [NSMutableDictionary dictionaryWithContentsOfFile: config];
201204 if (profile) {
202205 return profile;
203206 }
204207 }
205208 }
206209 config = [[NSBundle mainBundle ] pathForResource: @" Default" ofType: @" plist" inDirectory: @" Profiles" ];
207- return config ? [NSDictionary dictionaryWithContentsOfFile: config] : nil ;
210+ return config ? [NSMutableDictionary dictionaryWithContentsOfFile: config] : nil ;
208211}
209212
210213- (NSString *)getHumanReadableString : (NSString *)key
211214{
212- NSString *val = self.profile ? self.profile [key] : nil ;
213- return val ?: [self .smcSensors humanReadableNameForKey: key];
215+ if (self.profile ) {
216+ const char *k;
217+ NSString *val = self.profile [key];
218+ if (val) {
219+ return val;
220+ }
221+ k = [key UTF8String ];
222+ for (NSString *it in self.profile .allKeys ) {
223+ ssize_t i, j;
224+ const char *p = [it UTF8String ];
225+ for (i = 0 , j = -1 ; ; i++) {
226+ if (p[i] != k[i]) {
227+ if (p[i] != ' ?' || !isdigit (k[i]) || j != -1 ) {
228+ break ;
229+ }
230+ j = i;
231+ }
232+ if (p[i] == ' \0 ' ) {
233+ const char *v;
234+ char buf[1024 ];
235+ int n = k[j] - ' 0' ;
236+ int replacements = 0 ;
237+ val = self.profile [it];
238+ if (![val isKindOfClass: [NSString class ]]) {
239+ return val;
240+ }
241+ v = [val UTF8String ];
242+ for (i = 0 , j = 0 ; v[i] && j < 1000 ; i++) {
243+ if (v[i] == ' \\ ' && isxdigit (v[i + 1 ])) {
244+ int c = v[++i];
245+ if (n + c > ' 9' && c <= ' 9' ) {
246+ c += ' A' - ' 9' - 1 ;
247+ }
248+ buf[j++] = n + c;
249+ replacements++;
250+ continue ;
251+ }
252+ buf[j++] = v[i];
253+ }
254+ buf[j] = ' \0 ' ;
255+ if (replacements == 0 ) {
256+ sprintf (&buf[j], " #%d " , n);
257+ }
258+ return self.profile [key] = [NSString stringWithUTF8String: buf];
259+ }
260+ }
261+ }
262+ }
263+ return [self .smcSensors humanReadableNameForKey: key];
214264}
215265
216266#pragma mark - NSMenuDelegate
0 commit comments