You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java CLI中实现类似Go survey库的交互式选择组件?

实现Java CLI交互式选择元素的可行方案

方案1:使用JLine库

JLine是专注于Java命令行交互的工具库,完美支持Windows的cmd、PowerShell以及类Unix终端,可快速实现类似go-survey的选择交互效果。

  • 核心特性:支持光标移动、菜单选择、自动补全,兼容主流终端环境
  • 基础示例代码:
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.reader.impl.completer.CandidateListCompleter;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import java.util.List;

public class CliSelector {
    public static void main(String[] args) throws Exception {
        Terminal terminal = TerminalBuilder.builder().build();
        LineReader reader = LineReaderBuilder.builder()
                .terminal(terminal)
                .completer(new CandidateListCompleter(List.of(
                        new CandidateListCompleter.Candidate("选项1"),
                        new CandidateListCompleter.Candidate("选项2"),
                        new CandidateListCompleter.Candidate("选项3")
                )))
                .build();

        String selection = reader.readLine("请选择一个选项: ");
        terminal.writer().println("你选择了: " + selection);
        terminal.flush();
    }
}
  • 进阶优化:可自定义菜单渲染逻辑,通过监听键盘事件实现上下箭头切换选中项的交互,完全匹配go-survey的操作体验。

方案2:自行基于Java控制台API实现轻量选择器

若不想引入第三方依赖,可直接利用Java原生控制台API实现基础选择功能,适配Windows终端。

  • 实现思路:

    1. 打印选项列表,用特殊标记(如*)标识当前选中项
    2. 监听上下箭头按键(需适配不同终端的键盘编码)
    3. 刷新控制台输出,实时更新选中项标记
    4. 监听回车键确认选择
  • 简化示例代码:

import java.io.IOException;

public class SimpleSelector {
    private static final String[] OPTIONS = {"选项A", "选项B", "选项C"};
    private static int selectedIndex = 0;

    public static void main(String[] args) throws IOException {
        while (true) {
            // 清屏(兼容多数终端)
            System.out.print("\033[H\033[2J");
            System.out.flush();

            // 打印选项列表
            for (int i = 0; i < OPTIONS.length; i++) {
                System.out.println(i == selectedIndex ? "* " + OPTIONS[i] : "  " + OPTIONS[i]);
            }
            System.out.println("\n使用上下箭头选择,回车确认");

            // 处理键盘输入
            int key = System.in.read();
            if (key == 10) { // 回车键
                break;
            } else if (key == 27) { // 箭头键前缀
                System.in.read(); // 跳过[
                int arrowKey = System.in.read();
                if (arrowKey == 65 && selectedIndex > 0) { // 上箭头
                    selectedIndex--;
                } else if (arrowKey == 66 && selectedIndex < OPTIONS.length - 1) { // 下箭头
                    selectedIndex++;
                }
            }
        }
        System.out.println("你选择了: " + OPTIONS[selectedIndex]);
    }
}
  • 注意:需针对Windows cmd/PowerShell和类Unix终端的键盘编码差异做少量适配,确保箭头键响应正常。

方案3:结合Apache Commons CLI扩展交互

Apache Commons CLI本身用于处理命令行参数,可搭配自定义输入逻辑实现选择交互,但更推荐直接使用JLine,它对交互式场景的支持更完备。


内容的提问来源于stack exchange,提问作者JF Meier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 11:35:16