Skip to content

Commit

Permalink
added a list of suggested commands based on what's been typed so far (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
eupraxia05 authored Sep 3, 2024
1 parent c8c95d0 commit 6d67f7d
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}
}
Expand Down Expand Up @@ -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::<Vec<_>>();

// 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))
{
Expand Down

0 comments on commit 6d67f7d

Please sign in to comment.