mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:07:36 +00:00
jp: set input to stdin if there is no file specified and add
and customizable indentation level An example: cat /proc/net/adapters | jp Another example: cat /proc/all | jp -i 2 (indents are set to 2 spaces, instead of 4 by default)
This commit is contained in:
parent
4f22c92b99
commit
6c2f690a74
1 changed files with 18 additions and 14 deletions
|
@ -34,11 +34,11 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static void print(const JsonValue& value, int indent = 0, bool use_color = true);
|
static void print(const JsonValue& value, int spaces_per_indent, int indent = 0, bool use_color = true);
|
||||||
static void print_indent(int indent)
|
static void print_indent(int indent, int spaces_per_indent)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < indent; ++i)
|
for (int i = 0; i < indent * spaces_per_indent; ++i)
|
||||||
out(" ");
|
out(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
|
@ -49,12 +49,16 @@ int main(int argc, char** argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* path = nullptr;
|
const char* path = nullptr;
|
||||||
|
int spaces_in_indent = 4;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
args_parser.set_general_help("Pretty-print a JSON file with syntax-coloring and indentation.");
|
args_parser.set_general_help("Pretty-print a JSON file with syntax-coloring and indentation.");
|
||||||
args_parser.add_positional_argument(path, "Path to JSON file", "path");
|
args_parser.add_option(spaces_in_indent, "Indent size", "indent-size", 'i', "spaces_in_indent");
|
||||||
|
args_parser.add_positional_argument(path, "Path to JSON file", "path", Core::ArgsParser::Required::No);
|
||||||
|
VERIFY(spaces_in_indent >= 0);
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(argc, argv);
|
||||||
|
if (path == nullptr)
|
||||||
|
path = "/dev/stdin";
|
||||||
auto file = Core::File::construct(path);
|
auto file = Core::File::construct(path);
|
||||||
if (!file->open(Core::IODevice::ReadOnly)) {
|
if (!file->open(Core::IODevice::ReadOnly)) {
|
||||||
warnln("Couldn't open {} for reading: {}", path, file->error_string());
|
warnln("Couldn't open {} for reading: {}", path, file->error_string());
|
||||||
|
@ -73,13 +77,13 @@ int main(int argc, char** argv)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
print(json.value(), 0, isatty(STDOUT_FILENO));
|
print(json.value(), spaces_in_indent, 0, isatty(STDOUT_FILENO));
|
||||||
outln();
|
outln();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print(const JsonValue& value, int indent, bool use_color)
|
void print(const JsonValue& value, int spaces_per_indent, int indent, bool use_color)
|
||||||
{
|
{
|
||||||
if (value.is_object()) {
|
if (value.is_object()) {
|
||||||
size_t printed_members = 0;
|
size_t printed_members = 0;
|
||||||
|
@ -87,17 +91,17 @@ void print(const JsonValue& value, int indent, bool use_color)
|
||||||
outln("{{");
|
outln("{{");
|
||||||
object.for_each_member([&](auto& member_name, auto& member_value) {
|
object.for_each_member([&](auto& member_name, auto& member_value) {
|
||||||
++printed_members;
|
++printed_members;
|
||||||
print_indent(indent + 1);
|
print_indent(indent + 1, spaces_per_indent);
|
||||||
if (use_color)
|
if (use_color)
|
||||||
out("\"\033[33;1m{}\033[0m\": ", member_name);
|
out("\"\033[33;1m{}\033[0m\": ", member_name);
|
||||||
else
|
else
|
||||||
out("\"{}\": ", member_name);
|
out("\"{}\": ", member_name);
|
||||||
print(member_value, indent + 1, use_color);
|
print(member_value, spaces_per_indent, indent + 1, use_color);
|
||||||
if (printed_members < static_cast<size_t>(object.size()))
|
if (printed_members < static_cast<size_t>(object.size()))
|
||||||
out(",");
|
out(",");
|
||||||
outln();
|
outln();
|
||||||
});
|
});
|
||||||
print_indent(indent);
|
print_indent(indent, spaces_per_indent);
|
||||||
out("}}");
|
out("}}");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -107,13 +111,13 @@ void print(const JsonValue& value, int indent, bool use_color)
|
||||||
outln("[");
|
outln("[");
|
||||||
array.for_each([&](auto& entry_value) {
|
array.for_each([&](auto& entry_value) {
|
||||||
++printed_entries;
|
++printed_entries;
|
||||||
print_indent(indent + 1);
|
print_indent(indent + 1, spaces_per_indent);
|
||||||
print(entry_value, indent + 1, use_color);
|
print(entry_value, spaces_per_indent, indent + 1, use_color);
|
||||||
if (printed_entries < static_cast<size_t>(array.size()))
|
if (printed_entries < static_cast<size_t>(array.size()))
|
||||||
out(",");
|
out(",");
|
||||||
outln();
|
outln();
|
||||||
});
|
});
|
||||||
print_indent(indent);
|
print_indent(indent, spaces_per_indent);
|
||||||
out("]");
|
out("]");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue