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 /null/ /\s-\v\$ /
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
29 assert [x === ENV.PWD]
30 echo pass
31
32 ## STDOUT:
33 x=$
34 pass
35 ## END
36
37 #### promptVal() with invalid chars
38
39 # \D{} will be supported with date and time functions
40 var x = io.promptVal('D')
41 echo x=$x
42
43 # something else
44 var x = io.promptVal('/')
45 echo x=$x
46
47 var x = io.promptVal('ZZ')
48 echo x=$x
49
50 ## status: 3
51 ## STDOUT:
52 x=<Error: \D{} not in promptVal()>
53 x=<Error: \/ is invalid or unimplemented in $PS1>
54 ## END
55
56
57 #### ysh respects PS1
58
59 setglobal ENV.PS1 = r'myprompt\$ '
60 echo 'echo hi' | $[ENV.SH] -i
61
62 ## STDOUT:
63 hi
64 ^D
65 ## END
66 ## stderr-json: "ysh myprompt$ ysh myprompt$ "
67
68 #### ysh respects renderPrompt() over PS1
69
70 export PS1='myprompt\$ '
71
72 cat >yshrc <<'EOF'
73 func renderPrompt(io) {
74 var parts = []
75 call parts->append('hi')
76 call parts->append(io.promptVal('$'))
77 call parts->append(' ')
78 return (join(parts))
79 }
80 EOF
81
82 echo 'echo hi' | $[ENV.SH] -i --rcfile yshrc
83
84 ## STDOUT:
85 hi
86 ^D
87 ## END
88 ## stderr-json: "hi$ hi$ "
89
90 #### renderPrompt() doesn't return string
91
92 export PS1='myprompt\$ '
93
94 cat >yshrc <<'EOF'
95 func renderPrompt(io) {
96 return ([42, 43])
97 }
98 EOF
99
100 echo 'echo hi' | $[ENV.SH] -i --rcfile yshrc
101
102 ## STDOUT:
103 hi
104 ^D
105 ## END
106 ## stderr-json: "<Error: renderPrompt() should return Str, got List> <Error: renderPrompt() should return Str, got List> "
107
108
109 #### renderPrompt() raises error
110
111 export PS1='myprompt\$ '
112
113 cat >yshrc <<'EOF'
114 func renderPrompt(io) {
115 error 'oops'
116 }
117 EOF
118
119 echo 'echo hi' | $[ENV.SH] -i --rcfile yshrc
120
121 ## STDOUT:
122 hi
123 ^D
124 ## END
125 ## stderr-json: "<Runtime error: oops><Runtime error: oops>"
126
127
128 #### renderPrompt() has wrong signature
129
130 export PS1='myprompt\$ '
131
132 cat >yshrc <<'EOF'
133 func renderPrompt() {
134 error 'oops'
135 }
136 EOF
137
138 echo 'echo hi' | $[ENV.SH] -i --rcfile yshrc
139
140 ## STDOUT:
141 hi
142 ^D
143 ## END
144 ## stderr-json: "<Runtime error: Func 'renderPrompt' takes no positional args, but got 1><Runtime error: Func 'renderPrompt' takes no positional args, but got 1>"
145