Makes sense, thanks; done.  That now looks like the following.  The statedir 
stuff was also in the _cd wrapper function before, and is part of something 
meant to save the current directory if the Apple Terminal.app is restarted 
after forced termination, so that each restarted tab can in turn have the shell 
in it return to whatever directory it was in (previous scroll back text is 
shown by Terminal.app’s restart processing, but it’s nice to actually come up 
in the same directory as before, too).  I haven’t played with that bit in 
awhile, but it doesn’t hurt to move it here so that the _cd wrapper could be 
eliminated.

Are initializers for typeset -S variables only done once, on first execution?  
That’s what I want - no need to run uname -n repeatedly for example.

The privchar check was meant to be an easy way using strictly builtins to 
choose between # and >  at the end of the prompt, depending on privileges; 
details are non-portable.  If “id” was builtin, I could improve on that.

You don’t really want to know about the other strange things I do.  For 
instance, I have a wrapper (alias and function) on ‘rm’ for interactive use, 
that turns off globbing then runs the function which checks if the args when 
expanded are any different from unexpanded, then prompts, so that a typo of say
rm junk *
for
rm junk*

gives an opportunity to say “no”.  That depends on the oddity that actions in a 
function’s EXIT trap take place in a scope outside the function, so that I can 
re-enable globbing when the wrapper exits.  The idea for that came from a very 
old in-house (don’t ask where) version of the shell source that had glob 
prompting like that added for rm; ksh has sufficient facilities that I could 
approximate that behavior without needing to alter the shell itself…back before 
shell source was widely available (or not wanting to have to maintain a shell 
source patch).

function PS1.get {
        typeset -S hostname="$(uname -n)"
        typeset -S statedir="${HOME}/.TerminalShellState"
        typeset time="$(printf "%(%H:%M:%S)T")"
        typeset label
        typeset privchar="$(if [ -r /etc/master.passwd ]; then echo '#'; else 
echo '>'; fi)"
        typeset t1
        typeset t2
      
        case "${TERM}" in
        sun-cmd|dtterm)
"               label="${_hostname}:${PWD}\\\${_hostname}\\\
"               label="${_hostname}\\\${_hostname}\\\
                ;;
        xterm|xterm-color)
