diff options
author | Julian T <julian@jtle.dk> | 2020-10-01 16:10:31 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2020-10-01 16:10:31 +0200 |
commit | 6ae74f4df85f6307073c0d786a35e05e2ac917a6 (patch) | |
tree | 9ac5e4e31a5dc0d6972cf5c2e52b5d9d363dfcf1 /scripts | |
parent | 8006b3068f1df05a6671377658085a5471e1f708 (diff) |
Added goto script
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/Scripts/goto | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/scripts/Scripts/goto b/scripts/Scripts/goto new file mode 100755 index 0000000..e4180ad --- /dev/null +++ b/scripts/Scripts/goto @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 + +import argparse +import os +import sys +import os.path as path + +class Bookmarks(): + def __init__(self, storepath=None): + # Yeah XDG stuff does not really matter here + self.storepath = storepath or path.join(os.getenv("HOME"), ".bookmarks") + self.store = {} + + if not path.exists(self.storepath): + return + + with open(self.storepath, "r") as f: + for line in f: + # This breaks support for spaces in name and path + line = line.split(" ") + self.store[line[0]] = line[1][:-1] + + def save(self, name, path): + if not name in self.store: + # Easy just append + with open(self.storepath, "a") as f: + f.write(f"{name} {path}\n") + return + + # Truncate it + with open(self.storepath, "w") as f: + for (k, v) in self.store.items(): + # self.store should contain it's old values to we can jump to something we are saving to + if k is name: + v = path + f.write(f"{name} {path}\n") + + def load(self, name): + return self.store[name] + + def list(self): + for (k, v) in self.store.items(): + print(f"{k} => {v}") + +parser = argparse.ArgumentParser() +parser.add_argument("--save", "-s", help="Save current location with name, if '.' pwd is used as name") +parser.add_argument("--path", "-p", help="Specifies if `dest` is a real path and not a bookmark", action="store_true") +parser.add_argument("--list", "-l", help="List bookmarks", action="store_true") +parser.add_argument("dest", nargs="?", help="Where to go") + +args = parser.parse_args() + +bm = Bookmarks() + +if args.list: + bm.list() + +# Whether to save +if args.save: + name = args.save if args.save is not "." else os.getcwd() + bm.save(name, os.getcwd()) + +if args.dest: + where = bm.load(args.dest) if not args.path else args.dest + with open("/tmp/where", "w") as f: + f.write(where) + # This means zsh should go here + sys.exit(3) + |