aboutsummaryrefslogtreecommitdiffstats
path: root/teleterm/src/session_list.rs
blob: 7fd4d0a5292ddce27d8d1aff007ea1d2c0dde66e (plain) (blame)
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
pub struct SessionList {
    sessions: Vec<crate::protocol::Session>,
    offset: usize,
    size: crate::term::Size,
}

impl SessionList {
    pub fn new(
        sessions: Vec<crate::protocol::Session>,
        size: crate::term::Size,
    ) -> Self {
        let mut by_name = std::collections::HashMap::new();
        for session in sessions {
            if !by_name.contains_key(&session.username) {
                by_name.insert(session.username.clone(), vec![]);
            }
            by_name.get_mut(&session.username).unwrap().push(session);
        }
        let mut names: Vec<_> = by_name.keys().cloned().collect();
        names.sort_by(|a: &String, b: &String| {
            let a_idle =
                by_name[a].iter().min_by_key(|session| session.idle_time);
            let b_idle =
                by_name[b].iter().min_by_key(|session| session.idle_time);
            // these unwraps are safe because we know that none of the vecs in
            // the map can be empty
            a_idle.unwrap().idle_time.cmp(&b_idle.unwrap().idle_time)
        });
        for name in &names {
            if let Some(sessions) = by_name.get_mut(name) {
                sessions.sort_by_key(|s| s.idle_time);
            }
        }

        let mut sorted = vec![];
        for name in names {
            let sessions = by_name.remove(&name).unwrap();
            for session in sessions {
                sorted.push(session);
            }
        }

        Self {
            sessions: sorted,
            offset: 0,
            size,
        }
    }

    pub fn visible_sessions(&self) -> &[crate::protocol::Session] {
        let start = self.offset;
        let end = self.offset + self.limit();
        let end = end.min(self.sessions.len());
        &self.sessions[start..end]
    }

    pub fn visible_sessions_with_chars(
        &self,
    ) -> impl Iterator<Item = (char, &crate::protocol::Session)> {
        self.visible_sessions()
            .iter()
            .enumerate()
            .map(move |(i, s)| (self.idx_to_char(i).unwrap(), s))
    }

    pub fn size(&self) -> crate::term::Size {
        self.size
    }

    pub fn resize(&mut self, size: crate::term::Size) {
        self.size = size;
    }

    pub fn id_for(&self, c: char) -> Option<&str> {
        self.char_to_idx(c).and_then(|i| {
            self.sessions.get(i + self.offset).map(|s| s.id.as_ref())
        })
    }

    pub fn next_page(&mut self) {
        let inc = self.limit();
        if self.offset + inc < self.sessions.len() {
            self.offset += inc;
        }
    }

    pub fn prev_page(&mut self) {
        let dec = self.limit();
        if self.offset >= dec {
            self.offset -= dec;
        }
    }

    pub fn current_page(&self) -> usize {
        self.offset / self.limit() + 1
    }

    pub fn total_pages(&self) -> usize {
        if self.sessions.is_empty() {
            1
        } else {
            (self.sessions.len() - 1) / self.limit() + 1
        }
    }

    fn idx_to_char(&self, mut i: usize) -> Option<char> {
        if i >= self.limit() {
            return None;
        }

        // 'q' shouldn't be a list option, since it is bound to quit
        if i >= 16 {
            i += 1;
        }

        #[allow(clippy::cast_possible_truncation)]
        Some(std::char::from_u32(('a' as u32) + (i as u32)).unwrap())
    }

    fn char_to_idx(&self, c: char) -> Option<usize> {
        if c == 'q' {
            return None;
        }

        let i = ((c as i32) - ('a' as i32)) as isize;
        if i < 0 {
            return None;
        }
        #[allow(clippy::cast_sign_loss)]
        let mut i = i as usize;

        // 'q' shouldn't be a list option, since it is bound to quit
        if i > 16 {
            i -= 1;
        }

        if i >= self.limit() {
            return None;
        }

        Some(i)
    }

    fn limit(&self) -> usize {
        let limit = self.size.rows as usize - 6;

        // enough for a-z except q - if we want to allow more than this, we'll
        // need to come up with a better way of choosing streams
        if limit > 25 {
            25
        } else {
            limit
        }
    }
}

#[cfg(test)]
#[allow(clippy::cognitive_complexity)]
#[allow(clippy::redundant_clone)]
#[allow(clippy::shadow_unrelated)]
mod test {
    use super::*;

    fn session(username: &str, idle: u32) -> crate::protocol::Session {
        crate::protocol::Session {
            id: format!("{}", uuid::Uuid::new_v4()),
            username: username.to_string(),
            term_type: "screen".to_string(),
            size: crate::term::Size { rows: 24, cols: 80 },
            idle_time: idle,
            title: "title".to_string(),
            watchers: 0,
        }
    }