"               label="
"               label="
                ;;
        *)
                label=""
                ;;
        esac

        # Reduce pathname length to 15 chars:
        if (( ${#PWD} > 15 ))
        then
                t1="${PWD%/*???????????????}"
                t2="${PWD#$t1/}"
                if (( ${#PWD} > ( ${#t2} + 3 ) ))
                then
                        .sh.value="${label}${time}[!]${t2}${privchar} "
                else
                        .sh.value="${label}${time}[!]$PWD${privchar} "
                fi
        else
                .sh.value="${label}${time}[!]$PWD${privchar} "
        fi
        if [ -d "${statedir}" -a -w "${statedir}" -a -x "${statedir}" -a 
x"${TERM_PROGRAM}" = x"Apple_Terminal" -a -n "${TERM_SESSION_ID}" ]; then
                print -r -- "$PWD" >"${statedir}/${TERM_SESSION_ID}"
        fi
}



On Mar 12, 2015, at 7:13 PM, Mark McCullough <mark.mc...@gmail.com> wrote:

> Why not just create the PS1.get discipline function and then set PS1 inside 
> that function?  I do that all the time for my shell:
> 
> function PS1.get {
> # Logic here to work out what PS1 is to be and set it.
> }
> 
> I consider overloading cd with an alias to be awkward and more prone to 
> accidental overwrites.  That combined with the references to printf should 
> drastically simplify your code.
> 
>> On 2015-03-12, at 14:26 , Richard L. Hamilton <rlham...@gmail.com> wrote:
>> 
>> My code does NOT attempt to provide bash PS1 compatibility, it just plugs in 
>> a time and truncated (leading levels) directory; the time is based on a 
>> computed offset since midnight to add to SECONDS, so it doesn’t need to run 
>> builtins when PS1 is evaluated.  It works, and the performance isn’t 
>> horrible, but it’s ugly beyond all recognition.
>> 
>> Some of it’s mine, some of it (the directory part for sure; can’t remember 
>> whether the start of the time code was mine or a scrounge) is scrounged.  
>> Basically I make a PS0 that will later (within function _cd) become part of 
>> PS1.
>> 
>> Before, I actually reset SECONDS to be seconds since midnight, but I 
>> recently changed it to be an offset added to SECONDS, so as to leave SECONDS 
>> unaltered and be able to stick it in PS4 along with a set -x at the top of 
>> my .profile, trying to track down an annoying variation from < 1 sec to > 10 
>> sec in how long it takes to start a new login shell.
>> 
>> cd is aliased to function _cd, which runs real ‘cd’ and diddles the path 
>> part of PS1.
>> 
>> Here’s the PS0 (time) part:
>>  eval export _offset="$(( $(/bin/date '+3600*%H+60*%M+%S') - ${SECONDS} ))"
>>  _pad[1]=0;_pad[2]=''
>>  _hh="((SECONDS+${_offset})/3600)%24"
>>  _mm="((SECONDS+${_offset})/60)%60"
>>  _ss="((SECONDS+${_offset}))%60"
>>  
>> _time='${_x[(_m=_mm)==(_h=_hh)==(_s=_ss)]}${_pad[${#_h}]}$_h:${_pad[${#_m}]}$_m:${_pad[${#_s}]}$_s'
>>  PS0="$_time[!]${_hostname%%.*}:"
>> 
>> Like I said, I don’t even really understand the line starting with _time= 
>> anymore.  The _pad[] part I understand, but not the _x[] part, although I 
>> think that might have been a way to cause the evaluation to happen as I 
>> needed it to.
>> 
>> Within _cd, PS1 finally gets set as follows:
>>        if (( ${#PWD} > 15 ))
>>        then
>>           t1="${PWD%/*???????????????}"
>>           t2="${PWD#$t1/}"
>>           if (( ${#PWD} > ( ${#t2} + 3 ) ))
>>           then
>>              PS1="${label}$PS0...${t2}${p} "
>>           else
>>              PS1="${label}$PS0$PWD${p} "
>>           fi
>>        else
>>           PS1="${label}$PS0$PWD${p} "
>>        fi
>> 
>> $label is (depending on the terminal type) an escape sequence to incorporate 
>> the hostname and current directory into the window title bar.  To keep that 
>> from confusing ksh’s idea of display column, it ends with a ^M.
>> 
>> It all works, but the ugliness is OTT.  There’s got to be a better way…
>> 
>> 
>> On Mar 12, 2015, at 8:51 AM, Janis Papanagnou <janis_papanag...@hotmail.com> 
>> wrote:
>> 
>>> To understand your situation...
>>> You have already functional code?
>>> Your code works but it has a performance problem building the PS1 prompt?
>>> If you want something "cleaner" and "faster" it would help if you post your 
>>> code so that we see where you are.
>>> 
>>> 
>>>> From: rlham...@gmail.com
>>>> Date: Thu, 12 Mar 2015 08:20:42 -0400
>>>> To: ast-users@lists.research.att.com
>>>> Subject: [ast-users] how to get functionality of bash compatible PS1 
>>>> without       rest of bash compatibility?
>>>> 
>>>> Yep, that was the question: more or less bash-compatible PS1 handling 
>>>> without the rest of bash compatibility. I like timestamps and other odd 
>>>> things (shortened directory) in my prompt, but I’d rather do it without 
>>>> enabling any more bash compatibility than that. I’ve got some odd code 
>>>> that even I no longer understand that gives the output I want, but it’s 
>>>> very ugly (certainly not using a get discipline or anything like that, but 
>>>> computing a date having determined an offset to apply to SECONDS to count 
>>>> since midnight). Something cleaner and faster might be nice.
>>>> 
>>>> _______________________________________________
>>>> ast-users mailing list
>>>> ast-users@lists.research.att.com
>>>> http://lists.research.att.com/mailman/listinfo/ast-users
>> 
>> _______________________________________________
>> ast-users mailing list
>> ast-users@lists.research.att.com
>> http://lists.research.att.com/mailman/listinfo/ast-users
> 
> 
> ----
> "The speed of communications is wondrous to behold. It is also true that 
> speed can multiply the distribution of information that we know to be 
> untrue." Edward R Murrow (1964)
> 
> Mark McCullough
> mark.mc...@gmail.com
> 
> 
> 
> 

_______________________________________________
ast-users mailing list
ast-users@lists.research.att.com
http://lists.research.att.com/mailman/listinfo/ast-users

Reply via email to