Comments (6)
Hi @seancarroll!
That's right, I managed to write a few tests and actually planned to expose this API sometime in the future, but the reason I didn't until now was because it still makes tests very dependent on the current implementation. Each prompt handles almost raw key events as opposed to "commands" (e.g. handles Key::DownArrow instead of Command::MoveDown). Back then I was still planning to improve this part, allowing this custom backend to re-map any desirable key bindings.
I am totally okay in prioritizing this next development for you, as I don't have any other major plans for this project at the moment. But to be really honest, I'll start a new job abroad in 1 month and I'm reserving this time I have left here to enjoy my family, so I'm not sure how much I'll be able to contribute for now.
If you're interested, we can discuss more this direction and I'd be very happy to accept contributions!
from inquire.
We can also consider exposing the current method as-is in an alpha release or something, it if makes your life easier sooner :)
from inquire.
@mikaelmello this isn't an immediate need for me so happy to discuss direction and options. Given you mentioned "commands" I started some very preliminary research and stumbled up enigo which they say is a cross platform input simulation in Rust. Perhaps this is a direction worth looking into?
from inquire.
@seancarroll that is an awesome find and it would be a great addition for E2E tests in the future, but for unit tests I was thinking about something else, which I honestly don't know if it is indeed better.
For context, each Terminal
has a method to convert a key input from their respective exported enums to inquire
's own unified Key
enum. https://github.com/mikaelmello/inquire/blob/main/src/ui/key.rs#L15-L33. Each Prompt then updates its state based on the last Key
pressed.
The current problem (at least for me) is that key binding definitions are made very deep in the code, not clear from a first glance. This is a problem for users in general (what if we break the API without noticing) and for tests that shouldn't have to worry at this low level of implementation.
I was wondering if it was worth for each prompt to have something like:
pub enum MultiSelectCommand {
MoveUp,
MoveDown,
ToggleSelection,
...
}
impl From<Key> for MultiSelectCommand { ... }
IMO, we have the following pros and cons:
Pros:
- Easier to reason about the behavior of the key bindings in a certain prompt, we centralize this definition
- Allows us to write tests that do not have to worry about keys input, only programatically changing state.
Cons:
- Adds more complexity in the overall logic of a prompt.
- Might not work well with the current way I used to process keys input when the prompt has a "text box".
- When processing a key input, prompts with a text box (here called
Input
) catch any keys relevant to the prompt itself and then call theInput
's key handler to process any bindings related to processing text, such as writing a letter in the text box or pressing backspace to delete something. - We would need to add a catch-all or something in the
<Prompt>Command
, such asMultiSelectCommand::Other(Key)
, and then, when calling theInput
's key handler, we would get the inner key to convert to something likeTextBoxCommand
. Looks a little bit ugly.
- When processing a key input, prompts with a text box (here called
Finally, I believe we should also add E2E tests to ensure we don't break our lib's external API, and enigo looks like the path forward.
from inquire.
@mikaelmello, thanks for the explanation and insights.
from inquire.
I want to improve this crate's testability for both the crate and its users. I have created #71 to track the developments on this part.
from inquire.
Related Issues (20)
- Release HOT 1
- Use tty instead of stdin HOT 1
- with_starting_cursor does not apply HOT 2
- Fully support piped inputs in parallel with interactive inputs.
- Prompt crashes if program is run with crossterm+piped input+macos
- Add code coverage metrics to get a better idea of test coverage
- inquire derive and attribute macro HOT 3
- Show proper error message for CustomType prompt HOT 1
- Alt+{left, right, backspace} support HOT 3
- DateSelect default help message incorrect ? HOT 2
- Support for up-arrow previous prompt history, and control-keys HOT 2
- Inquire leaves the terminal in a broken state HOT 3
- Render issue using the `console` backend HOT 2
- Inquire forgets to add newlines in 0.7.1 HOT 5
- "external_print" Functionality
- Make Scrolling More Obvious (Customizable maybe?) HOT 1
- MultiSelect list doesn't show after filter is cleared on selection HOT 1
- Suppressing static lifetimes from Text (and other modules ?) HOT 1
- typo in README for termion link
- Password prompt with redirected `stdout`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from inquire.