    #[test]
    fn test_session_list_sorting() {
        let size = crate::term::Size { rows: 24, cols: 80 };

        let session1 = session("doy", 35);
        let session2 = session("doy", 3);
        let mut session3 = session("sartak", 12);
        let session4 = session("sartak", 100);
        let mut session5 = session("toft", 5);
        let mut sessions = vec![
            session1.clone(),
            session2.clone(),
            session3.clone(),
            session4.clone(),
            session5.clone(),
        ];

        assert_eq!(
            SessionList::new(sessions.clone(), size.clone()).sessions,
            vec![
                session2.clone(),
                session1.clone(),
                session5.clone(),
                session3.clone(),
                session4.clone(),
            ]
        );

        session3.idle_time = 2;
        sessions[2].idle_time = 2;
        assert_eq!(
            SessionList::new(sessions.clone(), size.clone()).sessions,
            vec![
                session3.clone(),
                session4.clone(),
                session2.clone(),
                session1.clone(),
                session5.clone(),
            ]
        );

        session5.idle_time = 1;
        sessions[4].idle_time = 1;
        assert_eq!(
            SessionList::new(sessions.clone(), size.clone()).sessions,
            vec![
                session5.clone(),
                session3.clone(),
                session4.clone(),
                session2.clone(),
                session1.clone(),
            ]
        );
    }

    #[test]
    fn test_session_list_pagination() {
        let size = crate::term::Size { rows: 11, cols: 80 };
        let sessions = vec![
            session("doy", 0),
            session("doy", 1),
            session("doy", 2),
            session("doy", 3),
            session("doy", 4),
            session("doy", 5),
            session("doy", 6),
            session("doy", 7),
            session("doy", 8),
            session("doy", 9),
            session("doy", 10),
        ];
        let mut list = SessionList::new(sessions.clone(), size);
        assert_eq!(list.limit(), 5);
        assert_eq!(list.total_pages(), 3);
        assert_eq!(list.current_page(), 1);

        list.next_page();
        assert_eq!(list.limit(), 5);
        assert_eq!(list.total_pages(), 3);
        assert_eq!(list.current_page(), 2);

        list.next_page();
        assert_eq!(list.limit(), 5);
        assert_eq!(list.total_pages(), 3);
        assert_eq!(list.current_page(), 3);

        list.next_page();
        assert_eq!(list.limit(), 5);
        assert_eq!(list.total_pages(), 3);
        assert_eq!(list.current_page(), 3);

        list.prev_page();
        assert_eq!(list.limit(), 5);
        assert_eq!(list.total_pages(), 3);
        assert_eq!(list.current_page(), 2);

        list.prev_page();
        assert_eq!(list.limit(), 5);
        assert_eq!(list.total_pages(), 3);
        assert_eq!(list.current_page(), 1);

        list.prev_page();
        assert_eq!(list.limit(), 5);
        assert_eq!(list.total_pages(), 3);
        assert_eq!(list.current_page(), 1);

        let id = list.id_for('a').unwrap();
        assert_eq!(id, sessions[0].id);
        let id = list.id_for('e').unwrap();
        assert_eq!(id, sessions[4].id);
        let id = list.id_for('f');
        assert!(id.is_none());

        list.next_page();
        let id = list.id_for('a').unwrap();
        assert_eq!(id, sessions[5].id);

        list.next_page();
        let id = list.id_for('a').unwrap();
        assert_eq!(id, sessions[10].id);
        let id = list.id_for('b');
        assert!(id.is_none());

        let size = crate::term::Size { rows: 24, cols: 80 };
        let sessions = vec![
            session("doy", 0),
            session("doy", 1),
            session("doy", 2),
            session("doy", 3),
            session("doy", 4),
            session("doy", 5),
            session("doy", 6),
            session("doy", 7),
            session("doy", 8),
            session("doy", 9),
            session("doy", 10),
            session("doy", 11),
            session("doy", 12),
            session("doy", 13),
            session("doy", 14),
            session("doy", 15),
            session("doy", 16),
            session("doy", 17),
            session("doy", 18),
            session("doy", 19),
            session("doy", 20),
            session("doy", 21),
        ];
        let list = SessionList::new(sessions.clone(), size);
        assert_eq!(list.limit(), 18);
        assert_eq!(list.total_pages(), 2);
        assert_eq!(list.current_page(), 1);

        let id = list.id_for('a').unwrap();
        assert_eq!(id, sessions[0].id);
        let id = list.id_for('p').unwrap();
        assert_eq!(id, sessions[15].id);
        let id = list.id_for('q');
        assert!(id.is_none());
        let id = list.id_for('r').unwrap();
        assert_eq!(id, sessions[16].id);
        let id = list.id_for('s').unwrap();
        assert_eq!(id, sessions[17].id);
        let id = list.id_for('t');
        assert!(id.is_none());
    }
}