/
prompt.go
180 lines (154 loc) · 4.67 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package edit
import (
"io"
"os"
"os/user"
"sync"
"time"
"github.com/markusbkk/elvish/pkg/cli"
"github.com/markusbkk/elvish/pkg/cli/prompt"
"github.com/markusbkk/elvish/pkg/eval"
"github.com/markusbkk/elvish/pkg/eval/vals"
"github.com/markusbkk/elvish/pkg/eval/vars"
"github.com/markusbkk/elvish/pkg/fsutil"
"github.com/markusbkk/elvish/pkg/ui"
)
//elvdoc:var prompt
//
// See [Prompts](#prompts).
//elvdoc:var -prompt-eagerness
//
// See [Prompt Eagerness](#prompt-eagerness).
//elvdoc:var prompt-stale-threshold
//
// See [Stale Prompt](#stale-prompt).
//elvdoc:var prompt-stale-transformer.
//
// See [Stale Prompt](#stale-prompt).
//elvdoc:var rprompt
//
// See [Prompts](#prompts).
//elvdoc:var -rprompt-eagerness
//
// See [Prompt Eagerness](#prompt-eagerness).
//elvdoc:var rprompt-stale-threshold
//
// See [Stale Prompt](#stale-prompt).
//elvdoc:var rprompt-stale-transformer.
//
// See [Stale Prompt](#stale-prompt).
//elvdoc:var rprompt-persistent
//
// See [RPrompt Persistency](#rprompt-persistency).
func initPrompts(appSpec *cli.AppSpec, nt notifier, ev *eval.Evaler, nb eval.NsBuilder) {
promptVal, rpromptVal := getDefaultPromptVals()
initPrompt(&appSpec.Prompt, "prompt", promptVal, nt, ev, nb)
initPrompt(&appSpec.RPrompt, "rprompt", rpromptVal, nt, ev, nb)
rpromptPersistentVar := newBoolVar(false)
appSpec.RPromptPersistent = func() bool { return rpromptPersistentVar.Get().(bool) }
nb.AddVar("rprompt-persistent", rpromptPersistentVar)
}
func initPrompt(p *cli.Prompt, name string, val eval.Callable, nt notifier, ev *eval.Evaler, nb eval.NsBuilder) {
computeVar := vars.FromPtr(&val)
nb.AddVar(name, computeVar)
eagernessVar := newIntVar(5)
nb.AddVar("-"+name+"-eagerness", eagernessVar)
staleThresholdVar := newFloatVar(0.2)
nb.AddVar(name+"-stale-threshold", staleThresholdVar)
staleTransformVar := newFnVar(
eval.NewGoFn("<default stale transform>", defaultStaleTransform))
nb.AddVar(name+"-stale-transform", staleTransformVar)
*p = prompt.New(prompt.Config{
Compute: func() ui.Text {
return callForStyledText(nt, ev, name, computeVar.Get().(eval.Callable))
},
Eagerness: func() int { return eagernessVar.GetRaw().(int) },
StaleThreshold: func() time.Duration {
seconds := staleThresholdVar.GetRaw().(float64)
return time.Duration(seconds * float64(time.Second))
},
StaleTransform: func(original ui.Text) ui.Text {
return callForStyledText(nt, ev, name+" stale transform", staleTransformVar.Get().(eval.Callable), original)
},
})
}
func getDefaultPromptVals() (prompt, rprompt eval.Callable) {
user, userErr := user.Current()
isRoot := userErr == nil && user.Uid == "0"
username := "???"
if userErr == nil {
username = user.Username
}
hostname, err := os.Hostname()
if err != nil {
hostname = "???"
}
return getDefaultPrompt(isRoot), getDefaultRPrompt(username, hostname)
}
func getDefaultPrompt(isRoot bool) eval.Callable {
p := ui.T("> ")
if isRoot {
p = ui.T("# ", ui.FgRed)
}
return eval.NewGoFn("default prompt", func() ui.Text {
return ui.Concat(ui.T(fsutil.Getwd()), p)
})
}
func getDefaultRPrompt(username, hostname string) eval.Callable {
rp := ui.T(username+"@"+hostname, ui.Inverse)
return eval.NewGoFn("default rprompt", func() ui.Text {
return rp
})
}
func defaultStaleTransform(original ui.Text) ui.Text {
return ui.StyleText(original, ui.Inverse)
}
// Calls a function with the given arguments and closed input, and concatenates
// its outputs to a styled text. Used to call prompts and stale transformers.
func callForStyledText(nt notifier, ev *eval.Evaler, ctx string, fn eval.Callable, args ...interface{}) ui.Text {
var (
result ui.Text
resultMutex sync.Mutex
)
add := func(v interface{}) {
resultMutex.Lock()
defer resultMutex.Unlock()
newResult, err := result.Concat(v)
if err != nil {
nt.notifyf("invalid output type from prompt: %s", vals.Kind(v))
} else {
result = newResult.(ui.Text)
}
}
// Value outputs are concatenated.
valuesCb := func(ch <-chan interface{}) {
for v := range ch {
add(v)
}
}
// Byte output is added to the prompt as a single unstyled text.
bytesCb := func(r *os.File) {
allBytes, err := io.ReadAll(r)
if err != nil {
nt.notifyf("error reading prompt byte output: %v", err)
}
if len(allBytes) > 0 {
add(ui.ParseSGREscapedText(string(allBytes)))
}
}
port1, done1, err := eval.PipePort(valuesCb, bytesCb)
if err != nil {
nt.notifyf("cannot create pipe for prompt: %v", err)
return nil
}
port2, done2 := makeNotifyPort(nt)
err = ev.Call(fn,
eval.CallCfg{Args: args, From: "[" + ctx + "]"},
eval.EvalCfg{Ports: []*eval.Port{nil, port1, port2}})
done1()
done2()
if err != nil {
nt.notifyError(ctx, err)
}
return result
}