Bläddra i källkod

[Closes #33] Minify output with --compress

isundil 9 år sedan
förälder
incheckning
860f016e6e
5 ändrade filer med 56 tillägg och 31 borttagningar
  1. 2 0
      include/params.hh
  2. 4 6
      include/simpleOutput.hh
  3. 1 1
      src/main.cpp
  4. 6 0
      src/params.cpp
  5. 43 24
      src/simpleOutput.cpp

+ 2 - 0
include/params.hh

@@ -84,6 +84,7 @@ class Params: public AParams
         bool isIgnoringUnicode() const;
         bool isDiff() const;
         bool sortObjects() const;
+        bool compressed() const;
 
     private:
         /**
@@ -100,5 +101,6 @@ class Params: public AParams
         bool strict;
         bool diffMode;
         bool sorted;
+        bool compressMode;
 };
 

+ 4 - 6
include/simpleOutput.hh

@@ -15,12 +15,10 @@ class SimpleOutput
 
         std::string getIndent() const;
 
-        inline void writeObjectEntry(const JSonObjectEntry *);
-        inline void writePrimitive(const AJSonPrimitive *);
-        inline void writeContainer(const JSonContainer *);
-        inline void write(const JSonElement *);
-
-        void indent_inc(int i =1);
+        inline void writeObjectEntry(const JSonObjectEntry *, bool);
+        inline void writePrimitive(const AJSonPrimitive *, bool);
+        inline void writeContainer(const JSonContainer *, bool);
+        inline void write(const JSonElement *, bool prependComma);
 
     private:
         std::ostream &out;

+ 1 - 1
src/main.cpp

@@ -166,7 +166,7 @@ int main(int ac, char **av)
             runDiff(*params);
         else
         {
-            if (isatty(fileno(stdout)))
+            if (!params->compressed() && isatty(fileno(stdout)))
                 run(*params);
             else
                 runStdout(*params);

+ 6 - 0
src/params.cpp

@@ -35,6 +35,7 @@ bool Params::read()
     std::stringstream *input = nullptr;
     ignoreUnicode = false;
     colorMode = sorted = true;
+    compressMode = false;
 
     for (std::list<std::string>::const_iterator i = params.cbegin(); i != params.cend(); i++)
     {
@@ -51,6 +52,8 @@ bool Params::read()
                 colorMode = true;
             else if (tmp == "--diff")
                 diffMode = true;
+            else if (tmp == "--compress")
+                compressMode = true;
             else if (tmp == "--help" || tmp == "-h")
             {
                 usage();
@@ -184,6 +187,9 @@ bool Params::isIgnoringUnicode() const
 const std::string &Params::getProgName() const
 { return progName; }
 
+bool Params::compressed() const
+{ return compressMode; }
+
 bool Params::isDiff() const
 { return diffMode; }
 

+ 43 - 24
src/simpleOutput.cpp

@@ -10,61 +10,80 @@
 SimpleOutput::SimpleOutput(std::ostream &output, const Params &p): out(output), params(p), indent(0)
 { }
 
-void SimpleOutput::writeObjectEntry(const JSonObjectEntry *item)
+void SimpleOutput::writeObjectEntry(const JSonObjectEntry *item, bool prependComma)
 {
-    out << getIndent() << item->stringify() << ": ";
+    out << getIndent() << (prependComma ? ",\"" : "\"") << item->stringify() << "\":";
 
     if (dynamic_cast<const JSonContainer *> (**item))
     {
-        out << std::endl;
-        writeContainer((const JSonContainer*) **item);
+        if (!params.compressed())
+            out << std::endl;
+        writeContainer((const JSonContainer*) **item, false);
     }
     else
-        out << (**item)->stringify() << std::endl;
+    {
+        out << (**item)->stringify();
+        if (!params.compressed())
+            out << std::endl;
+    }
 }
 
-void SimpleOutput::writePrimitive(const AJSonPrimitive *item)
+void SimpleOutput::writePrimitive(const AJSonPrimitive *item, bool prependComma)
 {
-    if (indent)
-        out << getIndent() << item->stringify() << std::endl;
+    out << getIndent() << (prependComma ? "," : "");
+
+    if (dynamic_cast<const JSonPrimitive<std::string>*> (item))
+        out << "\"" << item->stringify() << "\"";
     else
-        out << item->stringify() << std::endl;
+        out << item->stringify();
+    if (!params.compressed())
+        out << std::endl;
 }
 
-void SimpleOutput::writeContainer(const JSonContainer *item)
+void SimpleOutput::writeContainer(const JSonContainer *item, bool prependComma)
 {
     std::string _indent = getIndent();
     const char *brackets = item->stringify().c_str();
+    bool first = true;
 
-    out << _indent << std::string(1, brackets[0]) << std::endl;
+    out << _indent << (prependComma ? "," : "") << std::string(1, brackets[0]);
+    if (!params.compressed())
+        out << std::endl;
 
-    indent_inc();
+    indent++;
     for (JSonElement *i : *item)
-        write(i);
-    indent_inc(-1);
+    {
+        write(i, !first);
+        if (first)
+            first = false;
+    }
+    indent--;
 
-    out << _indent << std::string(1, brackets[2]) << std::endl;
+    out << _indent << std::string(1, brackets[2]);
+    if (!params.compressed())
+        out << std::endl;
 }
 
-void SimpleOutput::write(const JSonElement *item)
+void SimpleOutput::write(const JSonElement *item, bool prependComma)
 {
     if (dynamic_cast<const JSonContainer *>(item))
-        writeContainer((const JSonContainer *) item);
+        writeContainer((const JSonContainer *) item, prependComma);
     else if (dynamic_cast<const JSonObjectEntry*> (item))
-        writeObjectEntry((const JSonObjectEntry *) item);
+        writeObjectEntry((const JSonObjectEntry *) item, prependComma);
     else
-        writePrimitive((const AJSonPrimitive *) item);
+        writePrimitive((const AJSonPrimitive *) item, prependComma);
 }
 
-void SimpleOutput::indent_inc(int i)
-{ indent += i; }
-
 std::string SimpleOutput::getIndent() const
-{ return std::string(indent * INDENT_LEVEL, ' '); }
+{
+    if (params.compressed())
+        return "";
+    return std::string(indent * INDENT_LEVEL, ' ');
+}
 
 void SimpleOutput::display(std::ostream &out, const JSonElement *root, const Params &p)
 {
     SimpleOutput writer(out, p);
-    writer.write(root);
+    writer.write(root, false);
 }