You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/10/17 21:50:13 UTC

[GitHub] andrzej-kaczmarek closed pull request #225: Add diff-friendly output for "newt size"

andrzej-kaczmarek closed pull request #225: Add diff-friendly output for "newt size"
URL: https://github.com/apache/mynewt-newt/pull/225
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/newt/builder/size.go b/newt/builder/size.go
index c0a19bf0..4753433c 100644
--- a/newt/builder/size.go
+++ b/newt/builder/size.go
@@ -511,7 +511,7 @@ func (b *Builder) Size() error {
 	return nil
 }
 
-func (t *TargetBuilder) SizeReport(sectionName string) error {
+func (t *TargetBuilder) SizeReport(sectionName string, diffFriendly bool) error {
 
 	err := t.PrepBuild()
 
@@ -520,22 +520,22 @@ func (t *TargetBuilder) SizeReport(sectionName string) error {
 	}
 
 	fmt.Printf("Size of Application Image: %s\n", t.AppBuilder.buildName)
-	err = t.AppBuilder.SizeReport(sectionName)
+	err = t.AppBuilder.SizeReport(sectionName, diffFriendly)
 
 	if err == nil {
 		if t.LoaderBuilder != nil {
 			fmt.Printf("Size of Loader Image: %s\n", t.LoaderBuilder.buildName)
-			err = t.LoaderBuilder.SizeReport(sectionName)
+			err = t.LoaderBuilder.SizeReport(sectionName, diffFriendly)
 		}
 	}
 
 	return err
 }
 
-func (b *Builder) SizeReport(sectionName string) error {
+func (b *Builder) SizeReport(sectionName string, diffFriendly bool) error {
 	srcBase := interfaces.GetProject().Path() + "/"
 
-	err := SizeReport(b.AppElfPath(), srcBase, sectionName)
+	err := SizeReport(b.AppElfPath(), srcBase, sectionName, diffFriendly)
 	if err != nil {
 		return util.NewNewtError(err.Error())
 	}
diff --git a/newt/builder/size_report.go b/newt/builder/size_report.go
index bfb4ad7c..0673fefd 100644
--- a/newt/builder/size_report.go
+++ b/newt/builder/size_report.go
@@ -290,7 +290,7 @@ func logMemoryRegionStats(memRegion *MemoryRegion, sectionName string) {
 	util.StatusMessage(util.VERBOSITY_VERBOSE, "\n")
 }
 
-func SizeReport(elfFilePath, srcBase string, sectionName string) error {
+func SizeReport(elfFilePath, srcBase string, sectionName string, diffFriendly bool) error {
 	symbolsPath, err := loadSymbolsAndPaths(elfFilePath, srcBase)
 	if err != nil {
 		return err
@@ -315,6 +315,6 @@ func SizeReport(elfFilePath, srcBase string, sectionName string) error {
 		}
 	}
 	fmt.Printf("%s report:\n", sectionName)
-	fmt.Printf("%v", sectionNodes.ToString(sectionRegion.TotalSize))
+	fmt.Printf("%v", sectionNodes.ToString(sectionRegion.TotalSize, diffFriendly))
 	return nil
 }
diff --git a/newt/builder/symbol_tree.go b/newt/builder/symbol_tree.go
index 2318da10..f2732b3b 100644
--- a/newt/builder/symbol_tree.go
+++ b/newt/builder/symbol_tree.go
@@ -23,6 +23,7 @@ import (
 	"fmt"
 	"sort"
 	"strings"
+	"strconv"
 )
 
 type Symbol struct {
@@ -42,6 +43,77 @@ type Folder struct {
 	Folders map[string]*Folder
 }
 
+type outputFormatter interface {
+	Header(nameStr string, sizeStr string, percentStr string) string
+	Container(level int, name string, size uint64, percent float64) string
+	Symbol(level int, name string, size uint64, percent float64) string
+	Separator() string
+}
+
+type outputFormatterDefault struct {
+	indentStr string
+	headerStr string
+	symbolStr string
+}
+
+type outputFormatterDiffable struct {
+	indentStr    string
+	headerStr    string
+	containerStr string
+	symbolStr    string
+}
+
+func newSymbolFormatterDefault() *outputFormatterDefault {
+	return &outputFormatterDefault{
+		indentStr: "  ",
+		headerStr: "%-59s %9s %9s\n",
+		symbolStr: "%-59s %9d %8.2f%%\n",
+	}
+}
+
+func (fmtr *outputFormatterDefault) Header(nameStr string, sizeStr string, percentStr string) string {
+	return fmt.Sprintf(fmtr.headerStr, nameStr, sizeStr, percentStr)
+}
+
+func (fmtr *outputFormatterDefault) Container(level int, name string, size uint64, percent float64) string {
+	return fmtr.Symbol(level, name, size, percent)
+}
+
+func (fmtr *outputFormatterDefault) Symbol(level int, name string, size uint64, percent float64) string {
+	return fmt.Sprintf(fmtr.symbolStr, strings.Repeat(fmtr.indentStr, level) + name, size, percent)
+}
+
+func (fmtr *outputFormatterDefault) Separator() string {
+	// -1 is to cut \n
+	return strings.Repeat("=", len(fmtr.Header("", "", "")) - 1) + "\n"
+}
+
+func newSymbolFormatterDiffable() *outputFormatterDiffable {
+	return &outputFormatterDiffable{
+		indentStr:    "  ",
+		headerStr:    "%-70s %9s\n",
+		containerStr: "%-70s\n",
+		symbolStr:    "%-70s %9d\n",
+	}
+}
+
+func (fmtr *outputFormatterDiffable) Header(nameStr string, sizeStr string, percentStr string) string {
+	return fmt.Sprintf(fmtr.headerStr, nameStr, sizeStr)
+}
+
+func (fmtr *outputFormatterDiffable) Container(level int, name string, size uint64, percent float64) string {
+	return fmt.Sprintf(fmtr.containerStr, strings.Repeat(fmtr.indentStr, level) + name)
+}
+
+func (fmtr *outputFormatterDiffable) Symbol(level int, name string, size uint64, percent float64) string {
+	return fmt.Sprintf(fmtr.symbolStr, strings.Repeat(fmtr.indentStr, level) + name, size)
+}
+
+func (fmtr *outputFormatterDiffable) Separator() string {
+	// -1 is to cut \n
+	return strings.Repeat("=", len(fmtr.Header("", "", "")) - 1) + "\n"
+}
+
 func newFolder(name string) *Folder {
 	return &Folder{name, make(map[string]*File), make(map[string]*Folder)}
 }
@@ -127,17 +199,14 @@ func (f *Folder) addSymbol(symbol *Symbol, path string) *Symbol {
 	return sym
 }
 
-var outputFormatting string = "%-59s %9d %8.2f%%\n"
-
-func (f *File) String(indent string, level int, total uint64) string {
+func (f *File) toString(fmtr outputFormatter, level int, total uint64) string {
 	var str string
 	if f.sumSize() <= 0 {
 		return ""
 	}
 	size := f.sumSize()
 	percent := 100 * float64(size) / float64(total)
-	str += fmt.Sprintf(outputFormatting, strings.Repeat(indent, level)+
-		f.Name, size, percent)
+	str += fmtr.Container(level, f.Name, size, percent)
 
 	var sorted []string
 	for symName := range f.Symbols {
@@ -148,15 +217,13 @@ func (f *File) String(indent string, level int, total uint64) string {
 		size := f.Symbols[sym].Size
 		percent := 100 * float64(size) / float64(total)
 		if f.Symbols[sym].Size > 0 {
-			str += fmt.Sprintf(outputFormatting,
-				strings.Repeat(indent, level+1)+ f.Symbols[sym].Name,
-				size, percent)
+			str += fmtr.Symbol(level + 1, f.Symbols[sym].Name, size, percent)
 		}
 	}
 	return str
 }
 
-func (f *Folder) StringRec(indent string, level int, total uint64) string {
+func (f *Folder) toString(fmtr outputFormatter, level int, total uint64) string {
 	var str string
 
 	var sorted []string
@@ -172,24 +239,30 @@ func (f *Folder) StringRec(indent string, level int, total uint64) string {
 		if folder, ok := f.Folders[name]; ok {
 			size := folder.sumSize()
 			percent := 100 * float64(size) / float64(total)
-			str += fmt.Sprintf(outputFormatting,
-				strings.Repeat(indent, level)+folder.Name, size, percent)
-			str += folder.StringRec(indent, level+1, total)
+			str += fmtr.Container(level, folder.Name, size, percent)
+			str += folder.toString(fmtr, level+1, total)
 		} else {
-			str += f.Files[name].String(indent, level, total)
+			str += f.Files[name].toString(fmtr, level, total)
 		}
 	}
 	return str
 }
 
-func (f *Folder) ToString(total uint64) string {
-	indent := "  "
+func (f *Folder) ToString(total uint64, diffFriendly bool) string {
 	var str string
-	str += fmt.Sprintf("%-59s %9s %9s\n", "Path", "Size", "%")
-	str += strings.Repeat("=", 79) + "\n"
-	str += f.StringRec(indent, 0, total)
-	str += strings.Repeat("=", 79) + "\n"
-	str += fmt.Sprintf("%-59s %9d %9s\n",
-		"Total symbol size (i.e. excluding padding, etc.)", f.sumSize(), "")
+	var fmtr outputFormatter
+
+	if diffFriendly {
+		fmtr = newSymbolFormatterDiffable()
+	} else {
+		fmtr = newSymbolFormatterDefault()
+	}
+
+	str += fmtr.Header("Path", "Size", "%")
+	str += fmtr.Separator()
+	str += f.toString(fmtr, 0, total)
+	str += fmtr.Separator()
+	str += fmtr.Header("Total symbol size (i.e. excluding padding, etc.)",
+		strconv.FormatUint(f.sumSize(), 10), "")
 	return str
 }
diff --git a/newt/cli/build_cmds.go b/newt/cli/build_cmds.go
index 5865b0ab..d986510c 100644
--- a/newt/cli/build_cmds.go
+++ b/newt/cli/build_cmds.go
@@ -101,6 +101,7 @@ func pkgToUnitTests(pack *pkg.LocalPackage) []*pkg.LocalPackage {
 
 var extraJtagCmd string
 var noGDB_flag bool
+var diffFriendly_flag bool
 
 func buildRunCmd(cmd *cobra.Command, args []string, printShellCmds bool, executeShell bool) {
 	if len(args) < 1 {
@@ -398,7 +399,7 @@ func sizeRunCmd(cmd *cobra.Command, args []string, ram bool, flash bool, section
 
 	if len(sections) > 0 {
 		for _, sectionName := range sections {
-			if err := b.SizeReport(sectionName); err != nil {
+			if err := b.SizeReport(sectionName, diffFriendly_flag); err != nil {
 				NewtUsage(cmd, err)
 			}
 		}
@@ -507,6 +508,8 @@ func AddBuildCommands(cmd *cobra.Command) {
 		},
 	}
 
+	sizeCmd.PersistentFlags().BoolVarP(&diffFriendly_flag, "diffable", "d", false,
+		"Produce diff-friendly output of statistics")
 	sizeCmd.Flags().BoolVarP(&ram, "ram", "R", false, "Print RAM statistics")
 	sizeCmd.Flags().BoolVarP(&flash, "flash", "F", false,
 		"Print FLASH statistics")


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services