Files
dotfiles/fish/add-on.fish
T
FlintyLemming 50688c76fd 修复空目录下 ls 函数报数组下标越界
macOS 自带的 BSD seq 在 first > last 时默认步长为 -1,所以空目录下
`seq (count $lines)` 即 `seq 0` 会输出 "1 0" 而非空,导致循环取到
$lines[0] 报 "array indices start at 1, not 0"(GNU seq 无此问题)。
循环前加空数组短路返回。

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-31 04:56:41 -07:00

239 lines
7.7 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.
# =============================================================================
# 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 是你预期的默认行为)
set -l lines ($eza_bin -la --icons=always --color=always $argv)
# 建立纯净路径行:过滤掉所有格式化干扰项,确保只拿到绝对路径
# 必须保留你的自定义排序/过滤参数(如 --sort),但剔除 -l 和 --icons
set -l clean_args
for arg in $argv
switch $arg
case '-l' '--long' '--icons'
# 忽略影响长格式和图标的参数
case '-la' '-al' '-lA' '-Al'
# 如果是组合参数,剥离掉 'l',保留 'a' 或 'A' 保证文件对齐
set -l stripped (string replace -a 'l' '' $arg)
if test "$stripped" != "-"
set -a clean_args $stripped
end
case '*'
set -a clean_args $arg
end
end
# 核心修正:只用 -a(显示隐藏)和 -1(单列),配合 --absolute=on 拿到纯路径
set -l paths ($eza_bin -a -1 --absolute=on --color=never $clean_args 2>/dev/null)
# 安全网:如果由于特殊参数导致行数不匹配,安全回退到原装输出防止错位
if test (count $lines) -ne (count $paths)
$eza_bin -la --icons=always $argv
return
end
# 空目录直接返回:BSD seqmacOS)遇到 `seq 0` 会倒数输出 "1 0"
# 导致下面用 $lines[0] 取值时报 "array indices start at 1"
if test (count $lines) -eq 0
return
end
# 双路循环渲染
for i in (seq (count $lines))
set -l line $lines[$i]
set -l target_path $paths[$i]
# 此时 $target_path 是完美的纯绝对路径,test 能够精准命中
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