Files
dotfiles/fish/add-on.fish
T
2026-06-24 05:29:12 +00:00

214 lines
6.7 KiB
Fish

# =============================================================================
# 1. 环境配置 & 路径兼容
# =============================================================================
if functions -q fish_add_path
fish_add_path ~/.flinty/bin
else
# 针对 Fish < 3.2.0 的版本
if not contains "$HOME/.flinty/bin" $PATH
set -gx PATH "$HOME/.flinty/bin" $PATH
end
end
set -gx LANG C.UTF-8
set -gx LC_ALL C.UTF-8
set -gx EDITOR nano
# 禁用 Fish 默认欢迎语
set -g fish_greeting ""
function fish_greeting
echo ""
# 顶部欢迎与 补正后的时区时间
set_color -o cyan
echo "Welcome back, $USER"
# 在 TZ 环境变量中,GMT-8 指向东八区 (北京时间)
set -l current_time (env TZ="GMT-8" date "+%Y-%m-%d %H:%M:%S")
echo "Time (Asia/Shanghai): $current_time"
set_color normal
echo ""
# 包管理器逻辑 (优先检查 oma)
set -l pm_name ""
if type -q oma
set pm_name (set_color -o green)"oma"(set_color normal)
else
# 回退到系统默认包管理器
if type -q brew; set pm_name "Homebrew"
else if type -q apt; set pm_name "apt"
else if type -q pacman; set pm_name "pacman"
else if type -q dnf; set pm_name "dnf"
else; set pm_name "Default PM"
end
end
echo "Package Manager: $pm_name"
echo ""
end
# =============================================================================
# 2. 核心增强函数 (ls / eza 包装器)
# =============================================================================
function ls --description 'alias ls to eza with directory `.tldr` descriptions'
# 1. 核心路由:确定可用的 eza 二进制文件路径(融合你原有的多架构判定)
set -l eza_bin ""
if type -q eza
set eza_bin "eza"
else
set -l arch (uname -m)
set -l target_bin ""
switch $arch
case x86_64
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 "$HOME/.flinty/bin/$target_bin"
if test -n "$target_bin"; and test -x "$custom_eza"
set eza_bin "$custom_eza"
end
end
# 2. 兜底策略:如果最终没有找到任何 eza,完全回退到原生 command ls
if test -z "$eza_bin"
command ls -la $argv
return
end
# 3. 动态描述注入核心引擎
# 捕获带颜色的标准输出 (继承你原本期望的 -la --icons)
set -l lines ($eza_bin -la --icons --color=always $argv)
# 捕获绝对路径列表用于精准定位 .tldr 文件
set -l paths ($eza_bin -la --icons -1 --absolute=on --color=never $argv 2>/dev/null)
# 安全网:如果由于特殊参数导致行数不匹配,安全回退到原装输出
if test (count $lines) -ne (count $paths)
$eza_bin -la --icons $argv
return
end
# 双路循环渲染
for i in (seq (count $lines))
set -l line $lines[$i]
set -l target_path $paths[$i]
# 检查是否为目录,且该目录下是否存在 .tldr 备注文件
if test -d "$target_path"; and test -f "$target_path/.tldr"
set -l desc (head -n 1 "$target_path/.tldr" | string trim)
if test -n "$desc"
# 在行尾追加青色 (Cyan) 的备忘后缀
echo -e "$line \e[36m<- $desc\e[0m"
continue
end
end
echo -e "$line"
end
end
## 按修改时间排序列出文件 (由于 ls 已增强,此命令自动继承备忘显示)
function lt --description 'ls sorted by time'
ls --sort=modified $argv
end
# =============================================================================
# 3. 运维与效率工具函数
# =============================================================================
## 快捷为目录添加或查看备忘贴纸
function tldr --description 'Add or view description note for a directory'
if test (count $argv) -eq 0
echo "Usage: tldr \"你的项目描述\" [目录路径]"
echo " Or: tldr -d [目录路径] (删除备忘)"
return 1
end
if test "$argv[1]" = "-d"
set -l target_dir "."
if test (count $argv) -gt 1; set target_dir $argv[2]; end
if test -f "$target_dir/.tldr"
rm "$target_dir/.tldr"
echo "已移除 '$target_dir' 的备忘贴纸。"
end
return 0
end
set -l desc $argv[1]
set -l target_dir "."
if test (count $argv) -gt 1; set target_dir $argv[2]; end
if not test -d "$target_dir"
echo (set_color -o red)"Error: $target_dir 不是一个有效的目录"(set_color normal)
return 1
end
echo "$desc" > "$target_dir/.tldr"
echo "成功贴上标签 ➜ 有效目录: '$target_dir' [ $desc ]"
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
## 自动递增探测可用端口
function portcheck
set -l port 8840
if test (count $argv) -gt 0; set port $argv[1]; end
while ss -tuln | grep -q ":$port "
set port (math $port + 1)
end
echo $port
end
## Yazi 联动:退出时自动切换到最后所在的目录
function y
set -l tmp (mktemp -t "yazi-cwd.XXXXXX")
yazi $argv --cwd-file="$tmp"
if set cwd (cat -- "$tmp"); and [ -n "$cwd" ]; and [ "$cwd" != "$PWD" ]
builtin cd -- "$cwd"
end
rm -f -- "$tmp"
end
# =============================================================================
# 4. 别名与缩写 (Abbr / Alias)
# =============================================================================
abbr -a cc 'claude --permission-mode bypassPermissions'
abbr -a port 'sudo ss -tulnp | grep'
abbr -a process 'sudo ps aux | grep'
abbr -a service 'sudo systemctl list-units --type=service --all | grep'
alias fastfetch="fastfetch --color-keys blue"
## 会话级代理控制
function proxy --description 'Set HTTP proxy for current session'
echo -n "HTTP proxy address (e.g. http://192.168.5.14:6152): "
read proxy_addr
if test -n "$proxy_addr"
set -gx HTTP_PROXY "$proxy_addr"
set -gx HTTPS_PROXY "$proxy_addr"
echo "Proxy set: $proxy_addr"
else
echo "Proxy cleared"
set -e HTTP_PROXY
set -e HTTPS_PROXY
end
end