Instantly share code, notes, and snippets.

@rahiel /pwds.py
Last active Jul 28, 2016

Embed
What would you like to do?
Python version of https://github.com/rahiel/pwds
#!/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