duat_core/widgets/status_line/
state.rs

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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! The acceptable patterns in a [`StatusLine`]
//!
//! The patterns that can be put in a [`StatusLine`] are largely the
//! same as the ones that can be put inside a [`text!`] macro,
//! however, some additions are made.
//!
//! Specifically, arguments that read from the [`File`] and its
//! related structs are also accepted by [`status!`].
//!
//! In addition, arguments with arbitrary update schedules are also
//! accepted, such as the [data] types, and parser/checker function
//! pairs.
//!
//! [`StatusLine`]: super::StatusLine
//! [`status!`]: super::status
//! [data]: crate::data
use std::{fmt::Display, marker::PhantomData};

use crate::{
    context::FileReader,
    data::{DataMap, RoData, RwData},
    mode::Cursors,
    text::{Builder, Tag, Text, text},
    ui::Ui,
    widgets::{File, Widget},
};

/// A struct that reads state in order to return [`Text`].
enum Appender<T> {
    NoArgsStr(Box<dyn FnMut() -> String + Send + Sync + 'static>),
    NoArgsText(Box<dyn FnMut() -> Text + Send + Sync + 'static>),
    FromRelatedStr(RelatedStrFn<T>),
    FromRelatedText(RelatedTextFn<T>),
    FromFileAndRelatedStr(FileAndRelatedStrFn<T>),
    FromFileAndRelatedText(FileAndRelatedTextFn<T>),
    Str(String),
    Text(Text),
}

/// A part of the [`StatusLine`]
///
/// This can either be a static part, like [`Text`], [`impl Display`]
/// type, or it can be a reader of the [`File`] and its structs, or it
/// can update independently.
///
/// [`StatusLine`]: super::StatusLine
/// [`impl Display`]: std::fmt::Display
pub struct State<T: 'static, Dummy, U> {
    appender: Appender<T>,
    checker: Option<Box<dyn Fn() -> bool + Send + Sync>>,
    ghost: PhantomData<(Dummy, U)>,
}

impl<T: 'static, Dummy, U: Ui> State<T, Dummy, U> {
    pub fn fns(self) -> (ReaderFn<U>, Box<dyn Fn() -> bool>) {
        (
            match self.appender {
                Appender::NoArgsStr(mut f) => Box::new(move |builder, _| builder.push_str(f())),
                Appender::NoArgsText(mut f) => Box::new(move |builder, _| builder.push_text(f())),
                Appender::FromRelatedStr(mut f) => Box::new(move |builder, reader| {
                    if let Some(str) = reader.inspect_related(&mut f) {
                        builder.push_str(str)
                    }
                }),
                Appender::FromRelatedText(mut f) => Box::new(move |builder, reader| {
                    if let Some(text) = reader.inspect_related(&mut f) {
                        builder.push_text(text)
                    }
                }),
                Appender::FromFileAndRelatedStr(mut f) => Box::new(move |builder, reader| {
                    if let Some(str) = reader.inspect_file_and(|file, widget| f(file, widget)) {
                        builder.push_str(str)
                    }
                }),
                Appender::FromFileAndRelatedText(mut f) => Box::new(move |builder, reader| {
                    if let Some(text) = reader.inspect_file_and(|file, widget| f(file, widget)) {
                        builder.push_text(text)
                    }
                }),
                Appender::Str(str) => Box::new(move |builder, _| {
                    if !(str == " " && builder.last_was_empty()) {
                        builder.push_str(str.clone())
                    }
                }),
                Appender::Text(text) => Box::new(move |builder, _| builder.push_text(text.clone())),
            },
            Box::new(move || self.checker.as_ref().is_some_and(|check| check())),
        )
    }
}

impl<D: Display + Send + Sync, U: Ui> From<D> for State<(), String, U> {
    fn from(value: D) -> Self {
        Self {
            appender: Appender::Str::<()>(value.to_string()),
            checker: None,
            ghost: PhantomData,
        }
    }
}

impl<U: Ui> From<Text> for State<(), Text, U> {
    fn from(value: Text) -> Self {
        Self {
            appender: Appender::Text::<()>(value),
            checker: None,
            ghost: PhantomData,
        }
    }
}

impl<U: Ui> From<Tag> for State<(), Tag, U> {
    fn from(value: Tag) -> Self {
        Self {
            appender: Appender::Text::<()>(text!(value)),
            checker: None,
            ghost: PhantomData,
        }
    }
}

impl<D: Display + Send + Sync, U: Ui> From<RwData<D>> for State<(), DataArg<String>, U> {
    fn from(value: RwData<D>) -> Self {
        Self {
            appender: Appender::NoArgsStr::<()>({
                let value = RoData::from(&value);
                Box::new(move || value.read().to_string())
            }),
            checker: Some(Box::new(move || value.has_changed())),
            ghost: PhantomData,
        }
    }
}

