Skip to content

Commit

Permalink
Add shorthand method for creating a Color from RGB components (#73)
Browse files Browse the repository at this point in the history
* Add shorthand method for creating a Color from RGB components

Allows an Rgb Color variant to be created as if it was a tuple variant
while maintaining the named fields of the struct type.

Fixes #41

* Changelog entry for new rgb method

Co-authored-by: Mikael Mello <git@mikaelmello.com>
  • Loading branch information
tpoliaw and mikaelmello committed Sep 12, 2022
1 parent 48baef2 commit 4a136e9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
@@ -1,8 +1,11 @@
# Changelog

<!-- next-header -->

## [Unreleased] - ReleaseDate

- Add shorthand method `rgb(r: u8, g: u8, b: u8)` to create a `Color` struct from RGB components. Thanks to @tpoliaw for the PR! [#73](https://github.com/mikaelmello/inquire/pull/73)

## [0.3.0] - 2022-08-19

### Breaking Changes
Expand Down Expand Up @@ -39,7 +42,7 @@ Input validation, suggestions and completions are now fallible operations.

The return type of validators has been changed to `Result<Validation, CustomUserError>`. This means that validating the input can now be a fallible operation. The docs contain more thorough explanations and full-featured examples.

- Successful executions of the validator should return a variant of the `Validation` enum, which can be either `Valid` or `Invalid(ErrorMessage)`.
- Successful executions of the validator should return a variant of the `Validation` enum, which can be either `Valid` or `Invalid(ErrorMessage)`.
- Unsuccessful executions return a `CustomUserError` type, which is an alias for `Box<dyn std::error::Error + Send + Sync + 'static>`.

The return type of suggesters has also been changed to allow fallible executions. The return type in successful executions continues to be `Vec<String>`, while `CustomUserError` is used with errors.
Expand Down Expand Up @@ -193,7 +196,8 @@ The library is already featureful enough to warrant a higher version number, bum
- Add DateSelect prompt

<!-- next-url -->
[Unreleased]: https://github.com/mikaelmello/inquire/compare/v0.3.0...HEAD

[unreleased]: https://github.com/mikaelmello/inquire/compare/v0.3.0...HEAD
[unreleased]: https://github.com/mikaelmello/inquire/compare/v0.2.1...v0.3.0
[0.2.1]: https://github.com/mikaelmello/inquire/compare/v0.2.0...v0.2.1
[0.2.0]: https://github.com/mikaelmello/inquire/compare/v0.1.0...v0.2.0
Expand Down
13 changes: 13 additions & 0 deletions src/ui/color.rs
Expand Up @@ -153,3 +153,16 @@ pub enum Color {
/// Supported on all terminal back-ends: `crossterm`, `termion` and `console`.
AnsiValue(u8),
}

impl Color {
/// Shorthand method for creating a Color from RGB components
///
/// ```
/// # use inquire::ui::Color;
///
/// assert_eq!(Color::rgb(42, 17, 97), Color::Rgb { r: 42, g: 17, b: 97 });
/// ```
pub fn rgb(r: u8, g: u8, b: u8) -> Color {
Color::Rgb { r, g, b }
}
}

0 comments on commit 4a136e9

Please sign in to comment.