refactor: sshchat.getPrompt() -> message.User.Prompt()

This commit is contained in:
Andrey Petrov 2016-09-01 16:53:29 -04:00
parent b45efc0dae
commit f64b2a3607
3 changed files with 14 additions and 14 deletions

View File

@ -186,6 +186,16 @@ func (u *User) Send(m Message) error {
return nil return nil
} }
// Prompt renders a theme-colorized prompt string.
func (u *User) Prompt() string {
name := u.Name()
cfg := u.Config()
if cfg.Theme != nil {
name = cfg.Theme.ColorName(u)
}
return fmt.Sprintf("[%s] ", name)
}
// Container for per-user configurations. // Container for per-user configurations.
type UserConfig struct { type UserConfig struct {
Highlight *regexp.Regexp Highlight *regexp.Regexp

14
host.go
View File

@ -18,16 +18,6 @@ import (
const maxInputLength int = 1024 const maxInputLength int = 1024
// getPrompt will render the terminal prompt string based on the user.
func getPrompt(user *message.User) string {
name := user.Name()
cfg := user.Config()
if cfg.Theme != nil {
name = cfg.Theme.ColorName(user)
}
return fmt.Sprintf("[%s] ", name)
}
// Host is the bridge between sshd and chat modules // Host is the bridge between sshd and chat modules
// TODO: Should be easy to add support for multiple rooms, if we want. // TODO: Should be easy to add support for multiple rooms, if we want.
type Host struct { type Host struct {
@ -124,7 +114,7 @@ func (h *Host) Connect(term *sshd.Terminal) {
} }
// Successfully joined. // Successfully joined.
term.SetPrompt(getPrompt(user)) term.SetPrompt(user.Prompt())
term.AutoCompleteCallback = h.AutoCompleteFunction(user) term.AutoCompleteCallback = h.AutoCompleteFunction(user)
user.SetHighlight(user.Name()) user.SetHighlight(user.Name())
@ -172,7 +162,7 @@ func (h *Host) Connect(term *sshd.Terminal) {
// //
// FIXME: This is hacky, how do we improve the API to allow for // FIXME: This is hacky, how do we improve the API to allow for
// this? Chat module shouldn't know about terminals. // this? Chat module shouldn't know about terminals.
term.SetPrompt(getPrompt(user)) term.SetPrompt(user.Prompt())
user.SetHighlight(user.Name()) user.SetHighlight(user.Name())
} }
} }

View File

@ -29,7 +29,7 @@ func TestHostGetPrompt(t *testing.T) {
u := message.NewUser(&identity{id: "foo"}) u := message.NewUser(&identity{id: "foo"})
actual = getPrompt(u) actual = u.Prompt()
expected = "[foo] " expected = "[foo] "
if actual != expected { if actual != expected {
t.Errorf("Got: %q; Expected: %q", actual, expected) t.Errorf("Got: %q; Expected: %q", actual, expected)
@ -38,7 +38,7 @@ func TestHostGetPrompt(t *testing.T) {
u.SetConfig(message.UserConfig{ u.SetConfig(message.UserConfig{
Theme: &message.Themes[0], Theme: &message.Themes[0],
}) })
actual = getPrompt(u) actual = u.Prompt()
expected = "[\033[38;05;88mfoo\033[0m] " expected = "[\033[38;05;88mfoo\033[0m] "
if actual != expected { if actual != expected {
t.Errorf("Got: %q; Expected: %q", actual, expected) t.Errorf("Got: %q; Expected: %q", actual, expected)