Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 1 | /* |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 3 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 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 |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 7 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 9 | |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 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 | */ |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 16 | |
| 17 | package util |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "os" |
npujar | 16d6d5c | 2019-10-30 16:45:06 +0530 | [diff] [blame] | 22 | |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 23 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | /* |
| 27 | ProcessTable parses table structure and executes functions |
| 28 | */ |
| 29 | func 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 { |
npujar | 16d6d5c | 2019-10-30 16:45:06 +0530 | [diff] [blame] | 38 | _, err := os.Stdin.Read(b) |
| 39 | if err != nil { |
| 40 | log.Errorw("unable-to-read-from-stdin-file", log.Fields{"error": err}) |
| 41 | } |
Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 42 | 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 | } |