Skip to content

Commit

Permalink
change sorting of channels in the sidebar
Browse files Browse the repository at this point in the history
  • Loading branch information
4e554c4c committed Oct 18, 2024
1 parent 4c75e7a commit dd9b8bf
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion data/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use chrono::{DateTime, Utc};
use futures::channel::mpsc;
use irc::proto::{self, command, Command};
use itertools::{Either, Itertools};
use std::cmp::Ordering;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt;
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -1281,8 +1282,27 @@ impl Client {
}
}

// TODO allow configuring the "sorting method"
// this function sorts channels together which have similar names when the chantype prefix
// (sometimes multipled) is removed
// e.g. '#chat', '##chat-offtopic' and '&chat-local' all get sorted together instead of in
// wildly different places.
fn compare_channels(&self, a: &str, b: &str) -> Ordering {
let (Some(a_chantype), Some(b_chantype)) = (a.chars().nth(0), b.chars().nth(0)) else {
return a.cmp(b);
};

if [a_chantype, b_chantype].iter().all(|c| self.chantypes().contains(c)) {
let ord = a.trim_start_matches(a_chantype).cmp(b.trim_start_matches(b_chantype));
if ord != Ordering::Equal {
return ord;
}
}
return a.cmp(b);
}

fn sync(&mut self) {
self.channels = self.chanmap.keys().cloned().collect();
self.channels = self.chanmap.keys().cloned().sorted_by(|a, b| self.compare_channels(a, b)).collect();
self.users = self
.chanmap
.iter()
Expand Down

0 comments on commit dd9b8bf

Please sign in to comment.