Skip to content
Draft
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "Rust Development Environment",
"image": "rust:latest",
"customizations": {
"vscode": {
"extensions": [
"rust-lang.rust-analyzer"
]
}
},
"postCreateCommand": "apt-get update && apt-get install g++ pkg-config libx11-dev libasound2-dev libudev-dev libxkbcommon-x11-0"
}
67 changes: 49 additions & 18 deletions src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,30 @@ pub struct ConsoleCommandEntered {
pub args: Vec<String>,
}

/// Resource representing the Last Console Line
#[derive(Resource, Debug, Clone, Eq, PartialEq, Default)]
pub struct LastConsoleLine{
pub line: String
}

/// Events to print to the console.
#[derive(Clone, Debug, Eq, Event, PartialEq)]
pub struct PrintConsoleLine {
/// Console line
pub line: String,
/// Whether the line should print
pub should_print: bool
}

impl PrintConsoleLine {
/// Creates a new console line to print.
pub const fn new(line: String) -> Self {
Self { line }
Self { line, should_print: true }
}

/// Creates a new console line which doesn't print.
pub const fn quiet(line: String) -> Self{
Self { line, should_print: false}
}
}

Expand Down Expand Up @@ -516,27 +529,41 @@ pub(crate) fn console_ui(
state.history.pop_back();
}

let mut args = Shlex::new(&state.buf).collect::<Vec<_>>();

if !args.is_empty() {
let command_name = args.remove(0);
debug!("Command entered: `{command_name}`, with args: `{args:?}`");

let command = config.commands.get(command_name.as_str());
//Use Shlex to Get Commands to Run
let mut commands_to_run: Vec<Vec<String>> = vec![Vec::new()];
for arg in Shlex::new(&state.buf){
if arg == "|"{
commands_to_run.push(Vec::new());
}else{
commands_to_run.last_mut().unwrap().push(arg);
}
}

if command.is_some() {
command_entered
.send(ConsoleCommandEntered { command_name, args });
//Get the first bad command that is not registered
if let Some(bad_command) = commands_to_run.iter().find_map(|command_to_run| {
if !command_to_run.is_empty() && config.commands.get(command_to_run.first().unwrap().as_str()).is_none() {
Some(command_to_run.first().unwrap())
} else {
debug!(
"Command not recognized, recognized commands: `{:?}`",
config.commands.keys().collect::<Vec<_>>()
);
None
}
}) {
debug!(
"Command not recognized: {}, recognized commands: `{:?}`",
bad_command,
config.commands.keys().collect::<Vec<_>>()
);

state.scrollback.push("error: Invalid command".into());
state.scrollback.push("error: Invalid command".into());
} else {
for mut command_args in commands_to_run{
if !command_args.is_empty(){
let command_name = command_args.remove(0);
debug!("Command entered: `{command_name}`, with args: `{command_args:?}`");
command_entered
.send(ConsoleCommandEntered { command_name, args: command_args });
}
}
}

state.buf.clear();
}
}
Expand Down Expand Up @@ -586,10 +613,14 @@ pub(crate) fn console_ui(
pub(crate) fn receive_console_line(
mut console_state: ResMut<ConsoleState>,
mut events: EventReader<PrintConsoleLine>,
mut last_line: ResMut<LastConsoleLine>
) {
for event in events.read() {
let event: &PrintConsoleLine = event;
console_state.scrollback.push(event.line.clone());
if event.should_print{
console_state.scrollback.push(event.line.clone());
}
last_line.line = event.line.clone();
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use bevy::prelude::*;
pub use bevy_console_derive::ConsoleCommand;
use bevy_egui::EguiPlugin;
use console::LastConsoleLine;

use crate::commands::clear::{clear_command, ClearCommand};
use crate::commands::exit::{exit_command, ExitCommand};
Expand Down Expand Up @@ -52,6 +53,7 @@ impl Plugin for ConsolePlugin {
app.init_resource::<ConsoleConfiguration>()
.init_resource::<ConsoleState>()
.init_resource::<ConsoleOpen>()
.init_resource::<LastConsoleLine>()
.add_event::<ConsoleCommandEntered>()
.add_event::<PrintConsoleLine>()
.add_console_command::<ClearCommand, _>(clear_command)
Expand Down
2 changes: 1 addition & 1 deletion src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn send_log_buffer_to_console(
// read and clean buffer
let buffer = buffer.get_mut();
for line in buffer.lines().map_while(Result::ok) {
console_lines.send(PrintConsoleLine { line });
console_lines.send(PrintConsoleLine::new(line));
}
buffer.clear();
}
Expand Down