superseedr 1.0.5

A BitTorrent Client in your Terminal.
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
420
421
422
423
424
425
426
// SPDX-FileCopyrightText: 2025 The superseedr Contributors
// SPDX-License-Identifier: GPL-3.0-or-later

use crate::app::torrent_is_effectively_incomplete;
use crate::app::AppState;
use crate::config::{PeerSortColumn, TorrentSortColumn};
use ratatui::prelude::Constraint;

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ColumnId {
    Status,
    Name,
    DownSpeed,
    UpSpeed,
}

pub struct ColumnDefinition {
    pub id: ColumnId,
    pub header: &'static str,
    pub min_width: u16,
    pub priority: u8,
    pub default_constraint: Constraint,
    pub sort_enum: Option<TorrentSortColumn>,
}

pub fn get_torrent_columns() -> Vec<ColumnDefinition> {
    vec![
        ColumnDefinition {
            id: ColumnId::Status,
            header: "Done",
            min_width: 7,
            priority: 2,
            default_constraint: Constraint::Length(7),
            sort_enum: Some(TorrentSortColumn::Progress),
        },
        ColumnDefinition {
            id: ColumnId::Name,
            header: "Name",
            min_width: 15,
            priority: 0,
            default_constraint: Constraint::Fill(1),
            sort_enum: Some(TorrentSortColumn::Name),
        },
        ColumnDefinition {
            id: ColumnId::UpSpeed,
            header: "UL",
            min_width: 10,
            priority: 1,
            default_constraint: Constraint::Length(10),
            sort_enum: Some(TorrentSortColumn::Up),
        },
        ColumnDefinition {
            id: ColumnId::DownSpeed,
            header: "DL",
            min_width: 10,
            priority: 1,
            default_constraint: Constraint::Length(10),
            sort_enum: Some(TorrentSortColumn::Down),
        },
    ]
}

pub fn torrent_has_download_activity(app_state: &AppState) -> bool {
    app_state
        .torrents
        .values()
        .any(|t| t.smoothed_download_speed_bps > 0)
}

pub fn torrent_has_upload_activity(app_state: &AppState) -> bool {
    app_state
        .torrents
        .values()
        .any(|t| t.smoothed_upload_speed_bps > 0)
}

pub fn has_incomplete_torrents(app_state: &AppState) -> bool {
    app_state
        .torrents
        .values()
        .any(|t| torrent_is_effectively_incomplete(&t.latest_state))
}

pub fn active_torrent_column_indices(app_state: &AppState) -> Vec<usize> {
    let has_dl_activity = torrent_has_download_activity(app_state);
    let has_ul_activity = torrent_has_upload_activity(app_state);
    let has_incomplete = has_incomplete_torrents(app_state);

    get_torrent_columns()
        .iter()
        .enumerate()
        .filter_map(|(idx, col)| {
            let is_active = match col.id {
                ColumnId::DownSpeed => has_dl_activity,
                ColumnId::UpSpeed => has_ul_activity,
                ColumnId::Status => has_incomplete,
                ColumnId::Name => true,
            };
            is_active.then_some(idx)
        })
        .collect()
}

