add fish config
This commit is contained in:
47
bin/tzp
Normal file
47
bin/tzp
Normal file
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env fish
|
||||
|
||||
set -l arg_count (count $argv)
|
||||
|
||||
# 1. 检查是否有输入参数
|
||||
if test $arg_count -eq 0
|
||||
echo "用法: zp <文件或目录1> [文件或目录2] ..."
|
||||
exit 1
|
||||
end
|
||||
|
||||
# 2. 预检:检查是否安装了 pigz
|
||||
set -l compress_tool ""
|
||||
if command -v pigz >/dev/null
|
||||
# 使用 pigz 并指定压缩率 5
|
||||
set compress_tool "-I 'pigz -5'"
|
||||
set -l core_count (nproc 2>/dev/null; or echo "多")
|
||||
echo "⚡ 检测到 pigz,将使用 $core_count 核并行压缩 (Level 5)"
|
||||
else
|
||||
# 回退到标准 gzip
|
||||
set compress_tool "-z"
|
||||
echo "🐢 未检测到 pigz,使用标准 gzip 压缩"
|
||||
end
|
||||
|
||||
# 3. 执行压缩逻辑
|
||||
if test $arg_count -eq 1
|
||||
# 逻辑 A:单个参数,生成同名 .tar.gz
|
||||
# 去掉末尾斜杠以获取干净的名称
|
||||
set -l name (string trim -r -c / $argv[1])
|
||||
set -l target "$name.tar.gz"
|
||||
|
||||
echo "🚀 正在压缩单项目: $target"
|
||||
# tar 默认保留软链接(对应 7z 的 -sni -snh 行为)
|
||||
eval tar $compress_tool -cf "$target" "$argv[1]"
|
||||
else
|
||||
# 逻辑 B:多个参数,生成 package_时间戳.tar.gz
|
||||
set -l timestamp (date "+%Y%m%d_%H%M%S")
|
||||
set -l target "package_$timestamp.tar.gz"
|
||||
|
||||
echo "🚀 正在将 $arg_count 个项目打包至: $target"
|
||||
eval tar $compress_tool -cf "$target" $argv
|
||||
end
|
||||
|
||||
if test $status -eq 0
|
||||
echo "✅ 压缩完成: $target"
|
||||
else
|
||||
echo "❌ 压缩过程中出现错误"
|
||||
end
|
||||
45
bin/utzp
Normal file
45
bin/utzp
Normal file
@@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env fish
|
||||
|
||||
# 预检:检查是否安装了 pigz
|
||||
set -l has_pigz (command -v pigz)
|
||||
|
||||
for file in $argv
|
||||
if not test -f "$file"
|
||||
echo "错误: $file 不是有效文件"
|
||||
continue
|
||||
end
|
||||
|
||||
# 1. 获取不带后缀的文件名
|
||||
set -l base_name (string replace -r '\.(tar\..*|tgz|tbz2|txz)$' '' $file | string replace -r '\.[^.]+$' '' )
|
||||
|
||||
# 2. 判断解压程序
|
||||
# 如果是 gzip 格式且系统有 pigz,则使用 pigz 加速
|
||||
set -l tar_opts "-xf"
|
||||
if test -n "$has_pigz"
|
||||
# 匹配 .tar.gz, .tgz, .gz 后缀
|
||||
if string match -qr '\.(gz|tgz)$' "$file"
|
||||
set tar_opts "-I pigz -xf"
|
||||
echo "🚀 检测到 Gzip 格式,启用 pigz 并行加速"
|
||||
end
|
||||
end
|
||||
|
||||
# 3. 预检:获取压缩包根路径下的唯一项目数量
|
||||
set -l root_items (tar $tar_opts "$file" -tf 2>/dev/null | string split -m 1 -f 1 / | sort -u)
|
||||
set -l item_count (count $root_items)
|
||||
|
||||
if test $item_count -eq 0
|
||||
echo "跳过: $file 无法读取内容或为空"
|
||||
continue
|
||||
end
|
||||
|
||||
# 4. 智能判断执行
|
||||
if test $item_count -gt 1
|
||||
echo "📦 检测到多个根项目 ($item_count),解压至目录: $base_name/"
|
||||
mkdir -p "$base_name"
|
||||
# 使用 -C 指定目标目录
|
||||
tar $tar_opts "$file" -C "$base_name"
|
||||
else
|
||||
echo "📄 检测到单个根项目 ($root_items[1]),直接解压..."
|
||||
tar $tar_opts "$file"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user