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 <[email protected]> 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 <[email protected]>
> 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: [email protected]
>>> Date: Thu, 12 Mar 2015 08:20:42 -0400
>>> To: [email protected]
>>> 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
>>> [email protected]
>>> http://lists.research.att.com/mailman/listinfo/ast-users
>
> _______________________________________________
> ast-users mailing list
> [email protected]
> 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
[email protected]
_______________________________________________
ast-users mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-users