gron.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/JsonArray.h>
  27. #include <AK/JsonObject.h>
  28. #include <AK/JsonValue.h>
  29. #include <AK/StringBuilder.h>
  30. #include <LibCore/File.h>
  31. #include <stdio.h>
  32. static bool use_color = false;
  33. static void print(const String& name, const JsonValue&, Vector<String>& trail);
  34. static const char* color_name = "";
  35. static const char* color_index = "";
  36. static const char* color_brace = "";
  37. static const char* color_bool = "";
  38. static const char* color_null = "";
  39. static const char* color_string = "";
  40. static const char* color_off = "";
  41. int main(int argc, char** argv)
  42. {
  43. if (pledge("stdio tty rpath", nullptr) < 0) {
  44. perror("pledge");
  45. return 1;
  46. }
  47. if (isatty(STDOUT_FILENO))
  48. use_color = true;
  49. if (pledge("stdio rpath", nullptr) < 0) {
  50. perror("pledge");
  51. return 1;
  52. }
  53. if (argc != 2) {
  54. fprintf(stderr, "usage: gron <file>\n");
  55. return 0;
  56. }
  57. auto file = Core::File::construct(argv[1]);
  58. if (!file->open(Core::IODevice::ReadOnly)) {
  59. fprintf(stderr, "Couldn't open %s for reading: %s\n", argv[1], file->error_string());
  60. return 1;
  61. }
  62. if (pledge("stdio", nullptr) < 0) {
  63. perror("pledge");
  64. return 1;
  65. }
  66. auto file_contents = file->read_all();
  67. auto json = JsonValue::from_string(file_contents);
  68. if (use_color) {
  69. color_name = "\033[33;1m";
  70. color_index = "\033[35;1m";
  71. color_brace = "\033[36m";
  72. color_bool = "\033[32;1m";
  73. color_string = "\033[31;1m";
  74. color_null = "\033[34;1m";
  75. color_off = "\033[0m";
  76. }
  77. Vector<String> trail;
  78. print("json", json, trail);
  79. return 0;
  80. }
  81. static void print(const String& name, const JsonValue& value, Vector<String>& trail)
  82. {
  83. for (size_t i = 0; i < trail.size(); ++i)
  84. printf("%s", trail[i].characters());
  85. printf("%s%s%s = ", color_name, name.characters(), color_off);
  86. if (value.is_object()) {
  87. printf("%s{}%s;\n", color_brace, color_off);
  88. trail.append(String::format("%s%s%s.", color_name, name.characters(), color_off));
  89. value.as_object().for_each_member([&](auto& on, auto& ov) { print(on, ov, trail); });
  90. trail.take_last();
  91. return;
  92. }
  93. if (value.is_array()) {
  94. printf("%s[]%s;\n", color_brace, color_off);
  95. trail.append(String::format("%s%s%s", color_name, name.characters(), color_off));
  96. for (int i = 0; i < value.as_array().size(); ++i) {
  97. auto element_name = String::format("%s%s[%s%s%d%s%s]%s", color_off, color_brace, color_off, color_index, i, color_off, color_brace, color_off);
  98. print(element_name, value.as_array()[i], trail);
  99. }
  100. trail.take_last();
  101. return;
  102. }
  103. switch (value.type()) {
  104. case JsonValue::Type::Null:
  105. case JsonValue::Type::Undefined:
  106. printf("%s", color_null);
  107. break;
  108. case JsonValue::Type::Bool:
  109. printf("%s", color_bool);
  110. break;
  111. case JsonValue::Type::String:
  112. printf("%s", color_string);
  113. break;
  114. default:
  115. printf("%s", color_index);
  116. break;
  117. }
  118. printf("%s%s;\n", value.serialized<StringBuilder>().characters(), color_off);
  119. }