feat: lazysecrets list right-aligns the marks by the icon, so names line up
This commit is contained in:
2 files changed
+12
-5
No files matched your search
@@ -170,8 +170,9 @@ 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
|
// marks says what goes before the name, in a field of its own so a
|
||||||
// search leaves it out: ★ for a starred entry, ◷ for one used lately,
|
// search leaves it out: ★ for a starred entry, ◷ for one used lately,
|
||||||
// ⚠ for one expiring within warn and ✗ for an expired one, as the TUI
|
// ⚠ for one expiring within warn and ✗ for an expired one, as the TUI
|
||||||
// marks them, then the provider's icon. Only the marks an entry has take
|
// marks them, then the provider's icon. The marks are right-aligned in
|
||||||
// space.
|
// room for every kind turned on, so they sit by the icon and names line
|
||||||
|
// up.
|
||||||
type marks struct {
|
type marks struct {
|
||||||
theme styles.Styles
|
theme styles.Styles
|
||||||
icons icons.Set
|
icons icons.Set
|
||||||
@@ -183,7 +184,9 @@ type marks struct {
|
|||||||
func (m marks) of(e listing.Entry) string {
|
func (m marks) of(e listing.Entry) string {
|
||||||
render := renderer(m.color)
|
render := renderer(m.color)
|
||||||
var parts []string
|
var parts []string
|
||||||
|
room := 0 // marks turned on
|
||||||
mark := func(on bool, st lipgloss.Style, s string) {
|
mark := func(on bool, st lipgloss.Style, s string) {
|
||||||
|
room++
|
||||||
if on {
|
if on {
|
||||||
parts = append(parts, render(st, s))
|
parts = append(parts, render(st, s))
|
||||||
}
|
}
|
||||||
@@ -205,6 +208,7 @@ func (m marks) of(e listing.Entry) string {
|
|||||||
mark(true, m.theme.Warning, "⚠")
|
mark(true, m.theme.Warning, "⚠")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pad := strings.Repeat(" ", room-len(parts)) // each mark and its space
|
||||||
kind := provider.Kind("")
|
kind := provider.Kind("")
|
||||||
for _, k := range []provider.Kind{provider.KindAzure, provider.KindDatabricks, provider.KindRepo} {
|
for _, k := range []provider.Kind{provider.KindAzure, provider.KindDatabricks, provider.KindRepo} {
|
||||||
if strings.EqualFold(string(k), e.Provider) {
|
if strings.EqualFold(string(k), e.Provider) {
|
||||||
@@ -213,8 +217,10 @@ func (m marks) of(e listing.Entry) string {
|
|||||||
}
|
}
|
||||||
if icon := strings.TrimSpace(m.icons.Prefix(kind)); icon != "" {
|
if icon := strings.TrimSpace(m.icons.Prefix(kind)); icon != "" {
|
||||||
parts = append(parts, render(m.theme.Kind(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
|
||||||
}
|
}
|
||||||
return strings.Join(parts, " ")
|
return pad + strings.Join(parts, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderer(color bool) func(lipgloss.Style, string) string {
|
func renderer(color bool) func(lipgloss.Style, string) string {
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ func TestListMarks(t *testing.T) {
|
|||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{plain, listing.Entry{Provider: "azure", Starred: true}, "★ " + az},
|
{plain, listing.Entry{Provider: "azure", Starred: true}, "★ " + az},
|
||||||
{plain, listing.Entry{Provider: "azure"}, az}, // no space for a star it hasn't
|
{plain, listing.Entry{Provider: "azure"}, " " + az}, // room for the star, so names line up
|
||||||
{marks{icons: icons.NerdFont}, listing.Entry{Provider: "azure", Starred: true}, az},
|
{marks{icons: icons.NerdFont}, listing.Entry{Provider: "azure", Starred: true}, az},
|
||||||
{noIcons, listing.Entry{Provider: "azure", Starred: true}, "★"},
|
{noIcons, listing.Entry{Provider: "azure", Starred: true}, "★"},
|
||||||
{marks{}, listing.Entry{Provider: "azure", Starred: true}, ""},
|
{marks{}, listing.Entry{Provider: "azure", Starred: true}, ""},
|
||||||
@@ -54,11 +54,12 @@ func TestListRecentAndExpiryMarks(t *testing.T) {
|
|||||||
e listing.Entry
|
e listing.Entry
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
|
// Right-aligned in room for three: by the icon, names lined up.
|
||||||
{listing.Entry{Provider: "azure", Starred: true, Recent: true}, " ★ ◷ " + az},
|
{listing.Entry{Provider: "azure", Starred: true, Recent: true}, " ★ ◷ " + az},
|
||||||
{listing.Entry{Provider: "azure", Recent: true}, " ◷ " + az},
|
{listing.Entry{Provider: "azure", Recent: true}, " ◷ " + az},
|
||||||
{listing.Entry{Provider: "azure", Expires: now.Add(-time.Hour)}, " ✗ " + az},
|
{listing.Entry{Provider: "azure", Expires: now.Add(-time.Hour)}, " ✗ " + az},
|
||||||
{listing.Entry{Provider: "azure", Expires: now.Add(10 * 24 * time.Hour)}, " ⚠ " + az},
|
{listing.Entry{Provider: "azure", Expires: now.Add(10 * 24 * time.Hour)}, " ⚠ " + az},
|
||||||
{listing.Entry{Provider: "azure", Expires: now.Add(60 * 24 * time.Hour)}, az},
|
{listing.Entry{Provider: "azure", Expires: now.Add(60 * 24 * time.Hour)}, " " + az},
|
||||||
} {
|
} {
|
||||||
if got := m.of(c.e); got != c.want {
|
if got := m.of(c.e); got != c.want {
|
||||||
t.Errorf("%+v: %q, want %q", c.e, got, c.want)
|
t.Errorf("%+v: %q, want %q", c.e, got, c.want)
|
||||||
|
|||||||
Reference in new issue
Block a user