[libcamera-devel,07/10] utils: raspberrypi: ctt: json_pretty_print: Fix indentation handling

Message ID 20200703001422.24324-8-laurent.pinchart@ideasonboard.com
State Accepted
Headers show
Series
  • utils: raspberrypi: ctt: Improve JSON pretty printer
Related show

Commit Message

Laurent Pinchart July 3, 2020, 12:14 a.m. UTC
Indentation is handled by outputting spaces right after outputting a
newline character. That works in most cases, but would result in the
input '{}' being printed as

{
    }

instead of

{
}

Fix it by outputting the indentation before outputting the next
character after a newline. The indentation value will be updated by
then.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 utils/raspberrypi/ctt/ctt_pretty_print_json.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Patch

diff --git a/utils/raspberrypi/ctt/ctt_pretty_print_json.py b/utils/raspberrypi/ctt/ctt_pretty_print_json.py
index 9d450f6d300c..f1c61347d8fc 100644
--- a/utils/raspberrypi/ctt/ctt_pretty_print_json.py
+++ b/utils/raspberrypi/ctt/ctt_pretty_print_json.py
@@ -16,16 +16,20 @@  class JSONPrettyPrinter(object):
             "indent": 0,
             "inarray": [False],
             "arraycount": [],
-            "skipnewline": True
+            "skipnewline": True,
+            "need_indent": False,
         }
 
         self.fout = fout
 
     def newline(self):
         self.fout.write('\n')
-        self.fout.write(' ' * self.state["indent"] * 4)
+        self.state["need_indent"] = True
 
     def write(self, c):
+        if self.state["need_indent"]:
+            self.fout.write(' ' * self.state["indent"] * 4)
+            self.state["need_indent"] = False
         self.fout.write(c)
 
     def process_char(self, c):