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
use crate::cell::*;

pub trait RowWriteCore {
    fn group_start(&mut self);
    fn group_end(&mut self, header: impl CellSource);
}

/// Used to define column information.
///
/// - Use [`column`](Self::column) to create column.
/// - Use [`group`](Self::group) to create multi level header.
/// - Use [`content`](Self::content) to create shared header columns.
pub trait RowWrite: RowWriteCore {
    type Source;

    /// Define column group. Used to create multi row header.
    ///
    /// - header : Column group header's cell. If horizontal alignment is not specified, it is set to the center.
    ///
    /// # Examples
    ///
    /// ```
    /// use text_grid::*;
    /// struct RowData {
    ///     a: u32,
    ///     b_1: u32,
    ///     b_2: u32,
    /// }
    /// impl RowSource for RowData {
    ///     fn fmt_row<'a>(w: &mut impl RowWrite<Source=&'a Self>) {
    ///         w.column("a", |s| s.a);
    ///         w.group("b").with(|w| {
    ///             w.column("1", |s| s.b_1);
    ///             w.column("2", |s| s.b_2);
    ///         });
    ///     }
    /// }
    ///
    /// let mut g = Grid::new();
    /// g.push_row(&RowData {
    ///     a: 300,
    ///     b_1: 10,
    ///     b_2: 20,
    /// });
    /// g.push_row(&RowData {
    ///     a: 300,
    ///     b_1: 1,
    ///     b_2: 500,
    /// });
    ///
    /// ```
    ///
    /// Output:
    /// ```text
    ///   a  |    b     |
    /// -----|----------|
    ///      | 1  |  2  |
    /// -----|----|-----|
    ///  300 | 10 |  20 |
    ///  300 |  1 | 500 |
    ///  ```    
    fn group<C: CellSource>(&mut self, header: C) -> GroupGuard<Self, C> {
        self.group_start();
        GroupGuard {
            w: self,
            header: Some(header),
        }
    }

    /// Define column content. Used to create shared header column.
    ///
    /// - f : A function to obtain cell.
    ///
    /// # Examples
    ///
    /// ```
    /// use text_grid::*;
    /// struct RowData {
    ///     a: u32,
    ///     b_1: u32,
    ///     b_2: u32,
    /// }
    /// impl RowSource for RowData {
    ///     fn fmt_row<'a>(w: &mut impl RowWrite<Source = &'a Self>) {
    ///         w.column("a", |s| s.a);
    ///         w.group("b").with(|w| {
    ///             w.content(|s| s.b_1);
    ///             w.content(|_| " ");
    ///             w.content(|s| s.b_2);
    ///         });
    ///     }
    /// }
    ///
    /// let mut g = Grid::new();
    /// g.push_row(&RowData {
    ///     a: 300,
    ///     b_1: 10,
    ///     b_2: 20,
    /// });
    /// g.push_row(&RowData {
    ///     a: 300,
    ///     b_1: 1,
    ///     b_2: 500,
    /// });
    ///
    /// print!("{}", g);
    ///
    /// ```
    ///
    /// Output:
    /// ```text
    ///   a  |   b    |
    /// -----|--------|
    ///  300 | 10  20 |
    ///  300 |  1 500 |
    /// ```
    fn content<T: CellSource>(&mut self, f: impl FnOnce(Self::Source) -> T);

    /// Define column.
    ///
    /// - header : Column header's cell. If horizontal alignment is not specified, it is set to the center.
    /// - f : A function to obtain cell.
    ///
    /// # Examples
    ///
    /// ```
    /// use text_grid::*;
    /// struct RowData {
    ///     a: u32,
    ///     b: u32,
    /// }
    /// impl RowSource for RowData {
    ///     fn fmt_row<'a>(w: &mut impl RowWrite<Source=&'a Self>) {
    ///         w.column("a", |s| s.a);
    ///         w.column("b", |s| s.b);
    ///     }
    /// }
    ///
    /// let mut g = Grid::new();
    /// g.push_row(&RowData { a: 300, b: 1 });
    /// g.push_row(&RowData { a: 2, b: 200 });
    ///
    /// print!("{}", g);
    /// ```
    ///
    /// Output:
    /// ```text
    ///   a  |  b  |
    /// -----|-----|
    ///  300 |   1 |
    ///    2 | 200 |
    /// ```
    fn column<T: CellSource>(
        &mut self,
        header: impl CellSource,
        f: impl FnOnce(Self::Source) -> T,
    ) {
        self.group(header).content(f);
    }

