Skip to content

Commit

Permalink
Merge pull request #300 from fschaefer/master
Browse files Browse the repository at this point in the history
Unify output of fileSummarizeShort() and fileSummarizeLong() and simple language support for number formats
  • Loading branch information
boyter committed Nov 2, 2021
2 parents 8a8c674 + e3c69f4 commit ec8912f
Showing 1 changed file with 68 additions and 76 deletions.
144 changes: 68 additions & 76 deletions processor/formatters.go
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/csv"
"encoding/json"
"fmt"
"github.com/mattn/go-runewidth"
"io/ioutil"
"math"
"os"
Expand All @@ -16,7 +15,9 @@ import (
"strings"
"time"

glang "golang.org/x/text/language"
"github.com/mattn/go-runewidth"

"golang.org/x/text/language"
gmessage "golang.org/x/text/message"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -690,7 +691,7 @@ func fileSummarizeLong(input chan *FileJob) string {
}

languages := map[string]LanguageSummary{}
var sumFiles, sumLines, sumCode, sumComment, sumBlank, sumComplexity int64 = 0, 0, 0, 0, 0, 0
var sumFiles, sumLines, sumCode, sumComment, sumBlank, sumComplexity, sumBytes int64 = 0, 0, 0, 0, 0, 0, 0
var sumWeightedComplexity float64

for res := range input {
Expand All @@ -700,6 +701,7 @@ func fileSummarizeLong(input chan *FileJob) string {
sumComment += res.Comment
sumBlank += res.Blank
sumComplexity += res.Complexity
sumBytes += res.Bytes

var weightedComplexity float64
if res.Code != 0 {
Expand Down Expand Up @@ -816,23 +818,13 @@ func fileSummarizeLong(input chan *FileJob) string {
str.WriteString(getTabularWideBreak())

if !Cocomo {
estimatedEffort := EstimateEffort(int64(sumCode), EAF)
estimatedCost := EstimateCost(estimatedEffort, AverageWage, Overhead)
estimatedScheduleMonths := EstimateScheduleMonths(estimatedEffort)
estimatedPeopleRequired := estimatedEffort / estimatedScheduleMonths

p := gmessage.NewPrinter(glang.English)

str.WriteString(p.Sprintf("Estimated Cost to Develop $%d\n", int64(estimatedCost)))
str.WriteString(fmt.Sprintf("Estimated Schedule Effort %f months\n", estimatedScheduleMonths))
if math.IsNaN(estimatedPeopleRequired) {
str.WriteString(fmt.Sprintf("Estimated People Required 1 Grandparent\n"))
} else {
str.WriteString(fmt.Sprintf("Estimated People Required %f\n", estimatedPeopleRequired))
}
calculateCocomo(sumCode, &str)
str.WriteString(getTabularWideBreak())
}
if !Size {
calculateSize(sumBytes, &str)
str.WriteString(getTabularWideBreak())
}

return str.String()
}

Expand Down Expand Up @@ -977,8 +969,14 @@ func fileSummarizeShort(input chan *FileJob) string {
}
str.WriteString(getTabularShortBreak())

calculateCocomo(sumCode, &str)
calculateSize(sumBytes, &str)
if !Cocomo {
calculateCocomo(sumCode, &str)
str.WriteString(getTabularShortBreak())
}
if !Size {
calculateSize(sumBytes, &str)
str.WriteString(getTabularShortBreak())
}
return str.String()
}

Expand All @@ -990,71 +988,65 @@ func trimNameShort(summary LanguageSummary, trimmedName string) string {
}

func calculateCocomo(sumCode int64, str *strings.Builder) {
if !Cocomo {
estimatedEffort := EstimateEffort(int64(sumCode), EAF)
estimatedCost := EstimateCost(estimatedEffort, AverageWage, Overhead)
estimatedScheduleMonths := EstimateScheduleMonths(estimatedEffort)
estimatedPeopleRequired := estimatedEffort / estimatedScheduleMonths
estimatedEffort := EstimateEffort(int64(sumCode), EAF)
estimatedCost := EstimateCost(estimatedEffort, AverageWage, Overhead)
estimatedScheduleMonths := EstimateScheduleMonths(estimatedEffort)
estimatedPeopleRequired := estimatedEffort / estimatedScheduleMonths

p := gmessage.NewPrinter(glang.English)
p := gmessage.NewPrinter(language.Make(os.Getenv("LANG")))

str.WriteString(p.Sprintf("Estimated Cost to Develop (%s) %s%d\n", CocomoProjectType, CurrencySymbol, int64(estimatedCost)))
str.WriteString(fmt.Sprintf("Estimated Schedule Effort (%s) %f months\n", CocomoProjectType, estimatedScheduleMonths))
if math.IsNaN(estimatedPeopleRequired) {
str.WriteString(fmt.Sprintf("Estimated People Required 1 Grandparent\n"))
} else {
str.WriteString(fmt.Sprintf("Estimated People Required (%s) %f\n", CocomoProjectType, estimatedPeopleRequired))
}
str.WriteString(getTabularShortBreak())
str.WriteString(p.Sprintf("Estimated Cost to Develop (%s) %s%d\n", CocomoProjectType, CurrencySymbol, int64(estimatedCost)))
str.WriteString(p.Sprintf("Estimated Schedule Effort (%s) %f months\n", CocomoProjectType, estimatedScheduleMonths))
if math.IsNaN(estimatedPeopleRequired) {
str.WriteString(p.Sprintf("Estimated People Required 1 Grandparent\n"))
} else {
str.WriteString(p.Sprintf("Estimated People Required (%s) %f\n", CocomoProjectType, estimatedPeopleRequired))
}
}

func calculateSize(sumBytes int64, str *strings.Builder) {
if !Size {
var size float64

switch strings.ToLower(SizeUnit) {
case "binary":
size = float64(sumBytes) / 1_048_576
case "mixed":
size = float64(sumBytes) / 1_024_000
case "xkcd-kb":
str.WriteString("1000 bytes during leap years, 1024 otherwise\n")
tim := time.Now()
if isLeapYear(tim.Year()) {
size = float64(sumBytes) / 1_000_000
}
case "xkcd-kelly":
str.WriteString("compromise between 1000 and 1024 bytes\n")
size = float64(sumBytes) / (1012 * 1012)
case "xkcd-imaginary":
str.WriteString("used in quantum computing\n")
str.WriteString(fmt.Sprintf("Processed %d bytes, %s megabytes (%s)\n", sumBytes, `¯\_(ツ)_/¯`, strings.ToUpper(SizeUnit)))
case "xkcd-intel":
str.WriteString("calculated on pentium F.P.U.\n")
size = float64(sumBytes) / (1023.937528 * 1023.937528)
case "xkcd-drive":
str.WriteString("shrinks by 4 bytes every year for marketing reasons\n")
tim := time.Now()

s := 908 - ((tim.Year() - 2013) * 4) // comic starts with 908 in 2013 hence hardcoded values
s = min(s, 908) // just in case the clock is stupidly set

size = float64(sumBytes) / float64(s*s)
case "xkcd-bakers":
str.WriteString("9 bits to the byte since you're such a good customer\n")
size = float64(sumBytes) / (1152 * 1152)
default:
// SI value of 1000 bytes
size = float64(sumBytes) / 1_000_000
SizeUnit = "SI"
}

if strings.ToLower(SizeUnit) != "xkcd-imaginary" {
str.WriteString(fmt.Sprintf("Processed %d bytes, %.3f megabytes (%s)\n", sumBytes, size, strings.ToUpper(SizeUnit)))
var size float64

switch strings.ToLower(SizeUnit) {
case "binary":
size = float64(sumBytes) / 1_048_576
case "mixed":
size = float64(sumBytes) / 1_024_000
case "xkcd-kb":
str.WriteString("1000 bytes during leap years, 1024 otherwise\n")
tim := time.Now()
if isLeapYear(tim.Year()) {
size = float64(sumBytes) / 1_000_000
}
case "xkcd-kelly":
str.WriteString("compromise between 1000 and 1024 bytes\n")
size = float64(sumBytes) / (1012 * 1012)
case "xkcd-imaginary":
str.WriteString("used in quantum computing\n")
str.WriteString(fmt.Sprintf("Processed %d bytes, %s megabytes (%s)\n", sumBytes, `¯\_(ツ)_/¯`, strings.ToUpper(SizeUnit)))
case "xkcd-intel":
str.WriteString("calculated on pentium F.P.U.\n")
size = float64(sumBytes) / (1023.937528 * 1023.937528)
case "xkcd-drive":
str.WriteString("shrinks by 4 bytes every year for marketing reasons\n")
tim := time.Now()

s := 908 - ((tim.Year() - 2013) * 4) // comic starts with 908 in 2013 hence hardcoded values
s = min(s, 908) // just in case the clock is stupidly set

size = float64(sumBytes) / float64(s*s)
case "xkcd-bakers":
str.WriteString("9 bits to the byte since you're such a good customer\n")
size = float64(sumBytes) / (1152 * 1152)
default:
// SI value of 1000 bytes
size = float64(sumBytes) / 1_000_000
SizeUnit = "SI"
}

str.WriteString(getTabularShortBreak())
if strings.ToLower(SizeUnit) != "xkcd-imaginary" {
str.WriteString(fmt.Sprintf("Processed %d bytes, %.3f megabytes (%s)\n", sumBytes, size, strings.ToUpper(SizeUnit)))
}
}

Expand Down

0 comments on commit ec8912f

Please sign in to comment.