Skip to content

Commit

Permalink
cli: add pretty-print option
Browse files Browse the repository at this point in the history
PR-URL: #395
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Dmytro Nechai <nechaido@gmail.com>
  • Loading branch information
belochub committed Dec 21, 2018
1 parent fa4789c commit 02c4fd2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
6 changes: 6 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ const args = yargs
type: 'number',
describe: 'Heartbeat interval in milliseconds',
})
.option('pretty-print', {
alias: 'p',
type: 'string',
describe: 'Enable pretty-print. You can pass a string or a number' +
' to specify indentation',
})
.epilogue(`
Environment variables:
JSTP_CLI_HISTORY path to the persistent CLI history file
Expand Down
15 changes: 13 additions & 2 deletions lib/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ module.exports = class Cli extends EventEmitter {
this.client = {
heartbeatInterval: options.heartbeatInterval,
};
this.indent = options.prettyPrint;
const numIndent = Number(this.indent);
if (this.indent === '') {
this.indent = 2;
} else if (!Number.isNaN(numIndent) && numIndent !== 0) {
this.indent = Number(this.indent);
}

if (options.verbose) {
this.client.logger = new EventEmitter();
Expand All @@ -33,7 +40,9 @@ module.exports = class Cli extends EventEmitter {
(!message.pong && !message.ping) ||
options.verbose === VERBOSENESS_LEVEL.ALL
) {
this.log(`Outgoing message:\n${mdsf.stringify(message)}`);
this.log(`Outgoing message:\n${mdsf.stringify(
message, null, this.indent
)}`);
}
});

Expand All @@ -42,7 +51,9 @@ module.exports = class Cli extends EventEmitter {
(!message.pong && !message.ping) ||
options.verbose === VERBOSENESS_LEVEL.ALL
) {
this.log(`Incoming message:\n${mdsf.stringify(message)}`);
this.log(`Incoming message:\n${mdsf.stringify(
message, null, this.indent
)}`);
}
});
}
Expand Down
4 changes: 3 additions & 1 deletion lib/cli/command-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ module.exports = class CommandProcessor extends EventEmitter {
// TODO: make event registering generic
connection.on('event', (interfaceName, eventName, args) => {
this.cli.log(`Received remote event '${eventName}'` +
` in interface '${interfaceName}': ${mdsf.stringify(args)}`);
` in interface '${interfaceName}':\n${mdsf.stringify(
args, null, this.cli.indent
)}`);
});
connection.on('error', err => {
this.cli._logErr(err);
Expand Down
6 changes: 4 additions & 2 deletions lib/cli/line-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ module.exports = class LineProcessor {
}
const iface = args[0];
const method = args[1];
const res = mdsf.stringify(result);
const msg = `Method ${iface}.${method} returned: ${res}`;
const res = mdsf.stringify(
result, null, this.commandProcessor.cli.indent
);
const msg = `Method ${iface}.${method} returned:\n${res}`;
callback(null, msg);
});
}
Expand Down

0 comments on commit 02c4fd2

Please sign in to comment.