pub fn compute_visible_torrent_columns(
    app_state: &AppState,
    available_width: u16,
) -> (Vec<Constraint>, Vec<usize>) {
    let all_cols = get_torrent_columns();
    let active_indices = active_torrent_column_indices(app_state);

    let smart_cols: Vec<SmartCol> = active_indices
        .iter()
        .map(|&idx| {
            let c = &all_cols[idx];
            SmartCol {
                min_width: c.min_width,
                priority: c.priority,
                constraint: c.default_constraint,
            }
        })
        .collect();

    let (constraints, visible_active_indices) =
        compute_smart_table_layout(&smart_cols, available_width, 1);
    let visible_real_indices: Vec<usize> = visible_active_indices
        .into_iter()
        .filter_map(|idx| active_indices.get(idx).copied())
        .collect();

    (constraints, visible_real_indices)
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PeerColumnId {
    Flags,
    Address,
    Client,
    Action,
    Progress,
    DownSpeed,
    UpSpeed,
}

pub struct PeerColumnDefinition {
    pub id: PeerColumnId,
    pub header: &'static str,
    pub min_width: u16,
    pub priority: u8,
    pub default_constraint: Constraint,
    pub sort_enum: Option<PeerSortColumn>,
}

pub fn get_peer_columns() -> Vec<PeerColumnDefinition> {
    vec![
        PeerColumnDefinition {
            id: PeerColumnId::Flags,
            header: "Flag",
            min_width: 4,
            priority: 1,
            default_constraint: Constraint::Length(4),
            sort_enum: Some(PeerSortColumn::Flags),
        },
        PeerColumnDefinition {
            id: PeerColumnId::Progress,
            header: "Status",
            min_width: 6,
            priority: 2,
            default_constraint: Constraint::Length(6),
            sort_enum: Some(PeerSortColumn::Completed),
        },
        PeerColumnDefinition {
            id: PeerColumnId::Address,
            header: "Address",
            min_width: 25,
            priority: 0,
            default_constraint: Constraint::Fill(2),
            sort_enum: Some(PeerSortColumn::Address),
        },
        PeerColumnDefinition {
            id: PeerColumnId::UpSpeed,
            header: "Upload",
            min_width: 10,
            priority: 1,
            default_constraint: Constraint::Fill(1),
            sort_enum: Some(PeerSortColumn::UL),
        },
        PeerColumnDefinition {
            id: PeerColumnId::DownSpeed,
            header: "Download",
            min_width: 10,
            priority: 1,
            default_constraint: Constraint::Fill(1),
            sort_enum: Some(PeerSortColumn::DL),
        },
        PeerColumnDefinition {
            id: PeerColumnId::Client,
            header: "Client",
            min_width: 12,
            priority: 3,
            default_constraint: Constraint::Fill(1),
            sort_enum: Some(PeerSortColumn::Client),
        },
        PeerColumnDefinition {
            id: PeerColumnId::Action,
            header: "Action",
            min_width: 12,
            priority: 5,
            default_constraint: Constraint::Fill(1),
            sort_enum: Some(PeerSortColumn::Action),
        },
    ]
}

pub fn peer_has_download_activity(app_state: &AppState) -> bool {
    app_state
        .torrent_list_order
        .get(app_state.ui.selected_torrent_index)
        .and_then(|info_hash| app_state.torrents.get(info_hash))
        .is_some_and(|torrent| {
            torrent
                .latest_state
                .peers
                .iter()
                .any(|peer| peer.download_speed_bps > 0)
        })
}

pub fn peer_has_upload_activity(app_state: &AppState) -> bool {
    app_state
        .torrent_list_order
        .get(app_state.ui.selected_torrent_index)
        .and_then(|info_hash| app_state.torrents.get(info_hash))
        .is_some_and(|torrent| {
            torrent
                .latest_state
                .peers
                .iter()
                .any(|peer| peer.upload_speed_bps > 0)
        })
}

pub fn active_peer_column_indices(app_state: &AppState) -> Vec<usize> {
    let has_dl_activity = peer_has_download_activity(app_state);
    let has_ul_activity = peer_has_upload_activity(app_state);

    get_peer_columns()
        .iter()
        .enumerate()
        .filter_map(|(idx, col)| {
            let is_active = match col.id {
                PeerColumnId::DownSpeed => has_dl_activity,
                PeerColumnId::UpSpeed => has_ul_activity,
                PeerColumnId::Flags
                | PeerColumnId::Address
                | PeerColumnId::Client
                | PeerColumnId::Action
                | PeerColumnId::Progress => true,
            };
            is_active.then_some(idx)
        })
        .collect()
}

pub fn compute_visible_peer_columns(
    app_state: &AppState,
    available_width: u16,
) -> (Vec<Constraint>, Vec<usize>) {
    let all_cols = get_peer_columns();
    let active_indices = active_peer_column_indices(app_state);

    let smart_peer_cols: Vec<SmartCol> = active_indices
        .iter()
        .map(|&idx| {
            let c = &all_cols[idx];
            SmartCol {
                min_width: c.min_width,
                priority: c.priority,
                constraint: c.default_constraint,
            }
        })
        .collect();

    let (constraints, visible_active_indices) =
        compute_smart_table_layout(&smart_peer_cols, available_width, 1);
    let visible_real_indices: Vec<usize> = visible_active_indices
        .into_iter()
        .filter_map(|idx| active_indices.get(idx).copied())
        .collect();

    (constraints, visible_real_indices)
}

#[derive(Clone, Debug)]
pub struct SmartCol {
    pub min_width: u16,
    pub priority: u8,
    pub constraint: Constraint,
}

pub fn compute_smart_table_layout(
    columns: &[SmartCol],
    available_width: u16,
    horizontal_padding: u16,
) -> (Vec<Constraint>, Vec<usize>) {
    let mut indexed_cols: Vec<(usize, &SmartCol)> = columns.iter().enumerate().collect();

    indexed_cols.sort_by(|a, b| a.1.priority.cmp(&b.1.priority).then(a.0.cmp(&b.0)));

    let mut active_indices = Vec::new();
    let mut current_used_width = 0;

    let expansion_reserve = if available_width < 80 {
        15
    } else if available_width < 140 {
        25
    } else {
        0
    };

    for (idx, col) in indexed_cols {
        let spacing_cost = if active_indices.is_empty() {
            0
        } else {
            horizontal_padding
        };

        if col.priority == 0 {
            active_indices.push(idx);
            current_used_width += col.min_width + spacing_cost;
        } else {
            let projected_width = current_used_width + col.min_width + spacing_cost;
            let effective_budget = available_width.saturating_sub(expansion_reserve);

            if projected_width <= effective_budget {
                active_indices.push(idx);
                current_used_width = projected_width;
            }
        }
    }

    active_indices.sort();

    let final_constraints = active_indices
        .iter()
        .map(|&i| columns[i].constraint)
        .collect();

    (final_constraints, active_indices)
}

#[cfg(test)]
mod tests {
    use super::{compute_visible_peer_columns, get_peer_columns, PeerColumnId};
    use crate::app::{AppState, PeerInfo, TorrentDisplayState, TorrentMetrics};
    use ratatui::layout::Constraint;

    fn peer_test_app_state() -> AppState {
        let mut app_state = AppState::default();
        let torrent = TorrentDisplayState {
            latest_state: TorrentMetrics {
                peers: vec![
                    PeerInfo {
                        address: "127.0.0.1:6881".to_string(),
                        ..Default::default()
                    },
                    PeerInfo {
                        address: "127.0.0.1:6882".to_string(),
                        ..Default::default()
                    },
                ],
                ..Default::default()
            },
            ..Default::default()
        };
        let info_hash = b"hash_a".to_vec();
        app_state.torrents.insert(info_hash.clone(), torrent);
        app_state.torrent_list_order = vec![info_hash];
        app_state
    }

    #[test]
    fn peer_address_column_reserves_more_width_for_ipv6_addresses() {
        let columns = get_peer_columns();
        let address = columns
            .iter()
            .find(|column| column.id == PeerColumnId::Address)
            .expect("address column");

        assert_eq!(address.min_width, 25);
        assert_eq!(address.default_constraint, Constraint::Fill(2));
    }

    #[test]
    fn peer_columns_drop_low_priority_fields_before_address_on_medium_widths() {
        let mut app_state = peer_test_app_state();
        if let Some(torrent) = app_state.torrents.get_mut(b"hash_a".as_slice()) {
            torrent.latest_state.peers[0].download_speed_bps = 1;
            torrent.latest_state.peers[0].upload_speed_bps = 1;
        }
        let (_constraints, visible) = compute_visible_peer_columns(&app_state, 90);

        assert!(visible.contains(&2), "address column should stay visible");
        assert!(!visible.contains(&6), "action column should drop first");
    }

    #[test]
    fn peer_columns_hide_dl_and_ul_when_selected_torrent_has_no_activity() {
        let app_state = peer_test_app_state();
        let (_constraints, visible) = compute_visible_peer_columns(&app_state, 120);

        assert!(!visible.contains(&3), "upload column should be hidden");
        assert!(!visible.contains(&4), "download column should be hidden");
    }

    #[test]
    fn peer_columns_show_only_active_direction() {
        let mut app_state = peer_test_app_state();
        if let Some(torrent) = app_state.torrents.get_mut(b"hash_a".as_slice()) {
            torrent.latest_state.peers[0].download_speed_bps = 32;
        }
        let (_constraints, visible) = compute_visible_peer_columns(&app_state, 120);

        assert!(!visible.contains(&3), "upload column should stay hidden");
        assert!(visible.contains(&4), "download column should be visible");
    }
}