Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create custom lists of adjectives and nouns for generating session names #2122

Merged
merged 7 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dialoguer::Confirm;
use std::{fs::File, io::prelude::*, path::PathBuf, process};

use crate::sessions::{
assert_session, assert_session_ne, get_active_session, get_sessions,
assert_session, assert_session_ne, get_active_session, get_name_generator, get_sessions,
get_sessions_sorted_by_mtime, kill_session as kill_session_impl, match_session_name,
print_sessions, print_sessions_with_index, session_exists, ActiveSession, SessionNameMatch,
};
Expand Down Expand Up @@ -93,7 +93,7 @@ pub(crate) fn start_server(path: PathBuf, debug: bool) {
}

fn create_new_client() -> ClientInfo {
ClientInfo::New(names::Generator::default().next().unwrap())
ClientInfo::New(generate_unique_session_name())
}

fn find_indexed_session(
Expand Down Expand Up @@ -446,7 +446,7 @@ pub(crate) fn start_client(opts: CliArgs) {
process::exit(0);
}

let session_name = names::Generator::default().next().unwrap();
let session_name = generate_unique_session_name();
start_client_plan(session_name.clone());
start_client_impl(
Box::new(os_input),
Expand All @@ -459,3 +459,22 @@ pub(crate) fn start_client(opts: CliArgs) {
}
}
}

fn generate_unique_session_name() -> String {
let sessions = get_sessions();
let Ok(sessions) = sessions else {
eprintln!("Failed to list existing sessions: {:?}", sessions);
process::exit(1);
};

let name = get_name_generator()
.take(1000)
.find(|name| !sessions.contains(name));

if let Some(name) = name {
return name;
} else {
eprintln!("Failed to generate a unique session name, giving up");
process::exit(1);
}
}
148 changes: 148 additions & 0 deletions src/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,151 @@ pub(crate) fn assert_session_ne(name: &str) {
};
process::exit(1);
}

/// Create a new random name generator
///
/// Used to provide a memorable handle for a session when users don't specify a session name when the session is
/// created.
///
/// Uses the list of adjectives and nouns defined below, with the intention of avoiding unfortunate
/// and offensive combinations. Care should be taken when adding or removing to either list due to the birthday paradox/
/// hash collisions, e.g. with 4096 unique names, the likelihood of a collision in 10 session names is 1%.
pub(crate) fn get_name_generator() -> impl Iterator<Item = String> {
names::Generator::new(&ADJECTIVES, &NOUNS, names::Name::Plain)
}

const ADJECTIVES: &[&'static str] = &[
"adamant",
"adept",
"adventurous",
"arcadian",
"auspicious",
"awesome",
"blossoming",
"brave",
"charming",
"chatty",
"circular",
"considerate",
"cubic",
"curious",
"delighted",
"didactic",
"diligent",
"effulgent",
"erudite",
"excellent",
"exquisite",
"fabulous",
"fascinating",
"friendly",
"glowing",
"gracious",
"gregarious",
"hopeful",
"implacable",
"inventive",
"joyous",
"judicious",
"jumping",
"kind",
"likable",
"loyal",
"lucky",
"marvellous",
"mellifluous",
"nautical",
"oblong",
"outstanding",
"polished",
"polite",
"profound",
"quadratic",
"quiet",
"rectangular",
"remarkable",
"rusty",
"sensible",
"sincere",
"sparkling",
"splendid",
"stellar",
"tenacious",
"tremendous",
"triangular",
"undulating",
"unflappable",
"unique",
"verdant",
"vitreous",
"wise",
"zippy",
];

const NOUNS: &[&'static str] = &[
"aardvark",
"accordion",
"apple",
"apricot",
"bee",
"brachiosaur",
"cactus",
"capsicum",
"clarinet",
"cowbell",
"crab",
"cuckoo",
"cymbal",
"diplodocus",
"donkey",
"drum",
"duck",
"echidna",
"elephant",
"foxglove",
"galaxy",
"glockenspiel",
"goose",
"hill",
"horse",
"iguanadon",
"jellyfish",
"kangaroo",
"lake",
"lemon",
"lemur",
"magpie",
"megalodon",
"mountain",
"mouse",
"muskrat",
"newt",
"oboe",
"ocelot",
"orange",
"panda",
"peach",
"pepper",
"petunia",
"pheasant",
"piano",
"pigeon",
"platypus",
"quasar",
"rhinoceros",
"river",
"rustacean",
"salamander",
"sitar",
"stegosaurus",
"tambourine",
"tiger",
"tomato",
"triceratops",
"ukulele",
"viola",
"weasel",
"xylophone",
"yak",
"zebra",
];