diff --git a/fish/functions/execute-script.fish b/fish/functions/execute-script.fish index 70ce717..0b3f551 100644 --- a/fish/functions/execute-script.fish +++ b/fish/functions/execute-script.fish @@ -25,14 +25,24 @@ function execute-script if test -n "$script_path" -a -f "$script_path" echo "Running: $script_path" + set -l run_status 0 if test -x $script_path $script_path $argv[2..-1] + set run_status $status else if string match -q "*.py" $script_path python3 $script_path $argv[2..-1] + set run_status $status else bash $script_path $argv[2..-1] + set run_status $status end - return $status + if test "$script_name" = toggle-region + if not functions -q region_apply_from_custom + source ~/.dotfile/fish/functions/region.fish + end + region_apply_from_custom + end + return $run_status end echo "Error: Script not found: $script_name" diff --git a/fish/functions/ff.fish b/fish/functions/ff.fish index dd0c1ab..263e2fc 100644 --- a/fish/functions/ff.fish +++ b/fish/functions/ff.fish @@ -112,19 +112,16 @@ function ff return 1 end case region + if not functions -q region_toggle + source ~/.dotfile/fish/functions/region.fish + end set -l sub toggle if test (count $argv) -ge 2 set sub $argv[2] end switch $sub case toggle - if test "$CN_REGION" = "1" - set -gx CN_REGION 0 - echo "Switched to: 0 (国外)" - else - set -gx CN_REGION 1 - echo "Switched to: 1 (国内)" - end + region_toggle case '*' echo "Usage: ff region [toggle]" echo "Unknown subcommand: $sub" diff --git a/fish/functions/region.fish b/fish/functions/region.fish new file mode 100644 index 0000000..c78ff30 --- /dev/null +++ b/fish/functions/region.fish @@ -0,0 +1,58 @@ +# CN_REGION 读写 ~/.config/fish/custom.fish(与 script/common/region.sh 约定一致) + +function region_custom_path + echo ~/.config/fish/custom.fish +end + +function region_read + set -l path (region_custom_path) + if test -f $path + set -l matches (grep -E '^\s*set\s+-gx\s+CN_REGION\s+' $path 2>/dev/null) + if test (count $matches) -gt 0 + set -l line $matches[-1] + echo (string trim (string replace -r '.*\s' '' $line)) + return + end + end + if set -q CN_REGION + echo $CN_REGION + else + echo 0 + end +end + +function region_write + set -l new $argv[1] + set -l path (region_custom_path) + mkdir -p (dirname $path) + + set -l tmp (mktemp) + if test -f $path + grep -Ev '^\s*# dotfile: CN_REGION|^\s*set\s+-gx\s+CN_REGION\s+' $path >$tmp 2>/dev/null; or true + else + touch $tmp + end + echo "# dotfile: CN_REGION (managed by ff region / toggle-region)" >>$tmp + echo "set -gx CN_REGION $new" >>$tmp + mv $tmp $path + + set -gx CN_REGION $new +end + +function region_toggle + set -l current (region_read) + set -l new + if test "$current" = "1" + set new 0 + echo "Switched to: 0 (国外)" + else + set new 1 + echo "Switched to: 1 (国内)" + end + region_write $new + echo "Persisted to ~/.config/fish/custom.fish" +end + +function region_apply_from_custom + set -gx CN_REGION (region_read) +end diff --git a/script/common/region.sh b/script/common/region.sh new file mode 100644 index 0000000..4090f7e --- /dev/null +++ b/script/common/region.sh @@ -0,0 +1,71 @@ +# CN_REGION 持久化到 ~/.config/fish/custom.fish(供 toggle-region / ff region 使用) + +dotfile_region_custom_fish() { + echo "${HOME}/.config/fish/custom.fish" +} + +dotfile_region_read() { + local custom val + custom="$(dotfile_region_custom_fish)" + if [ -f "$custom" ]; then + val=$( + grep -E '^[[:space:]]*set[[:space:]]+-gx[[:space:]]+CN_REGION[[:space:]]+' "$custom" 2>/dev/null | + tail -1 | + awk '{print $NF}' + ) + if [ -n "$val" ]; then + echo "$val" + return 0 + fi + fi + if [ -n "${CN_REGION:-}" ]; then + echo "$CN_REGION" + else + echo "0" + fi +} + +dotfile_region_write() { + local val="$1" + local custom tmp + custom="$(dotfile_region_custom_fish)" + mkdir -p "$(dirname "$custom")" + tmp="$(mktemp)" + if [ -f "$custom" ]; then + grep -Ev '^[[:space:]]*# dotfile: CN_REGION|^[[:space:]]*set[[:space:]]+-gx[[:space:]]+CN_REGION[[:space:]]+' \ + "$custom" >"$tmp" || true + else + : >"$tmp" + fi + { + cat "$tmp" + if [ -s "$tmp" ]; then + echo + fi + echo "# dotfile: CN_REGION (managed by ff region / toggle-region)" + echo "set -gx CN_REGION ${val}" + } >"${custom}.new" + mv "${custom}.new" "$custom" + rm -f "$tmp" +} + +dotfile_region_toggle() { + local current next + current="$(dotfile_region_read)" + if [ "$current" = "1" ]; then + next=0 + else + next=1 + fi + dotfile_region_write "$next" + export CN_REGION="$next" + echo "$next" +} + +dotfile_region_label() { + if [ "$1" = "1" ]; then + echo "国内" + else + echo "国外" + fi +} diff --git a/script/toggle-region.sh b/script/toggle-region.sh index 9fe5eea..899ae07 100644 --- a/script/toggle-region.sh +++ b/script/toggle-region.sh @@ -1,33 +1,30 @@ #!/bin/bash -# 切换 CN_REGION 环境变量(国内/国外镜像与安装源) +# 切换 CN_REGION 并持久化到 ~/.config/fish/custom.fish SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # shellcheck disable=SC1091 source "$SCRIPT_DIR/common/help.sh" +# shellcheck disable=SC1091 +source "$SCRIPT_DIR/common/region.sh" usage() { cat <<'EOF' 用法: toggle-region -在当前 shell 中切换 CN_REGION: +切换 CN_REGION 并写入 ~/.config/fish/custom.fish: 1 -> 0 国内 -> 国外 0/未设置 -> 1 国外 -> 国内 -影响依赖 CN_REGION 的安装脚本(如 docker-install)。 +fish 中推荐: ff region +通过 ff script 调用后,当前 fish 会话会自动同步该变量。 选项: -h, --help 显示此帮助 - -说明: 仅影响当前 shell 会话;持久化请写入 fish/zsh 配置。 EOF } dotfile_help_requested "${1:-}" && dotfile_show_help -if [ "$CN_REGION" = "1" ]; then - export CN_REGION=0 - echo "[INFO] Region switched to: 0 (国外)" -else - export CN_REGION=1 - echo "[INFO] Region switched to: 1 (国内)" -fi +next="$(dotfile_region_toggle)" +echo "[INFO] Region switched to: ${next} ($(dotfile_region_label "$next"))" +echo "[INFO] 已写入 $(dotfile_region_custom_fish)"