@@ -50,6 +50,8 @@ use crate::prelude::*;
50
50
/// - [`Text::height`] returns the height.
51
51
/// - [`Text::patch_style`] patches the style of this `Text`, adding modifiers from the given style.
52
52
/// - [`Text::reset_style`] resets the style of the `Text`.
53
+ /// - [`Text::push_line`] adds a line to the text.
54
+ /// - [`Text::push_span`] adds a span to the last line of the text.
53
55
///
54
56
/// # Examples
55
57
///
@@ -441,6 +443,46 @@ impl<'a> Text<'a> {
441
443
pub fn iter_mut ( & mut self ) -> std:: slice:: IterMut < Line < ' a > > {
442
444
self . lines . iter_mut ( )
443
445
}
446
+
447
+ /// Adds a line to the text.
448
+ ///
449
+ /// `line` can be any type that can be converted into a `Line`. For example, you can pass a
450
+ /// `&str`, a `String`, a `Span`, or a `Line`.
451
+ ///
452
+ /// # Examples
453
+ ///
454
+ /// ```rust
455
+ /// # use ratatui::prelude::*;
456
+ /// let mut text = Text::from("Hello, world!");
457
+ /// text.push_line(Line::from("How are you?"));
458
+ /// text.push_line(Span::from("How are you?"));
459
+ /// text.push_line("How are you?");
460
+ /// ```
461
+ pub fn push_line < T : Into < Line < ' a > > > ( & mut self , line : T ) {
462
+ self . lines . push ( line. into ( ) ) ;
463
+ }
464
+
465
+ /// Adds a span to the last line of the text.
466
+ ///
467
+ /// `span` can be any type that is convertible into a `Span`. For example, you can pass a
468
+ /// `&str`, a `String`, or a `Span`.
469
+ ///
470
+ /// # Examples
471
+ ///
472
+ /// ```rust
473
+ /// # use ratatui::prelude::*;
474
+ /// let mut text = Text::from("Hello, world!");
475
+ /// text.push_span(Span::from("How are you?"));
476
+ /// text.push_span("How are you?");
477
+ /// ```
478
+ pub fn push_span < T : Into < Span < ' a > > > ( & mut self , span : T ) {
479
+ let span = span. into ( ) ;
480
+ if let Some ( last) = self . lines . last_mut ( ) {
481
+ last. push_span ( span) ;
482
+ } else {
483
+ self . lines . push ( Line :: from ( span) ) ;
484
+ }
485
+ }
444
486
}
445
487
446
488
impl < ' a > IntoIterator for Text < ' a > {
@@ -854,6 +896,70 @@ mod tests {
854
896
assert_eq ! ( Text :: default ( ) . italic( ) . style, Modifier :: ITALIC . into( ) ) ;
855
897
}
856
898
899
+ #[ test]
900
+ fn left_aligned ( ) {
901
+ let text = Text :: from ( "Hello, world!" ) . left_aligned ( ) ;
902
+ assert_eq ! ( text. alignment, Some ( Alignment :: Left ) ) ;
903
+ }
904
+
905
+ #[ test]
906
+ fn centered ( ) {
907
+ let text = Text :: from ( "Hello, world!" ) . centered ( ) ;
908
+ assert_eq ! ( text. alignment, Some ( Alignment :: Center ) ) ;
909
+ }
910
+
911
+ #[ test]
912
+ fn right_aligned ( ) {
913
+ let text = Text :: from ( "Hello, world!" ) . right_aligned ( ) ;
914
+ assert_eq ! ( text. alignment, Some ( Alignment :: Right ) ) ;
915
+ }
916
+
917
+ #[ test]
918
+ fn push_line ( ) {
919
+ let mut text = Text :: from ( "A" ) ;
920
+ text. push_line ( Line :: from ( "B" ) ) ;
921
+ text. push_line ( Span :: from ( "C" ) ) ;
922
+ text. push_line ( "D" ) ;
923
+ assert_eq ! (
924
+ text. lines,
925
+ vec![
926
+ Line :: raw( "A" ) ,
927
+ Line :: raw( "B" ) ,
928
+ Line :: raw( "C" ) ,
929
+ Line :: raw( "D" )
930
+ ]
931
+ ) ;
932
+ }
933
+
934
+ #[ test]
935
+ fn push_line_empty ( ) {
936
+ let mut text = Text :: default ( ) ;
937
+ text. push_line ( Line :: from ( "Hello, world!" ) ) ;
938
+ assert_eq ! ( text. lines, vec![ Line :: from( "Hello, world!" ) ] ) ;
939
+ }
940
+
941
+ #[ test]
942
+ fn push_span ( ) {
943
+ let mut text = Text :: from ( "A" ) ;
944
+ text. push_span ( Span :: raw ( "B" ) ) ;
945
+ text. push_span ( "C" ) ;
946
+ assert_eq ! (
947
+ text. lines,
948
+ vec![ Line :: from( vec![
949
+ Span :: raw( "A" ) ,
950
+ Span :: raw( "B" ) ,
951
+ Span :: raw( "C" )
952
+ ] ) ] ,
953
+ ) ;
954
+ }
955
+
956
+ #[ test]
957
+ fn push_span_empty ( ) {
958
+ let mut text = Text :: default ( ) ;
959
+ text. push_span ( Span :: raw ( "Hello, world!" ) ) ;
960
+ assert_eq ! ( text. lines, vec![ Line :: from( Span :: raw( "Hello, world!" ) ) ] , ) ;
961
+ }
962
+
857
963
mod widget {
858
964
use super :: * ;
859
965
use crate :: assert_buffer_eq;
@@ -958,24 +1064,6 @@ mod tests {
958
1064
}
959
1065
}
960
1066
961
- #[ test]
962
- fn left_aligned ( ) {
963
- let text = Text :: from ( "Hello, world!" ) . left_aligned ( ) ;
964
- assert_eq ! ( text. alignment, Some ( Alignment :: Left ) ) ;
965
- }
966
-
967
- #[ test]
968
- fn centered ( ) {
969
- let text = Text :: from ( "Hello, world!" ) . centered ( ) ;
970
- assert_eq ! ( text. alignment, Some ( Alignment :: Center ) ) ;
971
- }
972
-
973
- #[ test]
974
- fn right_aligned ( ) {
975
- let text = Text :: from ( "Hello, world!" ) . right_aligned ( ) ;
976
- assert_eq ! ( text. alignment, Some ( Alignment :: Right ) ) ;
977
- }
978
-
979
1067
mod iterators {
980
1068
use super :: * ;
981
1069
0 commit comments