inquire/
utils.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
// sorry for this file

use std::fmt::Debug;

pub struct Page<'a, T> {
    /// Whether this is the first page.
    pub first: bool,

    /// Whether this is the last page.
    pub last: bool,

    /// Content of the page.
    pub content: &'a [T],

    /// If a cursor exists on the original list, this is the index of the new cursor relative to the output list of choices, the page.
    pub cursor: Option<usize>,

    /// Total amount of elements in the original list of choices.
    pub total: usize,
}

pub fn paginate<T>(page_size: usize, choices: &[T], sel: Option<usize>) -> Page<T> {
    // if there is no selection, we default to the first page.
    // in practice, the same as selecting the 0 index.

    let (start, end, cursor) = if choices.len() <= page_size {
        (0, choices.len(), sel)
    } else if let Some(index) = sel {
        if index < page_size / 2 {
            // if we are in the first half page
            let start = 0;
            let end = page_size;
            let cursor = Some(index);

            (start, end, cursor)
        } else if choices.len() - index - 1 < page_size / 2 {
            // if we are in the last half page
            let start = choices.len() - page_size;
            let end = choices.len();
            let cursor = Some(index - start);

            (start, end, cursor)
        } else {
            // somewhere in the middle
            let above = page_size / 2;
            let below = page_size - above;

            let start = index - above;
            let end = index + below;
            let cursor = Some(page_size / 2);

            (start, end, cursor)
        }
    } else {
        // if we are in the first half page
        let start = 0;
        let end = page_size;

        (start, end, sel)
    };

    Page {
        first: start == 0,
        last: end == choices.len(),
        content: &choices[start..end],
        cursor,
        total: choices.len(),
    }
}

pub fn int_log10<T>(mut i: T) -> usize
where
    T: std::ops::DivAssign + std::cmp::PartialOrd + From<u8> + Copy,
{
    let mut len = 0;
    let zero = T::from(0);
    let ten = T::from(10);

    while i > zero {
        i /= ten;
        len += 1;
    }

    len
}

#[cfg(test)]
mod test {
    #![allow(clippy::bool_assert_comparison)]

    use crate::{
        list_option::ListOption,
        utils::{int_log10, paginate},
    };

    #[test]
    fn int_log10_works() {
        for i in 1..10 {
            assert_eq!(1, int_log10(i), "Int log 10 failed for value {i}");
        }
        for i in 10..100 {
            assert_eq!(2, int_log10(i), "Int log 10 failed for value {i}");
        }
        for i in 100..1000 {
            assert_eq!(3, int_log10(i), "Int log 10 failed for value {i}");
        }
        for i in 1000..10000 {
            assert_eq!(4, int_log10(i), "Int log 10 failed for value {i}");
        }
        for i in 10000..100000 {
            assert_eq!(5, int_log10(i), "Int log 10 failed for value {i}");
        }
    }

    #[test]
    fn paginate_too_few() {
        let choices = ListOption::from_list(vec!["1", "2", "3"]);

        let page_size = 4usize;
        let sel = Some(3usize);

        let page = paginate(page_size, &choices, sel);

        assert_eq!(choices[..], page.content[..]);
        assert_eq!(Some(3usize), page.cursor);
        assert_eq!(true, page.first);
        assert_eq!(true, page.last);
        assert_eq!(3, page.total);
    }

    #[test]
    fn paginate_too_few_no_cursor() {
        let choices = ListOption::from_list(vec!["1", "2", "3"]);

        let page_size = 4usize;
        let sel = None;

        let page = paginate(page_size, &choices, sel);

        assert_eq!(choices[..], page.content[..]);
        assert_eq!(None, page.cursor);
        assert_eq!(true, page.first);
        assert_eq!(true, page.last);
        assert_eq!(3, page.total);
    }

    #[test]
    fn paginate_first_half() {
        let choices = ListOption::from_list(vec!["1", "2", "3", "4", "5", "6"]);

        let page_size = 4usize;
        let sel = Some(2usize);

        let page = paginate(page_size, &choices, sel);

        assert_eq!(choices[0..4], page.content[..]);
        assert_eq!(Some(2), page.cursor);
        assert_eq!(true, page.first);
        assert_eq!(false, page.last);
        assert_eq!(6, page.total);
    }

    #[test]
    fn paginate_first_half_no_cursor() {
        let choices = ListOption::from_list(vec!["1", "2", "3", "4", "5", "6"]);

        let page_size = 4usize;
        let sel = None;

        let page = paginate(page_size, &choices, sel);

        assert_eq!(choices[0..4], page.content[..]);
        assert_eq!(None, page.cursor);
        assert_eq!(true, page.first);
        assert_eq!(false, page.last);
        assert_eq!(6, page.total);
    }

    #[test]
    fn paginate_middle() {
        let choices = ListOption::from_list(vec!["1", "2", "3", "4", "5", "6"]);

        let page_size = 2usize;
        let sel = Some(3usize);

        let page = paginate(page_size, &choices, sel);

        assert_eq!(choices[2..4], page.content[..]);
        assert_eq!(Some(1), page.cursor);
        assert_eq!(false, page.first);
        assert_eq!(false, page.last);
        assert_eq!(6, page.total);
    }

    #[test]
    fn paginate_middle_no_cursor() {
        let choices = ListOption::from_list(vec!["1", "2", "3", "4", "5", "6"]);

        let page_size = 2usize;
        let sel = None;

        let page = paginate(page_size, &choices, sel);

        assert_eq!(choices[0..2], page.content[..]);
        assert_eq!(None, page.cursor);
        assert_eq!(true, page.first);
        assert_eq!(false, page.last);
        assert_eq!(6, page.total);
    }

    #[test]
    fn paginate_last_half() {
        let choices = ListOption::from_list(vec!["1", "2", "3", "4", "5", "6"]);

        let page_size = 3usize;
        let sel = Some(5usize);

        let page = paginate(page_size, &choices, sel);

        assert_eq!(choices[3..6], page.content[..]);
        assert_eq!(Some(2), page.cursor);
        assert_eq!(false, page.first);
        assert_eq!(true, page.last);
        assert_eq!(6, page.total);
    }

    #[test]
    fn paginate_last_half_no_cursor() {
        let choices = ListOption::from_list(vec!["1", "2", "3", "4", "5", "6"]);

        let page_size = 3usize;
        let sel = None;

        let page = paginate(page_size, &choices, sel);

        assert_eq!(choices[0..3], page.content[..]);
        assert_eq!(None, page.cursor);
        assert_eq!(true, page.first);
        assert_eq!(false, page.last);
        assert_eq!(6, page.total);
    }
}

impl<'a, T> Debug for Page<'a, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Page")
            .field("first", &self.first)
            .field("last", &self.last)
            .field("content", &format!("({} elements)", &self.content.len()))
            .field("cursor", &self.cursor)
            .field("total", &self.total)
            .finish()
    }
}