diff --git a/assets/heart.jpg b/assets/heart.jpg new file mode 100644 index 0000000..e469f36 Binary files /dev/null and b/assets/heart.jpg differ diff --git a/assets/heart.png b/assets/heart.png new file mode 100644 index 0000000..f1f425e Binary files /dev/null and b/assets/heart.png differ diff --git a/assets/heart.xcf b/assets/heart.xcf new file mode 100644 index 0000000..d5fc907 Binary files /dev/null and b/assets/heart.xcf differ diff --git a/src/main.rs b/src/main.rs index 56a07b8..71a4f13 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ pub fn main() -> Result<(), String> { .window("rust-sdl2 demo: Video", 1024, 600) .position_centered() .opengl() + .fullscreen() .build() .map_err(|e| e.to_string())?; @@ -195,6 +196,12 @@ pub fn main() -> Result<(), String> { .load_texture_bytes(include_bytes!("../assets/dog.png")) .unwrap(), ); + textures.insert( + "heart", + texture_creator + .load_texture_bytes(include_bytes!("../assets/heart.png")) + .unwrap(), + ); let opts: Vec<&str> = textures.keys().map(|s| *s).collect(); @@ -251,26 +258,41 @@ pub fn main() -> Result<(), String> { } } + // Find the most-exact match to show let mut matches = vec![]; - let mut skip_chars = 0; let mut chars = line.chars(); for i in 0..line.len() { let part_line = find_matches(chars.as_str(), &opts); for part in part_line { matches.push((i, part)); } - if matches.len() == 0 { - // Advance the line so we don't leak chars - skip_chars = i; - } chars.next(); } let matches: Vec<&str> = matches.into_iter().map(|(_, s)| s).collect(); let show = if matches.len() > 0 { matches[0] } else { "" }; - // Discard extra characters from line + + // Discard extra characters from line; find any lines that could match + // and discard up to there + let mut skip_chars = 0; + let mut chars = line.chars(); + for i in 0..line.len() { + let part_line = find_could_match(chars.as_str(), &opts); + if part_line.len() == 0 { + // Advance the line so we don't leak chars + skip_chars = i; + } else { + break; + } + chars.next(); + } let (_, tline) = line.split_at(skip_chars); line = String::from(tline); + + //println!("Show {show}"); + //println!("Matches {:?}", matches); + //println!("Line {line}"); + canvas.clear(); if let Some(tex) = textures.get(show) { canvas.copy(tex, None, None).unwrap(); @@ -295,3 +317,17 @@ fn find_matches<'a, 'b>(word: &'b str, opts: &'a [&'a str]) -> Vec<&'a str> { } ret } + + +fn find_could_match<'a, 'b>(word: &'b str, opts: &'a [&'a str]) -> Vec<&'a str> { + if word.len() == 0 { + return vec![]; + } + let mut ret = vec![]; + for opt in opts { + if opt.starts_with(word) { + ret.push(*opt) + } + } + ret +}