Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add freeze command #7

Merged
merged 2 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ fn main() {
}
```

For general convenience, text can be passed as `String` or `&str`.
When an `Option`, can be passed as `String`, `&str` or `Option<&str>`

### Creating

```rust
Expand Down Expand Up @@ -106,6 +109,19 @@ s.stop_with("🥬", "spinach'd", Color::Ignore);
s.stop_with(None, None, Color::Blue);
```

### Freezing

Will freeze the current line with passed options and continue on a new line.

```rust
use spinach::{Color, Spinach};

let s = Spinach::new("Cutting spinaches");

// Similar to `stop_with`, but with an extra argument to change the spinner text.
s.freeze("🥬", "Spinaches cut", None, "Cutting carottes");
```

## FAQ

### How to avoid leaving terminal without prompt on interupt (ctrl^c)?
Expand Down
61 changes: 61 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,49 @@ impl Spinach {
self.stop_with("ℹ", text.into(), term::Color::Blue);
}

/// Freeze spinach spinner with passed optional configurations and
/// continue on the next line.
///
/// # Arguments
///
/// When `None`, use current value.
///
/// * `frozen_symbol` - Optional symbol used as the spinner's frozen line frame.
/// * `frozen_text` - Optional spinner's frozen line text.
/// * `frozen_color` - Optional spinner's frozen line spinner color.
/// * `text` - Optional spinner new line text.
///
/// # Examples
///
/// ```
/// use spinach::{Color, Spinach};
///
/// let s = Spinach::new("first task");
/// s.freeze("✔", "first task: ok", Color::Green, "second task");
/// s.stop_with("✔", "first task: ok", Color::Green);
/// ```
pub fn freeze<
A: Into<Option<&'static str>>,
B: Into<OptText>,
C: Into<Option<term::Color>>,
D: Into<OptText>,
>(
&self,
frozen_symbol: A,
frozen_text: B,
frozen_color: C,
text: D,
) {
self.sender
.send(SpinnerCommand::Freeze {
frozen_symbol: frozen_symbol.into(),
frozen_text: frozen_text.into().inner,
frozen_color: frozen_color.into(),
text: text.into().inner,
})
.expect("Could not stop spinner.");
}

fn run(config: Spinner, text: String, color: term::Color) -> Self {
term::hide_cursor();

Expand All @@ -300,6 +343,18 @@ impl Spinach {
context.color = color;
}
}
Ok(SpinnerCommand::Freeze {
frozen_symbol,
frozen_text,
frozen_color,
text,
}) => {
context.render_with_override(frozen_symbol, frozen_text, frozen_color);
term::new_line();
if let Some(text) = text {
context.text = text;
}
}
Ok(SpinnerCommand::Stop {
symbol,
text,
Expand Down Expand Up @@ -332,6 +387,12 @@ enum SpinnerCommand {
text: Option<String>,
color: Option<term::Color>,
},
Freeze {
frozen_symbol: Option<&'static str>,
frozen_text: Option<String>,
frozen_color: Option<term::Color>,
text: Option<String>,
},
Stop {
symbol: Option<&'static str>,
text: Option<String>,
Expand Down