impl<U: Ui> From<RwData<Text>> for State<(), DataArg<Text>, U> {
    fn from(value: RwData<Text>) -> Self {
        Self {
            appender: Appender::NoArgsText::<()>({
                let value = RoData::from(&value);
                Box::new(move || value.read().clone())
            }),
            checker: Some(Box::new(move || value.has_changed())),
            ghost: PhantomData,
        }
    }
}

impl<D: Display + Send + Sync, U: Ui> From<RoData<D>> for State<(), DataArg<String>, U> {
    fn from(value: RoData<D>) -> Self {
        Self {
            appender: Appender::NoArgsStr::<()>({
                let value = value.clone();
                Box::new(move || value.read().to_string())
            }),
            checker: Some(Box::new(move || value.has_changed())),
            ghost: PhantomData,
        }
    }
}

impl<U: Ui> From<RoData<Text>> for State<(), DataArg<Text>, U> {
    fn from(value: RoData<Text>) -> Self {
        Self {
            appender: Appender::NoArgsText::<()>({
                let value = value.clone();
                Box::new(move || value.read().clone())
            }),
            checker: Some(Box::new(move || value.has_changed())),
            ghost: PhantomData,
        }
    }
}

impl<U, I, O> From<DataMap<I, O>> for State<(), DataArg<String>, U>
where
    U: Ui,
    I: ?Sized + Send + Sync,
    O: Display + 'static,
{
    fn from(value: DataMap<I, O>) -> Self {
        let (mut reader, checker) = value.fns();
        State {
            appender: Appender::NoArgsStr(Box::new(move || reader().to_string())),
            checker: Some(checker),
            ghost: PhantomData,
        }
    }
}

impl<U, I> From<DataMap<I, Text>> for State<(), DataArg<Text>, U>
where
    U: Ui,
    I: ?Sized + Send + Sync,
{
    fn from(value: DataMap<I, Text>) -> Self {
        let (reader, checker) = value.fns();
        State {
            appender: Appender::NoArgsText(reader),
            checker: Some(checker),
            ghost: PhantomData,
        }
    }
}

impl<U, F, I, O> From<F> for State<(), IntoDataArg<String>, U>
where
    U: Ui,
    F: FnOnce() -> DataMap<I, O>,
    I: ?Sized + Send + Sync + 'static,
    O: Display + 'static,
{
    fn from(value: F) -> Self {
        let (mut reader, checker) = value().fns();
        State {
            appender: Appender::NoArgsStr(Box::new(move || reader().to_string())),
            checker: Some(checker),
            ghost: PhantomData,
        }
    }
}

impl<U, F, I> From<F> for State<(), IntoDataArg<Text>, U>
where
    U: Ui,
    F: FnOnce() -> DataMap<I, Text>,
    I: ?Sized + Send + Sync + 'static,
{
    fn from(value: F) -> Self {
        let (reader, checker) = value().fns();
        State {
            appender: Appender::NoArgsText(reader),
            checker: Some(checker),
            ghost: PhantomData,
        }
    }
}

impl<D, Reader, Checker, U> From<(Reader, Checker)> for State<(), NoArg<String>, U>
where
    D: Display,
    Reader: Fn() -> D + Send + Sync + 'static,
    Checker: Fn() -> bool + Send + Sync + 'static,
    U: Ui,
{
    fn from((reader, checker): (Reader, Checker)) -> Self {
        let reader = move || reader().to_string();
        State {
            appender: Appender::NoArgsStr::<()>(Box::new(reader)),
            checker: Some(Box::new(checker)),
            ghost: PhantomData,
        }
    }
}

impl<Reader, Checker, U> From<(Reader, Checker)> for State<(), NoArg<Text>, U>
where
    Reader: Fn() -> Text + Send + Sync + 'static,
    Checker: Fn() -> bool + Send + Sync + 'static,
    U: Ui,
{
    fn from((reader, checker): (Reader, Checker)) -> Self {
        State {
            appender: Appender::NoArgsText::<()>(Box::new(reader)),
            checker: Some(Box::new(checker)),
            ghost: PhantomData,
        }
    }
}

impl<D, W, ReadFn, U> From<ReadFn> for State<W, WidgetArg<String>, U>
where
    D: Display + Send + Sync,
    W: Widget<U> + Sized,
    ReadFn: Fn(&W) -> D + Send + Sync + 'static,
    U: Ui,
{
    fn from(reader: ReadFn) -> Self {
        let reader = move |arg: &W| reader(arg).to_string();
        State {
            appender: Appender::FromRelatedStr(Box::new(reader)),
            checker: None,
            ghost: PhantomData,
        }
    }
}

