From f64b2a3607c0671e53474e785d89dd5624213d50 Mon Sep 17 00:00:00 2001 From: Andrey Petrov <andrey.petrov@shazow.net> Date: Thu, 1 Sep 2016 16:53:29 -0400 Subject: [PATCH] refactor: sshchat.getPrompt() -> message.User.Prompt() --- chat/message/user.go | 10 ++++++++++ host.go | 14 ++------------ host_test.go | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/chat/message/user.go b/chat/message/user.go index b56d68b..46bfb52 100644 --- a/chat/message/user.go +++ b/chat/message/user.go @@ -186,6 +186,16 @@ func (u *User) Send(m Message) error { 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. type UserConfig struct { Highlight *regexp.Regexp diff --git a/host.go b/host.go index 028d757..ca3425d 100644 --- a/host.go +++ b/host.go @@ -18,16 +18,6 @@ import ( 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 // TODO: Should be easy to add support for multiple rooms, if we want. type Host struct { @@ -124,7 +114,7 @@ func (h *Host) Connect(term *sshd.Terminal) { } // Successfully joined. - term.SetPrompt(getPrompt(user)) + term.SetPrompt(user.Prompt()) term.AutoCompleteCallback = h.AutoCompleteFunction(user) 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 // this? Chat module shouldn't know about terminals. - term.SetPrompt(getPrompt(user)) + term.SetPrompt(user.Prompt()) user.SetHighlight(user.Name()) } } diff --git a/host_test.go b/host_test.go index b0ec48a..405792e 100644 --- a/host_test.go +++ b/host_test.go @@ -29,7 +29,7 @@ func TestHostGetPrompt(t *testing.T) { u := message.NewUser(&identity{id: "foo"}) - actual = getPrompt(u) + actual = u.Prompt() expected = "[foo] " if actual != expected { t.Errorf("Got: %q; Expected: %q", actual, expected) @@ -38,7 +38,7 @@ func TestHostGetPrompt(t *testing.T) { u.SetConfig(message.UserConfig{ Theme: &message.Themes[0], }) - actual = getPrompt(u) + actual = u.Prompt() expected = "[\033[38;05;88mfoo\033[0m] " if actual != expected { t.Errorf("Got: %q; Expected: %q", actual, expected)