MCPcopy Create free account
hub / github.com/efogdev/mpris-timer / MakeProgressCircle

Function MakeProgressCircle

internal/core/image.go:45–112  ·  view source on GitHub ↗
(progress float64)

Source from the content-addressed store, hash-verified

43}
44
45func MakeProgressCircle(progress float64) (string, error) {
46 progress = math.Max(0, math.Min(100, progress))
47 dirname := path.Join(CacheDir, strings.ToUpper(strings.Replace(Overrides.Color, "#", "", 1)))
48 footprint := fmt.Sprintf("sh%v.r%v.%.2f", bool2int(Overrides.HasShadow), bool2int(Overrides.Rounded), progress)
49 filename := path.Join(dirname, footprint+".svg")
50
51 cacheMu.RLock()
52 if cacheLoaded {
53 _, exists := cache[filename]
54 if exists {
55 cacheMu.RUnlock()
56 return filename, nil
57 }
58 }
59 cacheMu.RUnlock()
60
61 if _, err := os.Stat(filename); err == nil {
62 cacheMu.Lock()
63 cache[filename] = struct{}{}
64 cacheMu.Unlock()
65 return filename, nil
66 }
67
68 centerX := width / 2
69 centerY := height / 2
70 radius := float64(width)/2 - float64(strokeWidth) - float64(padding)
71 baseWidth := int(math.Round(strokeWidth * 0.25))
72 circumference := 2 * math.Pi * radius
73 dashOffset := circumference * (1 - progress/100)
74
75 params := svgParams{
76 Width: width,
77 Height: height,
78 CenterX: centerX,
79 CenterY: centerY,
80 Radius: radius,
81 BaseWidth: baseWidth,
82 StrokeWidth: strokeWidth,
83 FgStrokeColor: Overrides.Color,
84 BgStrokeColor: bgStrokeColor,
85 Circumference: circumference,
86 DashOffset: dashOffset,
87 HasShadow: Overrides.HasShadow,
88 Rounded: Overrides.Rounded,
89 CustomOrigin: roundedOrigin,
90 Progress: int(progress),
91 }
92
93 var buf bytes.Buffer
94 err := svgTpl.Execute(&buf, params)
95 if err != nil {
96 return "", err
97 }
98
99 _ = os.MkdirAll(dirname, 0755)
100 err = os.WriteFile(filename, buf.Bytes(), 0644)
101 if err != nil {
102 return "", fmt.Errorf("write SVG: %w", err)

Callers 2

renderPreviewFunction · 0.92
runTickerMethod · 0.85

Calls 1

bool2intFunction · 0.85

Tested by

no test coverage detected