Skip to content

Commit

Permalink
new-default formatter as comma separated key-value pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Aug 21, 2019
1 parent 3b002e3 commit b049ee5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ STATE can be one of: 1, on, 0, off, toggle
ID can be either address in form xxxxxxxxxxxx, xx-xx-xx-xx-xx-xx or xx:xx:xx:xx:xx:xx, or name of device to search in used devices
FORMAT can be one of:
default - human readable text output not intended for consumption by scripts
new-default - human readable comma separated key-value pairs (EXPERIMENTAL, THE BEHAVIOUR MAY CHANGE)
json - compact JSON
json-pretty - pretty printed JSON
```
Expand Down
25 changes: 23 additions & 2 deletions blueutil.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ void usage(FILE *io) {
io_puts(io, "ID can be either address in form xxxxxxxxxxxx, xx-xx-xx-xx-xx-xx or xx:xx:xx:xx:xx:xx, or name of device to search in used devices");
io_puts(io, "FORMAT can be one of:");
io_puts(io, " default - human readable text output not intended for consumption by scripts");
io_puts(io, " new-default - human readable comma separated key-value pairs (EXPERIMENTAL, THE BEHAVIOUR MAY CHANGE)");
io_puts(io, " json - compact JSON");
io_puts(io, " json-pretty - pretty printed JSON");
}
Expand Down Expand Up @@ -221,8 +222,7 @@ bool parse_unsigned_long_arg(char *arg, unsigned long *number) {
return device;
}

void list_devices_default(NSArray *devices, bool _first_only) {
(void)_first_only; // unused
void list_devices_default(NSArray *devices, bool first_only) {
for (IOBluetoothDevice* device in devices) {
printf("address: %s", [[device addressString] UTF8String]);
if ([device isConnected]) {
Expand All @@ -235,6 +235,22 @@ void list_devices_default(NSArray *devices, bool _first_only) {
printf(", name: \"%s\"", [[device name] UTF8String]);
printf(", recent access date: %s", [[[device recentAccessDate] description] UTF8String]);
printf("\n");
if (first_only) break;
}
}

void list_devices_new_default(NSArray *devices, bool first_only) {
const char *separator = first_only ? "\n" : ", ";
for (IOBluetoothDevice* device in devices) {
printf("address: %s%s", [[device addressString] UTF8String], separator);
printf("recent access: %s%s", [[[device recentAccessDate] description] UTF8String], separator);
printf("favourite: %s%s", [device isFavorite] ? "yes" : "no", separator);
printf("paired: %s%s", [device isPaired] ? "yes" : "no", separator);
printf("connected: %s%s", [device isConnected] ? ([device isIncoming] ? "slave" : "master") : "no", separator);
printf("rssi: %s%s", [device isConnected] ? [[NSString stringWithFormat: @"%d", [device RSSI]] UTF8String] : "-", separator);
printf("raw rssi: %s%s", [device isConnected] ? [[NSString stringWithFormat: @"%d", [device rawRSSI]] UTF8String] : "-", separator);
printf("name: %s\n", [[device name] UTF8String]);
if (first_only) break;
}
}

Expand Down Expand Up @@ -291,6 +307,11 @@ bool parse_output_formatter(char *arg, formatterFunc *formatter) {
return true;
}

if (0 == strcasecmp(arg, "new-default")) {
if (formatter) *formatter = list_devices_new_default;
return true;
}

if (0 == strcasecmp(arg, "json")) {
if (formatter) *formatter = list_devices_json_default;
return true;
Expand Down

0 comments on commit b049ee5

Please sign in to comment.