:: ELPATH.BTM
:: For author, licensing, revision history, see the end of file.
@LOADBTM ON
@echo off
SETLOCAL
:: If not manually set by the user, set max prompt path length to a little more
:: than half the current width of the screen. If PL is negative, assume we want
:: (screen width - |value|). Do this INSIDE the setlocal so we leave the user's
:: value alone. If not set in the external environment, it means this script
:: will adapt to current screen width every time.
iff "%PL" == "" then
set PL=%@EVAL[%_columns / 1.9 =0.0]
else if %PL LT 0 then
set PL=%@EVAL[ %_columns + %PL ]
endiff
:: Emit whole CWD if shorter than the max length.
iff %@LEN[%_CWD] LT %PL then
echos %_CWD
QUIT
endiff
:: This is the slower but SMARTER version that always prints INTACT subdir names
:: at the beginning and end of the shortened string...
:SMART_CODE
setlocal
:: Elision adds 4 characters, so eliding must save at least 5.
:: If you want to reduce the resulting width to something else,
:: this is the place to do it.
set PL=%@EVAL[%PL - 5]
:: Start with the leftmost and rightmost directories in the CWD.
set lhs=%@WORD["\\",0,%_CWD]
set rhs=%@WORD["\\",-0,%_CWD]
set cnt=%@WORDS["\\",%_CWD]
:: Now, build the elided path by alternately adding directories to the left
:: part (LHS) and then the right part (RHS) until either would push us over
:: the limit.
do i = 1 to %cnt
if %@LEN[%lhs %rhs] GE %PL LEAVE
set lh2=%@WORD["\\",%i,%_CWD]
if %@LEN[%[lhs]\%[lh2]\%[rhs]] GE %PL LEAVE
set lhs=%[lhs]\%lh2
set rh2=%@WORD["\\",-%i,%_CWD]
if %@LEN[%lhs\%rh2\%rhs] GE %PL LEAVE
set rhs=%[rh2]\%[rhs]
enddo
:: At this point we should have a left-hand-side and a right-hand-side that we
:: can concatenate together with the elision: left+dots+right. (Two dots are
:: sufficient for the user to get the idea.) NOTE: the (len-5) here is a safety
:: net, separate from the MaxPathLen calculation above, and it should not be
:: changed here.
iff %@LEN[%lhs %rhs] LT %@EVAL[%@LEN[%_CWD] - 5] then
switch "%_dos"
case "WIN32" .or. "NT" .or. "WIN2K" .or. "WINXP" .or. "WINDOWS10"
echos %[lhs]\úú\%[rhs]
default
echos %[lhs]\··\%[rhs]
endswitch
else
echos %_CWD
endiff
ENDLOCAL
QUIT
:: This is the simpler and faster version that just chops out the middle
:: and replaces it with a ".." elipsis. On much older machines, this made
:: a noticable difference in speed, but in 2021 there's really no reason
:: to use this.
:DUMB_CODE
set LL=%@EVAL[(%PL - 1) / 2.1 =0.0]
set NewWidth=%@EVAL[%LL*2 + 2 =0.0]
set OldWidth=@LEN[%CWD]
iff %LL GT 3 then
switch "%_dos"
case "WIN32" .or. "NT" .or. "WIN2K" .or. "WINXP" .or. "WINDOWS10"
echos %@LEFT[%LL,%_cwd]úú%@RIGHT[%LL,%_cwd]
default
echos %@LEFT[%LL,%_cwd]··%@RIGHT[%LL,%_cwd]
endswitch
else
echos %_CWD
endiff
ENDLOCAL
QUIT
::
:: =============================================================================
:: AUTHOR
:: John T. Baldwin
:: LICENSING
:: Licensed to my fellow JpSoft users under the MIT Open Source License.
:: Please maintain my original author's attribution and any revision history
:: of mine or your own.
:: =============================================================================
:: MODIFICATION HISTORY
:: =============================================================================
:: 2006-01-25 jtbaldwin Created.
::
:: 2007-03-16 jtbaldwin Simplified and sped up.
:: Left the old code in but disabled.
::
:: 2019-12-17 jtbaldwin Improved the old code so elision is smarter and
:: better balanced. Added check for Windows 10.
:: Machines are so much faster now, there's no reason
:: to use the simpler, dumber code, but I left it in
:: place in case I want to go back to it for some odd
:: reason.
::
:: 2020-01-16 jtbaldwin Moved sections around to remove GOTO which slows
:: things down a bit without benefit.
::
:: =============================================================================