Debian Codegraph @

src/cell.rs

raw symbols SCIP
1 use craterust-analyzer cargo comfy-table 7.2.2 crate/::stylerust-analyzer cargo comfy-table 7.2.2 style/::CellAlignmentrust-analyzer cargo comfy-table 7.2.2 style/cell/CellAlignment#;
2 #[cfg(feature = "tty")]
3 use craterust-analyzer cargo comfy-table 7.2.2 crate/::{Attributerust-analyzer cargo crossterm 0.29.0 style/types/attribute/Attribute#, Colorrust-analyzer cargo crossterm 0.29.0 style/types/color/Color#};
4
5 /// A stylable table cell with content.
6 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)]
7 pub struct Cellrust-analyzer cargo comfy-table 7.2.2 cell/Cell# {
8 /// The content is a list of strings.\
9 /// This is done to make working with newlines more easily.\
10 /// When creating a new [Cell], the given content is split by newline.
11 pub(craterust-analyzer cargo comfy-table 7.2.2 crate/) contentrust-analyzer cargo comfy-table 7.2.2 cell/Cell#content.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
12 /// The delimiter which is used to split the text into consistent pieces.\
13 /// The default is ` `.
14 pub(craterust-analyzer cargo comfy-table 7.2.2 crate/) delimiterrust-analyzer cargo comfy-table 7.2.2 cell/Cell#delimiter.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<char>,
15 pub(craterust-analyzer cargo comfy-table 7.2.2 crate/) alignmentrust-analyzer cargo comfy-table 7.2.2 cell/Cell#alignment.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<CellAlignmentrust-analyzer cargo comfy-table 7.2.2 style/cell/CellAlignment#>,
16 #[cfg(feature = "tty")]
17 pub(craterust-analyzer cargo comfy-table 7.2.2 crate/) fgrust-analyzer cargo comfy-table 7.2.2 cell/Cell#fg.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Colorrust-analyzer cargo crossterm 0.29.0 style/types/color/Color#>,
18 #[cfg(feature = "tty")]
19 pub(craterust-analyzer cargo comfy-table 7.2.2 crate/) bgrust-analyzer cargo comfy-table 7.2.2 cell/Cell#bg.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Colorrust-analyzer cargo crossterm 0.29.0 style/types/color/Color#>,
20 #[cfg(feature = "tty")]
21 pub(craterust-analyzer cargo comfy-table 7.2.2 crate/) attributesrust-analyzer cargo comfy-table 7.2.2 cell/Cell#attributes.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Attributerust-analyzer cargo crossterm 0.29.0 style/types/attribute/Attribute#>,
22 }
23
24 impl Cellrust-analyzer cargo comfy-table 7.2.2 cell/Cell# {
25 /// Create a new Cell
26 #[allow(clippy::needless_pass_by_value)]
27 pub fn newrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell]new().<T: ToStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/ToString#>(content: T) -> Selfrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell] {
28 Selfrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell]::new_ownedrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell]new_owned().(content.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/ToString#to_string().())
29 }
30
31 /// Create a new Cell from an owned String
32 pub fn new_ownedrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell]new_owned().(content: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Selfrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell] {
33 #[cfg_attr(not(feature = "custom_styling"), allow(unused_mut))]
34 let mut split_content: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> = content.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().('\n').maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(ToStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/ToString#::to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
35
36 // Correct ansi codes so style is terminated and resumed around the split
37 #[cfg(feature = "custom_styling")]
38 craterust-analyzer cargo comfy-table 7.2.2 crate/::utilsrust-analyzer cargo comfy-table 7.2.2 utils/::formattingrust-analyzer cargo comfy-table 7.2.2 utils/formatting/::content_splitrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/::fix_style_in_split_strrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/fix_style_in_split_str().(&mut split_content);
39
40 Selfrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell] {
41 contentrust-analyzer cargo comfy-table 7.2.2 cell/Cell#content.: split_content,
42 delimiterrust-analyzer cargo comfy-table 7.2.2 cell/Cell#delimiter.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
43 alignmentrust-analyzer cargo comfy-table 7.2.2 cell/Cell#alignment.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
44 #[cfg(feature = "tty")]
45 fgrust-analyzer cargo comfy-table 7.2.2 cell/Cell#fg.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
46 #[cfg(feature = "tty")]
47 bgrust-analyzer cargo comfy-table 7.2.2 cell/Cell#bg.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
48 #[cfg(feature = "tty")]
49 attributesrust-analyzer cargo comfy-table 7.2.2 cell/Cell#attributes.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(),
50 }
51 }
52
53 /// Return a copy of the content contained in this cell.
54 pub fn contentrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell]content().(&self) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
55 self.contentrust-analyzer cargo comfy-table 7.2.2 cell/Cell#content..joinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]join().("\n")
56 }
57
58 /// Set the delimiter used to split text for this cell. \
59 /// Normal text uses spaces (` `) as delimiters. This is necessary to help comfy-table
60 /// understand the concept of _words_.
61 #[must_use]
62 pub fn set_delimiterrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell]set_delimiter().(mut self, delimiter: char) -> Selfrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell] {
63 self.delimiterrust-analyzer cargo comfy-table 7.2.2 cell/Cell#delimiter. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(delimiter);
64
65 self
66 }
67
68 /// Set the alignment of content for this cell.
69 ///
70 /// Setting this overwrites alignment settings of the
71 /// [Column](crate::column::Column::set_cell_alignment) for this specific cell.
72 /// ```
73 /// use comfy_table::{Cell, CellAlignment};
74 ///
75 /// let mut cell = Cell::new("Some content").set_alignment(CellAlignment::Center);
76 /// ```
77 #[must_use]
78 pub fn set_alignmentrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell]set_alignment().(mut self, alignment: CellAlignmentrust-analyzer cargo comfy-table 7.2.2 style/cell/CellAlignment#) -> Selfrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell] {
79 self.alignmentrust-analyzer cargo comfy-table 7.2.2 cell/Cell#alignment. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(alignment);
80
81 self
82 }
83
84 /// Set the foreground text color for this cell.
85 ///
86 /// Look at [Color](crate::Color) for a list of all possible Colors.
87 /// ```
88 /// use comfy_table::{Cell, Color};
89 ///
90 /// let mut cell = Cell::new("Some content").fg(Color::Red);
91 /// ```
92 #[cfg(feature = "tty")]
93 #[must_use]
94 pub fn fgrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell]fg().(mut self, color: Colorrust-analyzer cargo crossterm 0.29.0 style/types/color/Color#) -> Selfrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell] {
95 self.fgrust-analyzer cargo comfy-table 7.2.2 cell/Cell#fg. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(color);
96
97 self
98 }
99
100 /// Set the background color for this cell.
101 ///
102 /// Look at [Color](crate::Color) for a list of all possible Colors.
103 /// ```
104 /// use comfy_table::{Cell, Color};
105 ///
106 /// let mut cell = Cell::new("Some content").bg(Color::Red);
107 /// ```
108 #[cfg(feature = "tty")]
109 #[must_use]
110 pub fn bgrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell]bg().(mut self, color: Colorrust-analyzer cargo crossterm 0.29.0 style/types/color/Color#) -> Selfrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell] {
111 self.bgrust-analyzer cargo comfy-table 7.2.2 cell/Cell#bg. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(color);
112
113 self
114 }
115
116 /// Add a styling attribute to the content cell.\
117 /// Those can be **bold**, _italic_, blinking and many more.
118 ///
119 /// Look at [Attribute](crate::Attribute) for a list of all possible Colors.
120 /// ```
121 /// use comfy_table::{Attribute, Cell};
122 ///
123 /// let mut cell = Cell::new("Some content").add_attribute(Attribute::Bold);
124 /// ```
125 #[cfg(feature = "tty")]
126 #[must_use]
127 pub fn add_attributerust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell]add_attribute().(mut self, attribute: Attributerust-analyzer cargo crossterm 0.29.0 style/types/attribute/Attribute#) -> Selfrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell] {
128 self.attributesrust-analyzer cargo comfy-table 7.2.2 cell/Cell#attributes..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(attribute);
129
130 self
131 }
132
133 /// Same as add_attribute, but you can pass a vector of [Attributes](Attribute)
134 #[cfg(feature = "tty")]
135 #[must_use]
136 pub fn add_attributesrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell]add_attributes().(mut self, mut attribute: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Attributerust-analyzer cargo crossterm 0.29.0 style/types/attribute/Attribute#>) -> Selfrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell] {
137 self.attributesrust-analyzer cargo comfy-table 7.2.2 cell/Cell#attributes..appendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]append().(&mut attribute);
138
139 self
140 }
141 }
142
143 /// Convert anything with [ToString] to a new [Cell].
144 ///
145 /// ```
146 /// # use comfy_table::Cell;
147 /// let cell: Cell = "content".into();
148 /// let cell: Cell = 5u32.into();
149 /// ```
150 impl<T: ToStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/ToString#> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<T> for Cellrust-analyzer cargo comfy-table 7.2.2 cell/Cell# {
151 fn fromrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell][`From<T>`]from().(content: T) -> Selfrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell][`From<T>`] {
152 Selfrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell][`From<T>`]::newrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell]new().(content)
153 }
154 }
155
156 /// A simple wrapper type for a `Vec<Cell>`.
157 ///
158 /// This wrapper is needed to support generic conversions between iterables and `Vec<Cell>`.
159 /// Check the trait implementations for more docs.
160 pub struct Cellsrust-analyzer cargo comfy-table 7.2.2 cell/Cells#(pub Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Cellrust-analyzer cargo comfy-table 7.2.2 cell/Cell#>);
161
162 /// Allow the conversion of a type to a [Cells], which is a simple vector of cells.
163 ///
164 /// By default this is implemented for all Iterators over items implementing [ToString].
165 ///
166 /// ```
167 /// use comfy_table::{Cells, Row};
168 ///
169 /// let cells_string: Cells = vec!["One", "Two", "Three"].into();
170 /// let cells_integer: Cells = vec![1, 2, 3, 4].into();
171 /// ```
172 impl<T> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<T> for Cellsrust-analyzer cargo comfy-table 7.2.2 cell/Cells#
173 where
174 T: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#,
175 T::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item#: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Cellrust-analyzer cargo comfy-table 7.2.2 cell/Cell#>,
176 {
177 fn fromrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cells][`From<T>`]from().(cells: T) -> Selfrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cells][`From<T>`] {
178 Selfrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cells][`From<T>`](cells.into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#into_iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#::intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().())
179 }
180 }
181
182 #[cfg(test)]
183 mod testsrust-analyzer cargo comfy-table 7.2.2 cell/tests/ {
184 use superrust-analyzer cargo comfy-table 7.2.2 cell/::*;
185
186 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
187 fn test_column_generationrust-analyzer cargo comfy-table 7.2.2 cell/tests/test_column_generation().() {
188 let content = "This is\nsome multiline\nstring".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
189 let cell = Cellrust-analyzer cargo comfy-table 7.2.2 cell/Cell#::newrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell]new().(content.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().());
190
191 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(cell.contentrust-analyzer cargo comfy-table 7.2.2 cell/impl#[Cell]content().(), content);
192 }
193 }