Implement console writer
This commit is contained in:
parent
4b5758ba51
commit
51b12870dd
4 changed files with 49 additions and 5 deletions
|
@ -27,6 +27,7 @@ tempfile = { version = "3.20.0", optional = true }
|
|||
thiserror = "2.0.12"
|
||||
tokio = { version = "1.45.0", features = ["macros", "rt"] }
|
||||
toml = "0.8.22"
|
||||
tracing = "0.1.41"
|
||||
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
|
||||
uuid = { version = "1.17.0", features = ["v4"] }
|
||||
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
mod writer;
|
||||
|
||||
use std::{collections::HashMap, ffi::OsString, hash::Hash, time::Duration};
|
||||
|
||||
use clap::{Args, Parser};
|
||||
use futures::{future::BoxFuture, StreamExt};
|
||||
use libp2p::{noise, ping, swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, Swarm};
|
||||
use tokio::time::sleep;
|
||||
use libp2p::{core::transport::dummy::DummyTransport, noise, ping, swarm::{NetworkBehaviour, SwarmEvent}, tcp, yamux, Swarm};
|
||||
use rustyline::ExternalPrinter;
|
||||
use tokio::{sync::Mutex, time::sleep};
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use writer::ConsoleWriter;
|
||||
|
||||
use crate::{error::Error, global::GLOBAL};
|
||||
|
||||
|
@ -78,6 +82,17 @@ impl ConsoleArgs {
|
|||
GLOBAL.launch_swarm().await
|
||||
});
|
||||
let mut rl = rustyline::DefaultEditor::new()?;
|
||||
let mut printer = rl.create_external_printer()?;
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(EnvFilter::from_default_env())
|
||||
.with_writer(std::sync::Mutex::new(ConsoleWriter::try_from(&mut rl)?)
|
||||
).init();
|
||||
tokio::spawn(async move {
|
||||
loop{
|
||||
tracing::event!(tracing::Level::ERROR, "test");
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
});
|
||||
loop {
|
||||
match rl.readline(">> ") {
|
||||
Ok(line) => {
|
31
lazy-supplements/src/cli/console/writer.rs
Normal file
31
lazy-supplements/src/cli/console/writer.rs
Normal file
|
@ -0,0 +1,31 @@
|
|||
use std::io::Read;
|
||||
|
||||
use rustyline::{DefaultEditor, ExternalPrinter};
|
||||
|
||||
use crate::error::Error;
|
||||
|
||||
pub struct ConsoleWriter {
|
||||
printer: Box<dyn ExternalPrinter + 'static + Send>
|
||||
}
|
||||
|
||||
impl TryFrom<&mut DefaultEditor> for ConsoleWriter {
|
||||
type Error = Error;
|
||||
fn try_from(e: &mut DefaultEditor) -> Result<Self, Error> {
|
||||
Ok(ConsoleWriter {
|
||||
printer: Box::new(e.create_external_printer()?)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl std::io::Write for ConsoleWriter {
|
||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||
let msg = String::from_utf8_lossy(buf).into_owned();
|
||||
let size = msg.as_bytes().len();
|
||||
self.printer.as_mut().print(msg);
|
||||
Ok(size)
|
||||
}
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -34,9 +34,6 @@ pub struct NodeConfig {
|
|||
|
||||
impl NodeConfig {
|
||||
pub async fn try_into_swarm (self) -> Result<Swarm<p2p::Behaviour>, Error> {
|
||||
let _ = tracing_subscriber::fmt()
|
||||
.with_env_filter(EnvFilter::from_default_env())
|
||||
.try_init();
|
||||
let mut swarm = libp2p::SwarmBuilder::with_existing_identity(self.secret)
|
||||
.with_tokio()
|
||||
.with_tcp(
|
||||
|
|
Loading…
Add table
Reference in a new issue