Skip to content
Open
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
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_console"
version = "0.14.1"
version = "0.15.0"
edition = "2021"
authors = ["RichoDemus <[email protected]>"]
homepage = "https://github.com/RichoDemus/bevy-console"
Expand All @@ -10,23 +10,23 @@ license = "MIT"
readme = "README.md"

[dependencies]
bevy = { version = "0.16", default-features = false, features = [
bevy = { version = "0.17", default-features = false, features = [
"std",
"bevy_log",
] }
clap = { version = "4.5", features = ["derive"] }
bevy_console_derive = { path = "./bevy_console_derive", version = "0.5.0" }
bevy_egui = { version = "0.34", default-features = false, features = [
bevy_egui = { version = "0.37", default-features = false, features = [
"render",
"default_fonts",
] }
shlex = "1.3"
ansi-parser = "0.9"
strip-ansi-escapes = "0.2"
trie-rs = "0.2"
trie-rs = "0.4"

[dev-dependencies]
bevy = { version = "0.16", features = ["std", "bevy_log"] }
bevy = { version = "0.17", features = ["std", "bevy_log"] }
color-print = { version = "0.3" }


Expand Down
1 change: 1 addition & 0 deletions examples/capture_bevy_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn main() {
level: log::Level::INFO,
filter: "error,capture_bevy_logs=info".to_owned(),
custom_layer: make_layer,
..default()
}),
ConsolePlugin,
))
Expand Down
2 changes: 1 addition & 1 deletion examples/raw_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() {
.run();
}

fn raw_commands(mut console_commands: EventReader<ConsoleCommandEntered>) {
fn raw_commands(mut console_commands: MessageReader<ConsoleCommandEntered>) {
for ConsoleCommandEntered { command_name, args } in console_commands.read() {
println!(r#"Entered command "{command_name}" with args {:#?}"#, args);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/write_to_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ fn main() {
.run();
}

fn write_to_console(mut console_line: EventWriter<PrintConsoleLine>) {
fn write_to_console(mut console_line: MessageWriter<PrintConsoleLine>) {
console_line.write(PrintConsoleLine::new("Hello".into()));
}
2 changes: 1 addition & 1 deletion src/commands/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) struct ExitCommand;

pub(crate) fn exit_command(
mut exit: ConsoleCommand<ExitCommand>,
mut exit_writer: EventWriter<AppExit>,
mut exit_writer: MessageWriter<AppExit>,
) {
if let Some(Ok(_)) = exit.take() {
exit_writer.write(AppExit::Success);
Expand Down
56 changes: 32 additions & 24 deletions src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ use crate::{
ConsoleSet,
};

type ConsoleCommandEnteredReaderSystemParam = EventReader<'static, 'static, ConsoleCommandEntered>;
type ConsoleCommandEnteredReaderSystemParam = MessageReader<'static, 'static, ConsoleCommandEntered>;

type PrintConsoleLineWriterSystemParam = EventWriter<'static, PrintConsoleLine>;
type PrintConsoleLineWriterSystemParam = MessageWriter<'static, PrintConsoleLine>;

/// A super-trait for command like structures
pub trait Command: NamedCommand + CommandFactory + FromArgMatches + Sized + Resource {}
Expand Down Expand Up @@ -69,7 +69,7 @@ pub trait NamedCommand {
/// ```
pub struct ConsoleCommand<'w, T> {
command: Option<Result<T, clap::Error>>,
console_line: EventWriter<'w, PrintConsoleLine>,
console_line: MessageWriter<'w, PrintConsoleLine>,
}

impl<T> ConsoleCommand<'_, T> {
Expand Down Expand Up @@ -119,7 +119,7 @@ impl<T> ConsoleCommand<'_, T> {

pub struct ConsoleCommandState<T> {
#[allow(clippy::type_complexity)]
event_reader: <ConsoleCommandEnteredReaderSystemParam as SystemParam>::State,
message_reader: <ConsoleCommandEnteredReaderSystemParam as SystemParam>::State,
console_line: <PrintConsoleLineWriterSystemParam as SystemParam>::State,
marker: PhantomData<T>,
}
Expand All @@ -128,15 +128,23 @@ unsafe impl<T: Command> SystemParam for ConsoleCommand<'_, T> {
type State = ConsoleCommandState<T>;
type Item<'w, 's> = ConsoleCommand<'w, T>;

fn init_state(world: &mut World, system_meta: &mut SystemMeta) -> Self::State {
let event_reader = ConsoleCommandEnteredReaderSystemParam::init_state(world, system_meta);
let console_line = PrintConsoleLineWriterSystemParam::init_state(world, system_meta);
fn init_state(world: &mut World) -> Self::State {
let message_reader = ConsoleCommandEnteredReaderSystemParam::init_state(world);
let console_line = PrintConsoleLineWriterSystemParam::init_state(world);
ConsoleCommandState {
event_reader,
message_reader,
console_line,
marker: PhantomData,
}
}

fn init_access(
_state: &Self::State,
_system_meta: &mut SystemMeta,
_component_access_set: &mut bevy::ecs::query::FilteredAccessSet,
_world: &mut World,
) {
}

#[inline]
unsafe fn get_param<'w, 's>(
Expand All @@ -145,8 +153,8 @@ unsafe impl<T: Command> SystemParam for ConsoleCommand<'_, T> {
world: UnsafeWorldCell<'w>,
change_tick: Tick,
) -> Self::Item<'w, 's> {
let mut event_reader = ConsoleCommandEnteredReaderSystemParam::get_param(
&mut state.event_reader,
let mut message_reader = ConsoleCommandEnteredReaderSystemParam::get_param(
&mut state.message_reader,
system_meta,
world,
change_tick,
Expand All @@ -158,7 +166,7 @@ unsafe impl<T: Command> SystemParam for ConsoleCommand<'_, T> {
change_tick,
);

let command = event_reader.read().find_map(|command| {
let command = message_reader.read().find_map(|command| {
if T::name() == command.command_name {
let clap_command = T::command().no_binary_name(true);
// .color(clap::ColorChoice::Always);
Expand Down Expand Up @@ -189,16 +197,16 @@ unsafe impl<T: Command> SystemParam for ConsoleCommand<'_, T> {
}
}
/// Parsed raw console command into `command` and `args`.
#[derive(Clone, Debug, Event)]
#[derive(Clone, Debug, Message)]
pub struct ConsoleCommandEntered {
/// the command definition
pub command_name: String,
/// Raw parsed arguments
pub args: Vec<String>,
}

/// Events to print to the console.
#[derive(Clone, Debug, Eq, Event, PartialEq)]
/// Messages to print to the console.
#[derive(Clone, Debug, Eq, Message, PartialEq)]
pub struct PrintConsoleLine {
/// Console line
pub line: String,
Expand Down Expand Up @@ -488,15 +496,15 @@ pub(crate) fn console_ui(
mut egui_context: EguiContexts,
config: Res<ConsoleConfiguration>,
mut cache: ResMut<ConsoleCache>,
mut keyboard_input_events: EventReader<KeyboardInput>,
mut keyboard_input_events: MessageReader<KeyboardInput>,
mut state: ResMut<ConsoleState>,
command_entered: EventWriter<ConsoleCommandEntered>,
command_entered: MessageWriter<ConsoleCommandEntered>,
mut console_open: ResMut<ConsoleOpen>,
) {
let keyboard_input_events = keyboard_input_events.read().collect::<Vec<_>>();

// If there is no egui context, return, this can happen when exiting the app
let ctx = if let Some(ctxt) = egui_context.try_ctx_mut() {
let ctx = if let Ok(ctxt) = egui_context.ctx_mut() {
ctxt
} else {
return;
Expand Down Expand Up @@ -669,7 +677,7 @@ fn handle_enter(
config: Res<'_, ConsoleConfiguration>,
cache: &ResMut<'_, ConsoleCache>,
state: &mut ResMut<'_, ConsoleState>,
mut command_entered: EventWriter<'_, ConsoleCommandEntered>,
mut command_entered: MessageWriter<'_, ConsoleCommandEntered>,
ui: &mut egui::Ui,
text_edit_response: &egui::Response,
) {
Expand Down Expand Up @@ -725,11 +733,11 @@ fn handle_enter(

pub(crate) fn receive_console_line(
mut console_state: ResMut<ConsoleState>,
mut events: EventReader<PrintConsoleLine>,
mut messages: MessageReader<PrintConsoleLine>,
) {
for event in events.read() {
let event: &PrintConsoleLine = event;
console_state.scrollback.push(event.line.clone());
for message in messages.read() {
let message: &PrintConsoleLine = message;
console_state.scrollback.push(message.line.clone());
}
}

Expand Down Expand Up @@ -765,7 +773,7 @@ pub fn block_mouse_input(
return;
}

let Some(context) = contexts.try_ctx_mut() else {
let Ok(context) = contexts.ctx_mut() else {
return;
};

Expand All @@ -783,7 +791,7 @@ pub fn block_keyboard_input(
return;
}

let Some(context) = contexts.try_ctx_mut() else {
let Ok(context) = contexts.ctx_mut() else {
return;
};

Expand Down
18 changes: 8 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use bevy::prelude::*;
pub use bevy_console_derive::ConsoleCommand;
use bevy_egui::{EguiContextPass, EguiPlugin, EguiPreUpdateSet};
use bevy_egui::{EguiPlugin, EguiPreUpdateSet, EguiPrimaryContextPass};
use console::{block_keyboard_input, block_mouse_input, ConsoleCache};
use trie_rs::TrieBuilder;

Expand Down Expand Up @@ -42,13 +42,13 @@ pub enum ConsoleSet {
Commands,

/// Systems running after command systems, which depend on the fact commands have executed beforehand (the output layer).
/// For example a system which makes use of [`PrintConsoleLine`] events should be placed in this set to be able to receive
/// For example a system which makes use of [`PrintConsoleLine`] messages should be placed in this set to be able to receive
/// New lines to print in the same frame
PostCommands,
}

/// Run condition which does not run any command systems if no command was entered
fn have_commands(commands: EventReader<ConsoleCommandEntered>) -> bool {
fn have_commands(commands: MessageReader<ConsoleCommandEntered>) -> bool {
!commands.is_empty()
}

Expand All @@ -72,8 +72,8 @@ impl Plugin for ConsolePlugin {
.init_resource::<ConsoleState>()
.init_resource::<ConsoleOpen>()
.init_resource::<ConsoleCache>()
.add_event::<ConsoleCommandEntered>()
.add_event::<PrintConsoleLine>()
.add_message::<ConsoleCommandEntered>()
.add_message::<PrintConsoleLine>()
.add_console_command::<ClearCommand, _>(clear_command)
.add_console_command::<ExitCommand, _>(exit_command)
.add_console_command::<HelpCommand, _>(help_command)
Expand All @@ -86,14 +86,14 @@ impl Plugin for ConsolePlugin {
.before(EguiPreUpdateSet::BeginPass),
)
.add_systems(
EguiContextPass,
EguiPrimaryContextPass,
(
console_ui.in_set(ConsoleSet::ConsoleUI),
receive_console_line.in_set(ConsoleSet::PostCommands),
),
)
.configure_sets(
EguiContextPass,
EguiPrimaryContextPass,
(
ConsoleSet::Commands
.after(ConsoleSet::ConsoleUI)
Expand All @@ -105,9 +105,7 @@ impl Plugin for ConsolePlugin {
// Don't initialize an egui plugin if one already exists.
// This can happen if another plugin is using egui and was installed before us.
if !app.is_plugin_added::<EguiPlugin>() {
app.add_plugins(EguiPlugin {
enable_multipass_for_primary_context: true,
});
app.add_plugins(EguiPlugin::default());
}
}
}
4 changes: 2 additions & 2 deletions src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use bevy::{
app::{App, Update},
log::tracing_subscriber::{self, EnvFilter, Layer, Registry},
prelude::{EventWriter, IntoScheduleConfigs, ResMut, Resource},
prelude::{MessageWriter, IntoScheduleConfigs, ResMut, Resource},
};

use crate::{ConsoleSet, PrintConsoleLine};
Expand Down Expand Up @@ -45,7 +45,7 @@ impl Write for BevyLogBufferWriter {
/// Flushes the log buffer and sends its content to the console
pub fn send_log_buffer_to_console(
buffer: ResMut<BevyLogBuffer>,
mut console_lines: EventWriter<PrintConsoleLine>,
mut console_lines: MessageWriter<PrintConsoleLine>,
) {
let mut buffer = buffer.0.lock().unwrap();
// read and clean buffer
Expand Down