Skip to content
Open
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
53 changes: 53 additions & 0 deletions core/src/string.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::ops::{Add, AddAssign};

use crate::{Display, Formatter, Result, Style, Write};

/// An attributed version of [`alloc::string::String`] which has a specific
Expand Down Expand Up @@ -82,3 +84,54 @@ impl Display for String {
Ok(())
}
}

impl From<&str> for String {
#[inline]
fn from(string: &str) -> Self {
Self::from(alloc::string::String::from(string))
}
}
impl From<alloc::string::String> for String {
#[inline]
fn from(string: alloc::string::String) -> Self {
Self {
string,
styles: alloc::vec![(0, Style::default())],
}
}
}

impl AddAssign<String> for String {
#[inline]
fn add_assign(&mut self, rhs: String) {
let offset = self.string.len();
self.string.push_str(&rhs.string);
for (index, style) in rhs.styles {
self.styles.push((index + offset, style));
}
}
}
impl Add<String> for String {
type Output = Self;

#[inline]
fn add(mut self, rhs: String) -> Self::Output {
self += rhs;
self
}
}
impl AddAssign<&str> for String {
#[inline]
fn add_assign(&mut self, rhs: &str) {
self.write_str(rhs, Style::default()).unwrap();
}
}
impl Add<&str> for String {
type Output = Self;

#[inline]
fn add(mut self, rhs: &str) -> Self::Output {
self += rhs;
self
}
}
Empty file removed core/src/style.rs
Empty file.
6 changes: 3 additions & 3 deletions core/src/to_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::{format, Display, String};
/// A trait for converting a value to a [`stylish::String`].
///
/// This trait is automatically implemented for any type which implements the
/// [`stylish::Display`] trait. As such, `ToString` shouldn’t be implemented
/// directly: [`stylish::Display`] should be implemented instead, and you get
/// the `ToString` implementation for free.
/// [`stylish::Display`] trait. As such, `ToStylishString` shouldn’t be
/// implemented directly: [`stylish::Display`] should be implemented instead,
/// and you get the `ToStylishString` implementation for free.
pub trait ToStylishString {
/// Converts the given value to a [`stylish::String`].
///
Expand Down
19 changes: 19 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ mod tests {
assert_eq!(stylish::plain::format!("{x}", x = 2), "2")
}

#[test]
fn equal_conversions() {
assert_eq!(
stylish::ansi::format!("abcd"),
stylish::ansi::format!("{:s}", stylish::String::from("abcd"))
);
}

#[test]
fn string_addition() {
let mut s = stylish::String::new();
s += stylish::format!("{:(fg=red)}", "Hello");
s += stylish::format!("{:(bg=blue)}", "World");
assert_eq!(
stylish::ansi::format!("{:s}", s),
stylish::ansi::format!("{:(fg=red)}{:(bg=blue)}", "Hello", "World")
)
}

#[test]
fn dyn_display() {
let x: &dyn std::fmt::Display = &1;
Expand Down
Loading