1 ## our_shell: ysh
2
3 #### default prompt doesn't confuse OSH and YSH
4
5 # Special ysh prefix if PS1 is set
6 setglobal ENV.PS1 = r'\$ '
7 $[ENV.SH] -i -c 'echo "/$[get(ENV, "PS1")]/ /$[get(__defaults__, "PS1")]/"'
8 call ENV->erase('PS1')
9
10 # No prefix if it's not set, since we already have \s for YSH
11 $[ENV.SH] -i -c 'echo "/$[get(ENV, "PS1")]/ /$[get(__defaults__, "PS1")]/"'
12
13 ## STDOUT:
14 /ysh \$ / /null/
15 /\s-\v\$ / /null/
16 ## END
17
18 #### promptVal() with various values
19
20 shopt -s ysh:upgrade
21
22 var x = io.promptVal('$')
23
24 # We're not root, so it should be $
25 echo x=$x
26
27 var x = io.promptVal('w')
28 if (x === PWD) {
29 echo pass
30 } else {
31 echo fail
32 }
33
34 ## STDOUT:
35 x=$
36 pass
37 ## END
38
39 #### promptVal() with invalid chars
40
41 # \D{} will be supported with date and time functions
42 var x = io.promptVal('D')
43 echo x=$x
44
45 # something else
46 var x = io.promptVal('/')
47 echo x=$x
48
49 var x = io.promptVal('ZZ')
50 echo x=$x
51
52 ## status: 3
53 ## STDOUT:
54 x=<Error: \D{} not in promptVal()>
55 x=<Error: \/ is invalid or unimplemented in $PS1>
56 ## END
57
58
59 #### ysh respects PS1
60
61 setglobal ENV.PS1 = r'myprompt\$ '
62 echo 'echo hi' | $[ENV.SH] -i
63
64 ## STDOUT:
65 hi
66 ^D
67 ## END
68 ## stderr-json: "ysh myprompt$ ysh myprompt$ "
69
70 #### ysh respects renderPrompt() over PS1
71
72 export PS1='myprompt\$ '
73
74 cat >yshrc <<'EOF'
75 func renderPrompt(io) {
76 var parts = []
77 call parts->append('hi')
78 call parts->append(io.promptVal('$'))
79 call parts->append(' ')
80 return (join(parts))
81 }
82 EOF
83
84 echo 'echo hi' | $[ENV.SH] -i --rcfile yshrc
85
86 ## STDOUT:
87 hi
88 ^D
89 ## END
90 ## stderr-json: "hi$ hi$ "
91
92 #### renderPrompt() doesn't return string
93
94 export PS1='myprompt\$ '
95
96 cat >yshrc <<'EOF'
97 func renderPrompt(io) {
98 return ([42, 43])
99 }
100 EOF
101
102 echo 'echo hi' | $[ENV.SH] -i --rcfile yshrc
103
104 ## STDOUT:
105 hi
106 ^D
107 ## END
108 ## stderr-json: "<Error: renderPrompt() should return Str, got List> <Error: renderPrompt() should return Str, got List> "
109
110
111 #### renderPrompt() raises error
112
113 export PS1='myprompt\$ '
114
115 cat >yshrc <<'EOF'
116 func renderPrompt(io) {
117 error 'oops'
118 }
119 EOF
120
121 echo 'echo hi' | $[ENV.SH] -i --rcfile yshrc
122
123 ## STDOUT:
124 hi
125 ^D
126 ## END
127 ## stderr-json: "<Runtime error: oops><Runtime error: oops>"
128
129
130 #### renderPrompt() has wrong signature
131
132 export PS1='myprompt\$ '
133
134 cat >yshrc <<'EOF'
135 func renderPrompt() {
136 error 'oops'
137 }
138 EOF
139
140 echo 'echo hi' | $[ENV.SH] -i --rcfile yshrc
141
142 ## STDOUT:
143 hi
144 ^D
145 ## END
146 ## stderr-json: "<Runtime error: Func 'renderPrompt' takes no positional args, but got 1><Runtime error: Func 'renderPrompt' takes no positional args, but got 1>"
147