From b01e0cbf903487e46ed83cebb136342bf003d4c9 Mon Sep 17 00:00:00 2001 From: Matt Nish-Lapidus Date: Tue, 8 Jul 2025 23:36:00 -0400 Subject: [PATCH] better ls for nu and other --- flake.lock | 6 +-- modules/home/shell-conf/nushell/config.nu | 64 ++++++++++++++++++----- modules/home/shell-conf/nushell/env.nu | 14 ----- 3 files changed, 53 insertions(+), 31 deletions(-) diff --git a/flake.lock b/flake.lock index f8199b2..290fa60 100644 --- a/flake.lock +++ b/flake.lock @@ -153,11 +153,11 @@ "nixpkgs-stable": "nixpkgs-stable" }, "locked": { - "lastModified": 1751940880, - "narHash": "sha256-lzPU5X3b8w68mUsGHgkRDOJ1DK+gzfov91vsBx2dsAM=", + "lastModified": 1752027847, + "narHash": "sha256-v+WdGcG0ra7YIyI2HruIYqWWxSXBEaG8UStzjAmkkHk=", "owner": "nix-community", "repo": "emacs-overlay", - "rev": "82937ee6d83abf5a9a5a8ae05329c37c0e9380cf", + "rev": "5a64a848253e773d14dffe72e95bf302932e6d02", "type": "github" }, "original": { diff --git a/modules/home/shell-conf/nushell/config.nu b/modules/home/shell-conf/nushell/config.nu index d239cac..7dc80ed 100644 --- a/modules/home/shell-conf/nushell/config.nu +++ b/modules/home/shell-conf/nushell/config.nu @@ -18,20 +18,6 @@ def --env doomup [...args] { systemctl --user restart emacs.service } -# TODO: is there a way to passthrough args? -def ls [...p] { - let pattern = if ($p | is-empty) { [ '.' ] } else { $p } - nuls -lm ...$pattern | select name size modified mode user | update modified {format date "%Y-%m-%d %H:%M:%S"} -} - -def lla [...p] { - let pattern = if ($p | is-empty) { [ '.' ] } else { $p } - nuls -alm ...$pattern | select name size modified mode user | update modified {format date "%Y-%m-%d %H:%M:%S"} -} - -alias "ll" = ls -alias "la" = lla - # def --env pullall [] { # } @@ -43,3 +29,53 @@ def pdf-compress [...args] { def nh-switch-gc [...args] { nh os switch; nh clean all --keep 3 --nogcroots; nix store optimise; } + +let external_completer = {|spans| + match $spans.0 { + _ => $fish_completer + } | do $in $spans +} + + +# List the filenames, sizes, and modification times of items in a directory. +@category filesystem +@search-terms dir +@example "List the files in the current directory" { ls } +@example "List visible files in a subdirectory" { ls subdir } +@example "List visible files with full path in the parent directory" { ls -f .. } +@example "List Rust files" { ls *.rs } +@example "List files and directories whose name do not contain 'bar'" { ls | where name !~ bar } +@example "List the full path of all dirs in your home directory" { ls -a ~ | where type == dir } +@example "List only the names (not paths) of all dirs in your home directory which have not been modified in 7 days" { ls -as ~ | where type == dir and modified < ((date now) - 7day) } +@example "Recursively list all files and subdirectories under the current directory using a glob pattern" { ls -a **/* } +@example "Recursively list *.rs and *.toml files using the glob command" { ls ...(glob **/*.{rs,toml}) } +@example "List given paths and show directories themselves" { ['/path/to/directory' '/path/to/file'] | each {|| ls -D $in } | flatten } +def ls [ + --all (-a), # Show hidden files + --long (-l), # Get all available columns for each entry (slower; columns are platform-dependent) + --short-names (-s), # Only print the file names, and not the path + --full-paths (-f), # display paths as absolute paths + --du (-d), # Display the apparent directory size ("disk usage") in place of the directory metadata size + --directory (-D), # List the specified directory itself instead of its contents + --mime-type (-m), # Show mime-type in type column instead of 'file' (based on filenames only; files' contents are not examined) + --threads (-t), # Use multiple threads to list contents. Output will be non-deterministic. + ...pattern: glob, # The glob pattern to use. +]: [nothing -> table] { + let pattern = if ($pattern | is-empty) { ['.'] } else { $pattern } + ( + nuls + --all=$all + --long + --short-names=$short_names + --full-paths=$full_paths + --du=$du + --directory=$directory + --mime-type + --threads=$threads + ...$pattern + ) | select name size modified mode user | update modified {format date "%Y-%m-%d %H:%M:%S"} +} + +alias "ll" = ls +alias "la" = ls -a +alias "lla" = la diff --git a/modules/home/shell-conf/nushell/env.nu b/modules/home/shell-conf/nushell/env.nu index 2e80612..cb2d3ca 100644 --- a/modules/home/shell-conf/nushell/env.nu +++ b/modules/home/shell-conf/nushell/env.nu @@ -18,17 +18,3 @@ } } -let zoxide_completer = {|spans| - $spans | skip 1 | zoxide query -l ...$in | lines | each {|line| $line | str replace $env.HOME '~' } | where {|x| $x != $env.PWD} -} - -let external_completer = {|spans| - - match $spans.0 { - -# use zoxide completions for zoxide commands - z | zi => $zoxide_completer - __zoxide_z | __zoxide_zi => $zoxide_completer - _ => $fish_completer - } | do $in $spans -}