mirror of https://github.com/nirenjan/dotfiles.git
Replace smartwd with python equivalent
parent
dd6a3182f8
commit
a1dbe3c5d0
90
smartwd
90
smartwd
|
@ -1,11 +1,8 @@
|
||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env python
|
||||||
# -*- ruby -*-
|
|
||||||
# Script to smartly chop off portions of the current working directory for
|
# Script to smartly chop off portions of the current working directory for
|
||||||
# use in the shell prompt
|
# use in the shell prompt
|
||||||
|
|
||||||
# Some of the methods don't work with Ruby versions older than 1.8.7
|
import os
|
||||||
min_release = "1.8.7 (2008-08-11)"
|
|
||||||
ruby_release = "#{RUBY_VERSION} (#{RUBY_RELEASE_DATE})"
|
|
||||||
|
|
||||||
# Constants used to decide when to start chopping
|
# Constants used to decide when to start chopping
|
||||||
DIRLIM = 5
|
DIRLIM = 5
|
||||||
|
@ -13,57 +10,48 @@ DIR_L = 3
|
||||||
DIR_R = 2
|
DIR_R = 2
|
||||||
PWDLEN = 14
|
PWDLEN = 14
|
||||||
|
|
||||||
def backslash_w
|
def smartwd():
|
||||||
homedir = ENV['HOME']
|
username = os.environ['USER']
|
||||||
pwd = ENV['PWD']
|
homedir = os.environ['HOME']
|
||||||
print pwd.gsub(homedir, '~')
|
pwd = os.environ['PWD']
|
||||||
end
|
|
||||||
|
|
||||||
def smartwd
|
path = pwd.split('/')
|
||||||
username = ENV['USER']
|
|
||||||
homedir = ENV['HOME']
|
|
||||||
pwd = ENV['PWD']
|
|
||||||
|
|
||||||
path = pwd.split('/')
|
# Ignore the root path
|
||||||
|
if len(path) == 1:
|
||||||
|
return pwd
|
||||||
|
|
||||||
# Ignore the root path
|
try:
|
||||||
if (path.length > 0)
|
username_index = path.index(username)
|
||||||
index = path.index(username)
|
except ValueError:
|
||||||
if index
|
username_index = None
|
||||||
prefix = path.shift(index + 1)
|
|
||||||
|
|
||||||
# We need to map additional paths in environments where the user
|
if username_index is not None:
|
||||||
# may have more than one available folder in his/her name.
|
prefix = '/'.join(path[:username_index+1])
|
||||||
if prefix.join('/') == homedir
|
|
||||||
path.unshift('~')
|
|
||||||
else
|
|
||||||
# The first entry in the prefix array is the empty string
|
|
||||||
pre = prefix[1].split('').shift(4).join('') + '~'
|
|
||||||
path.unshift(pre)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
# Replace the first two entries in the array with /<entry1>
|
|
||||||
# (because the first entry is always empty string)
|
|
||||||
prefix = path.shift(2).join('/')
|
|
||||||
path.unshift(prefix)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Check to make sure that both the DIRLIM and PWDLEN constraints
|
if prefix == homedir :
|
||||||
# are met before splitting
|
pre_path = '~'
|
||||||
pwd = path.join('/')
|
else:
|
||||||
if path.length > DIRLIM and pwd.length > PWDLEN
|
# The first element is always the empty string.
|
||||||
pwd_l = path.shift(DIR_L).join('/')
|
# We want the first 4 characters of the second element
|
||||||
pwd_r = path.pop(DIR_R).join('/')
|
pre_path = path[1][:4] + '~'
|
||||||
pwd = pwd_l + '/.../' + pwd_r
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
print pwd
|
del path[:username_index]
|
||||||
end
|
path[0] = pre_path
|
||||||
|
|
||||||
if (ruby_release < min_release)
|
pwd = '/'.join(path)
|
||||||
backslash_w
|
|
||||||
else
|
# If we exceed the dirlimit and the length of the joined pwd,
|
||||||
smartwd
|
# replace the pwd with left and right elements, with ellipses
|
||||||
end
|
# in between to simulate a long path.
|
||||||
|
if len(path) > DIRLIM and len(pwd) > PWDLEN:
|
||||||
|
newpwd = '/'.join(path[:DIR_L] + ['...'] + path[-DIR_R:])
|
||||||
|
|
||||||
|
if len(newpwd) < len(pwd):
|
||||||
|
pwd = newpwd
|
||||||
|
|
||||||
|
return pwd
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print smartwd()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue