blob: e0cfbc3ddc3839ec925545273cae3a3520c3336b [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"
npujar16d6d5c2019-10-30 16:45:06 +053022
23 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Don Newton2bdfd3f2019-04-08 17:06:33 -040024)
25
26/*
27ProcessTable parses table structure and executes functions
28*/
29func ProcessTable(functionTable map[string]func(bool), inputPrompt string) {
30 keys := []string{}
31 for k := range functionTable {
32 keys = append(keys, k)
33 }
34 var b = make([]byte, 1)
35 input := ""
36 fmt.Print(inputPrompt)
37 for {
npujar16d6d5c2019-10-30 16:45:06 +053038 _, err := os.Stdin.Read(b)
39 if err != nil {
40 log.Errorw("unable-to-read-from-stdin-file", log.Fields{"error": err})
41 }
Don Newton2bdfd3f2019-04-08 17:06:33 -040042 char := string(b)
43 if char == "\t" || char == "\n" || char == "?" {
44 fmt.Println("")
45 ret, prompt := Test(input, keys)
46 if len(ret) == 1 {
47 input = ret[0]
48 if input == "exit" {
49 return
50 }
51 if char == "\n" {
52 Route(input, functionTable, true)
53 } else {
54 Route(input, functionTable, false)
55 }
56 input = ""
57 fmt.Print(inputPrompt)
58 } else if len(ret) == 0 {
59 input = ""
60 fmt.Println("\nInvalid Input ")
61 fmt.Print(inputPrompt)
62 } else if len(ret) == 0 {
63 } else {
64
65 fmt.Println(ret)
66 input = prompt
67 fmt.Print(inputPrompt)
68 fmt.Print(prompt)
69 }
70 } else if char == " " {
71 _, ok := functionTable[input]
72 if ok {
73 Route(input, functionTable, false)
74 fmt.Print(inputPrompt)
75 input = ""
76 } else {
77 ret, prompt := Test(input, keys)
78 if len(ret) == 1 {
79 input = ret[0]
80 Route(input, functionTable, false)
81 input = ""
82 fmt.Print(inputPrompt)
83 } else if len(ret) == 0 {
84 input = ""
85 fmt.Println("\nInvalid Input ")
86 fmt.Print(inputPrompt)
87 } else {
88
89 fmt.Println(ret)
90 input = prompt
91 fmt.Print(inputPrompt + input)
92 }
93 }
94
95 } else if b[0] == 127 || char == "\b" {
96 sz := len(input)
97
98 if sz > 0 {
99 fmt.Print("\b \b")
100 input = input[:sz-1]
101 }
102 } else {
103 fmt.Print(char)
104 input += char
105 }
106 }
107}