68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
package panes
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/charmbracelet/x/ansi"
|
|
|
|
"git.kebcloud.no/keb/lazysecrets/internal/ui/styles"
|
|
)
|
|
|
|
// Fields is the fourth pane for anything but a secret (an account, and
|
|
// what describes vaults, scopes and files): labelled values, one per row,
|
|
// a label on the first of several values, to move through, filter and
|
|
// copy from. It's a List, so / filters it like the other panes. Secrets
|
|
// have Detail, for revealing and versions.
|
|
type Fields struct {
|
|
List[Field]
|
|
}
|
|
|
|
// Field is a row: a value under a label. Info rows (loading, errors)
|
|
// can't be copied.
|
|
type Field struct {
|
|
Label, Value string
|
|
Info bool
|
|
}
|
|
|
|
func NewFields() Fields {
|
|
l := NewList("", Item[Field]{
|
|
ID: func(f Field) string { return f.Label + "\x00" + f.Value },
|
|
Filter: func(f Field) string { return f.Value },
|
|
// "/tenant" finds the tenant's rows.
|
|
FilterExtra: func(f Field) string { return f.Label },
|
|
Render: func(st styles.Styles, f Field, width int) string {
|
|
value := f.Value
|
|
if f.Info {
|
|
value = st.Muted.Render(value)
|
|
}
|
|
return ansi.Truncate(st.Label.Render(fmt.Sprintf("%-10s", f.Label))+" "+value, width, "…")
|
|
},
|
|
})
|
|
l.Empty = "nothing to show"
|
|
return Fields{l}
|
|
}
|
|
|
|
// Set shows fields under title. Another title (another account or vault)
|
|
// starts over, without a filter.
|
|
func (s *Fields) Set(title string, fields []Field) {
|
|
if title != s.Title {
|
|
s.Reset()
|
|
s.Title = title
|
|
}
|
|
if len(fields) > 0 || len(s.Items()) > 0 {
|
|
s.SetItems(fields)
|
|
}
|
|
}
|
|
|
|
// Shown reports whether there's anything to show.
|
|
func (s Fields) Shown() bool { return len(s.Items()) > 0 }
|
|
|
|
// Selected is the field under the cursor, unless it's an info row.
|
|
func (s Fields) Selected() (Field, bool) {
|
|
f, ok := s.List.Selected()
|
|
if !ok || f.Info {
|
|
return Field{}, false
|
|
}
|
|
return f, true
|
|
}
|