repos / pico

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

commit
01ffc21
parent
0a3f88a
author
Antonio Mika
date
2023-11-12 22:13:17 +0000 UTC
Merge pull request #54 from picosh/am/dynamic-list

Add the ability to set the per page by window height
6 files changed,  +79, -38
M pgs/cms.go
+7, -3
 1@@ -118,6 +118,10 @@ func CmsMiddleware(cfg *config.ConfigCms, urls config.ConfigURL) bm.Handler {
 2 			menuChoice: unsetChoice,
 3 			styles:     common.DefaultStyles(),
 4 			spinner:    common.NewSpinner(),
 5+			terminalSize: tea.WindowSizeMsg{
 6+				Width:  80,
 7+				Height: 24,
 8+			},
 9 		}
10 
11 		user, err := m.findUser()
12@@ -144,13 +148,13 @@ type model struct {
13 	status        status
14 	menuIndex     int
15 	menuChoice    menuChoice
16-	terminalWidth int
17 	styles        common.Styles
18 	info          info.Model
19 	spinner       spinner.Model
20 	username      username.Model
21 	keys          keys.Model
22 	createAccount account.CreateModel
23+	terminalSize  tea.WindowSizeMsg
24 }
25 
26 func (m model) Init() tea.Cmd {
27@@ -188,7 +192,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
28 
29 	switch msg := msg.(type) {
30 	case tea.WindowSizeMsg:
31-		m.terminalWidth = msg.Width
32+		m.terminalSize = msg
33 	case tea.KeyMsg:
34 		switch msg.Type {
35 		case tea.KeyCtrlC:
36@@ -353,7 +357,7 @@ func (m model) errorView(err error) string {
37 }
38 
39 func (m model) View() string {
40-	w := m.terminalWidth - m.styles.App.GetHorizontalFrameSize()
41+	w := m.terminalSize.Width - m.styles.App.GetHorizontalFrameSize()
42 	s := m.styles.Logo.SetString(m.cfg.Domain).String() + "\n\n"
43 	switch m.status {
44 	case statusNoAccount:
M wish/cms/cms.go
+18, -6
 1@@ -3,6 +3,7 @@ package cms
 2 import (
 3 	"errors"
 4 	"fmt"
 5+	"math"
 6 
 7 	"github.com/charmbracelet/bubbles/spinner"
 8 	tea "github.com/charmbracelet/bubbletea"
 9@@ -127,6 +128,10 @@ func Middleware(cfg *config.ConfigCms, urls config.ConfigURL) bm.Handler {
10 			menuChoice: unsetChoice,
11 			styles:     common.DefaultStyles(),
12 			spinner:    common.NewSpinner(),
13+			terminalSize: tea.WindowSizeMsg{
14+				Width:  80,
15+				Height: 24,
16+			},
17 		}
18 
19 		user, err := m.findUser()
20@@ -153,7 +158,7 @@ type model struct {
21 	status        status
22 	menuIndex     int
23 	menuChoice    menuChoice
24-	terminalWidth int
25+	terminalSize  tea.WindowSizeMsg
26 	styles        common.Styles
27 	info          info.Model
28 	spinner       spinner.Model
29@@ -199,7 +204,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
30 
31 	switch msg := msg.(type) {
32 	case tea.WindowSizeMsg:
33-		m.terminalWidth = msg.Width
34+		m.terminalSize = msg
35 	case tea.KeyMsg:
36 		switch msg.Type {
37 		case tea.KeyCtrlC:
38@@ -245,17 +250,18 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
39 		m.user = msg
40 		m.username = username.NewModel(m.dbpool, m.user, m.sshUser)
41 		m.info = info.NewModel(m.cfg, m.urls, m.user)
42-		m.posts = posts.NewModel(m.cfg, m.urls, m.dbpool, m.user, m.st)
43 		m.keys = keys.NewModel(m.cfg, m.dbpool, m.user)
44 		m.tokens = tokens.NewModel(m.cfg, m.dbpool, m.user)
45 		m.createAccount = account.NewCreateModel(m.cfg, m.dbpool, m.publicKey)
46+
47+		perPage := math.Floor(float64(m.terminalSize.Height) / 10.0)
48+		m.posts = posts.NewModel(m.cfg, m.urls, m.dbpool, m.user, m.st, int(perPage))
49 	}
50 
51 	switch m.status {
52 	case statusInit:
53 		m.username = username.NewModel(m.dbpool, m.user, m.sshUser)
54 		m.info = info.NewModel(m.cfg, m.urls, m.user)
55-		m.posts = posts.NewModel(m.cfg, m.urls, m.dbpool, m.user, m.st)
56 		m.keys = keys.NewModel(m.cfg, m.dbpool, m.user)
57 		m.tokens = tokens.NewModel(m.cfg, m.dbpool, m.user)
58 		m.createAccount = account.NewCreateModel(m.cfg, m.dbpool, m.publicKey)
59@@ -264,6 +270,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
60 		} else {
61 			m.status = statusReady
62 		}
63+
64+		perPage := math.Floor(float64(m.terminalSize.Height) / 10.0)
65+		m.posts = posts.NewModel(m.cfg, m.urls, m.dbpool, m.user, m.st, int(perPage))
66 	}
67 
68 	m, cmd = updateChildren(msg, m)
69@@ -288,7 +297,10 @@ func updateChildren(msg tea.Msg, m model) (model, tea.Cmd) {
70 		cmd = newCmd
71 
72 		if m.posts.Exit {
73-			m.posts = posts.NewModel(m.cfg, m.urls, m.dbpool, m.user, m.st)
74+			perPage := math.Floor(float64(m.terminalSize.Height) / 10.0)
75+			m.posts = posts.NewModel(m.cfg, m.urls, m.dbpool, m.user, m.st, int(perPage))
76+
77+			m.posts = posts.NewModel(m.cfg, m.urls, m.dbpool, m.user, m.st, int(perPage))
78 			m.status = statusReady
79 		} else if m.posts.Quit {
80 			m.status = statusQuitting
81@@ -408,7 +420,7 @@ func (m model) errorView(err error) string {
82 }
83 
84 func (m model) View() string {
85-	w := m.terminalWidth - m.styles.App.GetHorizontalFrameSize()
86+	w := m.terminalSize.Width - m.styles.App.GetHorizontalFrameSize()
87 	s := m.styles.Logo.SetString(m.cfg.Domain).String() + "\n\n"
88 	switch m.status {
89 	case statusNoAccount:
M wish/cms/ui/keys/keys.go
+1, -1
1@@ -351,7 +351,7 @@ func keysView(m Model) string {
2 	// If there aren't enough keys to fill the view, fill the missing parts
3 	// with whitespace
4 	if len(slice) < m.pager.PerPage {
5-		for i := len(slice); i < keysPerPage; i++ {
6+		for i := len(slice); i < m.pager.PerPage; i++ {
7 			s += "\n\n\n"
8 		}
9 	}
M wish/cms/ui/posts/post_view.go
+45, -24
  1@@ -9,34 +9,46 @@ import (
  2 )
  3 
  4 type styledKey struct {
  5-	styles     common.Styles
  6-	date       string
  7-	gutter     string
  8-	postLabel  string
  9-	dateLabel  string
 10-	dateVal    string
 11-	title      string
 12-	urlLabel   string
 13-	url        string
 14-	views      int
 15-	viewsLabel string
 16+	styles         common.Styles
 17+	date           string
 18+	gutter         string
 19+	postLabel      string
 20+	dateLabel      string
 21+	dateVal        string
 22+	title          string
 23+	urlLabel       string
 24+	url            string
 25+	views          int
 26+	viewsLabel     string
 27+	model          Model
 28+	expiresAtLabel string
 29+	expiresAt      string
 30 }
 31 
 32 func (m Model) newStyledKey(styles common.Styles, post *db.Post, urls config.ConfigURL) styledKey {
 33 	publishAt := post.PublishAt
 34+
 35+	expiresAt := styles.LabelDim.Render("never")
 36+	if post.ExpiresAt != nil {
 37+		expiresAt = styles.LabelDim.Render(post.ExpiresAt.Format("02 Jan, 2006"))
 38+	}
 39+
 40 	// Default state
 41 	return styledKey{
 42-		styles:     styles,
 43-		gutter:     " ",
 44-		postLabel:  "post:",
 45-		date:       publishAt.String(),
 46-		dateLabel:  "publish_at:",
 47-		dateVal:    styles.LabelDim.Render(publishAt.Format("02 Jan, 2006")),
 48-		title:      post.Title,
 49-		urlLabel:   "url:",
 50-		url:        urls.PostURL(post.Username, post.Slug),
 51-		viewsLabel: "views:",
 52-		views:      post.Views,
 53+		styles:         styles,
 54+		gutter:         " ",
 55+		postLabel:      "post:",
 56+		date:           publishAt.String(),
 57+		dateLabel:      "publish_at:",
 58+		dateVal:        styles.LabelDim.Render(publishAt.Format("02 Jan, 2006")),
 59+		title:          post.Title,
 60+		urlLabel:       "url:",
 61+		url:            urls.PostURL(post.Username, post.Slug),
 62+		viewsLabel:     "views:",
 63+		views:          post.Views,
 64+		model:          m,
 65+		expiresAtLabel: "expires_at:",
 66+		expiresAt:      expiresAt,
 67 	}
 68 }
 69 
 70@@ -47,6 +59,7 @@ func (k *styledKey) selected() {
 71 	k.dateLabel = k.styles.Label.Render("publish_at:")
 72 	k.viewsLabel = k.styles.Label.Render("views:")
 73 	k.urlLabel = k.styles.Label.Render("url:")
 74+	k.expiresAtLabel = k.styles.Label.Render("expires_at:")
 75 }
 76 
 77 // Deleting state.
 78@@ -57,6 +70,7 @@ func (k *styledKey) deleting() {
 79 	k.urlLabel = k.styles.Delete.Render("url:")
 80 	k.viewsLabel = k.styles.Delete.Render("views:")
 81 	k.title = k.styles.DeleteDim.Render(k.title)
 82+	k.expiresAtLabel = k.styles.Delete.Render("expires_at:")
 83 }
 84 
 85 func (k styledKey) render(state postState) string {
 86@@ -66,11 +80,18 @@ func (k styledKey) render(state postState) string {
 87 	case postDeleting:
 88 		k.deleting()
 89 	}
 90-	return fmt.Sprintf(
 91-		"%s %s %s\n%s %s %s\n%s %s %d\n%s %s %s\n\n",
 92+
 93+	mainBody := fmt.Sprintf(
 94+		"%s %s %s\n%s %s %s\n%s %s %d\n%s %s %s\n",
 95 		k.gutter, k.postLabel, k.title,
 96 		k.gutter, k.dateLabel, k.dateVal,
 97 		k.gutter, k.viewsLabel, k.views,
 98 		k.gutter, k.urlLabel, k.url,
 99 	)
100+
101+	if k.model.cfg.Space == "pastes" {
102+		mainBody += fmt.Sprintf("%s %s %s\n", k.gutter, k.expiresAtLabel, k.expiresAt)
103+	}
104+
105+	return mainBody + "\n"
106 }
M wish/cms/ui/posts/posts.go
+7, -3
 1@@ -15,7 +15,7 @@ import (
 2 	"go.uber.org/zap"
 3 )
 4 
 5-const keysPerPage = 4
 6+const keysPerPage = 1
 7 
 8 type state int
 9 
10@@ -84,7 +84,7 @@ func (m *Model) UpdatePaging(msg tea.Msg) {
11 }
12 
13 // NewModel creates a new model with defaults.
14-func NewModel(cfg *config.ConfigCms, urls config.ConfigURL, dbpool db.DB, user *db.User, stor storage.ObjectStorage) Model {
15+func NewModel(cfg *config.ConfigCms, urls config.ConfigURL, dbpool db.DB, user *db.User, stor storage.ObjectStorage, perPage int) Model {
16 	logger := cfg.Logger
17 	st := common.DefaultStyles()
18 
19@@ -93,6 +93,10 @@ func NewModel(cfg *config.ConfigCms, urls config.ConfigURL, dbpool db.DB, user *
20 	p.Type = pager.Dots
21 	p.InactiveDot = st.InactivePagination.Render("•")
22 
23+	if perPage > 0 {
24+		p.PerPage = perPage
25+	}
26+
27 	return Model{
28 		cfg:     cfg,
29 		dbpool:  dbpool,
30@@ -275,7 +279,7 @@ func postsView(m Model) string {
31 	// If there aren't enough keys to fill the view, fill the missing parts
32 	// with whitespace
33 	if len(slice) < m.pager.PerPage {
34-		for i := len(slice); i < keysPerPage; i++ {
35+		for i := len(slice); i < m.pager.PerPage; i++ {
36 			s += "\n\n\n"
37 		}
38 	}
M wish/cms/ui/tokens/tokens.go
+1, -1
1@@ -319,7 +319,7 @@ func keysView(m Model) string {
2 	// If there aren't enough keys to fill the view, fill the missing parts
3 	// with whitespace
4 	if len(slice) < m.pager.PerPage {
5-		for i := len(slice); i < keysPerPage; i++ {
6+		for i := len(slice); i < m.pager.PerPage; i++ {
7 			s += "\n\n\n"
8 		}
9 	}