fix: toggle-resion
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user