From 6d67f7d97abbd1f368d28c43d1f03dcd1bbef0db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cassie=E2=80=BD?= <124477590+eupraxia05@users.noreply.github.com> Date: Tue, 3 Sep 2024 14:17:08 -0700 Subject: [PATCH] added a list of suggested commands based on what's been typed so far (#68) --- src/console.rs | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/console.rs b/src/console.rs index 2e5322a..007f20e 100644 --- a/src/console.rs +++ b/src/console.rs @@ -235,6 +235,8 @@ pub struct ConsoleConfiguration { pub background_color: Color32, /// Foreground (text) color pub foreground_color: Color32, + /// Number of suggested commands to show + pub num_suggestions: usize, } impl Default for ConsoleConfiguration { @@ -255,6 +257,7 @@ impl Default for ConsoleConfiguration { show_title_bar: true, background_color: Color32::from_black_alpha(102), foreground_color: Color32::LIGHT_GRAY, + num_suggestions: 4 } } } @@ -421,8 +424,44 @@ pub(crate) fn console_ui( .lock_focus(true) .font(egui::TextStyle::Monospace); - // Handle enter let text_edit_response = ui.add(text_edit); + + // show a few suggestions + if text_edit_response.has_focus() && !state.buf.is_empty() { + // create the area to show suggestions + let suggestions_area = + egui::Area::new(ui.auto_id_with("suggestions")) + .fixed_pos(ui.next_widget_position()) + .movable(false); + suggestions_area.show(ui.ctx(), |ui| { + + // collect the given number of commands starting + // with the given text + let command_names = &config.commands.iter() + .map(|c| *c.0 ) + .filter(|c| c.starts_with(&state.buf)) + .collect::>(); + + // show each command in the list + for command in command_names.iter().take(config.num_suggestions) { + let mut layout_job = egui::text::LayoutJob::default(); + layout_job.append(state.buf.as_str(), 0.0, TextFormat { + font_id: FontId::new(14.0, egui::FontFamily::Monospace), + underline: egui::Stroke::new(1., Color32::WHITE), + color: Color32::WHITE, + ..default() + }); + layout_job.append(&command[state.buf.len()..], 0.0, TextFormat { + font_id: FontId::new(14.0, egui::FontFamily::Monospace), + color: Color32::LIGHT_GRAY, + ..default() + }); + ui.label(layout_job); + } + }); + } + + // Handle enter if text_edit_response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) {