@@ -59,6 +59,8 @@ - (void)start
59
59
60
60
// Wait until homeManagerDidUpdateHomes is called or timeout
61
61
dispatch_group_wait (_homeManagerReadyGroup, dispatch_time (DISPATCH_TIME_NOW, kHomeManagerSetupTimeout ));
62
+
63
+ [self printHomes ];
62
64
}
63
65
64
66
- (void )stop
@@ -100,6 +102,73 @@ - (HMHome *)homeFor:(NSString *)controllerID
100
102
return homes[index ];
101
103
}
102
104
105
+ - (NSString *)paddedString : (NSString *)string width : (NSUInteger )width
106
+ {
107
+ // Using length might not account for all unicode width details, but it's a simple approximation.
108
+ NSUInteger length = string.length ;
109
+ if (length >= width) {
110
+ return string;
111
+ }
112
+ NSMutableString * result = [NSMutableString stringWithString: string];
113
+ for (NSUInteger i = 0 ; i < (width - length); i++) {
114
+ [result appendString: @" " ];
115
+ }
116
+ return result;
117
+ }
118
+
119
+ - (NSString *)repeatString : (NSString *)string count : (NSUInteger )count
120
+ {
121
+ NSMutableString * result = [NSMutableString string ];
122
+ for (NSUInteger i = 0 ; i < count; i++) {
123
+ [result appendString: string];
124
+ }
125
+ return result;
126
+ }
127
+
128
+ - (void )printHomes
129
+ {
130
+ for (HMHome * home in _homeManager.homes ) {
131
+ NSUInteger maxNameLength = 0 ;
132
+ NSUInteger maxNodeIDLength = 0 ;
133
+ NSUInteger maxManufacturerLength = 0 ;
134
+ NSUInteger maxModelLength = 0 ;
135
+
136
+ __auto_type * sortedAccessories = [home.accessories sortedArrayUsingComparator: ^NSComparisonResult (HMAccessory * a, HMAccessory * b) {
137
+ return [a.name localizedCaseInsensitiveCompare: b.name];
138
+ }];
139
+
140
+ for (HMAccessory * accessory in sortedAccessories) {
141
+ maxNameLength = MAX (maxNameLength, accessory.name .length );
142
+ maxManufacturerLength = MAX (maxManufacturerLength, accessory.manufacturer .length );
143
+ maxModelLength = MAX (maxModelLength, accessory.model .length );
144
+ maxNodeIDLength = MAX (maxNodeIDLength, [accessory.matterNodeID stringValue ].length );
145
+ }
146
+
147
+ __auto_type * rows = [NSMutableArray arrayWithCapacity: sortedAccessories.count];
148
+ [sortedAccessories enumerateObjectsUsingBlock: ^(HMAccessory * accessory, NSUInteger idx, BOOL * stop) {
149
+ if (accessory.matterNodeID == nil || [accessory.matterNodeID integerValue ] == 0 ) {
150
+ return ;
151
+ }
152
+
153
+ __auto_type * name = [self paddedString: accessory.name width: maxNameLength];
154
+ __auto_type * manufacturer = [self paddedString: accessory.manufacturer width: maxManufacturerLength];
155
+ __auto_type * model = [self paddedString: accessory.model width: maxModelLength];
156
+ __auto_type * nodeID = [self paddedString: [accessory.matterNodeID stringValue ] width: maxNodeIDLength];
157
+ __auto_type * formattedString = [NSString stringWithFormat: @" %@ │ %@ │ %@ │ %@ " , name, manufacturer, model, nodeID];
158
+ [rows addObject: formattedString];
159
+ }];
160
+
161
+ NSUInteger tableWidth = 1 + maxNameLength + 3 + maxManufacturerLength + 3 + maxModelLength + 3 + maxNodeIDLength + 1 ;
162
+ NSLog (@" ╔%@ ╗" , [self repeatString: @" ═" count: tableWidth]);
163
+ NSLog (@" ║%@ ║" , [self paddedString: [NSString stringWithFormat: @" %@ [%@ ] " , home.name, home.matterControllerID] width: tableWidth]);
164
+ NSLog (@" ╠%@ ╣" , [self repeatString: @" ═" count: tableWidth]);
165
+ for (NSString * row in rows) {
166
+ NSLog (@" ║%@ ║" , row);
167
+ }
168
+ NSLog (@" ╚%@ ╝" , [self repeatString: @" ═" count: tableWidth]);
169
+ }
170
+ }
171
+
103
172
- (NSString *)homeControllerIDFor : (NSString *)controllerID
104
173
{
105
174
__auto_type * home = [self homeFor: controllerID];
0 commit comments