Skip to content

Commit 8720563

Browse files
committed
feat(cursor_linux_id_modifier): 改进菜单选择功能和TTY兼容性
- 修改默认选择为"重置"选项,满足默认全部处理的需求 - 添加TTY兼容性支持,在非TTY环境下使用/dev/tty读取输入 - 增加应用光标模式的ANSI码兼容支持(eOA/eOB) - 支持数字键快捷选择(1-N),提高操作便利性 - 修复资源泄露问题,正确关闭TTY句柄 - 改进输入读取逻辑,增强脚本在不同环境下的稳定性
1 parent 18267ae commit 8720563

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

scripts/run/cursor_linux_id_modifier.sh

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,8 @@ generate_new_config() {
969969
970970
# 使用菜单选择函数询问用户是否重置机器码
971971
set +e
972-
select_menu_option "是否需要重置机器码? (通常情况下,只修改js文件即可):" "不重置 - 仅修改js文件即可|重置 - 同时修改配置文件和机器码" 0
972+
# 默认选择“重置”,满足“默认应全部处理”的需求
973+
select_menu_option "是否需要重置机器码? (默认:重置并同步修改配置文件):" "不重置 - 仅修改js文件即可|重置 - 同时修改配置文件和机器码" 1
973974
reset_choice=$?
974975
set -e
975976
@@ -1909,7 +1910,24 @@ select_menu_option() {
19091910
local key_input
19101911
local cursor_up=$'\e[A' # 更标准的 ANSI 码
19111912
local cursor_down=$'\e[B'
1913+
local cursor_up_alt=$'\eOA' # 兼容应用光标模式
1914+
local cursor_down_alt=$'\eOB'
19121915
local enter_key=$'\n'
1916+
# 兼容管道执行场景:stdin 非 TTY 时改用 /dev/tty 读取
1917+
local input_fd=0
1918+
local input_fd_opened=0
1919+
1920+
if [ -t 0 ]; then
1921+
input_fd=0
1922+
elif [ -r /dev/tty ]; then
1923+
exec 3</dev/tty
1924+
input_fd=3
1925+
input_fd_opened=1
1926+
else
1927+
# 无可用 TTY,直接使用默认选项返回
1928+
echo -e "$prompt ${GREEN}${options[$selected_index]}${NC}"
1929+
return $selected_index
1930+
fi
19131931
19141932
# 隐藏光标
19151933
tput civis
@@ -1946,9 +1964,9 @@ select_menu_option() {
19461964
# 读取按键 (使用 -sn1 或 -sn3 取决于系统对箭头键的处理)
19471965
# -N 1 读取单个字符,可能需要多次读取箭头键
19481966
# -N 3 一次读取3个字符,通常用于箭头键
1949-
read -rsn1 key_press_1 # 读取第一个字符
1967+
read -rsn1 -u "$input_fd" key_press_1 # 读取第一个字符
19501968
if [[ "$key_press_1" == $'\e' ]]; then # 如果是 ESC,读取后续字符
1951-
read -rsn2 key_press_2 # 读取 '[' 和 A/B
1969+
read -rsn2 -u "$input_fd" key_press_2 # 读取 '[' 和 A/B
19521970
key_input="$key_press_1$key_press_2"
19531971
elif [[ "$key_press_1" == "" ]]; then # 如果是 Enter
19541972
key_input=$enter_key
@@ -1959,19 +1977,26 @@ select_menu_option() {
19591977
# 检测按键
19601978
case "$key_input" in
19611979
# 上箭头键
1962-
"$cursor_up")
1980+
"$cursor_up"|"$cursor_up_alt")
19631981
if [ $selected_index -gt 0 ]; then
19641982
((selected_index--))
19651983
draw_menu
19661984
fi
19671985
;;
19681986
# 下箭头键
1969-
"$cursor_down")
1987+
"$cursor_down"|"$cursor_down_alt")
19701988
if [ $selected_index -lt $((${#options[@]}-1)) ]; then
19711989
((selected_index++))
19721990
draw_menu
19731991
fi
19741992
;;
1993+
# 数字键选择(1..N),避免方向键不可用
1994+
[1-9])
1995+
if [ "$key_input" -ge 1 ] && [ "$key_input" -le "$num_options" ]; then
1996+
selected_index=$((key_input - 1))
1997+
draw_menu
1998+
fi
1999+
;;
19752000
# Enter键
19762001
"$enter_key")
19772002
# 清除菜单区域
@@ -1983,6 +2008,10 @@ select_menu_option() {
19832008
19842009
# 恢复光标
19852010
tput cnorm
2011+
# 关闭 /dev/tty 句柄,避免资源占用
2012+
if [ "$input_fd_opened" -eq 1 ]; then
2013+
exec 3<&-
2014+
fi
19862015
# 返回选择的索引
19872016
return $selected_index
19882017
;;

0 commit comments

Comments
 (0)