Update rustyline.

This commit is contained in:
Stephen Chung 2023-02-20 10:06:26 +08:00
parent 6d888cc0e4
commit 426055d4d3
2 changed files with 23 additions and 12 deletions

View File

@ -34,7 +34,7 @@ serde_json = { version = "1.0", default-features = false, features = ["alloc"],
unicode-xid = { version = "0.2", default-features = false, optional = true } unicode-xid = { version = "0.2", default-features = false, optional = true }
rust_decimal = { version = "1.16", default-features = false, features = ["maths"], optional = true } rust_decimal = { version = "1.16", default-features = false, features = ["maths"], optional = true }
getrandom = { version = "0.2", optional = true } getrandom = { version = "0.2", optional = true }
rustyline = { version = "10", optional = true } rustyline = { version = "11", optional = true }
document-features = { version = "0.2", optional = true } document-features = { version = "0.2", optional = true }
[dev-dependencies] [dev-dependencies]
@ -151,4 +151,4 @@ features = ["document-features", "metadata", "serde", "internals", "decimal", "d
[patch.crates-io] [patch.crates-io]
# Notice that a custom modified version of `rustyline` is used which supports bracketed paste on Windows. # Notice that a custom modified version of `rustyline` is used which supports bracketed paste on Windows.
# This can be moved to the official version when bracketed paste is added. # This can be moved to the official version when bracketed paste is added.
rustyline = { git = "https://github.com/schungx/rustyline", branch = "v10" } rustyline = { git = "https://github.com/schungx/rustyline", branch = "v11" }

View File

@ -2,7 +2,8 @@ use rhai::plugin::*;
use rhai::{Dynamic, Engine, EvalAltResult, Module, Scope, AST, INT}; use rhai::{Dynamic, Engine, EvalAltResult, Module, Scope, AST, INT};
use rustyline::config::Builder; use rustyline::config::Builder;
use rustyline::error::ReadlineError; use rustyline::error::ReadlineError;
use rustyline::{Cmd, Editor, Event, EventHandler, KeyCode, KeyEvent, Modifiers, Movement}; use rustyline::history::{History, SearchDirection};
use rustyline::{Cmd, DefaultEditor, Event, EventHandler, KeyCode, KeyEvent, Modifiers, Movement};
use std::{env, fs::File, io::Read, path::Path, process::exit}; use std::{env, fs::File, io::Read, path::Path, process::exit};
@ -188,14 +189,14 @@ fn load_script_files(engine: &mut Engine) {
} }
// Setup the Rustyline editor. // Setup the Rustyline editor.
fn setup_editor() -> Editor<()> { fn setup_editor() -> DefaultEditor {
//env_logger::init(); //env_logger::init();
let config = Builder::new() let config = Builder::new()
.tab_stop(4) .tab_stop(4)
.indent_size(4) .indent_size(4)
.bracketed_paste(true) .bracketed_paste(true)
.build(); .build();
let mut rl = Editor::<()>::with_config(config).unwrap(); let mut rl = DefaultEditor::with_config(config).unwrap();
// Bind more keys // Bind more keys
@ -336,7 +337,10 @@ fn main() {
'main_loop: loop { 'main_loop: loop {
if let Some(replace) = replacement.take() { if let Some(replace) = replacement.take() {
input = replace; input = replace;
if rl.add_history_entry(input.clone()) { if rl
.add_history_entry(input.clone())
.expect("Failed to add history entry")
{
history_offset += 1; history_offset += 1;
} }
if input.contains('\n') { if input.contains('\n') {
@ -366,7 +370,9 @@ fn main() {
if !cmd.is_empty() if !cmd.is_empty()
&& !cmd.starts_with('!') && !cmd.starts_with('!')
&& cmd.trim() != "history" && cmd.trim() != "history"
&& rl.add_history_entry(input.clone()) && rl
.add_history_entry(input.clone())
.expect("Failed to add history entry")
{ {
history_offset += 1; history_offset += 1;
} }
@ -476,7 +482,7 @@ fn main() {
let json = engine let json = engine
.gen_fn_metadata_with_ast_to_json(&main_ast, false) .gen_fn_metadata_with_ast_to_json(&main_ast, false)
.unwrap(); .expect("Unable to generate JSON");
let mut f = std::fs::File::create("metadata.json") let mut f = std::fs::File::create("metadata.json")
.expect("Unable to create `metadata.json`"); .expect("Unable to create `metadata.json`");
f.write_all(json.as_bytes()).expect("Unable to write data"); f.write_all(json.as_bytes()).expect("Unable to write data");
@ -484,7 +490,7 @@ fn main() {
continue; continue;
} }
"!!" => { "!!" => {
match rl.history().last() { match rl.history().iter().last() {
Some(line) => { Some(line) => {
replacement = Some(line.clone()); replacement = Some(line.clone());
replacement_index = history_offset + rl.history().len() - 1; replacement_index = history_offset + rl.history().len() - 1;
@ -514,8 +520,12 @@ fn main() {
_ if cmd.starts_with('!') => { _ if cmd.starts_with('!') => {
if let Ok(num) = cmd[1..].parse::<usize>() { if let Ok(num) = cmd[1..].parse::<usize>() {
if num >= history_offset { if num >= history_offset {
if let Some(line) = rl.history().get(num - history_offset) { if let Some(line) = rl
replacement = Some(line.clone()); .history()
.get(num - history_offset, SearchDirection::Forward)
.expect("Failed to get history entry")
{
replacement = Some(line.entry.into());
replacement_index = num; replacement_index = num;
continue; continue;
} }
@ -578,7 +588,8 @@ fn main() {
main_ast.clear_statements(); main_ast.clear_statements();
} }
rl.save_history(HISTORY_FILE).unwrap(); rl.save_history(HISTORY_FILE)
.expect("Failed to save history");
println!("Bye!"); println!("Bye!");
} }