blob: 06ad8b8ad85747cec841d7e0ac78fdcece8fc469 [file] [log] [blame]
Don Newton2bdfd3f2019-04-08 17:06:33 -04001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15*/
16
17package util
18
19import (
20 "os"
21 "strings"
22)
23
24func Test(chars string, values []string) ([]string, string) {
25
26 var ret []string
27 for i := 0; i < len(values); i++ {
28 if strings.HasPrefix(values[i], chars) {
29 ret = append(ret, values[i])
30 }
31 }
32 if len(ret) == 0 {
33 return ret, ""
34 }
35 shortIndex := 0
36 if len(ret) > 1 {
37 for i := 0; i < len(ret); i++ {
38 if len(ret[i]) < len(ret[shortIndex]) {
39 shortIndex = i
40 }
41 }
42 }
43 for i := len(chars); i < len(ret[shortIndex]); i++ {
44 inAllWords := true
45 for j := 0; j < len(ret); j++ {
46 inAllWords = inAllWords && ret[j][i] == ret[shortIndex][i]
47 }
48 if inAllWords {
49 chars += string(ret[shortIndex][i])
50 } else {
51 return ret, chars
52 }
53
54 }
55
56 return ret, chars
57}
58
59func Route(command string, table map[string]func(bool), enterPressed bool) {
60 cmd := table[command]
61 cmd(enterPressed)
62
63}
64
65func Exit(notUsed bool) {
66 os.Exit(0)
67}