@@ -5,6 +5,7 @@ package badge
55
66import (
77 "strings"
8+ "sync"
89 "unicode"
910
1011 actions_model "code.gitea.io/gitea/models/actions"
@@ -49,23 +50,40 @@ func (b Badge) Width() int {
4950 return b .Label .width + b .Message .width
5051}
5152
53+ // Style follows https://shields.io/badges
54+ const (
55+ StyleFlat = "flat"
56+ StyleFlatSquare = "flat-square"
57+ )
58+
5259const (
5360 defaultOffset = 10
5461 defaultFontSize = 11
5562 DefaultColor = "#9f9f9f" // Grey
5663 DefaultFontFamily = "DejaVu Sans,Verdana,Geneva,sans-serif"
64+ DefaultStyle = StyleFlat
5765)
5866
59- var StatusColorMap = map [actions_model.Status ]string {
60- actions_model .StatusSuccess : "#4c1" , // Green
61- actions_model .StatusSkipped : "#dfb317" , // Yellow
62- actions_model .StatusUnknown : "#97ca00" , // Light Green
63- actions_model .StatusFailure : "#e05d44" , // Red
64- actions_model .StatusCancelled : "#fe7d37" , // Orange
65- actions_model .StatusWaiting : "#dfb317" , // Yellow
66- actions_model .StatusRunning : "#dfb317" , // Yellow
67- actions_model .StatusBlocked : "#dfb317" , // Yellow
68- }
67+ var GlobalVars = sync .OnceValue (func () (ret struct {
68+ StatusColorMap map [actions_model.Status ]string
69+ DejaVuGlyphWidthData map [rune ]uint8
70+ AllStyles []string
71+ },
72+ ) {
73+ ret .StatusColorMap = map [actions_model.Status ]string {
74+ actions_model .StatusSuccess : "#4c1" , // Green
75+ actions_model .StatusSkipped : "#dfb317" , // Yellow
76+ actions_model .StatusUnknown : "#97ca00" , // Light Green
77+ actions_model .StatusFailure : "#e05d44" , // Red
78+ actions_model .StatusCancelled : "#fe7d37" , // Orange
79+ actions_model .StatusWaiting : "#dfb317" , // Yellow
80+ actions_model .StatusRunning : "#dfb317" , // Yellow
81+ actions_model .StatusBlocked : "#dfb317" , // Yellow
82+ }
83+ ret .DejaVuGlyphWidthData = dejaVuGlyphWidthDataFunc ()
84+ ret .AllStyles = []string {StyleFlat , StyleFlatSquare }
85+ return ret
86+ })
6987
7088// GenerateBadge generates badge with given template
7189func GenerateBadge (label , message , color string ) Badge {
@@ -93,7 +111,7 @@ func GenerateBadge(label, message, color string) Badge {
93111
94112func calculateTextWidth (text string ) int {
95113 width := 0
96- widthData := DejaVuGlyphWidthData ()
114+ widthData := GlobalVars (). DejaVuGlyphWidthData
97115 for _ , char := range strings .TrimSpace (text ) {
98116 charWidth , ok := widthData [char ]
99117 if ! ok {
0 commit comments