Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎨 Improve the structure of some internal packages #441

Merged
merged 3 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 0 additions & 112 deletions internal/build/view.go

This file was deleted.

99 changes: 99 additions & 0 deletions internal/build/view/annotations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package view

import (
"fmt"
"regexp"

"github.com/buildkite/go-buildkite/v4"
"github.com/charmbracelet/lipgloss"
)

const (
styleDefault = lipgloss.Color("#DDD")
styleInfo = lipgloss.Color("#337AB7")
styleWarning = lipgloss.Color("#FF841C")
styleError = lipgloss.Color("#F45756")
styleSuccess = lipgloss.Color("#2ECC40")

// Maximum length for annotation body preview
maxAnnotationLength = 120
)

func (v *BuildView) renderAnnotations() string {
var sections []string

title := lipgloss.NewStyle().Bold(true).Padding(0, 1).Underline(true).Render("Annotations")
sections = append(sections, title)

for _, annotation := range v.Annotations {
sections = append(sections, renderAnnotation(&annotation))
}

return lipgloss.JoinVertical(lipgloss.Top, sections...)
}

func renderAnnotation(annotation *buildkite.Annotation) string {
return lipgloss.JoinVertical(lipgloss.Top,
lipgloss.NewStyle().Align(lipgloss.Left).Padding(0, 1).Render(""),
lipgloss.NewStyle().
Padding(0, 1).
Border(lipgloss.RoundedBorder()).
BorderForeground(getAnnotationStateColor(annotation.Style)).
Render(fmt.Sprintf("%s\t%s",
getAnnotationIcon(annotation.Style),
truncateAndStripTags(annotation.BodyHTML),
)),
)
}

func getAnnotationStateColor(state string) lipgloss.Color {
switch state {
case "success":
return styleSuccess
case "error":
return styleError
case "warning":
return styleWarning
case "info":
return styleInfo
default:
return styleDefault
}
}

func getAnnotationIcon(state string) string {
switch state {
case "success":
return "✔"
case "error":
return "✖"
case "warning":
return "⚠"
case "info":
return "ℹ"
default:
return "🗒️"
}
}

func truncateAndStripTags(body string) string {
// Strip HTML tags while preserving newlines
cleaned := stripTags(body)

if len(cleaned) > maxAnnotationLength {
return cleaned[:maxAnnotationLength] + "...(more)"
}
return cleaned
}

func stripTags(body string) string {
// Remove closing tags first
re := regexp.MustCompile(`</[^>]+>`)
body = re.ReplaceAllString(body, "")

// Then remove opening tags
re = regexp.MustCompile(`<[^>]*>`)
body = re.ReplaceAllString(body, "")

return body
}
54 changes: 54 additions & 0 deletions internal/build/view/artifacts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package view

import (
"fmt"

"github.com/buildkite/go-buildkite/v4"
"github.com/charmbracelet/lipgloss"
)

func (v *BuildView) renderArtifacts() string {
var sections []string

title := lipgloss.NewStyle().Bold(true).Padding(0, 1).Underline(true).Render("Artifacts")
sections = append(sections, title)

for _, artifact := range v.Artifacts {
sections = append(sections, renderArtifact(&artifact))
}

return lipgloss.JoinVertical(lipgloss.Top, sections...)
}

func renderArtifact(artifact *buildkite.Artifact) string {
return lipgloss.JoinVertical(lipgloss.Top,
lipgloss.NewStyle().Align(lipgloss.Left).Padding(0, 1).Render(""),
lipgloss.JoinHorizontal(lipgloss.Left,
lipgloss.NewStyle().Width(38).Align(lipgloss.Left).Padding(0, 1).Render(artifact.ID),
lipgloss.NewStyle().Width(50).Align(lipgloss.Left).Padding(0, 1).Render(artifact.Path),
lipgloss.NewStyle().Align(lipgloss.Right).Padding(0, 1).Render(formatBytes(artifact.FileSize)),
),
)
}

func formatBytes(bytes int64) string {
const (
KB = 1024
MB = 1024 * KB
GB = 1024 * MB
TB = 1024 * GB
)

switch {
case bytes >= TB:
return fmt.Sprintf("%.1fTB", float64(bytes)/TB)
case bytes >= GB:
return fmt.Sprintf("%.1fGB", float64(bytes)/GB)
case bytes >= MB:
return fmt.Sprintf("%.1fMB", float64(bytes)/MB)
case bytes >= KB:
return fmt.Sprintf("%.1fKB", float64(bytes)/KB)
default:
return fmt.Sprintf("%dB", bytes)
}
}
Loading