Crates.io | altio |
lib.rs | altio |
version | 0.2.0 |
source | src |
created_at | 2023-12-26 06:00:04.520081 |
updated_at | 2024-01-31 17:44:48.895859 |
description | Automating command tools by simulating piped io in process. |
homepage | |
repository | https://github.com/oooutlk/altio |
max_upload_size | |
id | 1080649 |
size | 41,496 |
This crate helps to automating command tools by simulating piped io in process.
Interactive command tools utilize stdin, stdout and stderr for communication. If you want to use command tools as libraries(no spawning processes) and tool authors agree, this crate can help to automating input/output, just 3 steps:
Define an Altio
variable e.g. let io = Altio::default();
.
Replace std APIs with altio's equivalents, e.g. replace println!(...)
with
writeln!( io.out(), ... )
, replace std::io::stdin()
with io.input()
.
Keep main.rs as simple as possible, e.g.
fn main() { the_tool::run( std::env::args_os() )}
.
[dependencies]
altio = { version = "0.2", no_default_features = true }
[features]
altio = ["altio/altio"]
// lib.rs
pub struct TheTool {
// fields omitted
pub io: Altio,
}
impl_altio_output!( TheTool );
When building the tool as an application, the "altio" feature is disabled and altio falls back to stdio.
When building the tool as a library, the tool users can invoke send/recv methods
to communicate with the tool, e.g. send_line()
, try_recv_line()
.
the_tool = { version = "1.0", features = ["altio"] }
let args = std::env::args_os(); // clap::Parser::parse_from()
let tool = the_tool::new();
let tool_io = tool.io.clone();
// `io.input().read_line()` called occasionally
std::thread::spawn( || tool.run( args ));
loop {
if let Some( received ) = tool_io.try_recv_line() {
if received == "Lorum" {
tool_io.send_line( "Ipsum" );
}
}
}
Under Apache License 2.0 or MIT License, at your will.