Skip to content

make the repl tool work with the tokio console #4418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
# invoking `cargo test` threads are allowed to have a large enough
# stack size without needing to use an optimised build.
RUST_MIN_STACK = "8388608"

[build]
rustflags = ["--cfg", "tokio_unstable"]
127 changes: 127 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ strum_macros = "0.24"
tagger = "4.3.4"
textwrap = "0.16.0"
thiserror = "1"
tokio = { version = "1", features = ["fs", "rt-multi-thread", "macros"] }
tokio = { version = "1", features = ["fs", "rt-multi-thread", "macros", "tracing"] }
tokio-io-timeout = "1.2.0"
tokio-stream = { version = "0.1.14", features = ["fs"] }
tokio-tar = { version = "0.3" } # TODO: integrate tokio into async-tar
Expand Down
1 change: 1 addition & 0 deletions deltachat-repl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
ansi_term = "0.12.1"
anyhow = "1"
console-subscriber = "0.1.9"
deltachat = { path = "..", features = ["internals"]}
dirs = "5"
log = "0.4.16"
Expand Down
109 changes: 57 additions & 52 deletions deltachat-repl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,13 @@ async fn start(args: Vec<String>) -> Result<(), Error> {
let context = Context::new(Path::new(&args[1]), 0, Events::new(), StockStrings::new()).await?;

let events = context.get_event_emitter();
tokio::task::spawn(async move {
while let Some(event) = events.recv().await {
receive_event(event.typ);
}
});
tokio::task::Builder::new()
.name("repl:receive_event")
.spawn(async move {
while let Some(event) = events.recv().await {
receive_event(event.typ);
}
})?;

println!("Delta Chat Core is awaiting your commands.");

Expand All @@ -331,61 +333,63 @@ async fn start(args: Vec<String>) -> Result<(), Error> {
let mut selected_chat = ChatId::default();

let ctx = context.clone();
let input_loop = tokio::task::spawn_blocking(move || {
let h = DcHelper {
completer: FilenameCompleter::new(),
highlighter: MatchingBracketHighlighter::new(),
hinter: HistoryHinter {},
};
let mut rl = Editor::with_config(config)?;
rl.set_helper(Some(h));
rl.bind_sequence(KeyEvent::alt('N'), Cmd::HistorySearchForward);
rl.bind_sequence(KeyEvent::alt('P'), Cmd::HistorySearchBackward);
if rl.load_history(".dc-history.txt").is_err() {
println!("No previous history.");
}
let input_loop = tokio::task::Builder::new()
.name("repl:input_loop")
.spawn_blocking(move || {
let h = DcHelper {
completer: FilenameCompleter::new(),
highlighter: MatchingBracketHighlighter::new(),
hinter: HistoryHinter {},
};
let mut rl = Editor::with_config(config)?;
rl.set_helper(Some(h));
rl.bind_sequence(KeyEvent::alt('N'), Cmd::HistorySearchForward);
rl.bind_sequence(KeyEvent::alt('P'), Cmd::HistorySearchBackward);
if rl.load_history(".dc-history.txt").is_err() {
println!("No previous history.");
}

loop {
let p = "> ";
let readline = rl.readline(p);

match readline {
Ok(line) => {
// TODO: ignore "set mail_pw"
rl.add_history_entry(line.as_str())?;
let should_continue = Handle::current().block_on(async {
match handle_cmd(line.trim(), ctx.clone(), &mut selected_chat).await {
Ok(ExitResult::Continue) => true,
Ok(ExitResult::Exit) => {
println!("Exiting ...");
false
loop {
let p = "> ";
let readline = rl.readline(p);

match readline {
Ok(line) => {
// TODO: ignore "set mail_pw"
rl.add_history_entry(line.as_str())?;
let should_continue = Handle::current().block_on(async {
match handle_cmd(line.trim(), ctx.clone(), &mut selected_chat).await {
Ok(ExitResult::Continue) => true,
Ok(ExitResult::Exit) => {
println!("Exiting ...");
false
}
Err(err) => {
println!("Error: {err:#}");
true
}
}
Err(err) => {
println!("Error: {err:#}");
true
}
}
});
});

if !should_continue {
if !should_continue {
break;
}
}
Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => {
println!("Exiting...");
break;
}
Err(err) => {
println!("Error: {err:#}");
break;
}
}
Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => {
println!("Exiting...");
break;
}
Err(err) => {
println!("Error: {err:#}");
break;
}
}
}

rl.save_history(".dc-history.txt")?;
println!("history saved");
Ok::<_, Error>(())
});
rl.save_history(".dc-history.txt")?;
println!("history saved");
Ok::<_, Error>(())
})?;

context.stop_io().await;
input_loop.await??;
Expand Down Expand Up @@ -481,6 +485,7 @@ async fn handle_cmd(

#[tokio::main]
async fn main() -> Result<(), Error> {
console_subscriber::init();
let _ = pretty_env_logger::try_init();

let args = std::env::args().collect();
Expand Down
Loading