DEV Community

Cover image for πŸš€ dialoguer-macro Framework 0.3 Release: Redefining Command Line Interaction Design
εΆεΈˆε‚…
εΆεΈˆε‚…

Posted on

πŸš€ dialoguer-macro Framework 0.3 Release: Redefining Command Line Interaction Design

πŸŽ‰ Dear developers, I am thrilled to present the latest masterpiece from the dialoguer-macro libraryβ€”version 0.3! This groundbreaking update will revolutionize your traditional understanding of command line user interaction design. We have proudly introduced powerful support for enums and nested structures in this new release, allowing you to effortlessly create structured, hierarchical command line interfaces with elegance and clarity.

πŸ“š Documentation Tutorial Direct Link: https://yexiyue.github.io/dialogue-macro/

🎯 Practical Demonstration Primer:

In this blog post, we'll delve into a vivid example demonstrating how to leverage the SubAsker attribute along with the associated Asker, Build, and EnumAsker traits in dialoguer-macro 0.3 to craft an efficient and user-friendly command line data collection process.

Let's dive right in!

First, define a struct called UserInput that is adorned with the #[derive(Asker)] and #[asker(theme = "...")] macros, thereby enabling a visually appealing and feature-rich command line input interface:

use dialogue_macro::{Asker, Build, EnumAsker};

#[derive(Debug, Asker)]
#[asker(theme = "dialogue_macro::ColorfulTheme")]
struct UserInput {
    // Basic information fields
    username: String,
    educational_institution: String,

    // Use SubAsker to collect detailed user information
    #[asker(SubAsker)]
    detailed_info: DetailedUserInfo,

    // Use SubAsker for identity selection
    #[asker(SubAsker)]
    identity: EnumSelection,
}
Enter fullscreen mode Exit fullscreen mode

Next, we carefully construct two nested data structures for questioning:

The DetailedUserInfo struct encapsulates the user's email address and age attributes. To work harmoniously with SubAsker, this struct implements not only the Debug and Clone traits but also Asker and Build. The .build() method is defined within the impl Build for DetailedUserInfo to sequentially collect email address and age from the user:

#[derive(Debug, Clone, Asker)]
struct DetailedUserInfo {
    email_address: String,
    age: u8,
}

impl Build for DetailedUserInfo {
    fn build() -> Self {
        Self::asker()
            .email_address("Please provide your email address")
            .age("Enter your age")
            .finish()
    }
}
Enter fullscreen mode Exit fullscreen mode

The EnumSelection enum consists of Teacher, Student, and Leader options. Besides implementing Debug, Clone, it derives the EnumAsker trait, which means it presents these choices in a clear and intuitive way to the user:

#[derive(Debug, EnumAsker, Clone)]
#[asker(prompt="Please select your identity", default="Student")]
enum EnumSelection {
    Teacher,
    Student,
    Leader,
}
Enter fullscreen mode Exit fullscreen mode

Finally, inside the main function, we instantiate the UserInput object and call the respective asking methods for each field in logical order:

fn main() {
    let collected_user_input = UserInput::asker()
        .username("Please enter your username")
        .educational_institution("Please enter your school name")

        // Invoke SubAsker to collect detailed user info
        .detailed_info()

        // Invoke another SubAsker to allow user selection from an enum
        .identity()

        // Gather all user inputs and complete the questioning process
        .finish();

    println!("{:?}", collected_user_input);
}
Enter fullscreen mode Exit fullscreen mode

When the program runs, users are guided through a series of questions in a pre-defined hierarchical sequence, experiencing a sophisticated yet organized command line interaction journey.

Through this sample code, we demonstrate how to use features like SubAsker to easily achieve well-structured command line interaction designs. If you're excited about what this library has to offer and would like to try out these innovative features in your projects, head straight to our GitHub repository:

πŸ‘‰ GitHub Project Homepage: https://github.com/yexiyue/dialogue-macro

We sincerely invite you to follow my project, test the new version of dialoguer-macro, and be part of its growth. By clicking the "Star" button on the repository page, you can lend us valuable support and encouragement. Moreover, I eagerly anticipate your feedback, contributions, and real-world application case studies!

Top comments (0)