That’s right, printf is a shell builtin now.  Some of this was written to avoid 
non-builtin commands quite a long time ago, when it might not have been, and/or 
I never realized there was time functionality in printf.  So a good 
simplification using printf should be possible.  Thanks!

On Mar 12, 2015, at 6:19 PM, Janis Papanagnou <[email protected]> 
wrote:

> Well, I'll abstain from diving into your code; you're right, it's quite ugly. 
>  :-)
> For the date calculations part of your question the easiest may be to use 
> some builtins.
> 
> The following create the current date and the date "t0" from the current day, 
> both in seconds...
> t_now_s=$( printf "%(%s)T\n" )
> t0_s=$( printf "%(%s)T\n" "$(date +%Y)" )
> seconds_diff=$(( t_now_s - t0_s ))
> 
> (Maybe it's even easier than that to achieve, but I think this is already 
> more legible then what you have.)
> 
> 
> Subject: Re: [ast-users] how to get functionality of bash compatible PS1 
> without rest of bash compatibility?
> From: [email protected]
> Date: Thu, 12 Mar 2015 17:26:58 -0400
> CC: [email protected]
> To: [email protected]
> 
> 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

Reply via email to