Requestty - An easy-to-use collection of interactive cli prompts inspired by Inquirer.js.

Overview

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.1.0"
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));

In-built prompts

There are 10 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

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.47

Comments
  • Implemented OrderSelect

    Implemented OrderSelect

    Here is a first beta of the order_select question (Discussed in [#13]), but i'ts not complete.

    I still have to :

    • [x] implement the automatic test module, since I tested it manually

    I intentionally not implemented many functions in the builder (choice, choice_with_default, separator, default_spearator, choice_with_default) for the following reasons :

    • Since the user will mix the order of the options in the list, separators aren't necessary,
    • "Choice" and "choice with default" don't seem necessary anymore to me, because the programmer can compose the list and set the order of the options before building the question.

    Let me know if you want changes in the code.

    Only tested on Windows 11.

    opened by NovaliX-Dev 34
  • Use shell-words to split editor command

    Use shell-words to split editor command

    Hey there! I love this library. I noticed a bug in the editor code, though, that I thought I might be able to fix for you.

    My EDITOR environment variable is set to subl -w because Sublime Text needs a -w to wait until the window is closed. However the code in requestty assumed EDITOR was just a single executable and thus I would get a no such file or directory error when the command tried to execute.

    To handle this I've added shell-words to handle splitting (since splitting commands with arguments and quotes is non-trivial) and this seems to work.

    I can run my program with EDITOR=vim and it works as well as EDITOR="subl -w" and it works. So this seems to still work for the default case but now also supports environments where users have arguments baked into their EDITOR variable.

    opened by kellygravelyn 6
  • [Feature] Choose order of all options

    [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.

    opened by NovaliX-Dev 3
  • default for input returns empty value

    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 :)

    opened by davidanthoff 3
  • Greyed-out text for the first autocompletion

    Greyed-out text for the first autocompletion

    Hello,

    Is it possible to have some fish-style greyed-out autocompletions with the first autocompletion? Like https://fishshell.com/assets/img/screenshots/autosuggestion.png Then tab would autocomplete to that (while still doing what it's currently doing, cycling through the possibilities of course). It would just be a display change.

    That would help the user know that they can autocomplete.

    opened by nitnelave 3
  • Incorrect cursor location when input prompt spans multiple lines

    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

    opened by alpha-tango-kilo 3
  • Bugs with wide characters (like CJK)

    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

    opened by Guyutongxue 2
  • dbg code in 0.4.0

    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.

    opened by anewton1998 2
  • `adjust_page` called before `height` or `render`

    `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.

    opened by George-Miao 2
  • Builder error: unsupported pair

    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?

    opened by alpha-tango-kilo 2
  • [Bug] Requestty view doens't addapt when terminal size is changed + Panics if smaller than current view height

    [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.

    opened by NovaliX-Dev 1
  • Allow filtering a Select prompt

    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.

    opened by its-danny 2
  • divisor of zero error requestty-ui-0.3.0 in MacOS 12.3.1 Docker 4.5.0 (74594)

    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.

    opened by eNipu 6
  • Remove screen size limit error

    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?

    opened by gopherine 1
  • Allow setting of custom symbols

    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.

    opened by Retoon 0
Owner
null
A Rust library for building interactive prompts

inquire is a library for building interactive prompts on terminals. Demo Source Usage Put this line in your Cargo.toml, under [dependencies]. inquire

Mikael Mello 426 Dec 26, 2022
Beautiful, minimal, opinionated CLI prompts inspired by the Clack NPM package

Effortlessly build beautiful command-line apps with Rust ?? ✨ Beautiful, minimal, opinionated CLI prompts inspired by the @clack/prompts npm package.

Alexander Fadeev 7 Jul 23, 2023
Simple OpenAI CLI wrapper written in Rust, feat. configurable prompts and models

Quick Start git clone https://github.com/ryantinder/ask-rs cd ask cargo install --path . Example ask tell me about the Lockheed Martin SR71 >> The Loc

Ryan Tinder 3 Aug 9, 2023
Collection of immutable and persistent data structures written in Rust, inspired by the standard libraries found in Haskell, Closure and OCaml

PRust: (P)ersistent & Immutable Data Structures in (Rust) This library houses a collection of immutable and persistent data structures, inspired by th

Victor Colombo 13 Aug 13, 2023
An interactive Bayesian Probability Calculator CLI that guides users through updating beliefs based on new evidence.

Bayesian Probability Calculator CLI Welcome to the Bayesian Probability Calculator CLI! This command-line tool is designed to help you update your bel

Ben Greenberg 4 Apr 25, 2023
A simple cross-platform easy to use STL Viewer with GUI and CLI options

Stlvi STL viewer written in Rust. This project is still in development. It is available in both CLI and GUI mode. Supported OS Linux Windows Mac Scree

Mantresh Khurana 4 Mar 2, 2023
A system clipboard command line tools which inspired by pbcopy & pbpaste but better to use.

rclip A command line tool which supports copy a file contents to the system clipboard or copy the contents of the system clipboard to a file. Install

yahaa 3 May 30, 2022
Configurable, extensible, interactive line reader

linefeed linefeed is a configurable, concurrent, extensible, interactive input reader for Unix terminals and Windows console. API Documentation linefe

Murarth 176 Jan 3, 2023
An interactive cheatsheet tool for the command-line

navi An interactive cheatsheet tool for the command-line. navi allows you to browse through cheatsheets (that you may write yourself or download from

Denis Isidoro 12.2k Dec 30, 2022
Simple Interactive Terminal Todo App in Rust

todo-rs Simple Interactive Terminal Todo App in Rust Quick Start $ cargo run TODO Controls Keys Description k, j Move cursor up and down Shift+K, Shif

Tsoding 56 Dec 8, 2022
Coinlive is an interactive command line tool that displays live cryptocurrency prices.

Coinlive is an interactive command line tool that displays live cryptocurrency prices. It can also display simple historical price charts.

Mayer Analytics 9 Dec 7, 2022
ruborute is an interactive command-line tool to get asphyxia@sdvx gaming data.

ruborute Are you 暴龍天 ?. The ruborute is an interactive command-line tool to get asphyxia@sdvx gaming data. asphyxia-core/plugins: https://github.com/a

RinChanNOW! 9 Sep 28, 2022
An interactive shell environment for exploring the p2panda protocol

An interactive shell environment for exploring the p2panda protocol. Uses a mock node and clients to simulate network logic.

null 4 Dec 12, 2021
Native cross-platform full feature terminal-based sequence editor for git interactive rebase.

Native cross-platform full feature terminal-based sequence editor for git interactive rebase.

Tim Oram 1.2k Jan 2, 2023
Terminal based, feature rich, interactive SQL tool

datafusion-tui (dft) DataFusion-tui provides a feature rich terminal application, built with tui-rs, for using DataFusion (and eventually Ballista). I

null 49 Dec 24, 2022
Non-interactive nREPL client for shell scripts and command-line

nreplops-tool (nr) nreplops-tool (nr) is a non-interactive nREPL client designed to be used in shell scripts and on the command-line. Early α warning:

Matti Hänninen 3 Jul 1, 2022
A toolkit for building your own interactive command-line tools in Rust

promkit A toolkit for building your own interactive command-line tools in Rust, utilizing crossterm. Getting Started Put the package in your Cargo.tom

null 70 Dec 18, 2022
Tricking shells into interactive mode when local PTY's are not available

Remote Pseudoterminals Remote Pseudoterminals or "RPTY" is a Rust library which intercepts calls to the Linux kernel's TTY/PTY-related libc functions

null 135 Dec 4, 2022
A simple interactive OSC (Open Sound Control) debugger for terminal

oscd, a simple interactive OSC debugger for the terminal by using nom as a lexer/parser, oscd offers auto type casting and support sending multiple osc arguments.

Karnpapon Boonput 5 Oct 19, 2022