Skip to content

Commit

Permalink
feat: set initial text at spinner creation
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-napoleone committed Aug 23, 2024
1 parent 215712f commit 1454ced
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Basic example.
use spinach::Spinner;

fn main() {
let s = Spinner::new().text("Cutting spinaches...").start();
let s = Spinner::new("Cutting spinaches...").start();
// Cut spinaches
s.text("Cutting tomatoes...").update();
// Cut tomatoes
Expand All @@ -42,12 +42,11 @@ fn main() {
use spinach::{Color, Spinner};

// With custom text
let s = Spinner::new().text("workin'...").start();
let s = Spinner::new("workin'...").start();

// With custom text, spinner, spinner speed and spinner color
let symbols = vec!["",""];
let s = Spinner::new()
.text("blip... blop...")
let s = Spinner::new("blip... blop...")
.color(Color::Red)
.symbols(symbols)
.frames_duration(80)
Expand All @@ -59,7 +58,7 @@ let s = Spinner::new()
```rust
use spinach::{Color, Spinner};

let s = Spinner::new().text("workin'...").start();
let s = Spinner::new("workin'...").start();

// Updating text
s.text("new text").update();
Expand All @@ -82,18 +81,18 @@ s.text("new text").color(Color::Red);
```rust
use spinach::{Color, Spinner};

let s = Spinner::new().text("workin'...").start();
let s = Spinner::new("workin'...").start();

// Stop with final `✔` frame, green color and optional text change.
// Stop with final `✔` frame and green color.
s.text("gg!").success();

// Stop with final `✖` frame, red color and optional text change.
// Stop with final `✖` frame and red color.
s.text("ups").failure();

// Stop with final `⚠` frame, yellow color and optional text change.
// Stop with final `⚠` frame and yellow color.
s.text("something may have happened?").warn();

// Stop with final `ℹ` frame, blue color and optional text change.
// Stop with final `ℹ` frame and blue color.
s.text("notice").stop();

// Stop current spinner (sends update at the same time)
Expand All @@ -119,7 +118,7 @@ fn main() {
})
.expect("Error setting Ctrl-C handler");

let s = Spinner::new().text("workin'...").start();
let s = Spinner::new("workin'...").start();
// ...
```

Expand Down
6 changes: 2 additions & 4 deletions examples/cooking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ use spinach::Color::Green;
use spinach::Spinner;

fn main() {
let spinner = Spinner::new()
let spinner = Spinner::new("Cutting spinaches")
.color(Green)
.text("Cutting spinaches")
.frames_duration(30)
.start();
sleep(Duration::from_secs(1));
spinner.text("Cutting tomatoes").update();
sleep(Duration::from_secs(1));
spinner.text("Vegetables cut").symbols(vec!["🔪"]).stop();

let spinner = Spinner::new()
let spinner = Spinner::new("Cooking vegetables")
.color(Green)
.text("Cooking vegetables")
.frames_duration(30)
.start();
sleep(Duration::from_secs(1));
Expand Down
28 changes: 14 additions & 14 deletions src/spinner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::term;
/// ```
/// use spinach::Spinner;
///
/// let spinner = Spinner::new().text("Loading...").start();
/// let spinner = Spinner::new("Loading...").start();
/// // Perform some tasks
/// spinner.text("gg!").success();
/// ```
Expand Down Expand Up @@ -44,7 +44,7 @@ impl<S> Spinner<S> {
/// ```
/// use spinach::{Spinner, Color};
///
/// let spinner = Spinner::new().color(Color::Blue).start();
/// let spinner = Spinner::new("workin'...").color(Color::Blue).start();
/// ```
pub fn color(&self, color: term::Color) -> &Self {
self.update.borrow_mut().color = Some(color);
Expand All @@ -58,7 +58,7 @@ impl<S> Spinner<S> {
/// ```
/// use spinach::Spinner;
///
/// let spinner = Spinner::new().text("workin'...").start();
/// let spinner = Spinner::new("workin'...").start();
/// ```
pub fn text(&self, text: &str) -> &Self {
self.update.borrow_mut().text = Some(text.to_string());
Expand All @@ -72,7 +72,7 @@ impl<S> Spinner<S> {
/// ```
/// use spinach::Spinner;
///
/// let spinner = Spinner::new().symbols(vec!["◐", "◓", "◑", "◒"]).start();
/// let spinner = Spinner::new("workin'...").symbols(vec!["◐", "◓", "◑", "◒"]).start();
/// ```
pub fn symbols(&self, symbols: Vec<&'static str>) -> &Self {
self.update.borrow_mut().symbols = Some(symbols);
Expand All @@ -86,7 +86,7 @@ impl<S> Spinner<S> {
/// ```
/// use spinach::Spinner;
///
/// let spinner = Spinner::new().frames_duration(40).start();
/// let spinner = Spinner::new("workin'...").frames_duration(40).start();
/// ```
pub fn frames_duration(&self, ms: u64) -> &Self {
self.update.borrow_mut().frames_duration_ms = Some(ms);
Expand All @@ -102,12 +102,12 @@ impl Spinner<Stopped> {
/// ```
/// use spinach::Spinner;
///
/// let spinner = Spinner::new().text("let's go...").start();
/// let spinner = Spinner::new("let's go...").start();
/// ```
#[must_use]
pub fn new() -> Self {
pub fn new(text: &str) -> Self {
Spinner {
update: RefCell::new(Update::default()),
update: RefCell::new(Update::new(text)),
state: Stopped,
}
}
Expand All @@ -119,7 +119,7 @@ impl Spinner<Stopped> {
/// ```
/// use spinach::Spinner;
///
/// let spinner = Spinner::new().text("let's go...").start();
/// let spinner = Spinner::new("let's go...").start();
/// ```
pub fn start(&self) -> Spinner<Running> {
term::hide_cursor();
Expand Down Expand Up @@ -175,7 +175,7 @@ impl Spinner<Running> {
/// ```
/// use spinach::Spinner;
///
/// let spinner = Spinner::new().text("Doing something...").start();
/// let spinner = Spinner::new("Doing something...").start();
/// // Perform some tasks
/// spinner.text("Doing something else...").update();
/// ```
Expand All @@ -191,7 +191,7 @@ impl Spinner<Running> {
/// ```
/// use spinach::Spinner;
///
/// let spinner = Spinner::new().text("Doing something...").start();
/// let spinner = Spinner::new("Doing something...").start();
/// // Perform some tasks
/// spinner.text("done!").stop();
/// ```
Expand All @@ -209,7 +209,7 @@ impl Spinner<Running> {
/// ```
/// use spinach::Spinner;
///
/// let spinner = Spinner::new().text("Doing something...").start();
/// let spinner = Spinner::new("Doing something...").start();
/// // Perform some task that succeeds
/// spinner.text("done!").success();
/// ```
Expand All @@ -227,7 +227,7 @@ impl Spinner<Running> {
/// ```
/// use spinach::Spinner;
///
/// let spinner = Spinner::new().text("Doing something...").start();
/// let spinner = Spinner::new("Doing something...").start();
/// // Perform some task that fails
/// spinner.text("oops").failure();
/// ```
Expand All @@ -245,7 +245,7 @@ impl Spinner<Running> {
/// ```
/// use spinach::Spinner;
///
/// let spinner = Spinner::new().text("Doing something...").start();
/// let spinner = Spinner::new("Doing something...").start();
/// // Perform some task with unexpected results
/// spinner.text("wait, what?").warn();
/// ```
Expand Down
9 changes: 9 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,12 @@ pub struct Update {
/// Optional new frame duration in milliseconds.
pub frames_duration_ms: Option<u64>,
}

impl Update {
pub fn new(text: &str) -> Self {
Self {
text: Some(text.to_owned()),
..Self::default()
}
}
}

0 comments on commit 1454ced

Please sign in to comment.