Files
dotfiles/fish/add-on.fish
FlintyLemming b086a2abc8 add fish config
2025-12-19 15:52:48 +08:00

152 lines
4.6 KiB
Fish
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
function sudo --description "让 sudo 识别 fish 函数"
if functions -q $argv[1]
set -l cmd_name $argv[1]
# 获取函数定义,并传递给 sudo 开启的 fish 环境执行
set -l cmd_body (functions $cmd_name)
command sudo fish -c "$cmd_body; $argv"
else
command sudo $argv
end
end
## 替换 ls 默认行为
function ls --description 'alias ls to eza if possible'
# 1. 优先使用系统安装的 eza (PATH 里的)
if type -q eza
eza -la --icons $argv
return
end
# 2. 如果系统没有,尝试根据架构寻找 ~/.flinty/bin 下的二进制文件
set -l arch (uname -m)
set -l target_bin ""
set -l bin_path "$HOME/.flinty/bin"
switch $arch
case x86_64
# 如果是 Alpine Linux通常需要用 musl 版本 (根据你提供的文件名)
if test -f /etc/alpine-release
set target_bin "eza_x86_64-unknown-linux-musl"
else
set target_bin "eza_x86_64-unknown-linux-gnu"
end
case aarch64 arm64
set target_bin "eza_aarch64-unknown-linux-gnu"
end
set -l custom_eza "$bin_path/$target_bin"
# 3. 检查文件是否存在且可执行
if test -n "$target_bin"; and test -x "$custom_eza"
$custom_eza -la --icons $argv
return
end
# 4. 既没有系统 eza 也没有本地二进制,回退到 command ls
command ls -la $argv
end
## 更新配置
function dotu
set -l current_dir (pwd)
cd $HOME/.flinty
if git pull
source ~/.config/fish/config.fish
echo "Config reloaded successfully!"
end
# 回到原位
cd $current_dir
end
## 修复光标消失
function restore_cursor --on-event fish_postexec
tput cnorm
end
# 自动化 Locale 修复逻辑 修复例如 nano 无法输入中文的问题
# 1. 如果 locale 命令执行报错
# 2. 或者 (LC_ALL 为空 并且 LANG 以 C 开头)
if not locale >/dev/null 2>&1
or begin
test -z "$LC_ALL"
and string match -qi "C*" "$LANG"
end
# 开始修复
if locale -a | string match -qi "C.utf8"
set -gx LANG C.UTF-8
set -gx LC_ALL C.UTF-8
else if locale -a | string match -qi "en_US.utf8"
set -gx LANG en_US.UTF-8
set -gx LC_ALL en_US.UTF-8
end
# 清理非法的 LC_CTYPE
set -e LC_CTYPE
end
# 多线程压缩为同名 zip支持传入多项目
function zp
set -l arg_count (count $argv)
# 检查是否有输入参数
if test $arg_count -eq 0
echo "用法: zp <文件或目录1> [文件或目录2] ..."
return 1
end
if test $arg_count -eq 1
# 逻辑 A单个参数保持原样生成同名 zip
set -l name (string trim -r -c / $argv[1])
set -l target "$name.zip"
echo "🚀 正在多线程压缩单项目: $target"
7z a -tzip -mmt=on -mx=5 "$target" "$argv[1]"
else
# 逻辑 B多个参数生成 package_20251219_1540.zip
set -l timestamp (date "+%Y%m%d_%H%M%S")
set -l target "package_$timestamp.zip"
echo "🚀 正在将 $arg_count 个项目打包至: $target"
# 直接传入 $argv7z 会自动处理多个路径
7z a -tzip -mmt=on -mx=5 "$target" $argv
end
end
# 多线程解压 zip
function uzp
for file in $argv
if not test -f "$file"
echo "错误: $file 不是有效文件"
continue
end
# 1. 获取不带后缀的文件名作为备选文件夹名
set -l base_name (string replace -r '\.[^.]+$' '' $file)
# 2. 预检:获取压缩包根路径下的唯一项目数量
# 逻辑:列出所有路径 -> 提取第一级目录/文件名 -> 去重 -> 计数
set -l root_items (7z l -slt "$file" | grep "^Path = " | string replace -r '^Path = ' '' | string replace -r '/.*$' '' | sort -u)
set -l item_count (count $root_items)
if test $item_count -eq 0
echo "跳过: $file 是空的"
continue
end
# 3. 智能判断执行
if test $item_count -gt 1
# 如果根部有多个文件/文件夹,创建同名目录解压
echo "📦 检测到多个根项目 ($item_count),解压至目录: $base_name/"
mkdir -p "$base_name"
7z x -mmt=on "$file" -o"$base_name"
else
# 如果根部只有一个项目,直接解压到当前目录
echo "📄 检测到单个根项目,直接解压..."
7z x -mmt=on "$file"
end
end
end
abbr -a port 'sudo ss -tulnp | grep'
abbr -a process 'ps aux | grep'