From aa4c3ecdaed19baf15b4751c1837125639ba24b6 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Thu, 13 Feb 2025 11:37:17 -0600 Subject: [PATCH] use python for logic --- home/private_bin/executable_mkscratch | 54 +++++++++++++++++++ home/private_dot_config/zsh/functions/scratch | 20 +------ 2 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 home/private_bin/executable_mkscratch diff --git a/home/private_bin/executable_mkscratch b/home/private_bin/executable_mkscratch new file mode 100644 index 0000000..0ae5914 --- /dev/null +++ b/home/private_bin/executable_mkscratch @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 + +from pathlib import Path +import sys +from datetime import datetime +from tempfile import mkdtemp + +SCRATCH_DIR = Path.home() / "scratch" +WEEK_SCRATCH = SCRATCH_DIR / datetime.now().strftime("%Y/%U") +(SCRATCH_DIR / "current").unlink() +(SCRATCH_DIR / "current").symlink_to(WEEK_SCRATCH) + + +def clean_up(): + for p in WEEK_SCRATCH.iterdir(): + if p.is_dir() and len(list(p.iterdir())) == 0: + sys.stderr.write(f"removing: {p}\n") + p.rmdir() + + +def make_temp() -> Path: + return Path(mkdtemp(prefix=datetime.now().strftime("%d") + ".", dir=WEEK_SCRATCH)) + + +def make_named(name: str) -> Path: + d = WEEK_SCRATCH / name + d.mkdir(exist_ok=True) + return d + +def return_last() -> Path: + dirs = [ d for d in WEEK_SCRATCH.iterdir() if d.is_dir()] + if len(dirs) == 0: + sys.stderr.write("no directores. exiting...") + + return sorted(dirs, key = lambda p: p.stat().st_mtime, reverse=True)[0] + +def main(): + clean_up() + if len(sys.argv) > 2: + sys.exit("unexpected number of arguments") + + elif len(sys.argv) > 1: + name = sys.argv[1] + if name == "last": + d = return_last() + else: + d = make_named(name) + else: + d = make_temp() + + print(d) + +if __name__ == "__main__": + main() diff --git a/home/private_dot_config/zsh/functions/scratch b/home/private_dot_config/zsh/functions/scratch index 17252a1..ad259e9 100644 --- a/home/private_dot_config/zsh/functions/scratch +++ b/home/private_dot_config/zsh/functions/scratch @@ -1,24 +1,6 @@ #!/usr/bin/env zsh ##? drop into a directory in ~/scratch/%Y/%U -# inspo: https://leahneukirchen.org/blog/archive/2006/01/keeping-your-home-clean-with-mess.html - -local dir date_scratch - -date_scratch=$HOME/scratch/$(date +"%Y/%U") -mkdir -p $date_scratch -ln -sfn $date_scratch $HOME/scratch/current - -# cleanup any empty directories while I'm here -find $HOME/scratch/$(date +"%Y") \ - -mindepth 2 -maxdepth 2 -type d -empty -print0 |\ - xargs -0 --no-run-if-empty rmdir --verbose - -if [[ -n $1 ]]; then - dir=$date_scratch/$1 - mkdir -p $dir -else - dir=$(mktemp -d $date_scratch/$(date +"%d").XXXX) -fi +dir=$(mkscratch "$@") pushd $dir