From 00936f60f13d413f74d65402dc95ea97cd9b1181 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Fri, 8 Apr 2022 10:07:12 -0400 Subject: [PATCH] clamp i value --- Cargo.toml | 8 ++++++-- src/widgets.rs | 21 +++++---------------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f65ae6d..f71ed54 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,9 @@ lto = false [dependencies] eframe = { git = "https://github.com/Titaniumtown/egui.git", default-features = false } shadow-rs = { version = "0.11.0", default-features = false } -const_format = { version = "0.2.22", default-features = false, features = ["fmt"] } +const_format = { version = "0.2.22", default-features = false, features = [ + "fmt", +] } cfg-if = "1.0.0" exmex = { git = "https://github.com/bertiqwerty/exmex.git", branch = "main", features = [ "partial", @@ -59,6 +61,8 @@ async-lock = { git = "https://github.com/smol-rs/async-lock.git", optional = tru instant = { version = "0.1.12", features = ["wasm-bindgen"] } console_error_panic_hook = "0.1.7" wee_alloc = "0.4.5" -wasm-bindgen = { version = "0.2.80", default-features = false, features = ["std"] } +wasm-bindgen = { version = "0.2.80", default-features = false, features = [ + "std", +] } web-sys = "0.3.57" tracing-wasm = "0.2.1" diff --git a/src/widgets.rs b/src/widgets.rs index 930102e..ff2c903 100644 --- a/src/widgets.rs +++ b/src/widgets.rs @@ -54,24 +54,13 @@ impl<'a> AutoComplete<'a> { } let max_i = hints.len() as i16 - 1; - let mut i = self.i as i16; - match movement { - Movement::Up => { - i -= 1; - if 0 > i { - i = max_i - } - } - Movement::Down => { - i += 1; - if i > max_i { - i = 0; - } - } - _ => {} + self.i = match movement { + Movement::Up => self.i as i16 - 1, + Movement::Down => self.i as i16 + 1, + _ => self.i as i16, } - self.i = i as usize; + .clamp(0, max_i) as usize; } HintEnum::Single(hint) => { if movement == &Movement::Complete {