package theme
import (
"github.com/charmbracelet/lipgloss"
)
// MethodStyle returns a coloured rendering of an HTTP method so verbs
// are distinguishable in the request list at a glance.
func (p Palette) MethodStyle(method string) lipgloss.Style {
base := lipgloss.NewStyle().Bold(true).PaddingRight(1)
switch method {
case "GET":
return base.Foreground(p.Accent)
case "POST":
return base.Foreground(p.Success)
case "PUT", "PATCH":
return base.Foreground(p.Warning)
case "DELETE":
return base.Foreground(p.Error)
default:
return base.Foreground(p.Foreground)
}
}
// StatusStyle colours a status code cell.
func (p Palette) StatusStyle(code int) lipgloss.Style {
base := lipgloss.NewStyle().Bold(true).PaddingRight(1)
switch {
case code >= 200 && code < 300:
return base.Foreground(p.Success)
case code >= 300 && code < 400:
return base.Foreground(p.Accent)
case code >= 400 && code < 500:
return base.Foreground(p.Warning)
case code >= 500 && code < 600:
return base.Foreground(p.Error)
default:
return base.Foreground(p.Dim)
}
}
// HeaderKey is the style for a header name in the detail view.
func (p Palette) HeaderKey() lipgloss.Style {
return lipgloss.NewStyle().Foreground(p.Accent).Bold(true)
}
// HeaderVal is the style for a header value in the detail view.
func (p Palette) HeaderVal() lipgloss.Style {
return lipgloss.NewStyle().Foreground(p.Foreground)
}
// Frame wraps content in the Focused or List border depending on active.
func (p Palette) Frame(content string, active bool) string {
if active {
return p.Focused.Render(content)
}
return p.List.Render(content)
}
// Truncate returns s shortened to w runes with a single-rune ellipsis.
// It is used to enforce row widths in the list view.
func Truncate(s string, w int) string {
runes := []rune(s)
if len(runes) <= w || w <= 1 {
if w <= 0 {
return ""
}
if len(runes) <= w {
return s
}
return string(runes[:w])
}
return string(runes[:w-1]) + "…"
}