impl<W, ReadFn, U> From<ReadFn> for State<W, WidgetArg<Text>, U>
where
    W: Widget<U> + Sized,
    ReadFn: Fn(&W) -> Text + Send + Sync + 'static,
    U: Ui,
{
    fn from(reader: ReadFn) -> Self {
        let reader = move |arg: &W| reader(arg);
        State {
            appender: Appender::FromRelatedText(Box::new(reader)),
            checker: None,
            ghost: PhantomData,
        }
    }
}

impl<D, W, ReadFn, U> From<ReadFn> for State<W, FileAndWidgetArg<String>, U>
where
    D: Display + Send + Sync,
    W: Widget<U>,
    ReadFn: Fn(&File, &W) -> D + Send + Sync + 'static,
    U: Ui,
{
    fn from(reader: ReadFn) -> Self {
        let reader = move |file: &File, arg: &W| reader(file, arg).to_string();
        State {
            appender: Appender::FromFileAndRelatedStr(Box::new(reader)),
            checker: None,
            ghost: PhantomData,
        }
    }
}

impl<W, ReadFn, U> From<ReadFn> for State<W, FileAndWidgetArg<Text>, U>
where
    W: Widget<U>,
    ReadFn: Fn(&File, &W) -> Text + Send + Sync + 'static,
    U: Ui,
{
    fn from(reader: ReadFn) -> Self {
        State {
            appender: Appender::FromFileAndRelatedText(Box::new(reader)),
            checker: None,
            ghost: PhantomData,
        }
    }
}

impl<D, ReadFn, U> From<ReadFn> for State<Cursors, CursorsArg<String>, U>
where
    D: Display + Send + Sync,
    ReadFn: Fn(&Cursors) -> D + Send + Sync + 'static,
    U: Ui,
{
    fn from(reader: ReadFn) -> Self {
        let reader = move |arg: &Cursors| reader(arg).to_string();
        State {
            appender: Appender::FromRelatedStr(Box::new(reader)),
            checker: None,
            ghost: PhantomData,
        }
    }
}

impl<ReadFn, U> From<ReadFn> for State<Cursors, CursorsArg<Text>, U>
where
    ReadFn: Fn(&Cursors) -> Text + Send + Sync + 'static,
    U: Ui,
{
    fn from(reader: ReadFn) -> Self {
        let reader = move |arg: &Cursors| reader(arg);
        State {
            appender: Appender::FromRelatedText(Box::new(reader)),
            checker: None,
            ghost: PhantomData,
        }
    }
}

impl<D, ReadFn, U> From<ReadFn> for State<Cursors, FileAndCursorsArg<String>, U>
where
    D: Display + Send + Sync,
    ReadFn: Fn(&File, &Cursors) -> D + Send + Sync + 'static,
    U: Ui,
{
    fn from(reader: ReadFn) -> Self {
        let reader = move |file: &File, arg: &Cursors| reader(file, arg).to_string();
        State {
            appender: Appender::FromFileAndRelatedStr(Box::new(reader)),
            checker: None,
            ghost: PhantomData,
        }
    }
}

impl<ReadFn, U> From<ReadFn> for State<Cursors, FileAndCursorsArg<Text>, U>
where
    ReadFn: Fn(&File, &Cursors) -> Text + Send + Sync + 'static,
    U: Ui,
{
    fn from(reader: ReadFn) -> Self {
        let reader = move |file: &File, arg: &Cursors| reader(file, arg);
        State {
            appender: Appender::FromFileAndRelatedText(Box::new(reader)),
            checker: None,
            ghost: PhantomData,
        }
    }
}

// Dummy structs to prevent implementation conflicts.
#[doc(hidden)]
pub struct DataArg<T>(PhantomData<T>);
#[doc(hidden)]
pub struct IntoDataArg<T>(PhantomData<T>);
#[doc(hidden)]
pub struct NoArg<T>(PhantomData<T>);
#[doc(hidden)]
pub struct WidgetArg<T>(PhantomData<T>);
#[doc(hidden)]
pub struct FileAndWidgetArg<T>(PhantomData<T>);
#[doc(hidden)]
pub struct CursorsArg<T>(PhantomData<T>);
#[doc(hidden)]
pub struct FileAndCursorsArg<T>(PhantomData<T>);

// The various types of function aliases
type RelatedStrFn<T> = Box<dyn FnMut(&T) -> String + Send + Sync + 'static>;
type RelatedTextFn<T> = Box<dyn FnMut(&T) -> Text + Send + Sync + 'static>;
type FileAndRelatedStrFn<T> = Box<dyn FnMut(&File, &T) -> String + Send + Sync + 'static>;
type FileAndRelatedTextFn<T> = Box<dyn FnMut(&File, &T) -> Text + Send + Sync + 'static>;

type ReaderFn<U> = Box<dyn FnMut(&mut Builder, &FileReader<U>) + Send + Sync>;