Add #command command + enable use from editor

This commit is contained in:
Condorra 2024-10-11 12:20:06 +11:00
parent 612bcfec50
commit 849d9b4c8a
4 changed files with 53 additions and 9 deletions

View File

@ -1,6 +1,9 @@
use std::{ops::Deref, rc::Rc};
use crate::{FrameId, GlobalLayoutState, GlobalMemoCell, PanelDirection, SplitPanel};
use crate::{
command_handler::execute_queue, FrameId, GlobalLayoutState, GlobalMemoCell, PanelDirection,
SplitPanel,
};
use anyhow::Error;
use create_script_dialog::CreateScriptDialog;
use editor_area::EditorArea;
@ -59,10 +62,11 @@ pub fn try_run_script(script: &str, global_memo: &GlobalMemoCell) -> anyhow::Res
Ok(())
}
fn run_script<F>(script: &str, global_memo: &GlobalMemoCell, set_err: Rc<F>)
fn run_script<F>(script: &str, global_memo: &GlobalMemoCell, set_err: Rc<F>, frame: &FrameId)
where
F: Fn(Option<&str>),
{
global_memo.lua_engine.borrow_mut().set_current_frame(frame);
match try_run_script(script, global_memo) {
Ok(()) => {
set_err(None);
@ -71,6 +75,7 @@ where
set_err(Some(&format!("Script error: {}", e)));
}
}
execute_queue(global_memo);
}
fn select_file(script: &AttrValue, state: &UseStateHandle<Rc<EditorViewState>>) {
@ -115,7 +120,12 @@ where
{
move |ev: KeyboardEvent| {
if ev.code() == "Enter" && ev.get_modifier_state("Control") {
run_script(&editor_state.open_file, &global_memo, set_err.clone());
run_script(
&editor_state.open_file,
&global_memo,
set_err.clone(),
&frame,
);
ev.prevent_default();
} else if ev.code() == "Escape" || (ev.code() == "KeyX" && ev.get_modifier_state("Control"))
{

View File

@ -238,9 +238,15 @@ pub(super) fn editor_area(props: &EditorViewDetailProps) -> Html {
});
let global_memo_runkey = props.global_memo.clone();
let frame_id = props.frame.clone();
let runkey_closure = use_memo((), |()| {
Closure::<dyn FnMut(EditorState) -> bool>::new(move |_ed_state| {
run_script(&current_script, &global_memo_runkey, set_err.clone());
run_script(
&current_script,
&global_memo_runkey,
set_err.clone(),
&frame_id,
);
true
})
});

View File

@ -63,11 +63,12 @@ pub(super) fn editor_nav(props: &EditorViewDetailProps) -> Html {
set_err_state.set(new_state.into());
});
let editor_state = props.editor_state.clone();
let frame_for_runclick = frame.clone();
html! {
<div class="editornav">
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<button class="btn" aria-label="Run" title="Run"
onclick={move |_ev| run_script(&current_script, &global_memo, set_err.clone())}>
onclick={move |_ev| run_script(&current_script, &global_memo, set_err.clone(), &frame_for_runclick)}>
<i class="bi bi-file-earmark-play"></i></button>
<button class="btn" aria-label="New file" title="New file"
onclick={move |_ev| show_create_dialog(&editor_state) }

View File

@ -4,9 +4,9 @@ use piccolo::{
async_callback::{AsyncSequence, Locals},
meta_ops::{self, MetaResult},
stash::Fetchable,
Callback, Closure, Context, Executor, ExternError, FromValue, Function, IntoValue, Lua,
MetaMethod, Stack, StashedError, StashedExecutor, StashedFunction, StashedValue, Table, Value,
Variadic,
Callback, CallbackReturn, Closure, Context, Executor, ExternError, FromValue, Function,
IntoValue, Lua, MetaMethod, Stack, StashedError, StashedExecutor, StashedFunction,
StashedValue, Table, Value, Variadic,
};
use yew::UseStateSetter;
@ -16,7 +16,7 @@ use crate::{
create_match_table, match_table_add, match_table_lua_table, match_table_remove,
match_table_try_run_sub,
},
parsing::ParsedCommand,
parsing::{parse_commands, ParsedCommand},
FrameId, GlobalLayoutCell, GlobalMemoCell,
};
@ -178,6 +178,7 @@ pub fn install_lua_globals(
register_stateless_command!(alias);
register_stateless_command!(unalias);
register_command!(close_mud);
register_command!(command);
register_command!(connect_mud);
register_stateless_command!(create_match_table);
register_command!(cmd_delete_logs, "deletelogs");
@ -417,3 +418,29 @@ where
seq.call(&call, 0).await?;
Ok(())
}
pub fn command<'gc>(
ctx: Context<'gc>,
global_memo: &GlobalMemoCell,
_global_layout: &UseStateSetter<GlobalLayoutCell>,
) -> Callback<'gc> {
let global_memo = global_memo.clone();
Callback::from_fn(&ctx, move |ctx, _ex, mut stack| {
let cur_frame_id = try_unwrap_frame(
ctx,
&ctx.get_global::<Table>("info")?
.get(ctx, ctx.intern_static(b"current_frame"))?,
)?;
let cmd: String = stack.consume(ctx).map_err(|_| {
anyhow::Error::msg("command takes only one argument, a string containing command(s)")
})?;
let cmds = parse_commands(&cmd).commands;
for cmd in cmds {
global_memo
.command_queue
.borrow_mut()
.push_front((cur_frame_id.clone(), cmd));
}
Ok(CallbackReturn::Return)
})
}