Note

Struct Note 

Source
pub struct Note { /* private fields */ }
Expand description

A styled callout block for warnings, tips, and info messages.

The Note struct provides a way to display important messages in a visually distinct box in the terminal. It supports various kinds (info, warning, tip), custom icons, colors, boldness, and different border styles.

§Examples

A basic information note:

use cliux::components::Note;

Note::new("This is an important piece of information.")
    .kind("info")
    .print();

A warning note with a custom width and square borders:

use cliux::components::Note;

Note::new("Be cautious when proceeding with this action.")
    .kind("warning")
    .width(60)
    .style("square")
    .print();

A tip with a custom icon and color:

use cliux::components::Note;

Note::new("Remember to save your work frequently!")
    .icon("✨")
    .color("magenta")
    .bold(true)
    .print();

Implementations§

Source§

impl Note

Source

pub fn new(text: &str) -> Self

Creates a new Note instance with the given text.

By default, the note will have “rounded” borders, a width of 50 characters, no specific icon, color, or boldness.

§Arguments
  • text - The main content string for the note.
§Returns

A new Note instance.

Examples found in repository?
examples/note.rs (line 4)
3fn main() {
4    Note::new("Be careful with this setting.")
5        .kind("warning")
6        .style("rounded")
7        .width(40)
8        .print();
9
10    Note::new("Tip: You can use --force here.")
11        .kind("info")
12        .style("+")
13        .width(40)
14        .print();
15}
Source

pub fn kind(self, kind: &str) -> Self

Applies a predefined “kind” style to the note.

This method consumes self and returns a new Note instance, allowing for method chaining. It sets a default icon, color, and boldness based on the kind.

Supported kinds:

  • "info": Sets icon to “ℹ️”, color to blue.
  • "warning": Sets icon to “⚠️”, color to yellow, and text to bold.
  • "tip": Sets icon to “💡”, color to green.

If an unknown kind is provided, the note remains unchanged.

§Arguments
  • kind - A string slice representing the predefined style kind.
§Returns

The Note instance with the applied kind style.

Examples found in repository?
examples/note.rs (line 5)
3fn main() {
4    Note::new("Be careful with this setting.")
5        .kind("warning")
6        .style("rounded")
7        .width(40)
8        .print();
9
10    Note::new("Tip: You can use --force here.")
11        .kind("info")
12        .style("+")
13        .width(40)
14        .print();
15}
Source

pub fn icon(self, icon: &str) -> Self

Sets a custom icon for the note.

This method consumes self and returns a new Note instance, allowing for method chaining. This will override any icon set by kind().

§Arguments
  • icon - A string slice representing the custom icon (e.g., an emoji like “✨”).
§Returns

The Note instance with the updated icon.

Source

pub fn color(self, color: &str) -> Self

Sets the foreground color of the note’s text and icon.

This method consumes self and returns a new Note instance, allowing for method chaining. Supported colors include “red”, “green”, “yellow”, “blue”, “magenta” (or “purple”), “cyan”, and “white”. Color names are case-insensitive.

§Arguments
  • color - A string slice representing the desired color name.
§Returns

The Note instance with the updated color.

Source

pub fn bold(self, bold: bool) -> Self

Sets whether the note’s text and icon should be bold.

This method consumes self and returns a new Note instance, allowing for method chaining.

§Arguments
  • bold - A boolean indicating whether the text should be bold (true) or not (false).
§Returns

The Note instance with the updated bold setting.

Source

pub fn style(self, style: &str) -> Self

Sets the border style for the note.

This method consumes self and returns a new Note instance, allowing for method chaining.

Supported styles:

  • "rounded" (default): Uses ╭╮╰╯─│ characters.
  • "square": Uses ┌┐└┘─│ characters.
  • "+": Uses ++++--|| characters for a simpler ASCII look.

If an unknown style is provided, it defaults to “square” borders.

§Arguments
  • style - A string slice representing the desired border style.
§Returns

The Note instance with the updated border style.

Examples found in repository?
examples/note.rs (line 6)
3fn main() {
4    Note::new("Be careful with this setting.")
5        .kind("warning")
6        .style("rounded")
7        .width(40)
8        .print();
9
10    Note::new("Tip: You can use --force here.")
11        .kind("info")
12        .style("+")
13        .width(40)
14        .print();
15}
Source

pub fn width(self, width: usize) -> Self

Sets the total width of the note box.

This method consumes self and returns a new Note instance, allowing for method chaining. The content will be padded to fit within this width.

§Arguments
  • width - The desired total width of the note box in characters.
§Returns

The Note instance with the updated width.

Examples found in repository?
examples/note.rs (line 7)
3fn main() {
4    Note::new("Be careful with this setting.")
5        .kind("warning")
6        .style("rounded")
7        .width(40)
8        .print();
9
10    Note::new("Tip: You can use --force here.")
11        .kind("info")
12        .style("+")
13        .width(40)
14        .print();
15}
Source

pub fn print(&self)

Prints the formatted note to the console.

This method constructs the note with its borders, icon, styled text, and padding, then prints it to standard output.

Examples found in repository?
examples/note.rs (line 8)
3fn main() {
4    Note::new("Be careful with this setting.")
5        .kind("warning")
6        .style("rounded")
7        .width(40)
8        .print();
9
10    Note::new("Tip: You can use --force here.")
11        .kind("info")
12        .style("+")
13        .width(40)
14        .print();
15}

Auto Trait Implementations§

§

impl Freeze for Note

§

impl RefUnwindSafe for Note

§

impl Send for Note

§

impl Sync for Note

§

impl Unpin for Note

§

impl UnwindSafe for Note

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.