mirror of
https://github.com/swaywm/sway.git
synced 2024-11-28 19:01:29 +00:00
add pretty output to swaymsg
eventually this will pass a flag to json-c to ignore slashes
This commit is contained in:
parent
c8cf3f70f9
commit
c6561f88e4
|
@ -1,9 +1,14 @@
|
|||
include_directories(
|
||||
${JSONC_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
add_executable(swaymsg
|
||||
main.c
|
||||
)
|
||||
|
||||
target_link_libraries(swaymsg
|
||||
sway-common
|
||||
${JSONC_LIBRARIES}
|
||||
)
|
||||
|
||||
install(
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
#include <sys/un.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#include <json-c/json.h>
|
||||
#include <json-c/printbuf.h>
|
||||
#include "stringop.h"
|
||||
#include "ipc-client.h"
|
||||
#include "readline.h"
|
||||
|
@ -17,6 +19,7 @@ void sway_terminate(void) {
|
|||
|
||||
int main(int argc, char **argv) {
|
||||
static int quiet = 0;
|
||||
static int pretty = 0;
|
||||
char *socket_path = NULL;
|
||||
char *cmdtype = NULL;
|
||||
|
||||
|
@ -35,6 +38,7 @@ int main(int argc, char **argv) {
|
|||
"Usage: swaymsg [options] [message]\n"
|
||||
"\n"
|
||||
" -h, --help Show help message and quit.\n"
|
||||
" -p, --pretty Decode the JSON response and format it.\n"
|
||||
" -q, --quiet Be quiet.\n"
|
||||
" -v, --version Show the version number and quit.\n"
|
||||
" -s, --socket <socket> Use the specified socket.\n"
|
||||
|
@ -43,7 +47,7 @@ int main(int argc, char **argv) {
|
|||
int c;
|
||||
while (1) {
|
||||
int option_index = 0;
|
||||
c = getopt_long(argc, argv, "hqvs:t:", long_options, &option_index);
|
||||
c = getopt_long(argc, argv, "hpqvs:t:", long_options, &option_index);
|
||||
if (c == -1) {
|
||||
break;
|
||||
}
|
||||
|
@ -51,6 +55,9 @@ int main(int argc, char **argv) {
|
|||
case 'q': // Quiet
|
||||
quiet = 1;
|
||||
break;
|
||||
case 'p':
|
||||
pretty = 1;
|
||||
break;
|
||||
case 's': // Socket
|
||||
socket_path = strdup(optarg);
|
||||
break;
|
||||
|
@ -112,7 +119,20 @@ int main(int argc, char **argv) {
|
|||
int socketfd = ipc_open_socket(socket_path);
|
||||
uint32_t len = strlen(command);
|
||||
char *resp = ipc_single_command(socketfd, type, command, &len);
|
||||
if (!quiet) {
|
||||
|
||||
if (pretty) {
|
||||
struct printbuf *pb;
|
||||
if(!(pb = printbuf_new())) {
|
||||
return -1;
|
||||
}
|
||||
printbuf_memappend(pb, resp, sizeof(char)*strlen(resp));
|
||||
struct json_object *obj = json_tokener_parse(pb->buf);
|
||||
|
||||
const char *resp_json = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PRETTY);
|
||||
printf("%s\n", resp_json);
|
||||
free((char*)resp_json);
|
||||
printbuf_free(pb);
|
||||
} else if (!quiet) {
|
||||
printf("%s\n", resp);
|
||||
}
|
||||
close(socketfd);
|
||||
|
|
Loading…
Reference in a new issue