feat: lazysecrets list marks what's wrong (✗) or due (⚠) by the icon, and writes expired or expires in 4d

This commit is contained in:
keb committed 2026-10-14 21:24:01 +02:00
1 parent 4473e037f8
commit 37164158ca
2 files changed
+81 -37

No files matched your search

+32 -21
View File
@@ -73,8 +73,9 @@ func listCmd(providers []provider.Provider, st *store.Store, cachePath string, t
if *color != "always" && *color != "never" {
return fmt.Errorf("--color: %q is not always or never", *color)
}
m := marks{theme: theme, icons: icons.None, stars: *stars, recent: *recent, color: *color == "always"}
now := time.Now()
m := marks{theme: theme, icons: icons.None, stars: *stars, recent: *recent, expiry: *expiry,
color: *color == "always", warn: expiryWarn, now: now}
if *iconsOn {
m.icons = icons.NerdFont
}
@@ -146,8 +147,9 @@ func listCmd(providers []provider.Provider, st *store.Store, cachePath string, t
var oneLine = strings.NewReplacer("\t", " ", "\r\n", " ", "\n", " ", "\r", " ")
// expiryOf says, as the TUI does, that a secret, key or certificate has
// expired (✗ expired) or will within warn (⚠ 12d), or is "".
// expiryOf says that a secret, key or certificate has expired, in red,
// or when it will if that's within warn (expires in 12d), in yellow, or
// is "". Its mark (✗, ⚠) is before the name.
func expiryOf(e listing.Entry, theme styles.Styles, warn time.Duration, now time.Time, color bool) string {
render := renderer(color)
left := e.Expires.Sub(now)
@@ -155,20 +157,17 @@ func expiryOf(e listing.Entry, theme styles.Styles, warn time.Duration, now time
case e.Expires.IsZero() || left > warn:
return ""
case left <= 0:
return render(theme.Error, "✗ expired")
return render(theme.Error, "expired")
case left < 24*time.Hour:
return render(theme.Warning, fmt.Sprintf("⚠ %dh", max(1, int(left.Hours()))))
return render(theme.Warning, fmt.Sprintf("expires in %dh", max(1, int(left.Hours()))))
}
return render(theme.Warning, fmt.Sprintf("⚠ %dd", int(left.Hours()/24)))
return render(theme.Warning, fmt.Sprintf("expires in %dd", int(left.Hours()/24)))
}
// errorOf is why a list couldn't be read, or "": with colour, a red ✗
// before it, as the TUI marks lists it can't read.
// errorOf is why a list couldn't be read, in red, or "". Its mark (✗)
// is before the name.
func errorOf(e listing.Entry, theme styles.Styles, color bool) string {
if e.Error == "" || !color {
return e.Error
}
return theme.Error.Render("✗ " + e.Error)
return renderer(color)(theme.Error, e.Error)
}
// nameOf is an entry's name for the text format: the path with the
@@ -189,14 +188,17 @@ func nameOf(e listing.Entry, theme styles.Styles, color bool) string {
}
// marks says what goes before the name, in a field of its own so a
// search leaves it out: ★ for a starred entry and ◷ for one used lately,
// as the TUI marks them, then the provider's icon. The marks are
// right-aligned in room for every kind turned on, so they sit by the icon
// and names line up.
// search leaves it out: ✗ in red when something's wrong (it couldn't be
// read, or has expired) or ⚠ when it expires within warn; ★ for a starred
// entry and ◷ for one used lately, as the TUI marks them; then the
// provider's icon. The marks are right-aligned in room for every kind
// turned on, so they sit by the icon and names line up.
type marks struct {
theme styles.Styles
icons icons.Set
stars, recent, color bool
theme styles.Styles
icons icons.Set
stars, recent, expiry, color bool
warn time.Duration // ui.expiryWarningDays
now time.Time
}
func (m marks) of(e listing.Entry) string {
@@ -209,6 +211,15 @@ func (m marks) of(e listing.Entry) string {
parts = append(parts, render(st, s))
}
}
// What's wrong, then what to watch: room for one of them.
switch left := e.Expires.Sub(m.now); {
case e.Error != "" || (m.expiry && !e.Expires.IsZero() && left <= 0):
mark(true, m.theme.Error, "✗")
case m.expiry && !e.Expires.IsZero() && left <= m.warn:
mark(true, m.theme.Warning, "⚠")
default:
mark(false, m.theme.Error, "")
}
if m.stars {
mark(e.Starred, m.theme.Favourite, "★")
}
@@ -224,8 +235,8 @@ func (m marks) of(e listing.Entry) string {
}
if icon := strings.TrimSpace(m.icons.Prefix(kind)); icon != "" {
parts = append(parts, render(m.theme.Kind(kind), icon))
} else if len(parts) > 0 {
pad = strings.TrimSuffix(pad, " ") // nothing after the last mark
} else if len(parts) == 0 {
pad = strings.TrimSuffix(pad, " ") // no mark after the room
}
return pad + strings.Join(parts, " ")
}