159 lines
4.3 KiB
Fish
159 lines
4.3 KiB
Fish
# 兼容性添加 PATH
|
||
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
|
||
|
||
# 禁用 Fish 默认欢迎语
|
||
set -g fish_greeting ""
|
||
|
||
function fish_greeting
|
||
echo ""
|
||
# 1. 顶部欢迎与 UTC-8 时间
|
||
set_color -o cyan
|
||
echo "Welcome back, $USER"
|
||
# 在 TZ 环境变量中,GMT+8 指向西八区 (UTC-8)
|
||
set -l current_time (env TZ="GMT-8" date "+%Y-%m-%d %H:%M:%S")
|
||
echo "Time (UTC-8): $current_time"
|
||
set_color normal
|
||
echo ""
|
||
|
||
# 3. 包管理器逻辑 (优先检查 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
|
||
|
||
## 替换 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 lt --description 'ls sorted by time'
|
||
ls --sort=modified $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
|
||
|
||
## 让 sudo 能够识别自定义 bin 下的脚本
|
||
function sudo --description "Replacement for sudo that preserves custom PATH commands"
|
||
# 1. 如果没有任何参数,直接运行原始 sudo (通常显示帮助)
|
||
if not set -q argv[1]
|
||
command sudo
|
||
return
|
||
end
|
||
|
||
# 2. 如果第一个参数是以 "-" 开头的选项 (比如 -i, -s, -u, -E)
|
||
# 我们直接把所有参数原样传给真正的 sudo,不做路径解析
|
||
if string match -q -- "-*" $argv[1]
|
||
command sudo $argv
|
||
return
|
||
end
|
||
|
||
# 3. 如果第一个参数是普通的命令名 (比如 zp, apt, vim)
|
||
# 使用 type -p 获取其完整路径。使用 -- 防止命令名本身带杠导致 type 报错
|
||
set -l command_path (type -p -- $argv[1] 2>/dev/null)
|
||
|
||
if test -n "$command_path"
|
||
# 找到了完整路径,调用真正的 sudo 运行该绝对路径
|
||
command sudo $command_path $argv[2..-1]
|
||
else
|
||
# 没找到路径(可能是内建命令或拼写错误),直接交给真正的 sudo 处理
|
||
command sudo $argv
|
||
end
|
||
end
|
||
|
||
function portcheck
|
||
# 设置起始端口,如果没有传入参数则默认为 8840
|
||
set -l port 8840
|
||
if test (count $argv) -gt 0
|
||
set port $argv[1]
|
||
end
|
||
|
||
# 循环检查端口是否被占用
|
||
# -t: TCP, -u: UDP, -l: Listening, -n: Numeric
|
||
while ss -tuln | grep -q ":$port "
|
||
set port (math $port + 1)
|
||
end
|
||
|
||
echo $port
|
||
end
|
||
|
||
abbr -a port 'sudo ss -tulnp | grep'
|
||
abbr -a process 'ps aux | grep'
|
||
|
||
alias fastfetch="fastfetch --color-keys blue" |