-
Notifications
You must be signed in to change notification settings - Fork 37
/
cli_manager.go
93 lines (86 loc) · 2.18 KB
/
cli_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/
package cli
import (
"fmt"
"strings"
)
type Cli interface {
Output() bool
ReadLine() (line string, exit bool, err error)
Interactive() bool
SetRespError(msg string)
GetRespError() string
SetSpace(string)
GetSpace() string
PlayingData(bool)
IsPlayingData() bool
Close()
}
type status struct {
// prompt
historyFile string
user string
space string
respErr string
playingData bool
promptLen int
promptColor int
// multi-line seperated by '\' or enclosed in triple quotes
line string
// tripleQuotes has a higher priority than backSlash
joinedByTripleQuotes bool
joinedByBackSlash bool
}
func (stat *status) checkJoined(input string) {
var pureInput = strings.TrimSpace(input)
var tripleQuotesFound = len(pureInput) == 3 && (pureInput == "\"\"\"" || pureInput == "'''")
var backSlashFound = len(input) >= 1 && input[len(input)-1] == 92 // '\'
if stat.joinedByTripleQuotes {
if tripleQuotesFound {
stat.joinedByTripleQuotes = false
} else {
if backSlashFound { // backslash can be used in a block enclosed by threequotes
stat.line += string(input[:len(input)-1])
} else {
stat.line += input
stat.line += " "
}
}
} else if stat.joinedByBackSlash {
if backSlashFound {
stat.line += string(input[:len(input)-1])
} else {
stat.line += input
stat.joinedByBackSlash = false
}
} else {
if tripleQuotesFound {
stat.line = ""
stat.joinedByTripleQuotes = true
} else if backSlashFound {
stat.line = string(input[:len(input)-1])
stat.joinedByBackSlash = true
} else {
stat.line = input
}
}
}
func (stat *status) nebulaPrompt() string {
//ttyColor := prompter.color + 31
//prompter.color = (prompter.color + 1) % 6
prompt := ""
//prompt += fmt.Sprintf("\033[%v;1m", ttyColor)
if stat.joinedByTripleQuotes || stat.joinedByBackSlash {
prompt += strings.Repeat(" ", stat.promptLen-3)
prompt += "-> "
} else {
promptString := fmt.Sprintf("(%s@nebula) [%s]> ", stat.user, stat.space)
stat.promptLen = len(promptString)
prompt += promptString
}
//prompt += "\033[0m"
return prompt
}