Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestions #68

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -415,8 +418,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