From f6a4a828d254eeb4d48a967f1f2df42020d49c51 Mon Sep 17 00:00:00 2001 From: Connor Johnstone Date: Thu, 20 Mar 2025 00:04:16 -0400 Subject: [PATCH] Finished for 2025 --- 2025/Cargo.lock | 32 ++++++++++++++++++++++++++++++++ 2025/Cargo.toml | 1 + 2025/src/main.rs | 24 ++++++++++++++++++++---- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/2025/Cargo.lock b/2025/Cargo.lock index 4818e46..8851c8e 100644 --- a/2025/Cargo.lock +++ b/2025/Cargo.lock @@ -56,6 +56,7 @@ version = "0.1.0" dependencies = [ "color-eyre", "crossterm", + "fuzzy_match", "ratatui", ] @@ -234,6 +235,16 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" +[[package]] +name = "fuzzy_match" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f018ca9f4ec5fd881ecc3ec3970cbb2f68ce539aeef7ccdf528d2669a1c16e8" +dependencies = [ + "rustc_version", + "sliding_windows", +] + [[package]] name = "gimli" version = "0.28.1" @@ -483,6 +494,15 @@ version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" +[[package]] +name = "rustc_version" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5f5376ea5e30ce23c03eb77cbe4962b988deead10910c372b226388b594c084" +dependencies = [ + "semver", +] + [[package]] name = "rustix" version = "0.38.44" @@ -514,6 +534,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "semver" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4f410fedcf71af0345d7607d246e7ad15faaadd49d240ee3b24e5dc21a820ac" + [[package]] name = "sharded-slab" version = "0.1.7" @@ -559,6 +585,12 @@ dependencies = [ "libc", ] +[[package]] +name = "sliding_windows" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a830e5d74dce35884bac85ecdc68c1ae96b9451918cc484f39340049c7a55f5e" + [[package]] name = "smallvec" version = "1.14.0" diff --git a/2025/Cargo.toml b/2025/Cargo.toml index 00072fb..18c3ac1 100644 --- a/2025/Cargo.toml +++ b/2025/Cargo.toml @@ -9,3 +9,4 @@ edition = "2024" crossterm = "0.28.1" ratatui = "0.29.0" color-eyre = "0.6.3" +fuzzy_match = "0.2.1" diff --git a/2025/src/main.rs b/2025/src/main.rs index 3dcab58..b3a2cbe 100644 --- a/2025/src/main.rs +++ b/2025/src/main.rs @@ -8,6 +8,9 @@ use ratatui::{ widgets::{Block, Paragraph, Widget}, }; use std::fs; +use fuzzy_match::fuzzy_match; + +const DATA_FILE: &str = "data/kenpom.csv"; fn main() -> Result<()> { color_eyre::install()?; @@ -75,7 +78,7 @@ impl BracketApp { /// Handles the key events and updates the state of [`BracketApp`]. fn on_key_event(&mut self, key: KeyEvent) { match (key.modifiers, key.code) { - (_, KeyCode::Esc | KeyCode::Char('q')) + (KeyModifiers::CONTROL, KeyCode::Char('d')) | (KeyModifiers::CONTROL, KeyCode::Char('c') | KeyCode::Char('C')) => self.quit(), (_, KeyCode::Tab) => { self.selected_input = match self.selected_input { @@ -83,7 +86,12 @@ impl BracketApp { Input::Team2 => Input::Team1, } } + (_, KeyCode::Esc) => { + self.team1.clear(); + self.team2.clear(); + }, (_, KeyCode::Enter) => { + self.fuzzy_match_teams(); self.calculate_differential(); } (_, KeyCode::Backspace) => match self.selected_input { @@ -103,10 +111,18 @@ impl BracketApp { } } + fn fuzzy_match_teams(&mut self) { + // Fuzzy match the teams from the input + let file = fs::read_to_string(DATA_FILE).expect("Should have been able to read the file"); + let teams = file.lines().map(|line| (line.split(',').nth(1).unwrap(), line.split(',').nth(1).unwrap())).collect::>(); + self.team1 = fuzzy_match(&self.team1, teams.clone()).unwrap_or_default().to_string(); + self.team2 = fuzzy_match(&self.team2, teams).unwrap_or_default().to_string(); + } + fn calculate_differential(&mut self) { // Calculate the differential based on the teams let rankings_file = - fs::read_to_string("data/kenpom.csv").expect("Should have been able to read the file"); + fs::read_to_string(DATA_FILE).expect("Should have been able to read the file"); let team1_line: Vec<&str> = rankings_file .lines() .find(|line| line.contains(&self.team1)) @@ -142,7 +158,7 @@ impl Widget for &BracketApp { .blue() .centered(); let text = "Choose two teams \n\ - Press `Esc`, `Ctrl-C` or `q` to stop running."; + Press `Ctrl-C` or `Ctrl-D` to stop running."; Paragraph::new(text) .block(Block::bordered().title(title)) .centered() @@ -178,6 +194,6 @@ impl Widget for &BracketApp { Paragraph::new(results) .block(Block::bordered()) .centered() - .render(Rect::new(0, 8, area.width, 10), buf); + .render(Rect::new(0, 8, area.width, 9), buf); } }