github.com/google/osv-scalibr@v0.4.1/guidedremediation/internal/tui/components/severity.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package components
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/charmbracelet/lipgloss"
    21  	"github.com/google/osv-scalibr/guidedremediation/internal/severity"
    22  	osvpb "github.com/ossf/osv-schema/bindings/go/osvschema"
    23  )
    24  
    25  var (
    26  	severityColors = map[string]lipgloss.Color{
    27  		"UNKNOWN":  lipgloss.Color("243"), // grey
    28  		"NONE":     lipgloss.Color("243"), // grey
    29  		"LOW":      lipgloss.Color("28"),  // green
    30  		"MEDIUM":   lipgloss.Color("208"), // orange
    31  		"HIGH":     lipgloss.Color("160"), // red
    32  		"CRITICAL": lipgloss.Color("88"),  // dark red
    33  	}
    34  	severityStyle = lipgloss.NewStyle().
    35  			Foreground(lipgloss.Color("15")). // white
    36  			Bold(true).
    37  			Align(lipgloss.Center)
    38  )
    39  
    40  // RenderSeverity renders for terminal the highest severity score & rating of a list of severities.
    41  func RenderSeverity(severities []*osvpb.Severity) string {
    42  	text := "UNKNOWN"
    43  	bestScore := -1.0
    44  	for _, sev := range severities {
    45  		score, rating, err := severity.CalculateScoreAndRating(sev)
    46  		if err != nil || score <= bestScore {
    47  			continue
    48  		}
    49  		bestScore = score
    50  		if rating != "UNKNOWN" {
    51  			text = fmt.Sprintf("%1.1f %s", score, rating)
    52  		}
    53  	}
    54  	return severityStyle.Width(16).Background(severityColors[text]).Render(text)
    55  }
    56  
    57  // RenderSeverityShort renders for terminal the highest severity score only of a list of severities.
    58  func RenderSeverityShort(severities []*osvpb.Severity) string {
    59  	bestScore := -1.0
    60  	bestRating := "UNKNOWN"
    61  	for _, sev := range severities {
    62  		score, rating, err := severity.CalculateScoreAndRating(sev)
    63  		if err != nil || score <= bestScore {
    64  			continue
    65  		}
    66  		bestScore = score
    67  		bestRating = rating
    68  	}
    69  	str := "???"
    70  	if bestScore >= 0 {
    71  		str = fmt.Sprintf("%1.1f", bestScore)
    72  	}
    73  	return severityStyle.Width(5).Background(severityColors[bestRating]).Render(str)
    74  }