1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
use self::HorizontalAlignment::*;
use std::fmt::*;

/// Cell`s style.
#[derive(Clone, Copy, Default)]
pub struct CellStyle {
    pub(crate) align_h: Option<HorizontalAlignment>,
}
impl CellStyle {
    /// Returns the styles in which the empty style has been replaced with the specified style.
    ///
    /// Judgment as to whether the style is empty or not is done for each individual element.
    pub fn or(self, style: CellStyle) -> CellStyle {
        CellStyle {
            align_h: self.align_h.or(style.align_h),
        }
    }
}

/// Possible horizontal alignments for cell's content.
#[derive(Clone, Copy)]
pub enum HorizontalAlignment {
    Left,
    Center,
    Right,
}

/// A data structure that can be formatted into cell.
///
/// Normally, [`cell()`] or [`macro@cell`] is used to create a value that implements `CellSource`.
pub trait CellSource {
    /// Output the cell text to given buffer.
    fn fmt(&self, s: &mut String);

    /// Return cell's style.
    fn style(&self) -> CellStyle {
        CellStyle::default()
    }

    /// Return cell's default style that associated with `Self` type.
    fn default_style() -> CellStyle {
        CellStyle::default()
    }
}
impl<T: CellSource> CellSource for &T {
    fn fmt(&self, s: &mut String) {
        T::fmt(*self, s)
    }
    fn style(&self) -> CellStyle {
        T::style(*self)
    }
    fn default_style() -> CellStyle {
        T::default_style()
    }
}
impl<T: CellSource> CellSource for &mut T {
    fn fmt(&self, s: &mut String) {
        T::fmt(*self, s)
    }
    fn style(&self) -> CellStyle {
        T::style(*self)
    }
    fn default_style() -> CellStyle {
        T::default_style()
    }
}
impl<T: CellSource> CellSource for Option<T> {
    fn fmt(&self, s: &mut String) {
        if let Some(value) = self {
            value.fmt(s);
        }
    }
    fn style(&self) -> CellStyle {
        if let Some(value) = self {
            value.style()
        } else {
            CellStyle::default()
        }
    }
    fn default_style() -> CellStyle {
        T::default_style()
    }
}

struct DisplayCellSource<T: Display>(T);
impl<T: Display> CellSource for DisplayCellSource<T> {
    fn fmt(&self, s: &mut String) {
        write!(s, "{}", self.0).unwrap()
    }
}

/// Create [`Cell`] from [`Display`].
///
/// The returned value owns the value passed in.
/// Therefore, the returned value can not be move out of the lifetime of the passed value.
///
/// ```ignore
/// use text_grid::*;
///
/// fn f_error() -> Cell<impl CellSource> {
///     let s = String::from("ABC");
///     cell(&s) // Error : returns a value referencing data owned by the current function
/// }
/// ```
///
/// ```
/// use text_grid::*;
///
/// fn f_ok() -> Cell<impl CellSource> {
///     let s = String::from("ABC");
///     cell(s) // OK
/// }
/// ```
pub fn cell(value: impl Display) -> Cell<impl CellSource> {
    Cell::new(DisplayCellSource(value))
}

struct FmtFnCellSource<F>(F);
impl<F: Fn(&mut String) -> Result> CellSource for FmtFnCellSource<F> {
    fn fmt(&self, s: &mut String) {
        (self.0)(s).unwrap()
    }
}

/// Create [`Cell`] from closure that call `std::write!` macro.
///
/// # Examples
///
/// ```
/// use text_grid::*;
/// use std::fmt::Write;
///
/// let s = String::from("ABC");
/// let cell_a = cell_by(|w| write!(w, "{}", &s));
/// let cell_b = cell_by(|w| write!(w, "{}", &s));
/// ```
pub fn cell_by<F: Fn(&mut String) -> Result>(f: F) -> Cell<impl CellSource> {
    Cell::new(FmtFnCellSource(f))
}

