-
Notifications
You must be signed in to change notification settings - Fork 61
/
main.go
195 lines (164 loc) · 3.8 KB
/
main.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"context"
"fmt"
"net"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/charmbracelet/bubbles/textarea"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish"
"github.com/charmbracelet/wish/activeterm"
"github.com/charmbracelet/wish/bubbletea"
"github.com/charmbracelet/wish/logging"
"github.com/muesli/termenv"
)
const (
host = "localhost"
port = "23234"
)
// app contains a wish server and the list of running programs.
type app struct {
*ssh.Server
progs []*tea.Program
}
// send dispatches a message to all running programs.
func (a *app) send(msg tea.Msg) {
for _, p := range a.progs {
go p.Send(msg)
}
}
func newApp() *app {
a := new(app)
s, err := wish.NewServer(
wish.WithAddress(net.JoinHostPort(host, port)),
wish.WithHostKeyPath(".ssh/id_ed25519"),
wish.WithMiddleware(
bubbletea.MiddlewareWithProgramHandler(a.ProgramHandler, termenv.ANSI256),
activeterm.Middleware(),
logging.Middleware(),
),
)
if err != nil {
log.Error("Could not start server", "error", err)
}
a.Server = s
return a
}
func (a *app) Start() {
var err error
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
log.Info("Starting SSH server", "host", host, "port", port)
go func() {
if err = a.ListenAndServe(); err != nil {
log.Error("Could not start server", "error", err)
done <- nil
}
}()
<-done
log.Info("Stopping SSH server")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer func() { cancel() }()
if err := a.Shutdown(ctx); err != nil {
log.Error("Could not stop server", "error", err)
}
}
func (a *app) ProgramHandler(s ssh.Session) *tea.Program {
model := initialModel()
model.app = a
model.id = s.User()
p := tea.NewProgram(model, bubbletea.MakeOptions(s)...)
a.progs = append(a.progs, p)
return p
}
func main() {
app := newApp()
app.Start()
}
type (
errMsg error
chatMsg struct {
id string
text string
}
)
type model struct {
*app
viewport viewport.Model
messages []string
id string
textarea textarea.Model
senderStyle lipgloss.Style
err error
}
func initialModel() model {
ta := textarea.New()
ta.Placeholder = "Send a message..."
ta.Focus()
ta.Prompt = "┃ "
ta.CharLimit = 280
ta.SetWidth(30)
ta.SetHeight(3)
// Remove cursor line styling
ta.FocusedStyle.CursorLine = lipgloss.NewStyle()
ta.ShowLineNumbers = false
vp := viewport.New(30, 5)
vp.SetContent(`Welcome to the chat room!
Type a message and press Enter to send.`)
ta.KeyMap.InsertNewline.SetEnabled(false)
return model{
textarea: ta,
messages: []string{},
viewport: vp,
senderStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("5")),
err: nil,
}
}
func (m model) Init() tea.Cmd {
return textarea.Blink
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
tiCmd tea.Cmd
vpCmd tea.Cmd
)
m.textarea, tiCmd = m.textarea.Update(msg)
m.viewport, vpCmd = m.viewport.Update(msg)
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC, tea.KeyEsc:
return m, tea.Quit
case tea.KeyEnter:
m.app.send(chatMsg{
id: m.id,
text: m.textarea.Value(),
})
m.textarea.Reset()
}
case chatMsg:
m.messages = append(m.messages, m.senderStyle.Render(msg.id)+": "+msg.text)
m.viewport.SetContent(strings.Join(m.messages, "\n"))
m.viewport.GotoBottom()
// We handle errors just like any other message
case errMsg:
m.err = msg
return m, nil
}
return m, tea.Batch(tiCmd, vpCmd)
}
func (m model) View() string {
return fmt.Sprintf(
"%s\n\n%s",
m.viewport.View(),
m.textarea.View(),
) + "\n\n"
}