Skip to content

Instantly share code, notes, and snippets.

@plar
Created June 8, 2018 20:27
Show Gist options
  • Save plar/da2bc512e883b769e3f937bcc574d92c to your computer and use it in GitHub Desktop.
Save plar/da2bc512e883b769e3f937bcc574d92c to your computer and use it in GitHub Desktop.
Abbreviated current directory
#!/bin/env python3
import os
def short_cwd(cwd, max_len=55):
home = os.environ.get('HOME')
cwd = cwd.replace(home, '~')
if len(cwd) < max_len:
return cwd
dirs = cwd.split('/')
subdirs_len = [ len(d) for d in dirs]
# print(subdirs_len, sum(subdirs_len))
short_cwd = []
for i, d in enumerate(dirs):
if len(d) < 3:
short_cwd.append(d)
else:
short_dir = d[:3]
if len(d) > 3:
short_dir += ".."
short_cwd.append(short_dir)
subdirs_len[i] = len(short_cwd)
if sum(subdirs_len) < max_len:
short_cwd.extend(dirs[i+1:])
break
return os.sep.join(short_cwd)
print(short_cwd(os.getcwd()))
# use in bash for nice work directory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment