Giter Club home page Giter Club logo

requestty's Introduction

Requestty

RustCI RustCI RustCI Crates.io License Documentation

requestty (request-tty) is an easy-to-use collection of interactive cli prompts inspired by Inquirer.js.

  • Easy-to-use - The builder API and macros allow you to easily configure and use the in-built prompts.

  • Extensible - Easily create and use custom prompts with a companion ui rendering library.

  • Flexible - All prompts can be used standalone or chained together.

  • Dynamic - You can dynamically decide what questions should be asked based on previous questions.

  • Validation - You can validate user input with any prompt.

  • Examples - Every prompt is accompanied with an example as well as other examples for more complex workflows

Usage

Add this to your Cargo.toml

[dependencies]
requestty = "0.4.1"

To ask a question:

let question = requestty::Question::expand("overwrite")
    .message("Conflict on `file.rs`")
    .choices(vec![
        ('y', "Overwrite"),
        ('a', "Overwrite this one and all next"),
        ('d', "Show diff"),
    ])
    .default_separator()
    .choice('x', "Abort")
    .build();

println!("{:#?}", requestty::prompt_one(question));

More examples are available in the documentation and the examples directory.

In-built prompts

There are 11 in-built prompts:

  • Input

    Prompt that takes user input and returns a String.

  • Password

    Prompt that takes user input and hides it.

  • Editor

    Prompt that takes launches the users preferred editor on a temporary file

  • Confirm

    Prompt that returns true or false.

  • Int

    Prompt that takes a i64 as input.

  • Float

    Prompt that takes a f64 as input.

  • Expand

    Prompt that allows the user to select from a list of options by key

  • Select

    Prompt that allows the user to select from a list of options

  • RawSelect

    Prompt that allows the user to select from a list of options with indices

  • MultiSelect

    Prompt that allows the user to select multiple items from a list of options

  • OrderSelect

    Prompt that allows the user to organize a list of options.

Optional features

  • macros: Enabling this feature will allow you to use the questions and prompt_module macros.

  • smallvec (default): Enabling this feature will use SmallVec instead of Vec for auto completions. This allows inlining single completions.

  • crossterm (default): Enabling this feature will use the crossterm library for terminal interactions such as drawing and receiving events.

  • termion: Enabling this feature will use the termion library for terminal interactions such as drawing and receiving events.

Minimum Supported Rust Version (MSRV)

Minimum supported rust version (as per cargo-msrv) is 1.56.1

requestty's People

Contributors

lutetium-vanadium avatar novalix-dev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

requestty's Issues

Allow setting of custom symbols

I've noticed some unicode symbols (e.x. the SelectBuilder cursor) is not supported by the windows command line cmd.exe. Having the option to set a custom char would be a nice addition to the library in my opinion. I'm still getting used to the concepts of the rust language itself so i don't have a concrete solution for this, but im assuming the chars for the library are defined as constants somewhere. My proposal is allowing them to be set to a custom char in the same way that PasswordBuilder is able to set a custom masking char, to keep everything nice and consistent.

Remove screen size limit error

UST_BACKTRACE=1 environment variable to display a backtrace`

This error occurs when the questions cannot be displayed on the screen this should not really error out unless there is some loop back error , its really easy to go off limit when using integrated terminal and having bunch of options

not sure if this is intended or there is a workaround to this?

Color input red as long as it doesn't pass validation

It would be nice to have an option to have a continuous validation function (same prototype as the current validation function, but ignores the error message) that would color the input red as long as it doesn't pass validation, i.e. the user gets immediate feedback that the input is wrong.

divisor of zero error requestty-ui-0.3.0 in MacOS 12.3.1 Docker 4.5.0 (74594)

thread 'main' panicked at 'attempt to calculate the remainder with a divisor of zero', /root/.cargo/registry/src/github.com-1ecc6299db9ec823/requestty-ui-0.3.0/src/prompt.rs:149:14
                                                                       note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I am getting the following error when I am using the requestty = "0.3.0".
Docker Engine version is: 20.10.12.

But I'm not getting the same error in Ubuntu.

[Feature] Choose order of all options

#Why ?
Certains programs need to let the user choose the order of certain things. For example, a tool allow to combine data all together and the user can choose in which order to combine it.

#How will it work ?
Similarly to Multi-Select : the space key is used to select an option, the arrows to navigate threw or move the selected option and the enter key to finalize the order.

For the returned value, it can be a Vector containing the ordered IDs of the options. The IDs will be just the index of them in the list given to the question constructor.

I don't think the separators sill be necessary, because the user will mix the options up.

EditorBuilder: Change Editor Programatically

Feature Request

I'm trying to use this library to write a set of applications designed for use with git, and I would really like to be able to allow users to configure which editor the application launches via git's own core.editor configuration value. I already have the necessary code in place to extract the value of core.editor as a string, but there doesn't seem to be a way to change the preferred editor when using EditorBuilder.

I don't think I'm the only person who would find this feature useful. Lots of applications provide their own configuration scheme, which should take precedence over the value of $VISUAL and $EDITOR.

From what I can tell, this should be a simple feature to add; you would just need to add a new mutation method to the EditorBuilder class, which would allow the user to set the value of self.editor.editor to some arbitrary string. The end result would be something like this:

use requestty::Question;

let preferred_editor: String = get_preferred_editor_from_my_config_file();

let editor = Question::editor("description")
    .message("Please enter a short description about yourself")
    // This next line is the feature I'm requesting:
    .prefer_editor(preferred_editor)
    .build();

That said, I'm still fairly new to rust, so please let me know if this could be more difficult than I'm thinking.

Assuming this feature request isn't met with any push-back, I would be happy to try writing this myself, and will submit a PR if and when I've made significant progress.

Thank you for your time!

Bugs with wide characters (like CJK)

Most CJK characters are as wide as two ASCII characters; they should also take up twice the width on the terminal.

Therefore, if you pass CJK characters into the prompt or input when using this library, the cursor position will be abnormal.

use requestty::*;

fn main() -> Result<()> {
    prompt_one(
        Question::input("name")
            .message("你叫什么名字")
            .default("王小明")
            .build(),
    )?;
    Ok(())
}

image

Incorrect cursor location when input prompt spans multiple lines

Hi there! Hopefully this is actually an issue with this library, unlike the last issue I raised 🙈

If a free text input prompt spans multiple lines, the cursor shows in the correct position horizontally, but on the first line of the prompt, like so:

image

(Notice the underscore on the "u" in "your")

Typing input moves the cursor horizontally as expected, but it remains on the first line as opposed to where the input actually is

Tested on Windows & Linux, issue present on both

Allow filtering a Select prompt

It would be nice if you could filter a select list by typing part of an option so you can choose it without having to scroll through a whole list if you already know what you want.

I can try and add this myself if this is a feature you'd be cool with.

Builder error: unsupported pair

Hi there!

First of all, thank you for the crate! It's a lovely simple way to start interacting with users in the terminal

I have the following function:

pub fn get_api_key(client: &reqwest::blocking::Client) -> Result<String> {
    let question = Question::input("api_key")
        .message("Please enter in your OMDb API key. If you need to, visit their website to get one (https://www.omdbapi.com/apikey.aspx)")
        .validate(|api_key, _| test_api_key(api_key, client))
        .build();
    Ok(requestty::prompt_one(question)?.try_into_string().unwrap())
}

It compiles fine, however regardless as to what the user inputs (and whether or not it should be valid), I get the error builder error: unsupported pair.

I can't work out what I'm doing to cause the error, any pointers?

dbg code in 0.4.0

Using version 0.4.0 on Ubuntu linux, I see the following when inputting data for prompts:

[/home/xxx/.cargo/registry/src/github.com-1ecc6299db9ec823/requestty-ui-0.4.0/src/input.rs:125] self.backend.size() = Ok(
                                                                                                                              Size {
                                                                                                                                            width: 372,
                                                                                                                                                               height: 74,
                                                                                                                                                                              },
                                                                                                                                                                                )

This appears to come from a dbg! on line 125 of input.rs. That sends the message to stderr. I'm using Gnome terminal, but it also happens with xterm. Oddly, this does not happen in rxvt or the vs-code terminal.

It's probably best to remove this line, as the rust docs say:

The dbg! macro works exactly the same in release builds. This is useful when debugging issues that only occur in release builds or when debugging in release mode is significantly faster.

Note that the macro is intended as a debugging tool and therefore you should avoid having uses of it in version control for long periods (other than in tests and similar). Debug output from production code is better done with other facilities such as the debug! macro from the log crate.

default for input returns empty value

It seems to me that if I specify a default for an input prompt, and then the user just presses Enter, we get an empty string returned, rather than the default value. The docs say the default should be returned, and that is also what would help us :)

Support abort key

It would be nice if there was an option so that users could use Esc to abort a prompt.

`adjust_page` called before `height` or `render`

thread 'debug::test' panicked at 'adjust_page called before height or render'

stack backtrace:
   0: rust_begin_unwind
             at /rustc/97032a6dfacdd3548e4bff98c90a6b3875a14077/library/std/src/panicking.rs:517:5
   1: core::panicking::panic_fmt
             at /rustc/97032a6dfacdd3548e4bff98c90a6b3875a14077/library/core/src/panicking.rs:101:14
   2: core::option::expect_failed
             at /rustc/97032a6dfacdd3548e4bff98c90a6b3875a14077/library/core/src/option.rs:1615:5
   3: core::option::Option<T>::expect
             at /rustc/97032a6dfacdd3548e4bff98c90a6b3875a14077/library/core/src/option.rs:698:21
   4: requestty_ui::select::Select<L>::adjust_page
             at [:REDACTED:]/requestty-ui-0.2.0/src/select/mod.rs:279:24
   5: requestty_ui::select::Select<L>::maybe_adjust_page
             at [:REDACTED:]/requestty-ui-0.2.0/src/select/mod.rs:382:13
   6: requestty_ui::select::Select<L>::set_at
             at [:REDACTED:]/requestty-ui-0.2.0/src/select/mod.rs:144:17
   7: requestty::question::select::Select::into_prompt
             at [:REDACTED:]/requestty-0.1.2/src/question/select/mod.rs:113:13
   8: requestty::question::select::Select::ask
             at [:REDACTED:]/requestty-0.1.2/src/question/select/mod.rs:130:34
   9: requestty::question::Question::ask
             at [:REDACTED:]/requestty-0.1.2/src/question/mod.rs:520:40
  10: requestty::prompt_one_with
             at [:REDACTED:]/requestty-0.1.2/src/lib.rs:283:15
  11: requestty::prompt_one
             at [:REDACTED:]/requestty-0.1.2/src/lib.rs:259:5
  12: clashctl::debug::test
             at ./src/debug.rs:4:5

Minimum reproducable case:

#[test]
fn test() {
    use requestty::{prompt_one, Question};
    prompt_one(
        Question::select("Debug")
            .choices(&["1".to_owned(), "2".to_owned(), "3".to_owned()])
            .default(0)
            .build(),
    )
    .unwrap();
}

This is the exact code produced the above backtrace.
Only when default = 0 will make the program panic.

[Bug] Requestty view doens't addapt when terminal size is changed + Panics if smaller than current view height

When we resize the terminal, the view height doesn't change.

When i change to a greather height, the size stays the same and requestty works fine.
But when i change to a small height, enough to hide a part of the requestty view, it panics and show this error :

thread 'main' panicked at 'attempt to subtract with overflow', C:\Users\noval\rust\requestty\requestty-ui\src\input.rs:150:28
stack backtrace:
   0: rust_begin_unwind
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library\std\src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library\core\src/panicking.rs:142:14
   2: core::panicking::panic
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library\core\src/panicking.rs:48:5
   3: requestty_ui::input::Input<P,B>::adjust_scrollback
             at C:\Users\noval\rust\requestty\requestty-ui\src\input.rs:150:28
   4: requestty_ui::input::Input<P,B>::render
             at C:\Users\noval\rust\requestty\src\question\mod.rs:567:45
   9: requestty::prompt_one_with
             at C:\Users\noval\rust\requestty\src\lib.rs:283:15
  10: requestty::prompt_one
             at C:\Users\noval\rust\requestty\src\lib.rs:259:5
  11: requestty_test::main
             at .\src\main.rs:9:23
  12: core::ops::function::FnOnce::call_once
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f\library\core\src\ops/function.rs:248:5

The error occurs with OrderSelect (not yet implemented, see [#16]) and MultiSelect.
It doesn't seems to occur with one-line questions, like confirm or float.

Tested with PowerShell in VSCode.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.