From da4cf67ec92f3f4d1759a8849bb136354928f014 Mon Sep 17 00:00:00 2001 From: Steve Kamerman Date: Wed, 21 Aug 2024 05:58:52 -0400 Subject: [PATCH] feat: Add username and date range text to model --- main.go | 6 ++++-- pkg/skyline/cad.go | 36 +++++++++++++++++++++++++++++++++++- pkg/skyline/github.go | 15 ++++++++++++++- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 4254fc8..9c7d606 100644 --- a/main.go +++ b/main.go @@ -31,6 +31,7 @@ var ( buildingWidth float64 buildingLength float64 interval string + font string openscadPath string showVersion bool showVersionRaw bool @@ -47,7 +48,7 @@ func init() { flag.StringVarP(&outputFile, "output", "o", "skyline.scad", "Output file (.scad and .stl are supported, but stl requires 'openscad')") flag.IntVarP(&startYear, "start", "b", 0, "Start year") flag.IntVarP(&endYear, "end", "e", 0, "End year") - flag.StringVarP(&aspectRatio, "aspect-ratio", "a", "16:9", "Aspect ratio of the skyline") + flag.StringVarP(&aspectRatio, "aspect-ratio", "a", "16:4", "Aspect ratio of the skyline") flag.Float64VarP(&baseAngle, "base-angle", "A", 22.5, "Slope of the base walls in degrees") flag.Float64VarP(&baseHeight, "base-height", "h", 5.0, "Height of the base (mm)") flag.Float64VarP(&baseMargin, "base-margin", "g", 1.0, "Distance from the buildings to the base walls (mm)") @@ -55,6 +56,7 @@ func init() { flag.Float64VarP(&buildingWidth, "building-width", "w", 2.0, "Building width (mm)") flag.Float64VarP(&buildingLength, "building-length", "l", 2.0, "Building length (mm)") flag.StringVarP(&interval, "interval", "i", "week", "Interval to use for contributions (day, week)") + flag.StringVarP(&font, "font", "F", "Liberation Sans:style=Bold", "Font to use for text") flag.StringVarP(&openscadPath, "openscad", "O", "openscad", "Path to the OpenSCAD executable") flag.BoolVarP(&showVersion, "version", "V", false, "Show version") flag.BoolVar(&showVersionRaw, "version-raw", false, "Show version (raw)") @@ -127,7 +129,7 @@ func main() { fmt.Printf("Total contributions: %d between %v and %v\n", contribs.TotalContributions, contribs.FirstDate, contribs.LastDate) fmt.Printf("Generating OpenSCAD ...\n") - sg := skyline.NewSkylineGenerator(*contribs, aspectRatioInts, maxBuildingHeight, buildingWidth, buildingLength) + sg := skyline.NewSkylineGenerator(*contribs, aspectRatioInts, maxBuildingHeight, buildingWidth, buildingLength, font) sl := sg.Generate(interval) sl.BaseAngle = baseAngle sl.BaseHeight = baseHeight diff --git a/pkg/skyline/cad.go b/pkg/skyline/cad.go index d080377..aa854a3 100644 --- a/pkg/skyline/cad.go +++ b/pkg/skyline/cad.go @@ -29,6 +29,7 @@ type SkylineGenerator struct { maxHeight float64 buildingWidth float64 buildingLength float64 + font string } type Building struct { @@ -60,6 +61,9 @@ type Skyline struct { BaseMargin float64 BaseHeight float64 BaseAngle float64 + Font string + TextLeft string + TextRight string } // NewSkylineGenerator creates a new SkylineGenerator @@ -68,13 +72,14 @@ type Skyline struct { // maxHeight: the maximum height of the buildings in mm // buildingWidth: the width of the buildings in mm // buildingLength: the length of the buildings in mm -func NewSkylineGenerator(Contributions Contributions, aspectRatio [2]int, maxHeight, buildingWidth, buildingLength float64) *SkylineGenerator { +func NewSkylineGenerator(Contributions Contributions, aspectRatio [2]int, maxHeight, buildingWidth, buildingLength float64, font string) *SkylineGenerator { sg := &SkylineGenerator{ contributions: Contributions, aspectRatio: float64(aspectRatio[0]) / float64(aspectRatio[1]), maxHeight: maxHeight, buildingWidth: buildingWidth, buildingLength: buildingLength, + font: font, } return sg @@ -110,6 +115,9 @@ func (sg *SkylineGenerator) Generate(interval string) *Skyline { BaseMargin: defaultBaseMargin, BaseHeight: defaultBaseHeight, BaseAngle: defaultBaseAngle, + Font: sg.font, + TextLeft: "@" + sg.contributions.Username, + TextRight: sg.contributions.YearRangeText(), } return skyline @@ -212,6 +220,24 @@ var ( color(baseColor) polyhedron(points, faces); + + if (textEnable) { + textOffset = baseOffset+baseMargin; + textSize = baseHeight-baseMargin; + + rotate([90-baseAngle, 0 ,0]) + translate([textOffset, 1, 0]) + color("red") + linear_extrude(textHeight) + text(textLeft, size=textSize, halign="left", valign="baseline", font=textFont) + ; + + rotate([90-baseAngle, 0 ,0]) + translate([bottomWidth-textOffset, 1, 0]) + color("red") + linear_extrude(textHeight) + text(textRight, size=textSize, halign="right", valign="baseline", font=textFont); + } }` buildingModule = `module building(x, y, contributions) { @@ -243,6 +269,14 @@ func (sl *Skyline) ToOpenSCAD(filename string) (time.Duration, error) { fmt.Fprintf(out, "baseOffset = baseHeight * tan(baseAngle);\n") fmt.Fprintf(out, `baseColor = "cyan";`+"\n") + fmt.Fprintf(out, "\n// Base Text\n") + fmt.Fprintf(out, "textEnable = true;\n") + fmt.Fprintf(out, "textFont = %q;\n", sl.Font) + fmt.Fprintf(out, "textLeft = %q;\n", sl.TextLeft) + fmt.Fprintf(out, "textRight = %q;\n", sl.TextRight) + fmt.Fprintf(out, `textColor = "red";`+"\n") + fmt.Fprintf(out, "textHeight = 0.4;\n") + fmt.Fprintf(out, "\n// Building Parameters\n") fmt.Fprintf(out, "buildingWidth = %f;\n", sl.BuildingWidth) fmt.Fprintf(out, "buildingLength = %f;\n", sl.BuildingLength) diff --git a/pkg/skyline/github.go b/pkg/skyline/github.go index 8bfeb12..bf505bc 100644 --- a/pkg/skyline/github.go +++ b/pkg/skyline/github.go @@ -35,12 +35,24 @@ func (sc StatsCollection) Max() int { } type Contributions struct { + Username string `json:"username"` TotalContributions int `json:"total_contributions"` FirstDate string `json:"first_date"` LastDate string `json:"last_date"` ByDate map[string]int `json:"by_date"` } +func (c *Contributions) YearRangeText() string { + startYear := c.FirstDate[:4] + endYear := c.LastDate[:4] + + if startYear == endYear { + return startYear + } + + return fmt.Sprintf("%s-%s", startYear, endYear) +} + func (c *Contributions) PerDay() StatsCollection { dayKeys := make([]string, 0, len(c.ByDate)) for key := range c.ByDate { @@ -170,7 +182,8 @@ type DateTime struct{ time.Time } func (gcf *GitHubContributionsFetcher) FetchContributions(startYear, endYear int) (*Contributions, error) { contrib := &Contributions{ - ByDate: make(map[string]int), + Username: gcf.username, + ByDate: make(map[string]int), } var firstDate time.Time