Sign in

    #MoonBit Inquirer

    一个用于MoonBit的命令行交互式问答库,灵感来自npm的Inquirer.js。

    #功能特性

    MoonBit Inquirer提供优雅的方式来创建交互式命令线界面,支持以下问题类型:

    • Input: 文本输入
    • Confirm: 确认(是/否)
    • List: 列表选择
    • RawList: 带数字的列表选择
    • Expand: 展开式选择
    • Checkbox: 多选框
    • Password: 密码输入
    • Editor: 多行文本输入

    注意: 当前版本是占位符实现,返回默认值而不进行实际的交互式输入。完整功能将在MoonBit的async/stdio API稳定后实现。

    #安装

    将此库添加到您的MoonBit项目:

    moon add wangmingfa/promptx/inquirer

    #API说明

    #核心函数

    • prompt(questions: Array[Question]) -> Answer: 处理问题数组并返回答案

    #问题构造函数

    每个问题类型都有对应的构造函数:

    • input(question_name~: String, question_message~: String, question_default?: AnswerValue?) -> Question
    • confirm(question_name~: String, question_message~: String, question_default?: AnswerValue?) -> Question
    • list(question_name~: String, question_message~: String, question_choices~: Array[String], question_default?: AnswerValue?) -> Question
    • rawlist(question_name~: String, question_message~: String, question_choices~: Array[String], question_default?: AnswerValue?) -> Question
    • expand(question_name~: String, question_message~: String, question_choices~: Array[String], question_default?: AnswerValue?) -> Question
    • checkbox(question_name~: String, question_message~: String, question_choices~: Array[String], question_default?: AnswerValue?) -> Question
    • password(question_name~: String, question_message~: String, question_default?: AnswerValue?) -> Question
    • editor(question_name~: String, question_message~: String, question_default?: AnswerValue?) -> Question

    #数据类型

    pub enum QuestionType {
    Input
    Confirm
    List
    RawList
    Expand
    Checkbox
    Password
    Editor
    }

    pub enum AnswerValue {
    StringValue(String)
    BoolValue(Bool)
    ArrayValue(Array[String])
    IntValue(Int)
    }

    pub type Answer = Map[String, AnswerValue]

    pub struct Question {
    question_type : QuestionType
    question_name : String
    question_message : String
    question_default : AnswerValue?
    question_choices : Array[String]?
    }

    #测试

    运行测试:

    moon test inquirer

    当前测试覆盖了:
    • 所有问题类型的创建
    • 答案值的类型匹配
    • parse_int函数
    • 辅助函数

    #开发状态

    • ✅ 核心类型定义
    • ✅ API设计
    • ✅ 单元测试
    • ⏳ 交互式输入(等待MoonBit async API稳定)
    • ⏳ 验证和过滤功能

    #许可证

    Apache-2.0

    #贡献

    欢迎贡献代码!请提交Pull Request。

    Answer

    type Answer = Map[String, AnswerValue]

    Mutable linked hash map that maintains the order of insertion, not thread safe.

    Example

    test {
    let map = { 3: "three", 8: "eight", 1: "one" }
    @test.assert_eq(map.get(2), None)
    @test.assert_eq(map.get(3), Some("three"))
    map.set(3, "updated")
    @test.assert_eq(map.get(3), Some("updated"))
    }

    AnswerValue

    pub(all) enum AnswerValue {
    StringValue(String)
    BoolValue(Bool)
    ArrayValue(Array[String])
    IntValue(Int)
    } derive(Eq,
    Debug
    )

    AnswerValue::from_array

    fn AnswerValue::from_array(arr : Array[String]) -> AnswerValue

    Question

    pub struct Question {
    question_type : QuestionType
    question_name : String
    question_message : String
    question_default : AnswerValue?
    question_choices : Array[String]?
    }

    QuestionType

    pub enum QuestionType {
    Input
    Confirm
    List
    RawList
    Expand
    Checkbox
    Password
    Editor
    } derive(Eq,
    Debug
    )

    checkbox

    fn checkbox(question_name~ : String, question_message~ : String, question_choices~ : Array[String], question_default? : AnswerValue) -> Question

    clear_line

    fn clear_line() -> Unit

    Placeholder for clearing line (no-op in this version)

    confirm

    fn confirm(question_name~ : String, question_message~ : String, question_default? : AnswerValue?) -> Question

    editor

    fn editor(question_name~ : String, question_message~ : String, question_default? : AnswerValue?) -> Question

    expand

    fn expand(question_name~ : String, question_message~ : String, question_choices~ : Array[String], question_default? : AnswerValue?) -> Question

    hide_cursor

    fn hide_cursor() -> Unit

    Placeholder for hiding cursor (no-op in this version)

    input

    fn input(question_name~ : String, question_message~ : String, question_default? : AnswerValue?) -> Question

    list

    fn list(question_name~ : String, question_message~ : String, question_choices~ : Array[String], question_default? : AnswerValue?) -> Question

    mask_password

    fn mask_password(input : String) -> String

    move_cursor_down

    fn move_cursor_down(_n : Int) -> Unit

    Placeholder for moving cursor down (no-op in this version)

    move_cursor_to_start

    fn move_cursor_to_start() -> Unit

    Placeholder for moving cursor to start (no-op in this version)

    move_cursor_up

    fn move_cursor_up(_n : Int) -> Unit

    Placeholder for moving cursor up (no-op in this version)

    parse_int

    fn parse_int(s : String) -> Int?

    Alias for backwards compatibility

    password

    fn password(question_name~ : String, question_message~ : String, question_default? : AnswerValue?) -> Question

    prompt

    async fn prompt(questions : Array[Question]) -> Map[String, AnswerValue]

    Interactive prompt implementation using async I/O.

    This module provides the core prompt function and helper utilities. Note: In MoonBit, async functions are called like regular functions - they automatically wait for completion when called from another async function.

    rawlist

    fn rawlist(question_name~ : String, question_message~ : String, question_choices~ : Array[String], question_default? : AnswerValue?) -> Question

    render_checkbox_item

    fn render_checkbox_item(_index : Int, choice : String, checked : Bool) -> String

    render_expand_item

    fn render_expand_item(key : String, choice : String) -> String

    render_list_item

    fn render_list_item(_index : Int, choice : String, selected : Bool) -> String

    render_prompt

    fn render_prompt(message : String, default? : String) -> String

    render_rawlist_item

    fn render_rawlist_item(index : Int, choice : String) -> String

    safe_parse_int

    fn safe_parse_int(s : String) -> Int?

    Safe wrapper for @string.parse_int that returns Int? instead of raising

    show_cursor

    fn show_cursor() -> Unit

    Placeholder for showing cursor (no-op in this version)