Skip to content

Commit

Permalink
Use dialoguer for prompts with history.
Browse files Browse the repository at this point in the history
Modify intro and help text.
  • Loading branch information
mikebenfield committed Nov 22, 2024
1 parent 7b9e525 commit b2b0513
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion interpreter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "leo-interpreter"
version = "2.3.0"
version = "2.3.1"
authors = [ "The Leo Team <[email protected]>" ]
description = "Interpreter for the Leo programming language"
homepage = "https://leo-lang.org"
Expand Down Expand Up @@ -48,6 +48,10 @@ workspace = true
[dependencies.indexmap]
workspace = true

[dependencies.dialoguer]
version = "0.11.0"
features = [ "history" ]

[dependencies.rand]
workspace = true

Expand Down
38 changes: 29 additions & 9 deletions interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,11 @@ impl Interpreter {
});
};

if matches!(act, LeoInterpretOver(..)) { self.cursor.over()? } else { self.cursor.step()? }
if matches!(act, LeoInterpretOver(..)) {
self.cursor.over()?
} else {
StepResult { finished: false, value: None }
}
}

Step => self.cursor.whole_step()?,
Expand Down Expand Up @@ -390,8 +394,10 @@ impl Interpreter {
}
}

const INSTRUCTIONS: &str = "
This is the Leo Interpreter. You probably want to start by running a function or transition.
const INTRO: &str = "This is the Leo Interpreter. Try the command `#help`.";

const HELP: &str = "
You probably want to start by running a function or transition.
For instance
#into program.aleo/main()
Once a function is running, commands include
Expand All @@ -409,12 +415,19 @@ When executing Aleo VM code, you can print the value of a register like this:
You may also use one letter abbreviations for these commands, such as #i.
You may simply enter expressions or statements on the command line
Note that this interpreter is not line oriented as in many common debuggers;
rather it is oriented around expressions and statements.
As you step into code, individual expressions or statements will
be evaluated one by one, including arguments of function calls.
You may simply enter Leo expressions or statements on the command line
to evaluate. For instance, if you want to see the value of a variable w:
w
If you want to set w to a new value:
w = z + 2u8;
Note that statements (like the assignment above) must end with a semicolon.
If there are futures available to be executed, they will be listed by
numerical index, and you may run them using `#future` (or `#f`); for instance
#future 0
Expand Down Expand Up @@ -530,10 +543,10 @@ pub fn interpret(
block_height: u32,
) -> Result<()> {
let mut interpreter = Interpreter::new(leo_filenames.iter(), aleo_filenames.iter(), signer, block_height)?;
let mut buffer = String::new();
println!("{}", INSTRUCTIONS);
println!("{}", INTRO);

let mut history = dialoguer::BasicHistory::new();
loop {
buffer.clear();
if let Some(v) = interpreter.view_current_in_context() {
println!("{v}");
} else if let Some(v) = interpreter.view_current() {
Expand All @@ -542,9 +555,16 @@ pub fn interpret(
for (i, future) in interpreter.cursor.futures.iter().enumerate() {
println!("{i}: {future}");
}
std::io::stdin().read_line(&mut buffer).expect("read_line");
let action = match buffer.trim() {

let user_input: String =
dialoguer::Input::new().with_prompt("Command?").history_with(&mut history).interact_text().unwrap();

let action = match user_input.trim() {
"" => continue,
"#h" | "#help" => {
println!("{}", HELP);
continue;
}
"#i" | "#into" => InterpreterAction::Into,
"#s" | "#step" => InterpreterAction::Step,
"#o" | "#over" => InterpreterAction::Over,
Expand Down

0 comments on commit b2b0513

Please sign in to comment.