/// Creates a [`Cell`] using interpolation of runtime expressions.
///
/// Use the `format!` syntax to create [`Cell`]. See [`std::fmt`] for more information.
///
/// # Examples
/// ```
/// use text_grid::*;
/// struct RowData {
///     a: f64,
///     b: f64,
/// }
/// impl RowSource for RowData {
///     fn fmt_row<'a>(w: &mut impl RowWrite<Source=&'a Self>) {
///         w.column("a", |s| cell!("{:.2}", s.a).right());
///         w.column("b", |s| cell!("{:.3}", s.b).right());
///     }
/// }
///
/// let mut g = Grid::new();
/// g.push_row(&RowData { a: 1.10, b: 1.11 });
/// g.push_row(&RowData { a: 1.00, b: 0.10 });
///
/// print!("{}", g);
/// ```
///
/// Output:
/// ```text
///   a   |   b   |
/// ------|-------|
///  1.10 | 1.110 |
///  1.00 | 0.100 |
/// ```
///
/// # Arguments ownership
///
/// This macro consumes the variable used in the argument.
/// ```ignore
/// use text_grid::*;
///
/// let s = String::from("ABC");
/// let cell_a = cell!("{}", &s); // `s` moved into `cell_a` here
/// let cell_b = cell!("{}", &s); // ERROR : `s` used here after move
/// ```
///
/// To avoid consume variables, use only variables that implements `Copy` .
///
/// ```
/// use text_grid::*;
///
/// let s = String::from("ABC");
/// let s = &s; // immutable reference implements `Copy`.
/// let cell_a = cell!("{}", s);
/// let cell_b = cell!("{}", s); // OK
/// // Return value owns the reference.
/// // Therefore, the returned value can not be move out of the lifetime of the reference.
/// ```
///
/// or use [`cell_by`] and `write!`.
///
/// ```
/// use text_grid::*;
/// use std::fmt::Write;
///
/// let s = String::from("ABC");
/// let cell_a = cell_by(|w| write!(w, "{}", &s));
/// let cell_b = cell_by(|w| write!(w, "{}", &s));
/// // Return value owns the reference.
/// // Therefore, the returned value can not be move out of the lifetime of the reference.
/// ```
///
/// or use [`cell()`] and `format!`.
///
/// ```
/// use text_grid::*;
///
/// let s = String::from("ABC");
/// let cell_a = cell(format!("{}", &s));
/// let cell_b = cell(format!("{}", &s));
/// // Retrun value owns formatted string.
/// // Therefore, the returned value can move anywhere.
/// ```
#[macro_export]
macro_rules! cell {
    ($ ( $ arg : tt ) *) => { {
            use std::fmt::Write;
            $crate::cell_by(move |w| write!(w, $($arg)*))
        }
    };
}

/// Implementation of [`CellSource`] that can specify styles.
pub struct Cell<T> {
    source: T,
    style: CellStyle,
}
impl<T: CellSource> CellSource for Cell<T> {
    fn fmt(&self, s: &mut String) {
        self.source.fmt(s)
    }
    fn style(&self) -> CellStyle {
        self.style
    }
}

impl<T: CellSource> Cell<T> {
    /// Create a new `Cell` with specified [`CellSource`] and empty style.
    pub fn new(source: T) -> Self {
        let style = source.style();
        Cell { source, style }
    }

    /// Return the cell with horizontal alignment set to the left.
    pub fn left(self) -> Self {
        self.with_align_h(Left)
    }

    /// Return the cell with horizontal alignment set to the right.
    pub fn right(self) -> Self {
        self.with_align_h(Right)
    }

    /// Return the cell with horizontal alignment set to the center.
    pub fn center(self) -> Self {
        self.with_align_h(Center)
    }

    /// Return the cell with an empty style replaced by the specified style.
    ///
    /// Judgment as to whether the style is empty or not is done for each individual element.
    pub fn with_base_style(self, style: CellStyle) -> Self {
        Cell {
            source: self.source,
            style: self.style.or(style),
        }
    }
    fn with_align_h(self, align_h: HorizontalAlignment) -> Self {
        Cell {
            source: self.source,
            style: CellStyle {
                align_h: Some(align_h),
            },
        }
    }
}
impl Cell<String> {
    /// Create a new `Cell` with empty string and empty style.
    pub fn empty() -> Self {
        Self::new(String::new())
    }
}

macro_rules! impl_cell_source {
    ($t:ty, $align:expr ) => {
        impl CellSource for $t {
            fn fmt(&self, s: &mut String) {
                write!(s, "{}", self).unwrap()
            }
            fn default_style() -> CellStyle {
                CellStyle {
                    align_h: Some($align),
                }
            }
        }
    };
}

impl_cell_source!(u8, Right);
impl_cell_source!(i8, Right);
impl_cell_source!(u16, Right);
impl_cell_source!(i16, Right);
impl_cell_source!(u32, Right);
impl_cell_source!(i32, Right);
impl_cell_source!(u64, Right);
impl_cell_source!(i64, Right);
impl_cell_source!(u128, Right);
impl_cell_source!(i128, Right);
impl_cell_source!(isize, Right);
impl_cell_source!(usize, Right);
impl_cell_source!(f32, Right);
impl_cell_source!(f64, Right);
impl_cell_source!(String, Left);
impl_cell_source!(&str, Left);
impl_cell_source!(char, Center);
impl_cell_source!(bool, Center);