Skip to content

Commit 59bf558

Browse files
committed
feat: set command
1 parent 9032588 commit 59bf558

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

crates/cmd_prompt/src/commands/commands/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ use crate::prelude::*;
22

33
mod clear;
44
mod echo;
5+
mod set;
56
mod show;
67

78
pub mod prelude {
89
pub use super::clear::clear_buffer;
910
}
1011

1112
pub fn plugin(app: &mut App) {
12-
app.add_plugins((show::plugin, echo::plugin, clear::plugin));
13+
app.add_plugins((show::plugin, echo::plugin, clear::plugin, set::plugin));
1314
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use crate::prelude::*;
2+
3+
#[derive(clap::Parser, Clone, Debug)]
4+
#[command(name = "set")]
5+
pub struct SetCmd {
6+
var: String,
7+
val: String,
8+
}
9+
10+
fn on_msg(
11+
mut reader: MessageReader<CommandMsg<SetCmd>>,
12+
mut assets: ResMut<Assets<ConsoleEnvVars>>,
13+
handles: Query<&ConsoleAssetHandle<ConsoleEnvVars>>,
14+
) {
15+
for msg in reader.read() {
16+
if let Ok(handle) = handles.get(msg.console_id)
17+
&& let Some(env_vars) = assets.get_mut(handle.id())
18+
{
19+
env_vars.insert(msg.command.var.clone(), msg.command.val.clone());
20+
} else {
21+
warn!("Could not set env vars for console id {}", msg.console_id);
22+
}
23+
}
24+
}
25+
26+
pub fn plugin(app: &mut App) {
27+
app.add_console_command::<SetCmd>();
28+
app.add_systems(PreUpdate, on_msg);
29+
}
30+
31+
#[cfg(test)]
32+
mod test {
33+
use super::*;
34+
use crate::test_harness;
35+
use q_test_harness::prelude::*;
36+
#[test]
37+
fn test_set_cmd() {
38+
let mut app = App::new();
39+
app.add_plugins(test_harness::plugin);
40+
let console_id = app.world_mut().spawn(Console).id();
41+
42+
app.add_step(
43+
0,
44+
move |mut commands: Commands, mut step: ResMut<NextState<Step>>| {
45+
commands.write_message(CommandMsg::<SetCmd> {
46+
console_id,
47+
command: SetCmd {
48+
var: "foo".into(),
49+
val: "bar".into(),
50+
},
51+
});
52+
step.set(Step(1));
53+
},
54+
);
55+
56+
app.add_step(
57+
1,
58+
move |mut commands: Commands,
59+
q: Query<&ConsoleAssetHandle<ConsoleEnvVars>>,
60+
assets: Res<Assets<ConsoleEnvVars>>| {
61+
let ok = (|| {
62+
let handle = q.get(console_id).ok()?;
63+
let vars = assets.get(handle.id())?;
64+
let ok = vars.get(&"foo".to_string()) == Some(&"bar".to_string());
65+
ok.then_some(true)
66+
})();
67+
if ok.unwrap_or_default() {
68+
commands.write_message(AppExit::Success);
69+
} else {
70+
commands.write_message(AppExit::error());
71+
}
72+
},
73+
);
74+
75+
assert!(app.run().is_success());
76+
}
77+
}

0 commit comments

Comments
 (0)