Debian Codegraph @

ui/autocomplete.go

raw symbols SCIP
1 // gomuks - A terminal Matrix client written in Go.
2 // Copyright (C) 2020 Tulir Asokan
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 package ui
18
19 import (
20 "fmt"
21 "io/ioutil"
22 "path/filepath"
23 "strings"
24 )
25
26 func autocompleteFile(cmd *CommandAutocomplete) (completions []string, newText string) {
27 inputPath, err := filepath.Abs(cmd.RawArgs)
28 if err != nil {
29 return
30 }
31
32 var searchNamePrefix, searchDir string
33 if strings.HasSuffix(cmd.RawArgs, "/") {
34 searchDir = inputPath
35 } else {
36 searchNamePrefix = filepath.Base(inputPath)
37 searchDir = filepath.Dir(inputPath)
38 }
39 files, err := ioutil.ReadDir(searchDir)
40 if err != nil {
41 return
42 }
43 for _, file := range files {
44 name := file.Name()
45 if !strings.HasPrefix(name, searchNamePrefix) || (name[0] == '.' && searchNamePrefix == "") {
46 continue
47 }
48 fullPath := filepath.Join(searchDir, name)
49 if file.IsDir() {
50 fullPath += "/"
51 }
52 completions = append(completions, fullPath)
53 }
54 if len(completions) == 1 {
55 newText = fmt.Sprintf("/%s %s", cmd.OrigCommand, completions[0])
56 }
57 return
58 }
59
60 func autocompleteToggle(cmd *CommandAutocomplete) (completions []string, newText string) {
61 completions = make([]string, 0, len(toggleMsg))
62 for k := range toggleMsg {
63 if strings.HasPrefix(k, cmd.RawArgs) {
64 completions = append(completions, k)
65 }
66 }
67 if len(completions) == 1 {
68 newText = fmt.Sprintf("/%s %s", cmd.OrigCommand, completions[0])
69 }
70 return
71 }
72
73 var staticPowerLevelKeys = []string{"ban", "kick", "redact", "invite", "state_default", "events_default", "users_default"}
74
75 func autocompletePowerLevel(cmd *CommandAutocomplete) (completions []string, newText string) {
76 if len(cmd.Args) > 1 {
77 return
78 }
79 for _, staticKey := range staticPowerLevelKeys {
80 if strings.HasPrefix(staticKey, cmd.RawArgs) {
81 completions = append(completions, staticKey)
82 }
83 }
84 for _, cpl := range cmd.Room.AutocompleteUser(cmd.RawArgs) {
85 completions = append(completions, cpl.id)
86 }
87 return
88 }