repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

pico / tui / tokens
Eric Bower · 18 May 24

tokenview.go

 1package tokens
 2
 3import (
 4	"fmt"
 5
 6	"github.com/picosh/pico/db"
 7	"github.com/picosh/pico/tui/common"
 8)
 9
10type styledKey struct {
11	styles    common.Styles
12	nameLabel string
13	name      string
14	date      string
15	gutter    string
16	dateLabel string
17	dateVal   string
18}
19
20func newStyledKey(styles common.Styles, token *db.Token, active bool) styledKey {
21	date := token.CreatedAt.Format(common.DateFormat)
22
23	// Default state
24	return styledKey{
25		styles:    styles,
26		date:      date,
27		name:      token.Name,
28		gutter:    " ",
29		nameLabel: "Name:",
30		dateLabel: "Added:",
31		dateVal:   styles.Label.Render(date),
32	}
33}
34
35// Selected state.
36func (k *styledKey) selected() {
37	k.gutter = common.VerticalLine(k.styles.Renderer, common.StateSelected)
38	k.nameLabel = k.styles.Label.Render("Name:")
39	k.dateLabel = k.styles.Label.Render("Added:")
40}
41
42// Deleting state.
43func (k *styledKey) deleting() {
44	k.gutter = common.VerticalLine(k.styles.Renderer, common.StateDeleting)
45	k.nameLabel = k.styles.Delete.Render("Name:")
46	k.dateLabel = k.styles.Delete.Render("Added:")
47	k.dateVal = k.styles.Delete.Render(k.date)
48}
49
50func (k styledKey) render(state keyState) string {
51	switch state {
52	case keySelected:
53		k.selected()
54	case keyDeleting:
55		k.deleting()
56	}
57	return fmt.Sprintf(
58		"%s %s %s\n%s %s %s\n\n",
59		k.gutter, k.nameLabel, k.name,
60		k.gutter, k.dateLabel, k.dateVal,
61	)
62}