| 67 | } |
| 68 | |
| 69 | func StartRawConsole(srv *server.Server) { |
| 70 | var char [1]byte |
| 71 | var currentLine string |
| 72 | |
| 73 | var previousLines []string |
| 74 | var previousLinesIndex int |
| 75 | |
| 76 | var currentLineIndex int |
| 77 | |
| 78 | charl: |
| 79 | for { |
| 80 | os.Stdin.Read(char[:]) |
| 81 | if char[0] == 27 { |
| 82 | os.Stdin.Read(char[:]) |
| 83 | if char[0] != 91 { |
| 84 | continue |
| 85 | } |
| 86 | os.Stdin.Read(char[:]) |
| 87 | |
| 88 | switch char[0] { |
| 89 | case 'A': //up |
| 90 | if len(previousLines[previousLinesIndex:]) == 0 { |
| 91 | continue |
| 92 | } |
| 93 | l := previousLines[previousLinesIndex] |
| 94 | previousLinesIndex++ |
| 95 | |
| 96 | fmt.Print("\r> ", l) |
| 97 | if l, l0 := len(currentLine), len(l); l > l0 { |
| 98 | fmt.Print(strings.Repeat(" ", l-l0)) |
| 99 | } |
| 100 | currentLineIndex = len(l) - 1 |
| 101 | currentLine = l |
| 102 | case 'B': //down |
| 103 | case 'C': //right |
| 104 | if currentLineIndex == len(currentLine) { |
| 105 | continue |
| 106 | } |
| 107 | fmt.Printf("%c", currentLine[currentLineIndex]) |
| 108 | currentLineIndex++ |
| 109 | case 'D': //left |
| 110 | if currentLineIndex == 0 { |
| 111 | continue |
| 112 | } |
| 113 | currentLineIndex-- |
| 114 | fmt.Print("\b") |
| 115 | } |
| 116 | continue |
| 117 | } |
| 118 | switch char[0] { |
| 119 | case '\b', 127: //backspace |
| 120 | if len(currentLine) == 0 { |
| 121 | continue |
| 122 | } |
| 123 | fmt.Print("\b \b") |
| 124 | currentLine = currentLine[:len(currentLine)-1] |
| 125 | currentLineIndex-- |
| 126 | case 3: //ctrl-c |