From 0935f549ee629b4b9eddd469741ad34dcf22bdbc Mon Sep 17 00:00:00 2001 From: koe Date: Sat, 31 Aug 2024 01:11:04 -0500 Subject: [PATCH] add alignment option to Buffer::set_rich_text --- examples/rich-text/src/main.rs | 2 +- src/buffer.rs | 24 ++++++++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/examples/rich-text/src/main.rs b/examples/rich-text/src/main.rs index 8a2f858..9df0981 100644 --- a/examples/rich-text/src/main.rs +++ b/examples/rich-text/src/main.rs @@ -103,7 +103,7 @@ fn set_buffer_text<'a>(buffer: &mut BorrowedWithFontSystem<'a, Buffer>) { ), ]; - buffer.set_rich_text(spans.iter().copied(), attrs, Shaping::Advanced); + buffer.set_rich_text(spans.iter().copied(), attrs, Shaping::Advanced, None); } fn main() { diff --git a/src/buffer.rs b/src/buffer.rs index 3601746..b81e9d7 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -6,9 +6,9 @@ use core::{cmp, fmt}; use unicode_segmentation::UnicodeSegmentation; use crate::{ - Affinity, Attrs, AttrsList, BidiParagraphs, BorrowedWithFontSystem, BufferLine, Color, Cursor, - FontSystem, LayoutCursor, LayoutGlyph, LayoutLine, LineEnding, LineIter, Motion, Scroll, - ShapeBuffer, ShapeLine, Shaping, Wrap, + Affinity, Align, Attrs, AttrsList, BidiParagraphs, BorrowedWithFontSystem, BufferLine, Color, + Cursor, FontSystem, LayoutCursor, LayoutGlyph, LayoutLine, LineEnding, LineIter, Motion, + Scroll, ShapeBuffer, ShapeLine, Shaping, Wrap, }; /// A line of visible text for rendering @@ -714,6 +714,7 @@ impl Buffer { /// ], /// attrs, /// Shaping::Advanced, + /// None, /// ); /// ``` pub fn set_rich_text<'r, 's, I>( @@ -722,6 +723,7 @@ impl Buffer { spans: I, default_attrs: Attrs, shaping: Shaping, + alignment: Option, ) where I: IntoIterator)>, { @@ -842,6 +844,10 @@ impl Buffer { // Discard excess lines now that we have reused as much of the existing allocations as possible. self.lines.truncate(line_count); + self.lines.iter_mut().for_each(|line| { + line.set_align(alignment); + }); + self.scroll = Scroll::default(); self.shape_until_scroll(font_system, false); @@ -1440,14 +1446,20 @@ impl<'a> BorrowedWithFontSystem<'a, Buffer> { /// ], /// attrs, /// Shaping::Advanced, + /// None, /// ); /// ``` - pub fn set_rich_text<'r, 's, I>(&mut self, spans: I, default_attrs: Attrs, shaping: Shaping) - where + pub fn set_rich_text<'r, 's, I>( + &mut self, + spans: I, + default_attrs: Attrs, + shaping: Shaping, + alignment: Option, + ) where I: IntoIterator)>, { self.inner - .set_rich_text(self.font_system, spans, default_attrs, shaping); + .set_rich_text(self.font_system, spans, default_attrs, shaping, alignment); } /// Apply a [`Motion`] to a [`Cursor`]