code.gitea.io/gitea@v1.22.3/modules/badge/badge.go (about)

     1  // Copyright 2024 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package badge
     5  
     6  import (
     7  	actions_model "code.gitea.io/gitea/models/actions"
     8  )
     9  
    10  // The Badge layout: |offset|label|message|
    11  // We use 10x scale to calculate more precisely
    12  // Then scale down to normal size in tmpl file
    13  
    14  type Label struct {
    15  	text  string
    16  	width int
    17  }
    18  
    19  func (l Label) Text() string {
    20  	return l.text
    21  }
    22  
    23  func (l Label) Width() int {
    24  	return l.width
    25  }
    26  
    27  func (l Label) TextLength() int {
    28  	return int(float64(l.width-defaultOffset) * 9.5)
    29  }
    30  
    31  func (l Label) X() int {
    32  	return l.width*5 + 10
    33  }
    34  
    35  type Message struct {
    36  	text  string
    37  	width int
    38  	x     int
    39  }
    40  
    41  func (m Message) Text() string {
    42  	return m.text
    43  }
    44  
    45  func (m Message) Width() int {
    46  	return m.width
    47  }
    48  
    49  func (m Message) X() int {
    50  	return m.x
    51  }
    52  
    53  func (m Message) TextLength() int {
    54  	return int(float64(m.width-defaultOffset) * 9.5)
    55  }
    56  
    57  type Badge struct {
    58  	Color    string
    59  	FontSize int
    60  	Label    Label
    61  	Message  Message
    62  }
    63  
    64  func (b Badge) Width() int {
    65  	return b.Label.width + b.Message.width
    66  }
    67  
    68  const (
    69  	defaultOffset    = 9
    70  	defaultFontSize  = 11
    71  	DefaultColor     = "#9f9f9f" // Grey
    72  	defaultFontWidth = 7         // approximate speculation
    73  )
    74  
    75  var StatusColorMap = map[actions_model.Status]string{
    76  	actions_model.StatusSuccess:   "#4c1",    // Green
    77  	actions_model.StatusSkipped:   "#dfb317", // Yellow
    78  	actions_model.StatusUnknown:   "#97ca00", // Light Green
    79  	actions_model.StatusFailure:   "#e05d44", // Red
    80  	actions_model.StatusCancelled: "#fe7d37", // Orange
    81  	actions_model.StatusWaiting:   "#dfb317", // Yellow
    82  	actions_model.StatusRunning:   "#dfb317", // Yellow
    83  	actions_model.StatusBlocked:   "#dfb317", // Yellow
    84  }
    85  
    86  // GenerateBadge generates badge with given template
    87  func GenerateBadge(label, message, color string) Badge {
    88  	lw := defaultFontWidth*len(label) + defaultOffset
    89  	mw := defaultFontWidth*len(message) + defaultOffset
    90  	x := lw*10 + mw*5 - 10
    91  	return Badge{
    92  		Label: Label{
    93  			text:  label,
    94  			width: lw,
    95  		},
    96  		Message: Message{
    97  			text:  message,
    98  			width: mw,
    99  			x:     x,
   100  		},
   101  		FontSize: defaultFontSize * 10,
   102  		Color:    color,
   103  	}
   104  }