bash上下键选择选项demo脚本

效果如下:

废话不多说,上代码:

bash 复制代码
#!/bin/bash

options=("111" "222" "333" "444")  # 选项列表
options_index=0  # 默认选中第一个选项
options_len=${#options[@]}


echo "请用上下方向键进行选择,空格键选中/取消,回车键确认结果"

# 定义一个数组来存储选中的结果
selected=()
for ((i=0; i<${options_len}; i++));do
    selected[$i]=0
done

# 渲染选项列表
render_options() {
    for i in "${!options[@]}"; do
        # 首先渲染已经选中的选项
        if [ ${selected[$i]} -eq 1 ];then
            if [ $i -eq $options_index ]; then
                echo -e "\033[1;41;34m${options[$i]}\033[0m"  # 已选中,已选择
            else
                echo -e "\033[1;41;33m${options[$i]}\033[0m"  # 已选中,未选择
            fi
        elif [ $i -eq $options_index ]; then
            echo -e "\033[1;34m${options[$i]}\033[0m"  # 未选中,已选择
        else
            echo "${options[$i]}"  # 未选中,未选择
        fi
    done
}

# 初始渲染
render_options

# 为了让read能读到空格键
IFS_store=$IFS
IFS=''

while true; do
    read -s -n 1 key  # 读取单个按键输入,不显示在终端上

    case $key in
        "A")  # 上箭头键
            if [ $options_index -gt 0 ]; then
                options_index=$((options_index - 1))
            # 在第一行按上键,到最后一行
            elif [ $options_index -eq 0 ]; then
                options_index=$((${options_len} - 1))
            fi
            ;;
        "B")  # 下箭头键
            if [ $options_index -lt $(( ${options_len} - 1 )) ]; then
                options_index=$((options_index + 1))
            # 在最后一行按下键,到第一行
            elif [ $options_index -eq $(( ${options_len} - 1 )) ]; then
                options_index=0
            fi
            ;;
        " ")  # 空格键
            # selected[$options_index]的值,0、1切换
            selected[$options_index]=$((1 - ${selected[$options_index]}))
            ;;
        "")  # 回车键
            break
            ;;
    esac

    tput cuu ${options_len}  # 光标移动回到选项列表的开头
    tput ed  # 清除当前行
    render_options  # 重新渲染选项列表
done

# 恢复IFS变量
IFS=$IFS_store

# 最后选中的所有结果
result=()
for ((i=0; i<options_len; i++));do
    if [ ${selected[$i]} -eq 1 ];then
        result+=(${options[$i]})
    fi
done

echo "选中:${result[@]}"
相关推荐
honker_X1 小时前
Python字符串全攻略:从入门到精通
开发语言·python
半杯咖啡半行码1 小时前
Qt 隐式共享(Implicit Sharing)深度解析:机制、陷阱与 Qt6迁移
开发语言·数据库·qt
吃我一窝jio1 小时前
Docker+Jenkins+K8s全流程部署
java·开发语言·笔记·python
eggwyw2 小时前
C++设计模式之解释器模式
开发语言·c++
mengge.cloud4 小时前
Python基础保姆级!从入门到入土
开发语言·python·云原生·pygame
在爪洼山玩泥巴5 小时前
fastjson2 JSONPath 取不存在路径,为什么会抛 “not support”?
java·开发语言·jsonpath·fastjson
星落梦生14 小时前
SpringAOP基础笔记(7)
java·开发语言
曹牧17 小时前
Java:“刷新雪崩”
java·开发语言
郝学胜-神的一滴17 小时前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl
西西弗Sisyphus18 小时前
C++ explicit 关键字工程实践
开发语言·c++