    /// Takes a closure and creates [`RowWrite`] whose source value was converted.
    fn map<F: Fn(Self::Source) -> R, R>(&mut self, f: F) -> Map<Self, F> {
        Map { w: self, f }
    }

    /// Creates [`RowWrite`] which uses a closure to determine if an content should be outputed.
    fn filter<F: Fn(&Self::Source) -> bool>(&mut self, f: F) -> Filter<Self, F> {
        Filter { w: self, f }
    }

    /// Creates [`RowWrite`] that both filters and maps.
    fn filter_map<F: Fn(Self::Source) -> Option<R>, R>(&mut self, f: F) -> FilterMap<Self, F> {
        FilterMap { w: self, f }
    }

    /// Apply `f` to self.
    fn with(&mut self, f: impl Fn(&mut Self)) {
        f(self);
    }
}

pub struct Map<'a, W: ?Sized, F> {
    w: &'a mut W,
    f: F,
}

impl<'a, R, W: RowWrite, F: Fn(W::Source) -> R> RowWrite for Map<'a, W, F> {
    type Source = R;

    fn content<T: CellSource>(&mut self, f: impl FnOnce(Self::Source) -> T) {
        let f0 = &self.f;
        self.w.content(|s| f(f0(s)))
    }
}

impl<'a, W: RowWriteCore, F> RowWriteCore for Map<'a, W, F> {
    fn group_start(&mut self) {
        self.w.group_start();
    }
    fn group_end(&mut self, header: impl CellSource) {
        self.w.group_end(header);
    }
}

pub struct Filter<'a, W: ?Sized, F> {
    w: &'a mut W,
    f: F,
}
impl<'a, W: RowWrite + ?Sized, F: Fn(&W::Source) -> bool> RowWrite for Filter<'a, W, F> {
    type Source = W::Source;

    fn content<T: CellSource>(&mut self, f: impl FnOnce(Self::Source) -> T) {
        let f0 = &self.f;
        self.w.content(|s| if f0(&s) { Some(f(s)) } else { None })
    }
}

impl<'a, W: RowWriteCore + ?Sized, F> RowWriteCore for Filter<'a, W, F> {
    fn group_start(&mut self) {
        self.w.group_start();
    }
    fn group_end(&mut self, header: impl CellSource) {
        self.w.group_end(header);
    }
}

pub struct FilterMap<'a, W: ?Sized, F> {
    w: &'a mut W,
    f: F,
}
impl<'a, R, W: RowWrite + ?Sized, F: Fn(W::Source) -> Option<R>> RowWrite for FilterMap<'a, W, F> {
    type Source = R;

    fn content<T: CellSource>(&mut self, f: impl FnOnce(Self::Source) -> T) {
        let f0 = &self.f;
        self.w.content(|s| f0(s).map(f))
    }
}

impl<'a, W: RowWriteCore + ?Sized, F> RowWriteCore for FilterMap<'a, W, F> {
    fn group_start(&mut self) {
        self.w.group_start();
    }
    fn group_end(&mut self, header: impl CellSource) {
        self.w.group_end(header);
    }
}

pub struct GroupGuard<'a, W: RowWriteCore + ?Sized, C: CellSource> {
    w: &'a mut W,
    header: Option<C>,
}
impl<'a, W: RowWrite + ?Sized, C: CellSource> RowWrite for GroupGuard<'a, W, C> {
    type Source = W::Source;

    fn content<T: CellSource>(&mut self, f: impl FnOnce(Self::Source) -> T) {
        self.w.content(f)
    }
}

impl<'a, W: RowWriteCore + ?Sized, C: CellSource> RowWriteCore for GroupGuard<'a, W, C> {
    fn group_start(&mut self) {
        self.w.group_start();
    }
    fn group_end(&mut self, header: impl CellSource) {
        self.w.group_end(header);
    }
}
impl<W: RowWriteCore + ?Sized, C: CellSource> Drop for GroupGuard<'_, W, C> {
    fn drop(&mut self) {
        self.w.group_end(self.header.take().unwrap())
    }
}