Skip to content

Commit 1e35912

Browse files
committed
Indent nested REPLs.
1 parent 9cb4384 commit 1e35912

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

jaq/src/repl.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ use jaq_core::Native;
33
use jaq_std::Filter;
44
use rustyline::error::ReadlineError;
55
use rustyline::DefaultEditor;
6+
use std::sync::atomic::{AtomicUsize, Ordering};
7+
8+
static DEPTH: AtomicUsize = AtomicUsize::new(0);
69

710
pub fn fun() -> Filter<Native<Val>> {
811
jaq_std::run(("repl", jaq_std::v(0), |_, cv| {
9-
repl_with(|s| match eval(s, cv.1.clone()) {
12+
let depth = DEPTH.fetch_add(1, Ordering::Relaxed);
13+
repl_with(depth, |s| match eval(s, cv.1.clone()) {
1014
Ok(()) => (),
1115
Err(e) => eprint!("{e}"),
1216
})
1317
.unwrap();
18+
DEPTH.fetch_sub(1, Ordering::Relaxed);
19+
1420
Box::new(core::iter::empty())
1521
}))
1622
}
@@ -24,15 +30,15 @@ fn eval(code: String, input: Val) -> Result<(), Error> {
2430
Ok(())
2531
}
2632

27-
fn repl_with(f: impl Fn(String)) -> Result<(), ReadlineError> {
33+
fn repl_with(depth: usize, f: impl Fn(String)) -> Result<(), ReadlineError> {
2834
use rustyline::config::{Behavior, Config};
2935
use yansi::Paint;
3036
let config = Config::builder()
3137
.behavior(Behavior::PreferTerm)
3238
.auto_add_history(true)
3339
.build();
3440
let mut rl = DefaultEditor::with_config(config)?;
35-
let prompt = "> ".bold().to_string();
41+
let prompt = format!("{}{} ", str::repeat(" ", depth), '>'.bold());
3642
loop {
3743
match rl.readline(&prompt) {
3844
Ok(line) => f(line),

0 commit comments

Comments
 (0)