-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinydir
42 lines (41 loc) · 1.56 KB
/
tinydir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#==========================================================================
# File : tinydir
# Description : Shortens the folder name with environment variables. For example try : export JAVA__HOME=<somedir>; cd $JAVA_HOME; tinydir $PWD
# @(#)
#/bin/env python
#
# Revision History:
#
# 18 Oct 2017 - Sivaswami Jeganathan
# Initial Version.
#==========================================================================
import os, sys
def folderShortener(curpath):
originalPath=curpath
folderMap={val:key for (key,val) in os.environ.items() if os.path.sep in val and os.path.pathsep not in val and "PWD" not in key}
#for (key,val) in folderMap.items():
# print(val + "=>" + key)
#curpath=os.getcwd()
remainingPath=""
while (curpath !="~" and curpath !="/" and os.path.ismount(curpath)==False and curpath not in folderMap) :
remainingPath=os.path.basename(curpath)+"/"+remainingPath
curpath=os.path.dirname(curpath)
#print("DEBUG: curpath = " + curpath + " remainingPath = " + remainingPath)
if (curpath !="~" and curpath !="/"):
curpath=folderMap.get(curpath);
if (curpath is not None):
return ("${"+curpath +"}/"+remainingPath)
else:
return originalPath
else:
return originalPath
if len(sys.argv) < 2:
pwd = os.getcwd()
else:
pwd = sys.argv[1]
homedir = os.path.expanduser('~')
pwd = pwd.replace(homedir, '~', 1)
pwd=folderShortener(pwd)
if len(pwd) > 45:
pwd = pwd[:20]+'...'+pwd[-25:] # first 10 chars+last 20 chars
print ('%s' % pwd)