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

feat(paragraph): allow Lines to be individually aligned #149

Merged
merged 4 commits into from
May 26, 2023
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
38 changes: 37 additions & 1 deletion src/text/line.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#![allow(deprecated)]
use super::{Span, Spans, Style};
use crate::layout::Alignment;

#[derive(Debug, Clone, PartialEq, Default, Eq)]
pub struct Line<'a> {
pub spans: Vec<Span<'a>>,
pub alignment: Option<Alignment>,
}

impl<'a> Line<'a> {
Expand Down Expand Up @@ -74,6 +76,27 @@ impl<'a> Line<'a> {
span.reset_style();
}
}

/// Sets the target alignment for this line of text.
/// Defaults to: [`None`], meaning the alignment is determined by the rendering widget.
///
/// ## Examples
///
/// ```rust
/// # use std::borrow::Cow;
/// # use ratatui::layout::Alignment;
/// # use ratatui::text::{Span, Line};
/// # use ratatui::style::{Color, Style, Modifier};
/// let mut line = Line::from("Hi, what's up?");
/// assert_eq!(None, line.alignment);
/// assert_eq!(Some(Alignment::Right), line.alignment(Alignment::Right).alignment)
/// ```
pub fn alignment(self, alignment: Alignment) -> Self {
Self {
alignment: Some(alignment),
..self
}
}
}

impl<'a> From<String> for Line<'a> {
Expand All @@ -90,7 +113,10 @@ impl<'a> From<&'a str> for Line<'a> {

impl<'a> From<Vec<Span<'a>>> for Line<'a> {
fn from(spans: Vec<Span<'a>>) -> Self {
Self { spans }
Self {
spans,
..Default::default()
}
}
}

Expand All @@ -117,6 +143,7 @@ impl<'a> From<Spans<'a>> for Line<'a> {

#[cfg(test)]
mod tests {
use crate::layout::Alignment;
use crate::style::{Color, Modifier, Style};
use crate::text::{Line, Span, Spans};

Expand Down Expand Up @@ -210,4 +237,13 @@ mod tests {
let s: String = line.into();
assert_eq!("Hello, world!", s);
}

#[test]
fn test_alignment() {
let line = Line::from("This is left").alignment(Alignment::Left);
assert_eq!(Some(Alignment::Left), line.alignment);

let line = Line::from("This is default");
assert_eq!(None, line.alignment);
}
}
21 changes: 21 additions & 0 deletions src/text/spans.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![allow(deprecated)]

use super::{Span, Style};
use crate::layout::Alignment;
use crate::text::Line;

/// A string composed of clusters of graphemes, each with their own style.
///
Expand Down Expand Up @@ -79,6 +82,24 @@ impl<'a> Spans<'a> {
span.reset_style();
}
}

/// Sets the target alignment for this line of text.
/// Defaults to: [`None`], meaning the alignment is determined by the rendering widget.
///
/// ## Examples
///
/// ```rust
/// # use std::borrow::Cow;
/// # use ratatui::layout::Alignment;
/// # use ratatui::text::{Span, Spans};
/// # use ratatui::style::{Color, Style, Modifier};
/// let mut line = Spans::from("Hi, what's up?").alignment(Alignment::Right);
/// assert_eq!(Some(Alignment::Right), line.alignment)
/// ```
pub fn alignment(self, alignment: Alignment) -> Line<'a> {
let line = Line::from(self);
line.alignment(alignment)
}
}

impl<'a> From<String> for Spans<'a> {
Expand Down
Loading