mirror of
https://github.com/lune-org/lune.git
synced 2025-05-04 10:43:57 +01:00
feat: impl input continuation
This commit is contained in:
parent
b0e52bc9cb
commit
90d51aeaf6
1 changed files with 26 additions and 5 deletions
|
@ -1,4 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
|
fmt::Write,
|
||||||
io::ErrorKind,
|
io::ErrorKind,
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
process::{exit, ExitCode},
|
process::{exit, ExitCode},
|
||||||
|
@ -12,6 +13,12 @@ use lune::Lune;
|
||||||
use mlua::ExternalError;
|
use mlua::ExternalError;
|
||||||
use rustyline::{error::ReadlineError, history::FileHistory, DefaultEditor, Editor};
|
use rustyline::{error::ReadlineError, history::FileHistory, DefaultEditor, Editor};
|
||||||
|
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
enum PromptState {
|
||||||
|
Regular,
|
||||||
|
Continuation
|
||||||
|
}
|
||||||
|
|
||||||
// Isn't dependency injection plain awesome?!
|
// Isn't dependency injection plain awesome?!
|
||||||
pub async fn show_interface(cmd: Command) -> Result<ExitCode> {
|
pub async fn show_interface(cmd: Command) -> Result<ExitCode> {
|
||||||
let lune_version = cmd.get_version();
|
let lune_version = cmd.get_version();
|
||||||
|
@ -58,13 +65,22 @@ pub async fn show_interface(cmd: Command) -> Result<ExitCode> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut interrupt_counter = 0u32;
|
let mut interrupt_counter = 0u32;
|
||||||
|
let mut prompt_state: PromptState = PromptState::Regular;
|
||||||
loop {
|
|
||||||
let mut source_code = String::new();
|
let mut source_code = String::new();
|
||||||
|
|
||||||
match repl.readline("> ") {
|
loop {
|
||||||
|
let prompt = match prompt_state {
|
||||||
|
PromptState::Regular => "> ",
|
||||||
|
PromptState::Continuation => ">> "
|
||||||
|
};
|
||||||
|
|
||||||
|
match repl.readline(prompt) {
|
||||||
Ok(code) => {
|
Ok(code) => {
|
||||||
|
if prompt_state == PromptState::Continuation {
|
||||||
|
write!(&mut source_code, "{}", code)?;
|
||||||
|
} else if prompt_state == PromptState::Regular {
|
||||||
source_code = code.clone();
|
source_code = code.clone();
|
||||||
|
}
|
||||||
|
|
||||||
repl.add_history_entry(code.as_str())?;
|
repl.add_history_entry(code.as_str())?;
|
||||||
|
|
||||||
|
@ -101,11 +117,16 @@ pub async fn show_interface(cmd: Command) -> Result<ExitCode> {
|
||||||
let eval_result = lune_instance.run("REPL", source_code.clone()).await;
|
let eval_result = lune_instance.run("REPL", source_code.clone()).await;
|
||||||
|
|
||||||
match eval_result {
|
match eval_result {
|
||||||
Ok(_) => (),
|
Ok(_) => prompt_state = PromptState::Regular,
|
||||||
|
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
if err.is_incomplete_input() {
|
||||||
|
prompt_state = PromptState::Continuation;
|
||||||
|
source_code.push_str("\n")
|
||||||
|
} else {
|
||||||
eprintln!("{}", pretty_format_luau_error(&err.into_lua_err(), true))
|
eprintln!("{}", pretty_format_luau_error(&err.into_lua_err(), true))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue