summaryrefslogtreecommitdiff
path: root/internal/client/client.go
blob: 938a265bba205be11ff965ecf9466c462b160aaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package client

import (
	"log"
	"reflect"

	tea "github.com/charmbracelet/bubbletea"
	"github.com/muesli/termenv"

	"github.com/kyren223/eko/internal/client/config"
	"github.com/kyren223/eko/internal/client/ui"
	"github.com/kyren223/eko/internal/client/ui/auth"
	"github.com/kyren223/eko/pkg/assert"
)

type BubbleTeaCloser struct {
	program *tea.Program
}

func (c BubbleTeaCloser) Close() error {
	c.program.Kill()
	return nil
}

func Run() {
	log.Println("client started")

	err := config.Load()
	if err != nil {
		log.Fatalf("Config file at '%v' was unable to load successfully\n%v\n", config.ConfigFile, err)
	}

	// HACK: Sets the terminal color and keeps it that way
	// I am not aware of a way to query the current background color (to revert this)
	// Ideally bubbletea will handle the setting/reventing, for now it's fine
	// It only changes the current pane so new terminal panes/windows are not affected.
	termenv.DefaultOutput().SetBackgroundColor(termenv.RGBColor(ui.BackgroundColor))

	program := tea.NewProgram(initialModel(), tea.WithAltScreen())
	assert.AddFlush(BubbleTeaCloser{program})

	// _, privKey, err := ed25519.GenerateKey(nil)
	// assert.NoError(err, "private key gen should not error")

	// gateway.Connect(context.Background(), program, privKey)
	if _, err := program.Run(); err != nil {
		log.Println(err)
	}
}

type model struct {
	model tea.Model
}

func initialModel() model {
	return model{auth.New()}
}

func (m model) Init() tea.Cmd {
	return m.model.Init()
}

func (m model) View() string {
	return m.model.View()
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch msg := msg.(type) {
	case tea.WindowSizeMsg:
		ui.Width, ui.Height = msg.Width, msg.Height
		return m, nil

	case ui.ModelTransition:
		log.Println("Transition model from", reflect.TypeOf(m.model).String(), "to", reflect.TypeOf(msg.Model).String())
		m.model = msg.Model
		return m, m.model.Init()

	default:
		var cmd tea.Cmd
		m.model, cmd = m.model.Update(msg)
		return m, cmd
	}
}