blob: 52bf6bb7688bbf6365632093a163606cd03957f7 [file] [log] [blame]
Don Newton2bdfd3f2019-04-08 17:06:33 -04001/*
Kent Hagerman0ab4cb22019-04-24 13:13:35 -04002 * Copyright 2018-present Open Networking Foundation
Don Newton2bdfd3f2019-04-08 17:06:33 -04003
Kent Hagerman0ab4cb22019-04-24 13:13:35 -04004 * 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
Don Newton2bdfd3f2019-04-08 17:06:33 -04007
Kent Hagerman0ab4cb22019-04-24 13:13:35 -04008 * http://www.apache.org/licenses/LICENSE-2.0
Don Newton2bdfd3f2019-04-08 17:06:33 -04009
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040010 * 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 */
Don Newton2bdfd3f2019-04-08 17:06:33 -040016
17package util
18
19import (
20 "fmt"
21 "os"
22)
23
24/*
25ProcessTable parses table structure and executes functions
26*/
27func ProcessTable(functionTable map[string]func(bool), inputPrompt string) {
28 keys := []string{}
29 for k := range functionTable {
30 keys = append(keys, k)
31 }
32 var b = make([]byte, 1)
33 input := ""
34 fmt.Print(inputPrompt)
35 for {
36 os.Stdin.Read(b)
37 char := string(b)
38 if char == "\t" || char == "\n" || char == "?" {
39 fmt.Println("")
40 ret, prompt := Test(input, keys)
41 if len(ret) == 1 {
42 input = ret[0]
43 if input == "exit" {
44 return
45 }
46 if char == "\n" {
47 Route(input, functionTable, true)
48 } else {
49 Route(input, functionTable, false)
50 }
51 input = ""
52 fmt.Print(inputPrompt)
53 } else if len(ret) == 0 {
54 input = ""
55 fmt.Println("\nInvalid Input ")
56 fmt.Print(inputPrompt)
57 } else if len(ret) == 0 {
58 } else {
59
60 fmt.Println(ret)
61 input = prompt
62 fmt.Print(inputPrompt)
63 fmt.Print(prompt)
64 }
65 } else if char == " " {
66 _, ok := functionTable[input]
67 if ok {
68 Route(input, functionTable, false)
69 fmt.Print(inputPrompt)
70 input = ""
71 } else {
72 ret, prompt := Test(input, keys)
73 if len(ret) == 1 {
74 input = ret[0]
75 Route(input, functionTable, false)
76 input = ""
77 fmt.Print(inputPrompt)
78 } else if len(ret) == 0 {
79 input = ""
80 fmt.Println("\nInvalid Input ")
81 fmt.Print(inputPrompt)
82 } else {
83
84 fmt.Println(ret)
85 input = prompt
86 fmt.Print(inputPrompt + input)
87 }
88 }
89
90 } else if b[0] == 127 || char == "\b" {
91 sz := len(input)
92
93 if sz > 0 {
94 fmt.Print("\b \b")
95 input = input[:sz-1]
96 }
97 } else {
98 fmt.Print(char)
99 input += char
100 }
101 }
102}