repos / pico

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

commit
c5c93c9
parent
8a5c190
author
Eric Bower
date
2024-05-16 16:54:41 +0000 UTC
refactor: use bubbletea's renderer fn
5 files changed,  +31, -88
M pgs/cli.go
+12, -7
 1@@ -15,7 +15,7 @@ import (
 2 	"github.com/picosh/pico/tui/common"
 3 )
 4 
 5-func projectTable(styles common.Styles, projects []*db.Project) *table.Table {
 6+func projectTable(styles common.Styles, projects []*db.Project, width int) *table.Table {
 7 	headers := []string{
 8 		"Name",
 9 		"Last Updated",
10@@ -42,12 +42,13 @@ func projectTable(styles common.Styles, projects []*db.Project) *table.Table {
11 	}
12 
13 	t := table.New().
14+		Width(width).
15 		Headers(headers...).
16 		Rows(data...)
17 	return t
18 }
19 
20-func getHelpText(styles common.Styles, userName string) string {
21+func getHelpText(styles common.Styles, userName string, width int) string {
22 	helpStr := "Commands: [help, stats, ls, rm, link, unlink, prune, retain, depends, acl]\n"
23 	helpStr += styles.Note.Render("NOTICE:") + " *must* append with `--write` for the changes to persist.\n"
24 
25@@ -97,7 +98,8 @@ func getHelpText(styles common.Styles, userName string) string {
26 	}
27 
28 	t := table.New().
29-		Border(lipgloss.HiddenBorder()).
30+		Width(width).
31+		Border(lipgloss.RoundedBorder()).
32 		Headers(headers...).
33 		Rows(data...)
34 
35@@ -113,6 +115,8 @@ type Cmd struct {
36 	Dbpool  db.DB
37 	Write   bool
38 	Styles  common.Styles
39+	Width   int
40+	Height  int
41 }
42 
43 func (c *Cmd) output(out string) {
44@@ -184,7 +188,7 @@ func (c *Cmd) RmProjectAssets(projectName string) error {
45 }
46 
47 func (c *Cmd) help() {
48-	c.output(getHelpText(c.Styles, c.User.Name))
49+	c.output(getHelpText(c.Styles, c.User.Name, c.Width))
50 }
51 
52 func (c *Cmd) stats(cfgMaxSize uint64) error {
53@@ -221,7 +225,8 @@ func (c *Cmd) stats(cfgMaxSize uint64) error {
54 	}
55 
56 	t := table.New().
57-		Border(lipgloss.HiddenBorder()).
58+		Width(c.Width).
59+		Border(lipgloss.RoundedBorder()).
60 		Headers(headers...).
61 		Rows(data)
62 	c.output(t.String())
63@@ -239,7 +244,7 @@ func (c *Cmd) ls() error {
64 		c.output("no projects found")
65 	}
66 
67-	t := projectTable(c.Styles, projects)
68+	t := projectTable(c.Styles, projects, c.Width)
69 	c.output(t.String())
70 
71 	return nil
72@@ -325,7 +330,7 @@ func (c *Cmd) depends(projectName string) error {
73 		return nil
74 	}
75 
76-	t := projectTable(c.Styles, projects)
77+	t := projectTable(c.Styles, projects, c.Width)
78 	c.output(t.String())
79 
80 	return nil
M pgs/wish.go
+15, -8
 1@@ -6,9 +6,9 @@ import (
 2 	"slices"
 3 	"strings"
 4 
 5-	"github.com/charmbracelet/lipgloss"
 6 	"github.com/charmbracelet/ssh"
 7 	"github.com/charmbracelet/wish"
 8+	bm "github.com/charmbracelet/wish/bubbletea"
 9 	"github.com/picosh/pico/db"
10 	uploadassets "github.com/picosh/pico/filehandlers/assets"
11 	"github.com/picosh/pico/shared"
12@@ -71,23 +71,28 @@ func WishMiddleware(handler *uploadassets.UploadAssetHandler) wish.Middleware {
13 
14 	return func(next ssh.Handler) ssh.Handler {
15 		return func(sesh ssh.Session) {
16-			_, _, activePty := sesh.Pty()
17-			if activePty {
18+			args := sesh.Command()
19+			if len(args) == 0 {
20 				next(sesh)
21 				return
22 			}
23 
24+			// default width and height when no pty
25+			width := 80
26+			height := 60
27+			pty, _, ok := sesh.Pty()
28+			if ok {
29+				width = pty.Window.Width
30+				height = pty.Window.Height
31+			}
32+
33 			user, err := getUser(sesh, dbpool)
34 			if err != nil {
35 				utils.ErrorHandler(sesh, err)
36 				return
37 			}
38 
39-			args := sesh.Command()
40-
41-			renderer := lipgloss.NewRenderer(sesh)
42-			// this might be dangerous but going with it for now
43-			// renderer.SetColorProfile(termenv.ANSI256)
44+			renderer := bm.MakeRenderer(sesh)
45 			styles := common.DefaultStyles(renderer)
46 
47 			opts := Cmd{
48@@ -98,6 +103,8 @@ func WishMiddleware(handler *uploadassets.UploadAssetHandler) wish.Middleware {
49 				Dbpool:  dbpool,
50 				Write:   false,
51 				Styles:  styles,
52+				Width:   width,
53+				Height:  height,
54 			}
55 
56 			cmd := strings.TrimSpace(args[0])
M tui/cms.go
+1, -3
 1@@ -5,7 +5,6 @@ import (
 2 	"io"
 3 
 4 	tea "github.com/charmbracelet/bubbletea"
 5-	"github.com/charmbracelet/lipgloss"
 6 	"github.com/charmbracelet/ssh"
 7 	"github.com/charmbracelet/wish"
 8 	bm "github.com/charmbracelet/wish/bubbletea"
 9@@ -86,8 +85,7 @@ func CmsMiddleware(cfg *shared.ConfigSite) bm.Handler {
10 
11 		sshUser := sesh.User()
12 		dbpool := postgres.NewDB(cfg.DbURL, cfg.Logger)
13-		renderer := lipgloss.NewRenderer(sesh)
14-		renderer.SetOutput(common.OutputFromSession(sesh))
15+		renderer := bm.MakeRenderer(sesh)
16 		styles := common.DefaultStyles(renderer)
17 
18 		m := model{
D tui/common/output.go
+0, -67
 1@@ -1,67 +0,0 @@
 2-package common
 3-
 4-import (
 5-	"fmt"
 6-	"os"
 7-	"strings"
 8-
 9-	"github.com/charmbracelet/ssh"
10-	"github.com/charmbracelet/wish"
11-	"github.com/kr/pty"
12-	"github.com/muesli/termenv"
13-)
14-
15-// Bridge Wish and Termenv so we can query for a user's terminal capabilities.
16-type sshOutput struct {
17-	ssh.Session
18-	tty *os.File
19-}
20-
21-func (s *sshOutput) Write(p []byte) (int, error) {
22-	return s.Session.Write(p)
23-}
24-
25-func (s *sshOutput) Read(p []byte) (int, error) {
26-	return s.Session.Read(p)
27-}
28-
29-func (s *sshOutput) Fd() uintptr {
30-	return s.tty.Fd()
31-}
32-
33-type sshEnviron struct {
34-	environ []string
35-}
36-
37-func (s *sshEnviron) Getenv(key string) string {
38-	for _, v := range s.environ {
39-		if strings.HasPrefix(v, key+"=") {
40-			return v[len(key)+1:]
41-		}
42-	}
43-	return ""
44-}
45-
46-func (s *sshEnviron) Environ() []string {
47-	return s.environ
48-}
49-
50-// Create a termenv.Output from the session.
51-func OutputFromSession(sesh ssh.Session) *termenv.Output {
52-	sshPty, _, _ := sesh.Pty()
53-	_, tty, err := pty.Open()
54-	if err != nil {
55-		wish.Fatalln(sesh, err)
56-		return nil
57-	}
58-	o := &sshOutput{
59-		Session: sesh,
60-		tty:     tty,
61-	}
62-	environ := sesh.Environ()
63-	environ = append(environ, fmt.Sprintf("TERM=%s", sshPty.Term))
64-	e := &sshEnviron{environ: environ}
65-	// We need to use unsafe mode here because the ssh session is not running
66-	// locally and we already know that the session is a TTY.
67-	return termenv.NewOutput(o, termenv.WithUnsafe(), termenv.WithEnvironment(e))
68-}
M wish/mdw.go
+3, -3
 1@@ -3,9 +3,9 @@ package wish
 2 import (
 3 	"fmt"
 4 
 5-	"github.com/charmbracelet/lipgloss"
 6 	"github.com/charmbracelet/ssh"
 7 	"github.com/charmbracelet/wish"
 8+	bm "github.com/charmbracelet/wish/bubbletea"
 9 	"github.com/picosh/pico/tui/common"
10 )
11 
12@@ -16,8 +16,8 @@ func SessionMessage(sesh ssh.Session, msg string) {
13 func DeprecatedNotice() wish.Middleware {
14 	return func(next ssh.Handler) ssh.Handler {
15 		return func(sesh ssh.Session) {
16-			renderer := lipgloss.NewRenderer(sesh)
17-			renderer.SetOutput(common.OutputFromSession(sesh))
18+
19+			renderer := bm.MakeRenderer(sesh)
20 			styles := common.DefaultStyles(renderer)
21 
22 			msg := fmt.Sprintf(