1mod cli;
2mod mirrors;
3
4pub use cli::Cli;
5use comfy_table::{modifiers::UTF8_ROUND_CORNERS, presets::UTF8_FULL, Cell, CellAlignment, Table};
6use mirrors::{count_countries, get_cache_file, get_mirror_status};
7
8pub async fn run(options: &Cli) {
9 if options.list_countries {
10 list_countries(&options.url).await;
11 }
12}
13
14pub async fn list_countries(url: &str) {
15 let cache_file = get_cache_file(None);
16 let status = get_mirror_status(10, 10, url, &cache_file).await.unwrap();
17 let counts = count_countries(&status.urls).await;
18 let mut sorted = vec![];
19 for (country, count) in counts {
20 sorted.push((country, count));
21 }
22 sorted.sort_by(|c1, c2| c1.0.code.cmp(&c2.0.code));
23
24 let mut table = Table::new();
25 table
26 .load_preset(UTF8_FULL)
27 .apply_modifier(UTF8_ROUND_CORNERS)
28 .set_header(vec!["Country", "Code", "Count"]);
29
30 for (country, count) in sorted {
31 table.add_row(vec![
32 Cell::new(country.kind.to_string()),
33 Cell::new(country.code.to_string()),
34 Cell::new(count.to_string()).set_alignment(CellAlignment::Right),
35 ]);
36 }
37 println!("{}", table);
38}