package gitctl import ( "fmt" "strings" "github.com/charmbracelet/bubbles/list" "github.com/charmbracelet/lipgloss" ) type repoItem struct{ repo repoRow } func (i repoItem) FilterValue() string { return i.repo.Ns + "/" + i.repo.Name } func (i repoItem) Title() string { return i.repo.Ns + "/" + i.repo.Name } func (i repoItem) Description() string { vis := "public" if i.repo.IsPrivate { vis = "private" } return fmt.Sprintf("%s · created %s · updated %s", vis, fmtTime(i.repo.Created), fmtTime(i.repo.Updated)) } type nsItem struct{ ns nsRow } func (i nsItem) FilterValue() string { return i.ns.Name } func (i nsItem) Title() string { return i.ns.Name } func (i nsItem) Description() string { password := "no password" if i.ns.HasPassword { password = "password enabled" } return fmt.Sprintf("%d repos · %s · created %s", i.ns.RepoCount, password, fmtTime(i.ns.Created)) } type sshKeyItem struct{ key sshKeyRow } func (i sshKeyItem) FilterValue() string { return i.key.Comment + " " + i.key.Fingerprint } func (i sshKeyItem) Title() string { c := i.key.Comment if c == "" { c = "(no comment)" } return fmt.Sprintf("%s · %s", i.key.KeyType, c) } func (i sshKeyItem) Description() string { return fmt.Sprintf("%s · %d grant(s)", shortFP(i.key.Fingerprint), i.key.GrantCount) } type localKeyItem struct{ key localKey } func (i localKeyItem) FilterValue() string { return i.key.Filename + " " + i.key.Comment + " " + i.key.Fingerprint } func (i localKeyItem) Title() string { tag := "not registered" if i.key.Registered { tag = "registered" } return fmt.Sprintf("%s · %s", i.key.Filename, tag) } func (i localKeyItem) Description() string { c := i.key.Comment if c == "" { c = "(no comment)" } return fmt.Sprintf("%s · %s · %s", i.key.KeyType, shortFP(i.key.Fingerprint), c) } type grantItem struct{ g grantRow } func (i grantItem) FilterValue() string { return i.g.TargetLabel + " " + i.g.Perm + " " + grantIdentity(i.g) } func (i grantItem) Title() string { return fmt.Sprintf("%s [%s]", i.g.TargetLabel, i.g.Perm) } func (i grantItem) Description() string { cred := grantIdentity(i.g) if cred == "" { cred = "(deleted credential)" } return fmt.Sprintf("%s %s · %s · #%d", i.g.CredType, cred, fmtTime(i.g.BoundAt), i.g.AclID) } func shortFP(fp string) string { i := strings.Index(fp, ":") if i < 0 { return fp } rest := fp[i+1:] if len(rest) > 12 { rest = rest[:12] + "…" } return fp[:i+1] + rest } func permPill(perm string) string { switch perm { case "admin": return permPillAdmin.Render("[admin]") case "write": return permPillWrite.Render("[write]") default: return permPillRead.Render("[read]") } } func newListModel(items []list.Item, width, height int) list.Model { d := list.NewDefaultDelegate() d.Styles.SelectedTitle = lipgloss.NewStyle(). Border(lipgloss.NormalBorder(), false, false, false, true). BorderForeground(colorAccent). Foreground(colorAccent).Bold(true). Padding(0, 0, 0, 1) d.Styles.SelectedDesc = lipgloss.NewStyle(). Border(lipgloss.NormalBorder(), false, false, false, true). BorderForeground(colorAccent). Foreground(colorAccent). Padding(0, 0, 0, 1) d.Styles.NormalTitle = lipgloss.NewStyle().Padding(0, 0, 0, 2) d.Styles.NormalDesc = lipgloss.NewStyle().Foreground(colorDim).Padding(0, 0, 0, 2) d.SetSpacing(0) l := list.New(items, d, width, height) l.SetShowTitle(false) l.SetShowStatusBar(false) l.SetShowHelp(false) l.SetShowPagination(true) l.KeyMap.Quit.SetEnabled(false) l.KeyMap.ForceQuit.SetEnabled(false) l.KeyMap.ShowFullHelp.SetEnabled(false) l.KeyMap.CloseFullHelp.SetEnabled(false) return l } func repoItems(repos []repoRow) []list.Item { items := make([]list.Item, len(repos)) for i := range repos { items[i] = repoItem{repo: repos[i]} } return items } func nsItems(namespaces []nsRow) []list.Item { items := make([]list.Item, len(namespaces)) for i := range namespaces { items[i] = nsItem{ns: namespaces[i]} } return items } func sshKeyItems(keys []sshKeyRow) []list.Item { items := make([]list.Item, len(keys)) for i := range keys { items[i] = sshKeyItem{key: keys[i]} } return items } func localKeyItems(keys []localKey) []list.Item { items := make([]list.Item, len(keys)) for i := range keys { items[i] = localKeyItem{key: keys[i]} } return items } func grantItems(grants []grantRow) []list.Item { items := make([]list.Item, len(grants)) for i := range grants { items[i] = grantItem{g: grants[i]} } return items }