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
use std::marker::PhantomData;

use tui::{
    layout::Rect,
    text::{Span, Spans},
    widgets::Widget,
};

use crate::Wrap;

#[derive(Debug, Clone, PartialEq)]
pub struct Footer<'a> {
    left_offset: u16,
    right_offset: u16,
    left: Vec<FooterItem<'a>>,
    right: Vec<FooterItem<'a>>,
    _life: PhantomData<&'a ()>,
}

impl<'a> Default for Footer<'a> {
    fn default() -> Self {
        Self {
            left_offset: 2,
            right_offset: 2,
            left: Default::default(),
            right: Default::default(),
            _life: Default::default(),
        }
    }
}

impl<'a> Footer<'a> {
    pub fn show(&mut self) {
        self.items_mut().for_each(FooterItem::show);
    }

    pub fn hide(&mut self) {
        self.items_mut().for_each(FooterItem::hide);
    }

    pub fn items(&self) -> impl Iterator<Item = &FooterItem<'_>> {
        self.left.iter().chain(self.right.iter())
    }

    pub fn items_mut(&mut self) -> impl Iterator<Item = &mut FooterItem<'a>> {
        self.left.iter_mut().chain(self.right.iter_mut())
    }

    pub fn left_offset(&mut self, offset: u16) -> &mut Self {
        self.left_offset = offset;
        self
    }

    pub fn right_offset(&mut self, offset: u16) -> &mut Self {
        self.right_offset = offset;
        self
    }

    pub fn push_left(&mut self, item: FooterItem<'a>) -> &mut Self {
        self.left.push(item);
        self
    }

    pub fn push_right(&mut self, item: FooterItem<'a>) -> &mut Self {
        self.right.push(item);
        self
    }

    pub fn append_left(&mut self, item: &mut Vec<FooterItem<'a>>) -> &mut Self {
        self.left.append(item);
        self
    }

    pub fn append_right(&mut self, item: &mut Vec<FooterItem<'a>>) -> &mut Self {
        self.right.append(item);
        self
    }

    pub fn pop_left(&mut self) -> Option<FooterItem<'a>> {
        self.left.pop()
    }

    pub fn pop_right(&mut self) -> Option<FooterItem<'a>> {
        self.right.pop()
    }
}

#[derive(Debug, Clone)]
pub struct FooterWidget<'a> {
    state: &'a Footer<'a>,
}

impl<'a> FooterWidget<'a> {
    pub fn render_one(&mut self, item: Spans, area: Rect, buf: &mut tui::buffer::Buffer) {
        buf.set_spans(area.x, area.y, &item, item.width() as u16);
    }

    pub fn new(state: &'a Footer) -> Self {
        Self { state }
    }
}

impl<'a> Widget for FooterWidget<'a> {
    fn render(self, area: Rect, buf: &mut tui::buffer::Buffer) {
        let y = area.y + area.height - 1;
        let (mut left, mut right) = (self.state.left.iter(), self.state.right.iter());
        let (mut left_x, mut right_x) = (
            area.x.saturating_add(self.state.left_offset),
            area.x
                .saturating_add(area.width + 1)
                .saturating_sub(self.state.right_offset),
        );
        loop {
            let mut changed = false;
            if let Some(spans) = left.next() {
                if spans.show {
                    let spans = spans.to_spans();
                    let width = spans.width() as u16;
                    if right_x.saturating_sub(left_x) <= width {
                        break;
                    }
                    buf.set_spans(left_x, y, &spans, width);
                    left_x += width + 1;

                    changed = true;
                }
            }

            if let Some(spans) = right.next() {
                if spans.show {
                    let spans = spans.to_spans();
                    let width = spans.width() as u16;
                    if right_x.saturating_sub(left_x) <= width {
                        break;
                    }
                    right_x = right_x.saturating_sub(width + 1);
                    buf.set_spans(right_x, y, &spans, width);
                    changed = true;
                }
            }
            if !changed {
                break;
            }
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct FooterItem<'a> {
    inner: FooterItemInner<'a>,
    show: bool,
}

impl<'a> FooterItem<'a> {
    pub fn to_spans(&self) -> Spans<'a> {
        match self.inner {
            FooterItemInner::Raw(ref raw) => Spans::from(raw.to_string()),
            FooterItemInner::Span(ref span) => span.to_owned().into(),
            FooterItemInner::Spans(ref spans) => spans.to_owned(),
        }
    }

    pub fn raw(content: String) -> Self {
        Self {
            inner: FooterItemInner::Raw(content),
            show: true,
        }
    }

    pub fn span(content: Span<'a>) -> Self {
        Self {
            inner: FooterItemInner::Span(content),
            show: true,
        }
    }

    pub fn spans(content: Spans<'a>) -> Self {
        Self {
            inner: FooterItemInner::Spans(content),
            show: true,
        }
    }

    pub fn set_show(&mut self, show: bool) {
        self.show = show
    }

    pub fn show(&mut self) {
        self.set_show(true)
    }

    pub fn hide(&mut self) {
        self.set_show(false)
    }
}

impl<'a> From<Span<'a>> for FooterItem<'a> {
    fn from(val: Span<'a>) -> Self {
        Self::span(val)
    }
}

impl<'a> From<Spans<'a>> for FooterItem<'a> {
    fn from(val: Spans<'a>) -> Self {
        Self::spans(val)
    }
}

impl<'a> From<String> for FooterItem<'a> {
    fn from(val: String) -> Self {
        Self::raw(val)
    }
}

#[derive(Debug, Clone, PartialEq)]
enum FooterItemInner<'a> {
    Raw(String),
    Span(Span<'a>),
    Spans(Spans<'a>),
}

impl<'a> From<FooterItemInner<'a>> for Spans<'a> {
    fn from(val: FooterItemInner<'a>) -> Self {
        match val {
            FooterItemInner::Raw(raw) => raw.into(),
            FooterItemInner::Span(span) => span.into(),
            FooterItemInner::Spans(spans) => spans,
        }
    }
}

impl<'a> From<FooterItem<'a>> for Spans<'a> {
    fn from(val: FooterItem<'a>) -> Self {
        val.inner.into()
    }
}

impl<'a> Wrap for FooterItem<'a> {
    fn wrap_by(mut self, char: char) -> Self {
        match self.inner {
            FooterItemInner::Raw(ref mut raw) => {
                raw.wrap_by(char);
            }
            FooterItemInner::Span(ref mut span) => {
                span.wrap_by(char);
            }
            FooterItemInner::Spans(ref mut spans) => {
                spans.wrap_by(char);
            }
        };
        self
    }
}