Skip to content

Commit

Permalink
pass theme by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 22, 2024
1 parent 2cf2681 commit 1fdb59b
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 48 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ readme = "README.md"
keywords = ["cli", "prompt", "console"]

[dependencies]
console = "0.15"
termcolor = "1"
console = "0.15.8"
once_cell = "1.19.0"
termcolor = "1.4.1"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ let theme = Theme {
Input::new("What's your e-mail?")
.description("Please enter your e-mail address.")
.placeholder("[email protected]")
.theme(theme.clone())
.theme(&theme)
.run()
.expect("error running input")?;
```
Expand Down
13 changes: 6 additions & 7 deletions examples/themes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ use demand::{Confirm, DemandOption, Input, MultiSelect, Theme};

fn main() {
let mut theme = Theme::new();
match args().nth(1) {
Some(arg) => match arg.as_str() {
if let Some(arg) = args().nth(1) {
match arg.as_str() {
"base16" => theme = Theme::base16(),
"charm" => theme = Theme::charm(),
"catppuccin" => theme = Theme::catppuccin(),
"dracula" => theme = Theme::dracula(),
_ => {}
},
None => {}
}
}

let i = Input::new("What's your e-mail?")
.description("Please enter your e-mail address.")
.placeholder("[email protected]")
.theme(theme.clone());
.theme(&theme);
i.run().expect("error running input");

let ms = MultiSelect::new("Interests")
Expand All @@ -33,13 +32,13 @@ fn main() {
.option(DemandOption::new("Technology"))
.option(DemandOption::new("Travel"))
.option(DemandOption::new("Sports"))
.theme(theme.clone());
.theme(&theme);
ms.run().expect("error running multi select");

let c = Confirm::new("Confirm privacy policy")
.description("Do you accept the privacy policy?")
.affirmative("Yes")
.negative("No")
.theme(theme.clone());
.theme(&theme);
c.run().expect("error running confirm");
}
19 changes: 10 additions & 9 deletions src/confirm.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::theme::Theme;

use console::{Key, Term};

use std::io;
use std::io::Write;

use console::{Key, Term};
use termcolor::{Buffer, WriteColor};

use crate::theme;
use crate::theme::Theme;

/// Select multiple options from a list
///
/// # Example
Expand All @@ -18,11 +19,11 @@ use termcolor::{Buffer, WriteColor};
/// let yes = ms.run().expect("error running confirm");
/// println!("yes: {}", yes);
/// ```
pub struct Confirm {
pub struct Confirm<'a> {
/// The title of the selector
pub title: String,
/// The colors/style of the selector
pub theme: Theme,
pub theme: &'a Theme,
/// A description to display above the selector
pub description: String,
/// The text to display for the affirmative option
Expand All @@ -35,13 +36,13 @@ pub struct Confirm {
height: usize,
}

impl Confirm {
impl<'a> Confirm<'a> {
/// Create a new multi select with the given title
pub fn new<S: Into<String>>(title: S) -> Self {
Self {
title: title.into(),
description: String::new(),
theme: Theme::default(),
theme: &*theme::DEFAULT,
term: Term::stderr(),
affirmative: "Yes".to_string(),
negative: "No".to_string(),
Expand Down Expand Up @@ -75,7 +76,7 @@ impl Confirm {
}

/// Set the theme of the dialog
pub fn theme(mut self, theme: Theme) -> Self {
pub fn theme(mut self, theme: &'a Theme) -> Self {
self.theme = theme;
self
}
Expand Down
12 changes: 6 additions & 6 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use console::{Key, Term};
use termcolor::{Buffer, WriteColor};

use crate::Theme;
use crate::{theme, Theme};

/// Single line text input
///
Expand All @@ -18,7 +18,7 @@ use crate::Theme;
/// .description("We'll use this to personalize your experience.")
/// .placeholder("Enter your name");
/// let name = input.run().expect("error running input");
pub struct Input {
pub struct Input<'a> {
/// The title of the input
pub title: String,
/// A description to display after the title
Expand All @@ -34,14 +34,14 @@ pub struct Input {
/// Input entered by the user
pub input: String,
/// Colors/style of the input
pub theme: Theme,
pub theme: &'a Theme,

cursor: usize,
height: usize,
term: Term,
}

impl Input {
impl<'a> Input<'a> {
/// Creates a new input with the given title.
pub fn new<S: Into<String>>(title: S) -> Self {
Self {
Expand All @@ -52,7 +52,7 @@ impl Input {
input: String::new(),
inline: false,
password: false,
theme: Theme::default(),
theme: &*theme::DEFAULT,
cursor: 0,
height: 0,
term: Term::stderr(),
Expand Down Expand Up @@ -95,7 +95,7 @@ impl Input {
}

/// Sets the theme of the input
pub fn theme(mut self, theme: Theme) -> Self {
pub fn theme(mut self, theme: &'a Theme) -> Self {
self.theme = theme;
self
}
Expand Down
18 changes: 10 additions & 8 deletions src/multiselect.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::theme::Theme;
use crate::DemandOption;
use console::{Key, Term};
use std::collections::HashSet;
use std::fmt::Display;
use std::io;
use std::io::Write;

use console::{Key, Term};
use termcolor::{Buffer, WriteColor};

use crate::theme::Theme;
use crate::{theme, DemandOption};

/// Select multiple options from a list
///
/// # Example
Expand All @@ -27,11 +29,11 @@ use termcolor::{Buffer, WriteColor};
/// .option(DemandOption::new("Nutella"));///
/// let toppings = ms.run().expect("error running multi select");
/// ```
pub struct MultiSelect<T: Display> {
pub struct MultiSelect<'a, T: Display> {
/// The title of the selector
pub title: String,
/// The colors/style of the selector
pub theme: Theme,
pub theme: &'a Theme,
/// A description to display above the selector
pub description: String,
/// The options which can be selected
Expand All @@ -53,7 +55,7 @@ pub struct MultiSelect<T: Display> {
capacity: usize,
}

impl<T: Display> MultiSelect<T> {
impl<'a, T: Display> MultiSelect<'a, T> {
/// Create a new multi select with the given title
pub fn new<S: Into<String>>(title: S) -> Self {
Self {
Expand All @@ -63,7 +65,7 @@ impl<T: Display> MultiSelect<T> {
min: 0,
max: usize::MAX,
filterable: false,
theme: Theme::default(),
theme: &*theme::DEFAULT,
err: None,
cursor: 0,
height: 0,
Expand Down Expand Up @@ -107,7 +109,7 @@ impl<T: Display> MultiSelect<T> {
}

/// Set the theme of the selector
pub fn theme(mut self, theme: Theme) -> Self {
pub fn theme(mut self, theme: &'a Theme) -> Self {
self.theme = theme;
self
}
Expand Down
1 change: 1 addition & 0 deletions src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ impl<T: Display> PartialEq for DemandOption<T> {
self.id == other.id
}
}

impl<T: Display> Eq for DemandOption<T> {}
19 changes: 10 additions & 9 deletions src/select.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::theme::Theme;
use crate::DemandOption;
use console::{Key, Term};

use std::fmt::Display;
use std::io;
use std::io::Write;

use console::{Key, Term};
use termcolor::{Buffer, WriteColor};

use crate::theme::Theme;
use crate::{theme, DemandOption};

/// Select multiple options from a list
///
/// # Example
Expand All @@ -25,11 +26,11 @@ use termcolor::{Buffer, WriteColor};
/// .option(DemandOption::new("Nutella"));
/// let topping = ms.run().expect("error running multi select");
/// ```
pub struct Select<T: Display> {
pub struct Select<'a, T: Display> {
/// The title of the selector
pub title: String,
/// The colors/style of the selector
pub theme: Theme,
pub theme: &'a Theme,
/// A description to display above the selector
pub description: String,
/// The options which can be selected
Expand All @@ -46,15 +47,15 @@ pub struct Select<T: Display> {
capacity: usize,
}

impl<T: Display> Select<T> {
impl<'a, T: Display> Select<'a, T> {
/// Create a new select with the given title
pub fn new<S: Into<String>>(title: S) -> Self {
Self {
title: title.into(),
description: String::new(),
options: vec![],
filterable: false,
theme: Theme::default(),
theme: &*theme::DEFAULT,
cursor: 0,
height: 0,
term: Term::stderr(),
Expand Down Expand Up @@ -85,7 +86,7 @@ impl<T: Display> Select<T> {
}

/// Set the theme of the selector
pub fn theme(mut self, theme: Theme) -> Self {
pub fn theme(mut self, theme: &'a Theme) -> Self {
self.theme = theme;
self
}
Expand Down
12 changes: 6 additions & 6 deletions src/spinner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
use console::Term;
use termcolor::{Buffer, WriteColor};

use crate::Theme;
use crate::{theme, Theme};

/// Show a spinner
///
Expand All @@ -24,26 +24,26 @@ use crate::Theme;
/// })
/// .expect("error running spinner");
/// ```
pub struct Spinner {
pub struct Spinner<'a> {
// The title of the spinner
pub title: String,
// The style of the spinner
pub style: SpinnerStyle,
/// The colors/style of the spinner
pub theme: Theme,
pub theme: &'a Theme,

term: Term,
frame: usize,
height: usize,
}

impl Spinner {
impl<'a> Spinner<'a> {
/// Create a new spinner with the given title
pub fn new<S: Into<String>>(title: S) -> Self {
Self {
title: title.into(),
style: SpinnerStyle::line(),
theme: Theme::default(),
theme: &*theme::DEFAULT,
term: Term::stderr(),
frame: 0,
height: 0,
Expand All @@ -57,7 +57,7 @@ impl Spinner {
}

/// Set the theme of the dialog
pub fn theme(mut self, theme: Theme) -> Self {
pub fn theme(mut self, theme: &'a Theme) -> Self {
self.theme = theme;
self
}
Expand Down
3 changes: 3 additions & 0 deletions src/theme.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use once_cell::sync::Lazy;
use termcolor::{Color, ColorSpec};

pub(crate) static DEFAULT: Lazy<Theme> = Lazy::new(Theme::default);

/// Theme for styling the UI.
///
/// # Example
Expand Down

0 comments on commit 1fdb59b

Please sign in to comment.