diff options
author | Hubert Hirtz <hubert@hirtz.pm> | 2020-11-19 16:08:38 +0100 |
---|---|---|
committer | Hubert Hirtz <hubert@hirtz.pm> | 2020-11-19 16:08:38 +0100 |
commit | 83e0e82ad08adeb910c13e4a53336cbf422356c9 (patch) | |
tree | 662ab140d62759c03dfef7a9be5eb6234d0c987e | |
parent | Move editor view when completing text (diff) |
Add completion for /msg
-rw-r--r-- | app.go | 1 | ||||
-rw-r--r-- | completions.go | 38 | ||||
-rw-r--r-- | irc/states.go | 8 |
3 files changed, 47 insertions, 0 deletions
@@ -460,6 +460,7 @@ func (app *App) completions(cursorIdx int, text []rune) []ui.Completion { cs = app.completionsChannelTopic(cs, cursorIdx, text) cs = app.completionsChannelMembers(cs, cursorIdx, text) } + cs = app.completionsMsg(cs, cursorIdx, text) if cs != nil { cs = append(cs, ui.Completion{ diff --git a/completions.go b/completions.go index 6fac5e8..db8a7f7 100644 --- a/completions.go +++ b/completions.go @@ -56,6 +56,44 @@ func (app *App) completionsChannelTopic(cs []ui.Completion, cursorIdx int, text return cs } +func (app *App) completionsMsg(cs []ui.Completion, cursorIdx int, text []rune) []ui.Completion { + if !hasPrefix(text, []rune("/msg ")) { + return cs + } + // Check if the first word (target) is already written and complete (in + // which case we don't have completions to provide). + var word string + hasMetALetter := false + for i := 5; i < cursorIdx; i += 1 { + if hasMetALetter && text[i] == ' ' { + return cs + } + if !hasMetALetter && text[i] != ' ' { + word = app.s.Casemap(string(text[i:cursorIdx])) + hasMetALetter = true + } + } + if word == "" { + return cs + } + for _, user := range app.s.Users() { + if strings.HasPrefix(app.s.Casemap(user), word) { + nickComp := append([]rune(user), ' ') + c := make([]rune, len(text)+5+len(nickComp)-cursorIdx) + copy(c[:5], []rune("/msg ")) + copy(c[5:], nickComp) + if cursorIdx < len(text) { + copy(c[5+len(nickComp):], text[cursorIdx:]) + } + cs = append(cs, ui.Completion{ + Text: c, + CursorIdx: 5 + len(nickComp), + }) + } + } + return cs +} + func hasPrefix(s, prefix []rune) bool { return len(prefix) <= len(s) && equal(prefix, s[:len(prefix)]) } diff --git a/irc/states.go b/irc/states.go index c702c43..98b05d4 100644 --- a/irc/states.go +++ b/irc/states.go @@ -265,6 +265,14 @@ func (s *Session) Casemap(name string) string { return CasemapASCII(name) } +func (s *Session) Users() []string { + users := make([]string, 0, len(s.users)) + for _, u := range s.users { + users = append(users, u.Name.Name) + } + return users +} + func (s *Session) Names(channel string) []Member { var names []Member if c, ok := s.channels[s.Casemap(channel)]; ok { |