Add interactive menu regression

This commit is contained in:
2026-04-17 11:09:25 +08:00
parent b5ae4a5668
commit 7b854c7a96
8 changed files with 114 additions and 18 deletions

View File

@@ -150,17 +150,16 @@ print_section() {
}
read_test_input() {
local reply=''
READ_TEST_INPUT_RESULT=''
if [[ -n ${IPF_TEST_INPUTS-} ]]; then
if [[ ${IPF_TEST_INPUTS} == *$'\n'* ]]; then
reply=${IPF_TEST_INPUTS%%$'\n'*}
READ_TEST_INPUT_RESULT=${IPF_TEST_INPUTS%%$'\n'*}
IPF_TEST_INPUTS=${IPF_TEST_INPUTS#*$'\n'}
else
reply=${IPF_TEST_INPUTS}
READ_TEST_INPUT_RESULT=${IPF_TEST_INPUTS}
IPF_TEST_INPUTS=''
fi
fi
printf '%s' "${reply}"
}
prompt_input() {
@@ -169,10 +168,12 @@ prompt_input() {
local reply=''
if [[ -n ${IPF_TEST_INPUTS-} ]]; then
reply=$(read_test_input)
read_test_input
reply=${READ_TEST_INPUT_RESULT}
if [[ -z ${reply} && -n ${default_value} ]]; then
reply=${default_value}
fi
PROMPT_INPUT_RESULT=${reply}
printf '%s\n' "${reply}" >&2
printf '%s\n' "${reply}"
return 0
@@ -184,9 +185,14 @@ prompt_input() {
else
read -r -p "${prompt}: " reply
fi
PROMPT_INPUT_RESULT=${reply}
printf '%s\n' "${reply}"
}
prompt_input_capture() {
prompt_input "$@" >/dev/null
}
prompt_confirm() {
local prompt=$1
local default_choice=${2:-n}
@@ -198,7 +204,8 @@ prompt_confirm() {
suffix='[y/N]'
fi
answer=$(prompt_input "${prompt} ${suffix}" "")
prompt_input_capture "${prompt} ${suffix}" ""
answer=${PROMPT_INPUT_RESULT}
if [[ -z ${answer} ]]; then
answer=${default_choice}
fi
@@ -218,15 +225,21 @@ prompt_select() {
done
while true; do
answer=$(prompt_input "${prompt}" "")
prompt_input_capture "${prompt}" ""
answer=${PROMPT_INPUT_RESULT}
if [[ ${answer} =~ ^[0-9]+$ ]] && (( answer >= 1 && answer <= count )); then
printf '%s\n' "$((answer - 1))"
PROMPT_SELECT_RESULT=$((answer - 1))
printf '%s\n' "${PROMPT_SELECT_RESULT}"
return 0
fi
log_warn "请输入 1-${count} 之间的编号。"
done
}
prompt_select_capture() {
prompt_select "$@" >/dev/null
}
pause_return() {
if [[ -n ${IPF_TEST_INPUTS-} ]]; then
return 0
@@ -272,7 +285,7 @@ _validate_ipv6_part_count() {
continue
fi
[[ ${field} =~ ^[0-9A-Fa-f]{1,4}$ ]] || return 1
((count++))
((count += 1))
done
printf '%s\n' "${count}"

View File

@@ -216,7 +216,8 @@ cmd_add() {
local proto_choice ipver_choice proto ipver lport tip tport desc
print_section '添加新的转发规则'
proto_choice=$(prompt_select '请选择协议' 'TCP' 'UDP' 'TCP + UDP')
prompt_select_capture '请选择协议' 'TCP' 'UDP' 'TCP + UDP'
proto_choice=${PROMPT_SELECT_RESULT}
case ${proto_choice} in
0) proto=tcp ;;
1) proto=udp ;;
@@ -225,12 +226,14 @@ cmd_add() {
esac
while true; do
lport=$(prompt_input '请输入本地监听端口' '')
prompt_input_capture '请输入本地监听端口' ''
lport=${PROMPT_INPUT_RESULT}
validate_port "${lport}" && break
log_warn '端口无效,请重新输入。'
done
ipver_choice=$(prompt_select '请选择 IP 版本' '仅 IPv4' '仅 IPv6' '同时 IPv4 + IPv6')
prompt_select_capture '请选择 IP 版本' '仅 IPv4' '仅 IPv6' '同时 IPv4 + IPv6'
ipver_choice=${PROMPT_SELECT_RESULT}
case ${ipver_choice} in
0) ipver=4 ;;
1) ipver=6 ;;
@@ -239,18 +242,21 @@ cmd_add() {
esac
while true; do
tip=$(prompt_input "$(rule_target_hint "${ipver}")" '')
prompt_input_capture "$(rule_target_hint "${ipver}")" ''
tip=${PROMPT_INPUT_RESULT}
rule_validate_target "${tip}" "${ipver}" && break
log_warn '目标地址无效,请重新输入。'
done
while true; do
tport=$(prompt_input '请输入目标端口' '')
prompt_input_capture '请输入目标端口' ''
tport=${PROMPT_INPUT_RESULT}
validate_port "${tport}" && break
log_warn '目标端口无效,请重新输入。'
done
desc=$(prompt_input '请输入描述(可留空)' '')
prompt_input_capture '请输入描述(可留空)' ''
desc=${PROMPT_INPUT_RESULT}
desc=$(rule_sanitize_desc "${desc}")
printf '协议: %s\n' "$(rule_proto_label "${proto}")"
@@ -319,7 +325,8 @@ cmd_delete() {
cmd_list 0
while true; do
answer=$(prompt_input '请输入要删除的规则编号' '')
prompt_input_capture '请输入要删除的规则编号' ''
answer=${PROMPT_INPUT_RESULT}
if [[ ${answer} =~ ^[0-9]+$ ]] && (( answer >= 1 && answer <= ${#RULES_CACHE[@]} )); then
index=$((answer - 1))
break

View File

@@ -127,7 +127,7 @@ storage_count() {
local count=0 line
while IFS= read -r line || [[ -n ${line} ]]; do
[[ -n ${line} ]] || continue
((count++))
((count += 1))
done < <(storage_list)
printf '%s\n' "${count}"
}