Skip to content

Commit 3ab50ee

Browse files
committed
refactor: sshchat.getPrompt() -> message.User.Prompt()
1 parent 10cc44c commit 3ab50ee

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

chat/message/user.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,16 @@ func (u *User) Send(m Message) error {
186186
return nil
187187
}
188188

189+
// Prompt renders a theme-colorized prompt string.
190+
func (u *User) Prompt() string {
191+
name := u.Name()
192+
cfg := u.Config()
193+
if cfg.Theme != nil {
194+
name = cfg.Theme.ColorName(u)
195+
}
196+
return fmt.Sprintf("[%s] ", name)
197+
}
198+
189199
// Container for per-user configurations.
190200
type UserConfig struct {
191201
Highlight *regexp.Regexp

host.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ import (
1818

1919
const maxInputLength int = 1024
2020

21-
// getPrompt will render the terminal prompt string based on the user.
22-
func getPrompt(user *message.User) string {
23-
name := user.Name()
24-
cfg := user.Config()
25-
if cfg.Theme != nil {
26-
name = cfg.Theme.ColorName(user)
27-
}
28-
return fmt.Sprintf("[%s] ", name)
29-
}
30-
3121
// Host is the bridge between sshd and chat modules
3222
// TODO: Should be easy to add support for multiple rooms, if we want.
3323
type Host struct {
@@ -124,7 +114,7 @@ func (h *Host) Connect(term *sshd.Terminal) {
124114
}
125115

126116
// Successfully joined.
127-
term.SetPrompt(getPrompt(user))
117+
term.SetPrompt(user.Prompt())
128118
term.AutoCompleteCallback = h.AutoCompleteFunction(user)
129119
user.SetHighlight(user.Name())
130120

@@ -172,7 +162,7 @@ func (h *Host) Connect(term *sshd.Terminal) {
172162
//
173163
// FIXME: This is hacky, how do we improve the API to allow for
174164
// this? Chat module shouldn't know about terminals.
175-
term.SetPrompt(getPrompt(user))
165+
term.SetPrompt(user.Prompt())
176166
user.SetHighlight(user.Name())
177167
}
178168
}

host_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestHostGetPrompt(t *testing.T) {
2929

3030
u := message.NewUser(&identity{id: "foo"})
3131

32-
actual = getPrompt(u)
32+
actual = u.Prompt()
3333
expected = "[foo] "
3434
if actual != expected {
3535
t.Errorf("Got: %q; Expected: %q", actual, expected)
@@ -38,7 +38,7 @@ func TestHostGetPrompt(t *testing.T) {
3838
u.SetConfig(message.UserConfig{
3939
Theme: &message.Themes[0],
4040
})
41-
actual = getPrompt(u)
41+
actual = u.Prompt()
4242
expected = "[\033[38;05;88mfoo\033[0m] "
4343
if actual != expected {
4444
t.Errorf("Got: %q; Expected: %q", actual, expected)

0 commit comments

Comments
 (0)