-
Notifications
You must be signed in to change notification settings - Fork 6
/
prompt.go
71 lines (54 loc) · 1.87 KB
/
prompt.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
package console
import (
"fmt"
"strings"
"github.com/reeflective/readline"
)
// Prompt - A prompt is a set of functions that return the strings to print
// for each prompt type. The console will call these functions to retrieve
// the prompt strings to print. Each menu has its own prompt.
type Prompt struct {
Primary func() string // Primary is the main prompt.
Secondary func() string // Secondary is the prompt used when the user is typing a multi-line command.
Transient func() string // Transient is used if the console shell is configured to be transient.
Right func() string // Right is the prompt printed on the right side of the screen.
Tooltip func(word string) string // Tooltip is used to hint on the root command, replacing right prompts if not empty.
console *Console
}
func newPrompt(app *Console) *Prompt {
prompt := &Prompt{console: app}
prompt.Primary = func() string {
promptStr := app.name
menu := app.activeMenu()
if menu.name == "" {
return promptStr + " > "
}
promptStr += fmt.Sprintf(" [%s]", menu.name)
// If the buffered command output is not empty,
// add a special status indicator to the prompt.
if strings.TrimSpace(menu.out.String()) != "" {
promptStr += " $(...)"
}
return promptStr + " > "
}
return prompt
}
// bind reassigns the prompt printing functions to the shell helpers.
func (p *Prompt) bind(shell *readline.Shell) {
prompt := shell.Prompt
// If the user has bound its own primary prompt and the shell
// must leave a newline after command/log output, wrap its function
// to add a newline before the prompt.
primary := func() string {
if p.Primary == nil {
return ""
}
prompt := p.Primary()
return prompt
}
prompt.Primary(primary)
prompt.Right(p.Right)
prompt.Secondary(p.Secondary)
prompt.Transient(p.Transient)
prompt.Tooltip(p.Tooltip)
}