From 70c845d0e1fdf425a4f70920ec8f4ec41cacfa97 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 15 Oct 2026 15:58:09 -0500 Subject: [PATCH] Add date prompt tests and fix date prompting. --- tests/sh.test | 17 ++++++++++++++++- toys/pending/sh.c | 6 +++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/tests/sh.test b/tests/sh.test index 608b2cab..439b1223 100644 --- a/tests/sh.test +++ b/tests/sh.test @@ -293,7 +293,7 @@ tst '${x,y}' 'x=BBB; echo ${x,B}' 'bBB\n' tst '${x,,y}' 'x=POTATO; echo ${x,,} ${x,,?} ${x,,*} ${x,,T}' \ 'potato potato potato POtAtO\n' -# TODO: dDtT@A vV w !# [] +# We skipped testing @P \v\V TODO: !# command history # Some of these tests are up top because root or container access needed tst '${x@P} \j jobs' 'x="-\\j-";echo ${x@P};sleep .5 & echo ${x@P}' '-0-\n-1-\n' tst '${x@P} \x\a\e\n\r\\' 'x="\\x\\a\\e\\n\\r\\\\"; echo -n "${x@P}"' \ @@ -305,6 +305,21 @@ tst '${x@P} \u' 'x=\\u; echo "${x@P}"' "$(whoami)\n" tst '${x@P} \W' 'x=\\W; echo "${x@P}"' "$(basename $PWD)\n" tst '${x@P} \w' 'HOME=/one PWD=/one/two x=\\w; echo "${x@P}"' '~/two\n' tst '${x@P} \W is strrchr not basename' 'PWD=/one/two/ x=\\W;echo "${x@P}"' '\n' +tst '${x@P} \[\] ignored' 'x=a\\[b\\]c; echo "${x@P}"' 'abc\n' + +# Try twice in case clock advances to a new second partway through. +# note: this avoids bash prompting turns \n into \r\n when using readline. +cat > datest.sh << 'EOF' +for i in one two; do + dd="$(date +$'D=%s\nd=%a %b %d\nt=%T\nT=%I:%M:%S\n@=%I:%M %p\nA=%H:%M')" + xx=D=\\D{%s}\\nd=\\d\\nt=\\t\\nT=\\T\\n@=\\@\\nA=\\A xx="${xx@P}" + diff="$(diff -u <(echo "$dd") <(echo "$xx"))" + [ -z "$diff" ] && break + [ $i = two ] && echo "$diff" +done +EOF +tst '${x@P} \DdtT@A' 'source datest.sh 2>&1 && echo yes' 'yes\n' +rm datest.sh # UuLQEPAKak *@ # // Of COURSE unicode case switch can change utf8 encoding length diff --git a/toys/pending/sh.c b/toys/pending/sh.c index bace0eda..a8370121 100644 --- a/toys/pending/sh.c +++ b/toys/pending/sh.c @@ -2029,16 +2029,16 @@ static int get_prompt(char *buf, int blen, char *prompt) // Ignore bash's "nonprintable" hack; query our cursor position instead. else if (cc=='[' || cc==']') continue; else if (cc=='$') *pp++ = getuid() ? '$' : '#'; - else if (strchr("DdtT@A", cc)) { + else if (1+(ll = stridx("DdtT@A", cc))) { char *end, *fmt = (char *[]){0, "%a %b %d", "%H:%M:%S", "%I:%M:%S", - "%I:%M %p", "%R"}[stridx("dtT@A", cc)]; + "%I:%M %p", "%R"}[ll]; time_t tt = time(0); if (!fmt) { // todo: slashcopy? Would allow escaped \} but can't handle missing } if (*prompt!='{' || !(end = strchr(prompt+1, '}'))) *pp++ = cc; else { - if (end==prompt+1) fmt = "%X"; + if (end==++prompt) fmt = "%X"; else fmt = xstrndup(prompt, end-prompt); prompt = end+1; } -- 2.39.5