refactor(shell): extract _fish_mkdir_p utility; improve poke output
- Add _fish_mkdir_p: reusable mkdir -p with three output modes: --path (default): "Created: ~/full/path/" on one line --tree: dimmed existing anchor + cyan tree of newly-created dirs --silent: no output HOME is substituted with ~ in all output paths - Refactor poke to delegate to _fish_mkdir_p --tree; removes inline mkdir/echo logic and gains ~ substitution and per-dir tree output
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
# Copyright (C) 2026 Rootiest
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
# mkdir -p with configurable output.
|
||||
#
|
||||
# Flags:
|
||||
# --path / -p (default) Print "Created: ~/full/path/" on one line
|
||||
# --tree / -t Tree view: dimmed existing anchor + cyan new dirs
|
||||
# --silent / -s No output
|
||||
#
|
||||
# Usage: _fish_mkdir_p [--path|--tree|--silent] <dir>
|
||||
function _fish_mkdir_p --description 'mkdir -p with configurable verbose output'
|
||||
set -l mode path
|
||||
set -l target
|
||||
|
||||
for _arg in $argv
|
||||
switch $_arg
|
||||
case -s --silent
|
||||
set mode silent
|
||||
case -t --tree
|
||||
set mode tree
|
||||
case -p --path
|
||||
set mode path
|
||||
case '*'
|
||||
set target $_arg
|
||||
end
|
||||
end
|
||||
|
||||
test -n "$target"; or return 1
|
||||
test -d "$target"; and return 0
|
||||
|
||||
# Walk up to find deepest existing ancestor; collect dirs to create (shallowest first).
|
||||
set -l to_create
|
||||
set -l cursor $target
|
||||
while not test -d $cursor
|
||||
set -p to_create $cursor
|
||||
set -l up (dirname $cursor)
|
||||
test "$up" = "$cursor"; and break # filesystem root guard
|
||||
set cursor $up
|
||||
end
|
||||
|
||||
mkdir -p $target; or return $status
|
||||
test $mode = silent; and return 0
|
||||
|
||||
if test $mode = path
|
||||
set -l display (string replace -- "$HOME" "~" $target)"/"
|
||||
echo (set_color --bold cyan)"Created: "(set_color cyan)"$display"(set_color normal)
|
||||
return 0
|
||||
end
|
||||
|
||||
# Tree mode: dimmed existing anchor, then cyan new dirs.
|
||||
echo (set_color --bold cyan)"Created:"(set_color normal)
|
||||
set -l anchor (string replace -- "$HOME" "~" $cursor)"/"
|
||||
echo (set_color brblack)" $anchor"(set_color normal)
|
||||
set -l i 1
|
||||
while test $i -le (count $to_create)
|
||||
set -l spaces (string repeat -n (math "($i - 1) * 4") " ")
|
||||
set -l name (basename $to_create[$i])"/"
|
||||
echo (set_color cyan)" $spaces└── $name"(set_color normal)
|
||||
set i (math $i + 1)
|
||||
end
|
||||
end
|
||||
+1
-4
@@ -8,10 +8,7 @@ function poke --description 'touch with automatic parent directory creation'
|
||||
end
|
||||
for _path in $argv
|
||||
set -l _dir (dirname $_path)
|
||||
if not test -d $_dir
|
||||
mkdir -p $_dir
|
||||
and echo (set_color --bold cyan)"Created: "(set_color cyan)"$_dir"(set_color normal)
|
||||
end
|
||||
_fish_mkdir_p --tree $_dir
|
||||
touch $_path
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user