Add pretty printing to swaymsg

If stdout is a tty, it will pretty print unless -r (--raw) is given.

Sample outputs:

```
~/s/s/build > ./bin/swaymsg fullscreen toggle
Error: Permission denied for fullscreen toggle via IPC

~/s/s/build > ./bin/swaymsg -t get_workspaces
Workspace 3:三
  Output: DVI-I-1
  Layout: splith

Workspace 1:一 (off-screen)
  Output: HDMI-A-1
  Layout: splith

Workspace 5:五 (focused)
  Output: HDMI-A-1
  Layout: splith

~/s/s/build > ./bin/swaymsg -t get_inputs
Input device Metadot - Das Keyboard Das Keyboard
  Type: Keyboard
  Sway ID: 9456:320:Metadot_-_Das_Keyboard_Das_Keyb

Input device Wacom Intuos S 2 Pen
  Type: Tablet tool
  Sway ID: 1386:827:Wacom_Intuos_S_2

Input device Wacom Intuos S 2 Pad
  Type: Tablet pad
  Sway ID: 1386:827:Wacom_Intuos_S_2

Input device Logitech Gaming Mouse G502
  Type: Keyboard, Mouse
  Sway ID: 1133:49277:Logitech_Gaming_Mous

~/s/s/build > ./bin/swaymsg -t get_outputs
Output DVI-I-1
  Geometry: 1920x1080 @ 3840,0
  Scale factor: 1x
  Workspace: 3:三

Output DVI-D-1
  Geometry: 1920x1080 @ 0,0
  Scale factor: 1x
  Workspace: 4:四

Output HDMI-A-1
  Geometry: 1920x1080 @ 1920,0
  Scale factor: 1x
  Workspace: 5:五
```
This commit is contained in:
Drew DeVault 2017-04-03 07:27:25 -04:00
parent 8d9a928058
commit 60ce81e06a
5 changed files with 248 additions and 17 deletions

View file

@ -420,17 +420,9 @@ void ipc_client_handle_command(struct ipc_client *client) {
}
json_object *inputs = json_object_new_array();
if (input_devices) {
for(int i=0; i<input_devices->length; i++) {
for(int i = 0; i<input_devices->length; i++) {
struct libinput_device *device = input_devices->items[i];
char* identifier = libinput_dev_unique_id(device);
json_object *device_object = json_object_new_object();
if (!identifier) {
json_object_object_add(device_object, "identifier", NULL);
} else {
json_object_object_add(device_object, "identifier", json_object_new_string(identifier));
}
json_object_array_add(inputs, device_object);
free(identifier);
json_object_array_add(inputs, ipc_json_describe_input(device));
}
}
const char *json_string = json_object_to_json_string(inputs);