From 85019a5bd728e0efddb88d1dfe724291e63b9a58 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Sun, 8 Jun 2025 17:21:48 +0300 Subject: [PATCH] nushell: clean up dump module, add dump list --- modules/common/nushell/environment.nu | 157 ++++++++++++++++++-------- 1 file changed, 107 insertions(+), 50 deletions(-) diff --git a/modules/common/nushell/environment.nu b/modules/common/nushell/environment.nu index c555193..a5eb78f 100644 --- a/modules/common/nushell/environment.nu +++ b/modules/common/nushell/environment.nu @@ -28,66 +28,123 @@ def --env mcg [path: path]: nothing -> nothing { jj git init --colocate } -let site_path = glob "~" | path join "projects" "site" +module dump { + def site-path []: nothing -> path { + $env.HOME | path join "Projects" "site" + } -# Edit a thought dump. -def "dump ed" [ - namespace: string # The thought dump to edit. Namespaced using '.', does not include file extension. -]: nothing -> nothing { - let dump_path = $site_path | path join "site" "dump" ...($namespace | split row ".") | $in + ".md" + def dump-path []: nothing -> path { + site-path | path join "site" "dump" + } - mkdir ($dump_path | path parse | get parent) - touch $dump_path + # Convert a thought dump namespace to the filesystem path. + export def to-path []: string -> path { + let namespace = $in - let old_dump_hash = open $dump_path | hash sha256 + dump-path + | path join ...($namespace | split row ".") + | $in + ".md" + } - ^$env.EDITOR $dump_path + # Convert a filesystem path to a thought dump namespace. + export def to-dump []: path -> string { + let path = $in - let dump_size = ls $dump_path | get 0.size - if $dump_size == 0b { - print $"(ansi red)thought dump was emptied(ansi reset)" - dump rm $namespace - } else if $old_dump_hash == (open $dump_path | hash sha256) { - print $"(ansi yellow)thought dump was not changed(ansi reset)" - } else { - print $"(ansi magenta)thought dump was edited(ansi reset)" + print $path (dump-path) + + $path + | path relative-to (dump-path) + | path split + | str join "." + | str substring 0..<-3 + } + + # List all thought dumps that start with the given namespace. + export def list [ + namespace: string = "" + ]: nothing -> table { + let dump_prefix = dump-path | path join ...($namespace | split row ".") + + let dump_parent_contents = glob ($dump_prefix | path parse | get parent | path join "**" "*.md") + let dump_matches = $dump_parent_contents | filter { str starts-with $dump_prefix } + + ls ...$dump_matches | each { + merge { path: $in.name } + | select path size modified + | merge { namespace: ($in.path | to-dump) } + } + } + + # Deploy the thought dumps and thus the website. + export def deploy []: nothing -> nothing { print $"(ansi green)deploying...(ansi reset)" - cd $site_path + cd (site-path) + ./apply.nu + } - jj commit --message $"dump\(($namespace)\): update" - jj bookmark set master --revision @- + # Edit a thought dump. + export def edit [ + namespace: string # The thought dump to edit. Namespaced using '.', does not include file extension. + ]: nothing -> nothing { + let dump_path = $namespace | to-path - [ - { jj git push --remote origin } - { jj git push --remote rad } - { ./apply.nu } - ] | par-each { do $in } + let old_dump_size = try { ls $dump_path } - cd - + mkdir ($dump_path | path parse | get parent) + touch $dump_path + + let old_dump_hash = open $dump_path | hash sha256 + + ^$env.EDITOR $dump_path + + let dump_size = ls $dump_path | get 0.size + if $dump_size == 0b { + print $"(ansi red)thought dump was emptied(ansi reset)" + delete $namespace --existed-before ($old_dump_size != null) + } else if $old_dump_hash == (open $dump_path | hash sha256) { + print $"(ansi yellow)thought dump was not modifier, doing nothing(ansi reset)" + } else { + print $"(ansi magenta)thought dump was edited(ansi reset)" + + let jj_arguments = [ "--repository", (site-path) ] + + jj ...$jj_arguments commit --message $"dump\(($namespace)\): update" + jj ...$jj_arguments bookmark set master --revision @- + + [ + { jj ...$jj_arguments git push --remote origin } + { jj ...$jj_arguments git push --remote rad } + { deploy } + ] | par-each { do $in } + } + } + + # Delete a thought dump. + export def delete [ + namespace: string # The thought dump to edit. Namespaced using '.', does not include file extension. + --existed-before = true + ]: nothing -> nothing { + let dump_path = $namespace | to-path + let parent_path = $dump_path | path parse | get parent + + print $"(ansi red)deleting thought dump...(ansi reset)" + print --no-newline (ansi red) + rm --verbose $dump_path + print --no-newline (ansi reset) + + if (ls $parent_path | length) == 0 { + print $"(ansi red)parent folder is empty, deleting that too...(ansi reset)" + print $"(ansi yellow)other parents will not be deleted, if you want to delete those do it manually(ansi reset)" + rm $parent_path + } + + if $existed_before { + deploy + } else { + print $"(ansi green)the thought dump didn't exist before, so skipping deployment(ansi reset)" + } } } -# Delete a thought dump. -def "dump rm" [ - namespace: string # The thought dump to edit. Namespaced using '.', does not include file extension. -]: nothing -> nothing { - print $namespace - let dump_path = $site_path | path join "site" "dump" ...($namespace | split row ".") | $in + ".md" - let parent_path = $dump_path | path parse | get parent - - print $"(ansi red)deleting thought dump...(ansi reset)" - rm $dump_path - - if (ls $parent_path | length) == 0 { - print $"(ansi red)parent folder is empty, deleting that too...(ansi reset)" - print $"(ansi yellow)other parents will not be deleted, if you want to delete those do it manually(ansi reset)" - rm $parent_path - } - - print $"(ansi green)deploying...(ansi reset)" - - cd $site_path - ./apply.nu - cd - -} +use dump