Skip to content

Instantly share code, notes, and snippets.

@rahiel
Last active July 28, 2016 15:28
Show Gist options
  • Save rahiel/cdfda857534bc9dd4456e404a996e38f to your computer and use it in GitHub Desktop.
Save rahiel/cdfda857534bc9dd4456e404a996e38f to your computer and use it in GitHub Desktop.
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