1package common
2
3import (
4 "context"
5
6 "github.com/aymanbagabas/go-osc52"
7 "github.com/charmbracelet/soft-serve/git"
8 "github.com/charmbracelet/soft-serve/server/config"
9 "github.com/charmbracelet/soft-serve/ui/keymap"
10 "github.com/charmbracelet/soft-serve/ui/styles"
11 "github.com/charmbracelet/ssh"
12 zone "github.com/lrstanley/bubblezone"
13)
14
15type contextKey struct {
16 name string
17}
18
19// Keys to use for context.Context.
20var (
21 ConfigKey = &contextKey{"config"}
22 RepoKey = &contextKey{"repo"}
23)
24
25// Common is a struct all components should embed.
26type Common struct {
27 ctx context.Context
28 Width, Height int
29 Styles *styles.Styles
30 KeyMap *keymap.KeyMap
31 Copy *osc52.Output
32 Zone *zone.Manager
33}
34
35// NewCommon returns a new Common struct.
36func NewCommon(ctx context.Context, copy *osc52.Output, width, height int) Common {
37 if ctx == nil {
38 ctx = context.TODO()
39 }
40 return Common{
41 ctx: ctx,
42 Width: width,
43 Height: height,
44 Copy: copy,
45 Styles: styles.DefaultStyles(),
46 KeyMap: keymap.DefaultKeyMap(),
47 Zone: zone.New(),
48 }
49}
50
51// SetValue sets a value in the context.
52func (c *Common) SetValue(key, value interface{}) {
53 c.ctx = context.WithValue(c.ctx, key, value)
54}
55
56// SetSize sets the width and height of the common struct.
57func (c *Common) SetSize(width, height int) {
58 c.Width = width
59 c.Height = height
60}
61
62// Config returns the server config.
63func (c *Common) Config() *config.Config {
64 v := c.ctx.Value(ConfigKey)
65 if cfg, ok := v.(*config.Config); ok {
66 return cfg
67 }
68 return nil
69}
70
71// Repo returns the repository.
72func (c *Common) Repo() *git.Repository {
73 v := c.ctx.Value(RepoKey)
74 if r, ok := v.(*git.Repository); ok {
75 return r
76 }
77 return nil
78}
79
80// PublicKey returns the public key.
81func (c *Common) PublicKey() ssh.PublicKey {
82 v := c.ctx.Value(ssh.ContextKeyPublicKey)
83 if p, ok := v.(ssh.PublicKey); ok {
84 return p
85 }
86 return nil
87}