Keep section headers, check skips, and layout separators aligned with the active glyph theme. Co-Authored-By: Virgil <virgil@lethean.io>
90 lines
2 KiB
Go
90 lines
2 KiB
Go
package cli
|
|
|
|
// CheckBuilder provides fluent API for check results.
|
|
type CheckBuilder struct {
|
|
name string
|
|
status string
|
|
style *AnsiStyle
|
|
icon string
|
|
duration string
|
|
}
|
|
|
|
// Check starts building a check result line.
|
|
//
|
|
// cli.Check("audit").Pass()
|
|
// cli.Check("fmt").Fail().Duration("2.3s")
|
|
// cli.Check("test").Skip()
|
|
func Check(name string) *CheckBuilder {
|
|
return &CheckBuilder{name: name}
|
|
}
|
|
|
|
// Pass marks the check as passed.
|
|
func (c *CheckBuilder) Pass() *CheckBuilder {
|
|
c.status = "passed"
|
|
c.style = SuccessStyle
|
|
c.icon = Glyph(":check:")
|
|
return c
|
|
}
|
|
|
|
// Fail marks the check as failed.
|
|
func (c *CheckBuilder) Fail() *CheckBuilder {
|
|
c.status = "failed"
|
|
c.style = ErrorStyle
|
|
c.icon = Glyph(":cross:")
|
|
return c
|
|
}
|
|
|
|
// Skip marks the check as skipped.
|
|
func (c *CheckBuilder) Skip() *CheckBuilder {
|
|
c.status = "skipped"
|
|
c.style = DimStyle
|
|
c.icon = Glyph(":skip:")
|
|
return c
|
|
}
|
|
|
|
// Warn marks the check as warning.
|
|
func (c *CheckBuilder) Warn() *CheckBuilder {
|
|
c.status = "warning"
|
|
c.style = WarningStyle
|
|
c.icon = Glyph(":warn:")
|
|
return c
|
|
}
|
|
|
|
// Duration adds duration to the check result.
|
|
func (c *CheckBuilder) Duration(d string) *CheckBuilder {
|
|
c.duration = d
|
|
return c
|
|
}
|
|
|
|
// Message adds a custom message instead of status.
|
|
func (c *CheckBuilder) Message(msg string) *CheckBuilder {
|
|
c.status = msg
|
|
return c
|
|
}
|
|
|
|
// String returns the formatted check line.
|
|
func (c *CheckBuilder) String() string {
|
|
icon := compileGlyphs(c.icon)
|
|
if c.style != nil {
|
|
icon = c.style.Render(icon)
|
|
}
|
|
|
|
status := compileGlyphs(c.status)
|
|
if c.style != nil && c.status != "" {
|
|
status = c.style.Render(status)
|
|
}
|
|
name := compileGlyphs(c.name)
|
|
|
|
if c.duration != "" {
|
|
return Sprintf(" %s %-20s %-10s %s", icon, name, status, DimStyle.Render(compileGlyphs(c.duration)))
|
|
}
|
|
if status != "" {
|
|
return Sprintf(" %s %s %s", icon, name, status)
|
|
}
|
|
return Sprintf(" %s %s", icon, name)
|
|
}
|
|
|
|
// Print outputs the check result.
|
|
func (c *CheckBuilder) Print() {
|
|
Println("%s", c.String())
|
|
}
|