diff --git a/cmd/lazysecrets/list.go b/cmd/lazysecrets/list.go index 9f5f7c6..1d43850 100644 --- a/cmd/lazysecrets/list.go +++ b/cmd/lazysecrets/list.go @@ -52,7 +52,7 @@ func listCmd(providers []provider.Provider, st *store.Store, cachePath string, t iconsOn := fs.Bool("icons", withIcons, "put the provider's Nerd Font icon before names (text)") stars := fs.Bool("stars", true, "put ★ before starred names, in the theme's favouriteColor (text)") recent := fs.Bool("recent", withRecent, "put ◷ before names used lately, in mutedColor (text; default ui.recentIcon)") - notes := fs.Bool("notes", true, "put a note icon (✎ without --icons) before names with a note, in mutedColor (text; the note is printed either way)") + notes := fs.Bool("notes", true, "start a note with a note icon (✎ without --icons) (text; the note is printed either way)") expiry := fs.Bool("expiry", true, "mark and say which have expired (✗ expired) or expire within ui.expiryWarningDays (⚠ expires in 12d) (text)") kinds := fs.String("kind", "", "only these kinds, comma-separated: "+strings.Join(listing.Kinds, ", ")+" (vault: also scopes and repo files)") showErrors := fs.Bool("errors", true, "list the lists that can't be read, with why (text: ✗ lines; json: \"error\")") @@ -76,7 +76,7 @@ func listCmd(providers []provider.Provider, st *store.Store, cachePath string, t return fmt.Errorf("--color: %q is not always or never", *color) } now := time.Now() - m := marks{theme: theme, icons: icons.None, stars: *stars, recent: *recent, notes: *notes, expiry: *expiry, + 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 @@ -94,7 +94,7 @@ func listCmd(providers []provider.Provider, st *store.Store, cachePath string, t if status == "" && *expiry { status = expiryOf(e, theme, expiryWarn, now, colored) } - note := renderer(colored)(theme.Muted, e.Note) + note := noteOf(e, theme, m.icons, *notes, colored) // A tab or line break in a name would break the line up. _, err := fmt.Fprintf(out, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", e.Kind, mark, oneLine.Replace(name), oneLine.Replace(status), oneLine.Replace(note), e.Account, e.Link) @@ -152,6 +152,19 @@ func listCmd(providers []provider.Provider, st *store.Store, cachePath string, t var oneLine = strings.NewReplacer("\t", " ", "\r\n", " ", "\n", " ", "\r", " ") +// noteOf is the entry's note, muted, after a note icon (✎ without Nerd +// Font icons) with icon; or "". +func noteOf(e listing.Entry, theme styles.Styles, iconSet icons.Set, icon, color bool) string { + if e.Note == "" { + return "" + } + note := e.Note + if icon { + note = cmp.Or(iconSet.Note, "✎") + " " + note + } + return renderer(color)(theme.Muted, note) +} + // 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. @@ -195,16 +208,15 @@ 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: ✗ 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; a note icon -// (✎ without Nerd Font icons) for one with a note; then the provider's -// icon. The marks are right-aligned in room for every kind +// 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, notes, expiry, color bool - warn time.Duration // ui.expiryWarningDays - now time.Time + 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 { @@ -232,9 +244,6 @@ func (m marks) of(e listing.Entry) string { if m.recent { mark(e.Recent, m.theme.Muted, "◷") } - if m.notes { - mark(e.Note != "", m.theme.Muted, cmp.Or(m.icons.Note, "✎")) - } pad := strings.Repeat(" ", room-len(parts)) // each mark and its space kind := provider.Kind("") for _, k := range []provider.Kind{provider.KindAzure, provider.KindDatabricks, provider.KindRepo} { diff --git a/cmd/lazysecrets/list_test.go b/cmd/lazysecrets/list_test.go index ba322c2..5b2ce20 100644 --- a/cmd/lazysecrets/list_test.go +++ b/cmd/lazysecrets/list_test.go @@ -87,25 +87,20 @@ func TestListExpiry(t *testing.T) { } } -func TestListNoteMark(t *testing.T) { - az := icons.NerdFont.Azure - m := marks{icons: icons.NerdFont, stars: true, notes: true} - for _, c := range []struct { - e listing.Entry - want string - }{ - {listing.Entry{Provider: "azure", Note: "rotate in May"}, " " + icons.NerdFont.Note + " " + az}, - {listing.Entry{Provider: "azure", Note: "x", Starred: true}, " ★ " + icons.NerdFont.Note + " " + az}, - {listing.Entry{Provider: "azure"}, " " + az}, - } { - if got := m.of(c.e); got != c.want { - t.Errorf("%+v: %q, want %q", c.e, got, c.want) - } +func TestListNotes(t *testing.T) { + e := listing.Entry{Note: "rotate in May"} + if got := noteOf(e, styles.Default(), icons.NerdFont, true, false); got != icons.NerdFont.Note+" rotate in May" { + t.Errorf("icons: %q", got) } - m.icons = icons.None // without Nerd Font icons - if got := m.of(listing.Entry{Provider: "azure", Note: "x"}); got != " ✎" { + if got := noteOf(e, styles.Default(), icons.None, true, false); got != "✎ rotate in May" { t.Errorf("no icons: %q", got) } + if got := noteOf(e, styles.Default(), icons.NerdFont, false, false); got != "rotate in May" { + t.Errorf("--notes=false: %q", got) + } + if got := noteOf(listing.Entry{}, styles.Default(), icons.NerdFont, true, true); got != "" { + t.Errorf("no note: %q", got) + } } func TestListStatusMark(t *testing.T) {