metrics: add colors to metrics output
authorAlexandre Terrasa <alexandre@moz-code.org>
Fri, 28 Feb 2014 18:33:26 +0000 (13:33 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Fri, 28 Feb 2014 18:35:16 +0000 (13:35 -0500)
Also --no-colors option to disable colors in outputs

Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/metrics/inheritance_metrics.nit
src/metrics/mclasses_metrics.nit
src/metrics/metrics_base.nit
src/metrics/mmodules_metrics.nit

index 94bbd5f..5e7f21f 100644 (file)
@@ -34,7 +34,7 @@ private class InheritanceMetricsPhase
        do
                if not toolcontext.opt_inheritance.value and not toolcontext.opt_all.value then return
 
-               print "\n# Inheritance metrics".yellow.bold
+               print toolcontext.format_h1("\n# Inheritance metrics")
 
                var hmetrics = new InheritanceMetricSet
                hmetrics.register(new MDUI, new MDUIC, new MDUII, new MIF, new MIFC, new MIFI)
@@ -49,13 +49,13 @@ private class InheritanceMetricsPhase
                var mclasses = new HashSet[MClass]
                for mproject in model.mprojects do
 
-                       print "\n ## project {mproject}".bold
+                       print toolcontext.format_h2("\n ## project {mproject}")
 
                        for mgroup in mproject.mgroups do
                                if mgroup.mmodules.is_empty then continue
 
                                # Scalar metrics
-                               print "  `- group {mgroup.full_name}"
+                               print toolcontext.format_h3("  `- group {mgroup.full_name}")
 
                                var mod_mclasses = new HashSet[MClass]
                                for mmodule in mgroup.mmodules do mod_mclasses.add_all(mmodule.intro_mclasses)
@@ -64,44 +64,44 @@ private class InheritanceMetricsPhase
                                mclasses.add_all(mod_mclasses)
                                cmetrics.collect(new HashSet[MClass].from(mod_mclasses), mainmodule)
                                for name, metric in cmetrics.metrics do
-                                       print "\t{name}: {metric.desc}".green
-                                       print "\t    avg: {metric.avg}".light_gray
+                                       print toolcontext.format_h4("\t{name}: {metric.desc}")
+                                       print toolcontext.format_p("\t    avg: {metric.avg}")
                                        var max = metric.max
-                                       print "\t    max: {max.first} ({max.second})".light_gray
+                                       print toolcontext.format_p("\t    max: {max.first} ({max.second})")
                                        var min = metric.min
-                                       print "\t    min: {min.first} ({min.second})".light_gray
+                                       print toolcontext.format_p("\t    min: {min.first} ({min.second})")
                                end
                                hmetrics.collect(new HashSet[MModule].from(mgroup.mmodules), mainmodule)
                                for name, metric in hmetrics.metrics do
-                                       print "\t{name}: {metric.desc}".green
-                                       print "\t    avg: {metric.avg}".light_gray
+                                       print toolcontext.format_h4("\t{name}: {metric.desc}")
+                                       print toolcontext.format_p("\t    avg: {metric.avg}")
                                        var max = metric.max
-                                       print "\t    max: {max.first} ({max.second})".light_gray
+                                       print toolcontext.format_p("\t    max: {max.first} ({max.second})")
                                        var min = metric.min
-                                       print "\t    min: {min.first} ({min.second})".light_gray
+                                       print toolcontext.format_p("\t    min: {min.first} ({min.second})")
                                end
                        end
                end
                if not mclasses.is_empty then
                        # Global metrics
-                       print "\n ## global metrics".bold
+                       print toolcontext.format_h2("\n ## global metrics")
                        cmetrics.collect(mclasses, mainmodule)
                        for name, metric in cmetrics.metrics do
-                               print "\t{name}: {metric.desc}".green
-                               print "\t    avg: {metric.avg}".light_gray
+                               print toolcontext.format_h4("\t{name}: {metric.desc}")
+                               print toolcontext.format_p("\t    avg: {metric.avg}")
                                var max = metric.max
-                               print "\t    max: {max.first} ({max.second})".light_gray
+                               print toolcontext.format_p("\t    max: {max.first} ({max.second})")
                                var min = metric.min
-                               print "\t    min: {min.first} ({min.second})".light_gray
+                               print toolcontext.format_p("\t    min: {min.first} ({min.second})")
                        end
                        hmetrics.collect(mmodules, mainmodule)
                        for name, metric in hmetrics.metrics do
-                               print "\t{name}: {metric.desc}".green
-                               print "\t    avg: {metric.avg}".light_gray
+                               print toolcontext.format_h4("\t{name}: {metric.desc}")
+                               print toolcontext.format_p("\t    avg: {metric.avg}")
                                var max = metric.max
-                               print "\t    max: {max.first} ({max.second})".light_gray
+                               print toolcontext.format_p("\t    max: {max.first} ({max.second})")
                                var min = metric.min
-                               print "\t    min: {min.first} ({min.second})".light_gray
+                               print toolcontext.format_p("\t    min: {min.first} ({min.second})")
                        end
                end
        end
index 3651aa7..c48cb35 100644 (file)
@@ -33,7 +33,7 @@ private class MClassesMetricsPhase
        do
                if not toolcontext.opt_mclasses.value and not toolcontext.opt_all.value then return
 
-               print "\n# MClasses metrics".yellow.bold
+               print toolcontext.format_h1("\n# MClasses metrics")
 
                var metrics = new MClassMetricSet
                metrics.register(new CNOA, new CNOP, new CNOC, new CNOD, new CDIT)
@@ -47,13 +47,13 @@ private class MClassesMetricsPhase
                var mclasses = new HashSet[MClass]
                for mproject in model.mprojects do
 
-                       print "\n ## project {mproject}".bold
+                       print toolcontext.format_h2("\n ## project {mproject}")
 
                        for mgroup in mproject.mgroups do
                                if mgroup.mmodules.is_empty then continue
 
                                # Scalar metrics
-                               print "  `- group {mgroup.full_name}"
+                               print toolcontext.format_h3("  `- group {mgroup.full_name}")
 
                                var mod_mclasses = new HashSet[MClass]
                                for mmodule in mgroup.mmodules do mod_mclasses.add_all(mmodule.intro_mclasses)
@@ -61,27 +61,27 @@ private class MClassesMetricsPhase
                                mclasses.add_all(mod_mclasses)
                                metrics.collect(new HashSet[MClass].from(mod_mclasses), mainmodule)
                                for name, metric in metrics.metrics do
-                                       print "\t{name}: {metric.desc}".green
-                                       print "\t    avg: {metric.avg}".light_gray
+                                       print toolcontext.format_h4("\t{name}: {metric.desc}")
+                                       print toolcontext.format_p("\t    avg: {metric.avg}")
                                        var max = metric.max
-                                       print "\t    max: {max.first} ({max.second})".light_gray
+                                       print toolcontext.format_p("\t    max: {max.first} ({max.second})")
                                        var min = metric.min
-                                       print "\t    min: {min.first} ({min.second})".light_gray
+                                       print toolcontext.format_p("\t    min: {min.first} ({min.second})")
                                end
                        end
                end
                if not mclasses.is_empty then
                        # Global metrics
-                       print "\n ## global metrics".bold
+                       print toolcontext.format_h2("\n ## global metrics")
 
                        metrics.collect(mclasses, mainmodule)
                        for name, metric in metrics.metrics do
-                               print "\t{name}: {metric.desc}".green
-                               print "\t    avg: {metric.avg}".light_gray
+                               print toolcontext.format_h4("\t{name}: {metric.desc}")
+                               print toolcontext.format_p("\t    avg: {metric.avg}")
                                var max = metric.max
-                               print "\t    max: {max.first} ({max.second})".light_gray
+                               print toolcontext.format_p("\t    max: {max.first} ({max.second})")
                                var min = metric.min
-                               print "\t    min: {min.first} ({min.second})".light_gray
+                               print toolcontext.format_p("\t    min: {min.first} ({min.second})")
                        end
                end
        end
index 8d46bd3..77878a9 100644 (file)
@@ -21,6 +21,7 @@ module metrics_base
 import model_utils
 import csv
 import counter
+import console
 
 redef class ToolContext
 
@@ -55,6 +56,10 @@ redef class ToolContext
        # --poset
        var opt_poset = new OptionBool("Complete metrics on posets", "--poset")
 
+       # --no-colors
+       var opt_nocolors = new OptionBool("Disable colors in console outputs", "--no-colors")
+
+
        var opt_dir = new OptionString("Directory where some statistics files are generated", "-d", "--dir")
        var output_dir: String = "."
 
@@ -76,6 +81,7 @@ redef class ToolContext
                self.option_context.add_option(opt_generate_hyperdoc)
                self.option_context.add_option(opt_poset)
                self.option_context.add_option(opt_dir)
+               self.option_context.add_option(opt_nocolors)
        end
 
        redef fun process_options
@@ -88,6 +94,33 @@ redef class ToolContext
                        self.output_dir = val
                end
        end
+
+       # colorize heading 1 for console output
+       fun format_h1(str: String): String do
+               if opt_nocolors.value then return str
+               return str.yellow.bold
+       end
+
+       fun format_h2(str: String): String do
+               if opt_nocolors.value then return str
+               return str.bold
+       end
+
+       fun format_h3(str: String): String do
+               if opt_nocolors.value then return str
+               return str
+       end
+
+       fun format_h4(str: String): String do
+               if opt_nocolors.value then return str
+               return str.green
+       end
+
+       fun format_p(str: String): String do
+               if opt_nocolors.value then return str
+               return str.light_gray
+       end
+
 end
 
 redef class MClass
index f729c75..fc3a781 100644 (file)
@@ -33,7 +33,7 @@ private class MModulesMetricsPhase
        do
                if not toolcontext.opt_mmodules.value and not toolcontext.opt_all.value then return
 
-               print "\n# MModules metrics".yellow.bold
+               print toolcontext.format_h1("\n# MModules metrics")
 
                var metrics = new MModuleMetricSet
                metrics.register(new MNOA, new MNOP, new MNOC, new MNOD, new MDIT)
@@ -43,38 +43,38 @@ private class MModulesMetricsPhase
                var mmodules = new HashSet[MModule]
                for mproject in model.mprojects do
 
-                       print "\n ## project {mproject}".bold
+                       print  toolcontext.format_h2("\n ## project {mproject}")
 
                        for mgroup in mproject.mgroups do
                                if mgroup.mmodules.is_empty then continue
 
                                # Scalar metrics
-                               print "  `- group {mgroup.full_name}"
+                               print  toolcontext.format_h3("  `- group {mgroup.full_name}")
 
                                mmodules.add_all(mgroup.mmodules)
                                metrics.collect(new HashSet[MModule].from(mgroup.mmodules), mainmodule)
                                for name, metric in metrics.metrics do
-                                       print "\t{name}: {metric.desc}".green
-                                       print "\t    avg: {metric.avg}".light_gray
+                                       print toolcontext.format_h4("\t{name}: {metric.desc}")
+                                       print toolcontext.format_p("\t    avg: {metric.avg}")
                                        var max = metric.max
-                                       print "\t    max: {max.first} ({max.second})".light_gray
+                                       print  toolcontext.format_p("\t    max: {max.first} ({max.second})")
                                        var min = metric.min
-                                       print "\t    min: {min.first} ({min.second})".light_gray
+                                       print  toolcontext.format_p("\t    min: {min.first} ({min.second})")
                                end
                        end
                end
                if not mmodules.is_empty then
                        # Global metrics
-                       print "\n ## global metrics".bold
+                       print  toolcontext.format_h2("\n ## global metrics")
 
                        metrics.collect(mmodules, mainmodule)
                        for name, metric in metrics.metrics do
-                               print "\t{name}: {metric.desc}".green
-                               print "\t    avg: {metric.avg}".light_gray
+                               print toolcontext.format_h4( "\t{name}: {metric.desc}")
+                               print  toolcontext.format_p("\t    avg: {metric.avg}")
                                var max = metric.max
-                               print "\t    max: {max.first} ({max.second})".light_gray
+                               print  toolcontext.format_p("\t    max: {max.first} ({max.second})")
                                var min = metric.min
-                               print "\t    min: {min.first} ({min.second})".light_gray
+                               print  toolcontext.format_p("\t    min: {min.first} ({min.second})")
                        end
                end
        end