Last active
July 28, 2016 15:28
-
-
Save rahiel/cdfda857534bc9dd4456e404a996e38f to your computer and use it in GitHub Desktop.
Python version of https://github.com/rahiel/pwds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import os | |
def main(): | |
try: | |
MAX_LENGTH = os.environ["PWDS_LENGTH"] | |
except KeyError: | |
MAX_LENGTH = 16 | |
p = os.getcwd().replace("/home/" + os.environ["USER"], '~') | |
root = True if p[0] == '/' else False | |
length = len(p) | |
if length > MAX_LENGTH: | |
dirs = p.split('/') | |
if root: | |
dirs = dirs[1:] | |
last = len(dirs) - 1 | |
i = 0 | |
while length > MAX_LENGTH and i < last: | |
if len(dirs[i]) > 1: | |
length = length - len(dirs[i][1:]) | |
dirs[i] = dirs[i][0] | |
i += 1 | |
p = '/'.join(dirs) | |
if root: | |
p = '/' + p | |
print(p) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment